duanjiagu0655 2017-04-04 06:52
浏览 56
已采纳

在工厂级Laravel 5.4前面实施Facade

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);
  • 写回答

1条回答 默认 最新

  • douchixu3686 2017-04-04 07:26
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?