Blast facades
![Software License][ico-license]
![Total Downloads][ico-downloads]
, (*1)
Blast facades is aiming to minimize complexity and represent dependencies as generic facades. This package is part of Blast component collection., (*2)
This package is compliant with PSR-1, PSR-2 and PSR-4. If you notice compliance oversights,
please send a patch via pull request., (*3)
This package is also supporting container-interop (PSR-11) and all container packages which are using container-interop., (*4)
Install
Via Composer, (*5)
``` bash
$ composer require blast/facades, (*6)
## Usage
### Initialize
First of all we need to tell FacadeFactory which container instance should be used. We could use [thephpleague/container](http://container.thephpleague.com/) for example:
A bootstrap is looking like this:
```php
<?php
use Blast\Facades\FacadeFactory;
use League\Container\Container;
$container = new Container();
FacadeFactory::setContainer($container);
Dealing with dependencies
We need to register a service to our container, for example with league/container., (*7)
There are several ways to now register this service with the container., (*8)
For a better transparency and design we recommend to pass an Interface or Contract FQCN as service id., (*9)
<?php
//add our service
$container->add('Acme\Service', 'Acme\Service\SomeService');
//returns an instance of Acme\Service\SomeService
$container->get('Acme\Service');
or, (*10)
<?php
//add our service
$container->add('Acme\Service\SomeService');
//returns an instance of Acme\Service\SomeService
$container->get('Acme\Service\SomeService');
or, (*11)
<?php
//returns an instance of Acme\Service\SomeService without registration
$container->get('Acme\Service\SomeService');
for more information please read league/container documentation, (*12)
Creating and using a facade
A Facade should be an instance of AbstractFacade and should provide an accessor., (*13)
The accessor is the service identifier. It will be used to fetch the service in the container (via Interop\Container\ContainerInterface::get($accessor)
., (*14)
<?php
namespace Acme\Facades\Service;
use Blast\Facades\AbstractFacade;
use Acme\Service;
class Service extends AbstractFacade
{
protected static function accessor()
{
return Acme\Service::class;
}
}
We could now call serivce methods from our facade, or the service instance itself., (*15)
Calling service instance
<?php
use Acme\Facades\Service;
//is returning the service instance
$service = Service::__instance();
Calling service methods
<?php
use Acme\Facades\Service;
Service::someMethod();
or, (*16)
<?php
use Acme\Facades\Service;
//is returning the service instance
$service = Service::__instance();
$service->someMethod();
or, (*17)
<?php
use Acme\Facades\Service;
forward_static_call([Service::class, 'someMethod']);
or, (*18)
<?php
use Acme\Facades\Service;
call_user_func(sprintf('%s::%s', Service::class, 'someMethod'));
You are also able to pass arguments. The method call itself is behaving like the call of original class., (*19)
Instance swaping
Sometimes service will be replaced by another service. As long as the service id is not changing, we don't need to modify
anything in our facade., (*20)
<?php
use Acme\Facades\Service;
//add a service
$container->add('Acme\Service', 'Acme\Service\SomeService');
//is returning the service instance Acme\Service\SomeService
$service = Service::__instance();
//replace a service with another one
$container->add('Acme\Service', 'Acme\Service\AnotherService');
//is now returning the service instance Acme\Service\AnotherService
$service = Service::__instance();
Supporting projects
Projects using ContainerInterface
Change log
Please see CHANGELOG for more information what has changed recently., (*21)
Testing
bash
$ composer test
, (*22)
Contributing
Please see CONTRIBUTING for details., (*23)
Security
If you discover any security related issues, please email :author_email instead of using the issue tracker., (*24)
Credits
Special thanks
License
The MIT License (MIT). Please see License File for more information., (*25)