golossus/php-lazy-proxy-loading

Provides a proxy class factory to load services in a lazy and seamless way

v1.0.2 2021-01-19 11:10 UTC

This package is auto-updated.

Last update: 2024-03-28 12:08:06 UTC


README

Build Status codecov Version Total Downloads License

58183018

php-lazy-proxy-loading is a package which tries to ease in the process of loading PHP classes (or services) by using a lazy strategy. This comes in handy when working with Dependency Injection containers which are in charge of building the services of many applications.

This package does so by providing a Proxy Class Factory which can create a Proxy Class for any PHP Class or service. Real service is only built when the service is really needed, that is at first call of its public methods.

It is specially interesting to combine this library with Laravel framework, given the fact that currently there's no a bult-in strategy in this framework to provide lazy-loading of services. We started this library in order to solve Dependency Injection issues we had using Laravel that could be solver very easily by lazy-loading some services.

Installation

composer require golossus/php-lazy-proxy-loading

Usage

Basic usage example:

use Golossus\LazyProxyLoading\ProxyClassFactory;
...
$myClassProxy = ProxyClassFactory::create(\MyClass::class, function () {
    return new \MyClass(/* dependencies */);
}); 

It's worth noting in the above example $myClassProxy extends MyClass type, and it serves as an adapter behaving as the underlying service, so it can be injected seamlessly as the real service. Final service will be built once we call any of its public methods by using the callback function passed as second argument of the ProxyClassFactory::create method.

echo $myProxyClass->myClassPrublicMethod(); // MyClass service is created at this point
echo $myProxyClass->myClassPrublicMethod(); // MyClass service is already created at this point, so it's not re-built

Laravel

For Laravel framework, this package provides a special trait to seamlessly use the proxy factory with its Dependency Injection container. You can use the trait in any Laravel service provider and define any service as lazy:

..
use Illuminate\Support\ServiceProvider;
use Golossus\LazyProxyLoading\LazyLoadingTrait;
..

class MainProvider extends ServiceProvider
{
    use LazyLoadingTrait;

    public function register()
    {
        // common laravel service registration, not lazy
        $this->app->singleton(CustomMailingService::class, function () {
                $mailer = $this->app->make(Mailer::class);
                return new CustomMailingService($mailer);
            }
        );
    
        // or same service registration but lazy loaded
        $this->app->singleton(...$this->lazy(CustomMailingService::class, function () {
                $mailer = $this->app->make(Mailer::class);

                return new CustomMailingService($mailer);
            }
        ));
    }
}

Community

Contributing

This is an Open Source project. The Golossus team wants to enable it to be community-driven and open to contributors. Take a look at contributing documentation.

Security Issues

If you discover a security vulnerability, please follow our disclosure procedure.

About Us

This package development is led by the Golossus Team Leaders and supported by contributors.