PHP code example of imanghafoori / laravel-smart-facades

1. Go to this page and download the library: Download imanghafoori/laravel-smart-facades library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imanghafoori / laravel-smart-facades example snippets


use Illuminate\Support\Facades\Facade;

class MyFacade extends Facade
{
    protected static function getFacadeAccessor() // <--- normal facade
    {
        return 'some_key'; 
    }
}

use Imanghafoori\SmartFacades\Facade;

class MyFacade extends Facade
{
    //                                          <--- smart facade
}


public function register()    // <-- in a service provider
{             
    if ($someCondition) {
        MyFacade::shouldProxyTo( SomeDriver::class );
    } else {
        MyFacade::shouldProxyTo( SomeOtherDriver::class );
    }
}

MyFacade::shouldProxyTo( DriverClass1::class );
MyFacade::shouldProxyTo( DriverClass2::class ); // <--- This wins!

MyFacade::withDriver(nonDefaultDriver::class)::myMethod();

class Foo { ... }

class Bar
{
    // This has dependencies: "Foo", "LoggerInterface"
    public function m1 (Foo $foo, LoggerInterface $logger, string $msg)
    {
       
    }
}

MyFacade::m1(resolve(Foo::class), resolve(LoggerInterface::class), 'hey there !'); 

 // This will work and $foo, $logger would be auto-injected for us.

MyFacade::m1('hey there !');          // normal facade

// or you may want to provide some dependencies yourself:
\Facades\Bar::m1(new Foo('hey man!'), 'hey there !');   //Now only the Logger is injected