GithubHelp home page GithubHelp logo

imanghafoori1 / laravel-smart-facades Goto Github PK

View Code? Open in Web Editor NEW
114.0 5.0 8.0 103 KB

Strategy design pattern in laravel, the easiest way.

License: MIT License

PHP 100.00%
strategy-pattern strategy-design-pattern laravel-framework laravel-package laravel-facade

laravel-smart-facades's Introduction

🍄 Laravel Smart Facades 🍄

The "Strategy pattern" in Laravel, made easy

by adding some features on top of Laravel facades.

Built with ❤️ for every smart Laravel developer

                                           Quality Score Code Quality Software License Check Imports StyleCI

🔦 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 binding a string to a concrete class with an IOC container, you can choose the low-level implementation class like this:

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

You can proxy to 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, let's 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

🔧 Automatic method injection when calling a method through a facade.

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

🙋 Contributing:

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

⭐ Your Stars Make Us Do More ⭐

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 willingness.

More from the author:

Laravel Microscope

💎 Automatically find bugs before they bite.


Laravel Widgetize

💎 A minimal yet powerful package to give a better structure and caching opportunity for your Laravel apps.


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

"Albert Einstein"

laravel-smart-facades's People

Contributors

amirsadeghi1 avatar i-iman-i avatar imanghafoori1 avatar krisell avatar mehradsadeghi avatar rahi69 avatar stylecibot avatar svenluijten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-smart-facades's Issues

shouldProxyTo does not work

hello, there seems to be a small problem
Here I should have an (A item) and a (B item) in the output but I only have two (A)
Please take a look, thank you
console router:

Artisan::command('test', function () {
    FooModelSmartFacade::shouldProxyTo(\App\FooA::class);
    $resultA = FooModelSmartFacade::query()->first()->toArray();

    FooModelSmartFacade::shouldProxyTo(\App\FooB::class);
    $resultB = FooModelSmartFacade::query()->first()->toArray();
    dump($resultA, $resultB);
});

my facade:

use Illuminate\Database\Eloquent\Model;

/**
 * @mixin Model
 */
class FooModelSmartFacade extends SmartFacade
{

}

models:

namespace App;

use Illuminate\Database\Eloquent\Model;

class FooA extends Model
{
    protected $table = 'foo_a';
    protected $fillable = [
        'title'
    ];

}
namespace App;

use Illuminate\Database\Eloquent\Model;

class FooB extends Model
{
    protected $table = 'foo_b';
    protected $fillable = [
        'title'
    ];

}

DB rows:
10-09-1401 22-21-01

console result:
10-09-1401 22-19-24

test on laravel version: (8.83.0) and (6.20.8)
package version: (v1.0.9)
php version: (7.4.19)

Releasing Latest Code Base

Hello Iman.
It seems you haven't released (tagged) the latest changes of your code-base on packagist.
When one downloads the latest version of package (v1.0.6) some functionalities are not available such as withDriver and changeProxyTo methods (these methods are missing from Imanghafoori\SmartFacades\Facade.php file).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.