golossus / php-lazy-proxy-loading
Provides a proxy class factory to load services in a lazy and seamless way
Installs: 4 268
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 0
Open Issues: 1
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: >=7.5
README
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
- Join our Slack to meet the community and get support.
- Follow us on GitHub.
- Read our Code of Conduct.
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.