قم بتوسيع Laravel بمكوناتنا الخاصة

مهمة


أضف نظام تنبيه للمستخدمين من خلال رسائل SMS مع خيار اختيار مزود.

القرار


أفضل حل ، في رأيي ، هو إضافة المكون الخاص بك.
المكون هو كتلة برنامج مع مجموعة من الإجراءات (العقد) المحددة جيدًا التي يمكنها حل المهام المسندة إليها من خلال محركات مختلفة. على سبيل المثال، المدمج في العنصر Cache قد تستخدم برنامج التشغيل: file، memcachedأو redis.

عند بناء مكوننا ، سنطبق مبدأ تصميم الجسر ، وهو نفس المبدأ الذي بنيت عليه مكونات Laravel.

لذلك دعونا نبدأ.

السائقين


وكما يقول أحد الفرضيات: البرنامج وفق الواجهة وليس مع التطبيق.

سنبدأ به ، ولكن في سياق المكون ، يكون استخدام مصطلح العقد أكثر دقة. العقد هو في الأساس مجموعة من الواجهات لوصف وظيفة المكون.

interface SmsContract
{
    public function send();
}

بعد ذلك ، أضف السائقين الذين ينفذون العقد. نأخذ المنطق العام في فئة مجردة.

abstract class Driver implements SmsContract
{
    protected $message;

    protected $recipient;

    public function to($phoneNumber)
    {
        $this->recipient = $phoneNumber;
    }

    public function content($message)
    {
        $this->message = $message;
    }

    abstract public function send();
}

وسنصف مباشرة منطق الإرسال في كل فئة سائق.

class SmsRuDriver extends Driver
{
    protected $client;

    protected $from;

    public function __construct(SmsRuClient $smsRuClient, $from)
    {
        $this->client = $smsRuClient;
        $this->from = $from;
    }

    public function send()
    {
        return $this->client->sendSms([
            'type' => 'text',
            'from' => $this->from,
            'to' => $this->recipient,
            'text' => trim($this->message)
        ]);
    }
}

class SmartSenderDriver extends Driver
{
    protected $client;

    protected $from;

    public function __construct(SmartSenderClient $smartSenderClient, $from)
    {
        $this->client = $smartSenderClient;
        $this->from = $from;
    }

    public function send()
    {
        return $this->client->message()->send([
            'type' => 'text',
            'from' => $this->from,
            'to' => $this->recipient,
            'text' => trim($this->message)
        ]);
    }
}

إدارة المكونات


لتحديد وتكوين برامج التشغيل ، أضف فئة SmsManagerموروثة من الفصل Manager.

use Illuminate\Support\Manager;

class SmsManager extends Manager
{
    public function setDriver($name = null)
    {
        return $this->driver($name);
    }

    public function createSmsRuDriver(): SmsContract
    {
        return new SmsRuDriver(
            $this->createSmsRuClient(),
            $this->app['config']['sms.sms_ru.from']
        );
    }

    public function createSmartSenderDriver(): SmsContract
    {
        return new SmartSenderDriver(
            $this->createSmartSenderClient(),
            $this->app['config']['sms.smart_sender.from']
        );
    }

    public function getDefaultDriver()
    {
        return $this->app['config']['sms.default'];
    }

    protected function createSmsRuClient()
    {
        return new SmsRuClient(
            $this->app['config']['sms.sms_ru.key'],
            $this->app['config']['sms.sms_ru.secret']
        );
    }

    protected function createSmartSenderClient()
    {
        return new SmartSenderClient(
            $this->app['config']['sms.smart_sender.key'],
            $this->app['config']['sms.smart_sender.secret']
        );
    }

}

الآن يكفي تحديد المعلمات الضرورية في ملف التكوين وسيخبر الفصل SmsManager المكون الخاص بنا من خلال برنامج التشغيل لإرسال رسائل SMS.

مظهر زائف


للوصول إلى وظائف المكون من أي مكان في التطبيق ، أضف واجهة .
أولاً ، قم بإنشاء فئة الواجهة نفسها:

use Illuminate\Support\Facades\Facade;

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

بعد ذلك ، نقوم بربط الصف SmsManager بالواجهة باستخدام موفر الخدمة وتسجيل اسم مستعار.

class SmsServiceProvider extends ServiceProvider
{
    protected $defer = true;
    
    public function register()
    {
        $this->app->singleton('sms', function ($app) {
            return new SmsManager($app);
        });
    }
    
    public function provides()
    {
        return ['sms'];
    }
}

وأخيرًا ، قم بتسجيل مزود الخدمة الخاص بنا جنبًا إلى جنب مع المزودين الآخرين في ملف التكوين ( app/config/app.php).

'providers' => [
    ...
    App\Providers\SmsServiceProvider::class,
],    

تطبيق


عندما يتم إنشاء واجهة لمكوننا ويتم تحديد المعلمات في ملف التكوين ، لإرسال رسالة SMS ، ما عليك سوى إضافة الرمز التالي:

Sms::to($phone)->content('Beware! He`s not who he is')->send();

أو ، إذا كنت بحاجة إلى تحديد برنامج التشغيل بشكل صريح:

Sms::setDriver('sms_ru')->to($phone)->content('why don`t you answer me?')->send();

استنتاج


تم بناء المكون الخاص بنا على مبدأ تصميم الجسر ، أي أنه يتم فصل التنفيذ والتجريد بطريقة يمكن تغييرها بشكل مستقل. يمكننا إدارة برامج التشغيل من خلال الفصل الدراسي SmsManager، بينما يظل رمز التطبيق نفسه دون تغيير.
توفر الواجهة وصولاً سهلاً إلى وظائف المكون الخاص بنا.

يوفر هذا النهج لتصميم المكونات البساطة والمرونة في استخدامها.

المراجع


لارافل الإجتماعي المكون للمصادقة من خلال مختلف الشبكات الاجتماعية

All Articles