在工厂类Laravel 5.4之前实现Facade [英] Implementation of Facade in front of Factory class Laravel 5.4

查看:127
本文介绍了在工厂类Laravel 5.4之前实现Facade的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些情况-今天早些时候,我正在努力弄清楚如何实现类似于Cache的外观-在这里我可以设置提供程序(如disk()),但在不提供时也具有通用的后备提供程序. /p>

现在,我已使基本基础结构正常工作,但是我认为我的实现令人讨厌.调用default()或provider()只是发臭.但是,有一个概念或我缺少的东西可以填补这里的空白.

对Cache实现类似的功能:: Laravel中的磁盘('x')

这就是我所做的.

// Factories\SMSFactory.php

namespace App\Factories;

use App\IError;


class SMSFactory
{
    public static function default()
    {
        $defaultProvider = config('sms.default_provider');
        return self::provider($defaultProvider);
    }

    public static function provider($providerId)
    {
        $providerClass = config('sms.' . $providerId);

        if (class_exists($providerClass))
        {
            return (new $providerClass);
        }

        return new class implements IError {

        };
    }

}

// sms.php (config)

return [
    /**
     * Set the default SMS provider for the application
     */
    'default_provider' => 'smsglobal',

    /**
     * Map the SMS provider to a class implementation
     */
    'smsglobal' => 'App\SMSGlobal\SMSGlobal',
];

// Providers\SMSServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;


class SMSServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('sms', 'App\Factories\SMSFactory');
    }
}

// Facades\SMS.php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class SMS extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'sms';
    }
}


// app.php

App\Providers\SMSServiceProvider::class,

# and in aliases

'SMS' => App\Facades\SMS::class,


// Controllers/TestController.php


namespace App\Http\Controllers\TestController;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Facades\SMS;


class TestController extends Controller
{
    public function sendSMS($destination, $message)
    {
        $data = $request->all();

        return SMS::default()->send([
            'destination' => $destination,
            'message' => $message,
        ]);
    }
}

真正困扰我的是必须始终使用default()...

我知道外观是一个静态类,但是是否可以以这种方式进行设置呢?

SMS::send($args);

// When I want to use another gateway
SMS::provider('nexmo')->send($args);

解决方案

您可以在SMS Factory类中使用__call方法,并且必须相应地对其进行更改:

class SMSFactory
{
    public function default()
    {
        $defaultProvider = config('sms.default_provider');

        return $this->provider($defaultProvider);
    }

    public function provider($providerId)
    {
        $providerClass = config('sms.' . $providerId);

        if (class_exists($providerClass))
        {
            return (new $providerClass);
        }

        return new class implements IError {

        };
    }

    public function __call($name, $arguments)
    {
        if (!method_exists($this, $name)) {
            $object = [$this->default(), $name];
        } else {
            $object = [$this, $name];
        }

        return call_user_func_array($object, $arguments);
    }
}

SMSFactory类不应具有静态方法,因为可以通过外观访问静态方法.

For some context - earlier today I was struggling to figure out how to implement a facade similar to Cache - where I could set a provider (like disk()), but also have a generic fall back provider when not supplied.

Now, I got the basic infrastructure working, but I think my implementation is nasty. Having call default() or provider() just stinks. However, there is a concept or something I'm missing to fill in the gaps here.

Implementing similar functionality to Cache::disk('x') in Laravel

Here is what I've done.

// Factories\SMSFactory.php

namespace App\Factories;

use App\IError;


class SMSFactory
{
    public static function default()
    {
        $defaultProvider = config('sms.default_provider');
        return self::provider($defaultProvider);
    }

    public static function provider($providerId)
    {
        $providerClass = config('sms.' . $providerId);

        if (class_exists($providerClass))
        {
            return (new $providerClass);
        }

        return new class implements IError {

        };
    }

}

// sms.php (config)

return [
    /**
     * Set the default SMS provider for the application
     */
    'default_provider' => 'smsglobal',

    /**
     * Map the SMS provider to a class implementation
     */
    'smsglobal' => 'App\SMSGlobal\SMSGlobal',
];

// Providers\SMSServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;


class SMSServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('sms', 'App\Factories\SMSFactory');
    }
}

// Facades\SMS.php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class SMS extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'sms';
    }
}


// app.php

App\Providers\SMSServiceProvider::class,

# and in aliases

'SMS' => App\Facades\SMS::class,


// Controllers/TestController.php


namespace App\Http\Controllers\TestController;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Facades\SMS;


class TestController extends Controller
{
    public function sendSMS($destination, $message)
    {
        $data = $request->all();

        return SMS::default()->send([
            'destination' => $destination,
            'message' => $message,
        ]);
    }
}

What is really bothering me is having to always use default()...

I understand that the facade acts as a static class, but is it possible to set it up in such a way that I could make calls like this?

SMS::send($args);

// When I want to use another gateway
SMS::provider('nexmo')->send($args);

解决方案

You can use the __call method in your SMS Factory class, and it must be changed accordingly:

class SMSFactory
{
    public function default()
    {
        $defaultProvider = config('sms.default_provider');

        return $this->provider($defaultProvider);
    }

    public function provider($providerId)
    {
        $providerClass = config('sms.' . $providerId);

        if (class_exists($providerClass))
        {
            return (new $providerClass);
        }

        return new class implements IError {

        };
    }

    public function __call($name, $arguments)
    {
        if (!method_exists($this, $name)) {
            $object = [$this->default(), $name];
        } else {
            $object = [$this, $name];
        }

        return call_user_func_array($object, $arguments);
    }
}

The SMSFactory class should not have static methods as the static methods can be accessed through facade.

这篇关于在工厂类Laravel 5.4之前实现Facade的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆