imanghafoori1 / laravel-smart-facades by imanghafoori1

Adds some features on the top of laravel facades
197,371
129
5
Package Data
Maintainer Username: imanghafoori1
Maintainer Contact: imanghafoori1@gmail.com (Iman Ghafoori)
Package Create Date: 2019-01-20
Package Last Update: 2024-09-30
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-12-16 15:03:49
Package Statistics
Total Downloads: 197,371
Monthly Downloads: 11,486
Daily Downloads: 298
Total Stars: 129
Total Watchers: 5
Total Forks: 12
Total Open Issues: 1

                                           Code Quality Build Status Software License StyleCI

:flashlight: Installation:

composer require imanghafoori/laravel-smart-facades

⚡️ No need to have getFacadeAccessor()

Before:

use Illuminate\Support\Facades\Facade;

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

After:

use Imanghafoori\SmartFacades\Facade;

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

⚡️ Setting the default driver by shouldProxyTo($class):

Instead of bind a string to a concrete class with IOC container, you can choose the low level implementation class like this:


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

You can proxyTo any abstract string (or closure) bound on the IoC container.

Note : If you invoke it twice, it will override:

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

⚡️ Using Non-default Driver:

If you want to change the driver at call site:

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

⚡️ Method Hooks:

You can introduce some code "Before" and "after" a method call, remotely: (like event listeners on eloquent models)

image

Here we have told the system evenever the MyFacade::findUser($id) method was called in the system, to perform a log.

⚡️ Choosing the driver, based on parameters value:

For example, lets say you want your facade to use an SMS based driver by default, but if the text is very long (more than 200 chars) it should use an email driver.

You can do it like this:

image

:wrench: Automatic method injection when calling a method through a facade.

This adds ability to enjoy automatic method injection when calling methods on POPOs (Plain Old Php Objects) WITHOUT any performance hit when you do not need it.

🐙 Example:

class Foo { ... }

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

Calling Bar through a Facade :

Before:

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

After:

 // 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 dependecies your self :
\Facades\Bar::m1(new Foo('hey man!'), 'hey there !');   //Now only the Logger is injected

:raising_hand: Contributing

If you find an issue, or have a better way to do something, feel free to open an issue or a pull request.

:star: Your Stars Make Us Do More :star:

As always if you found this package useful and you want to encourage us to maintain and work on it. Just press the star button to declare your willing.

More from the author:

Laravel Microscope (new*)

:gem: Automatically find bugs before they bite.

  • https://github.com/imanghafoori1/laravel-microscope

Laravel Widgetize

:gem: A minimal yet powerful package to give a better structure and caching opportunity for your laravel apps.

  • https://github.com/imanghafoori1/laravel-widgetize

Laravel Terminator

:gem: A minimal yet powerful package to give you opportunity to refactor your controllers.

  • https://github.com/imanghafoori1/laravel-terminator

🍌 Reward me a crypto-banana 🍌

so that I will have energy to start the next package for you.

  • Dodge Coin: DJEZr6GJ4Vx37LGF3zSng711AFZzmJTouN
  • LiteCoin: ltc1q82gnjkend684c5hvprg95fnja0ktjdfrhcu4c4
  • BitCoin: bc1q53dys3jkv0h4vhl88yqhqzyujvk35x8wad7uf9
  • Ripple: rJwrb2v1TR6rAHRWwcYvNZxjDN2bYpYXhZ
  • Etherium: 0xa4898246820bbC8f677A97C2B73e6DBB9510151e

It's not I am smarter or something, I just stay with the problems longer.

"Albert Einstein"