|
| 1 | +# container-facade |
| 2 | + |
| 3 | +A standalone PHP library inspired by Laravel's Facade implementation, which can be used with any [PSR-11 compatible](https://www.php-fig.org/psr/psr-11/) dependency injection container (DIC) such as (the ones used by) [Symfony](https://symfony.com/), [Pimple](https://github.com/silexphp/Pimple), or [Slim](https://www.slimframework.com/). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To use this package, require it with Composer. |
| 8 | + |
| 9 | +```bash |
| 10 | +composer install geekcell/container-facade |
| 11 | +``` |
| 12 | + |
| 13 | +## Motivation |
| 14 | + |
| 15 | +Although rare, there are situations when you want to obtain a container service without dependency injection. An example would be the [`AggregateRoot` pattern](https://martinfowler.com/bliki/DDD_Aggregate.html), which allows [dispatching domain events](https://learn.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/domain-events-design-implementation) directly from the aggregate, which is usually created directly and not via a DIC. In such a case, a corresponding (static) service facade can provide a comparable convenience as a singleton, but without the inherent [disadvantages of the singleton pattern](https://stackoverflow.com/a/138012). |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Let's imagine you have a `Logger` service inside your DIC of choice that logs a message into a file. |
| 20 | + |
| 21 | +```php |
| 22 | +<?php |
| 23 | + |
| 24 | +namespace App\Service; |
| 25 | + |
| 26 | +// ... |
| 27 | + |
| 28 | +class Logger |
| 29 | +{ |
| 30 | + public function __construct( |
| 31 | + private readonly FileWriter $writer, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function log(string $message, LogLevel $level = LogLevel::INFO): void |
| 36 | + { |
| 37 | + $line = sprintf( |
| 38 | + '%s (%s): %s', |
| 39 | + (new \DateTime)->format('c'), |
| 40 | + $level->value, |
| 41 | + $message, |
| 42 | + ); |
| 43 | + |
| 44 | + $this->writer->writeLine($line); |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +If you want to "facade" this service, just create a class which extends `GeekCell\Facade\Facade`. |
| 50 | + |
| 51 | +```php |
| 52 | +<?php |
| 53 | + |
| 54 | +namespace App\Support\Facade; |
| 55 | + |
| 56 | +use App\Service\Logger as LoggerRoot; |
| 57 | +use GeekCell\Facade\Facade; |
| 58 | + |
| 59 | +class Logger extends Facade |
| 60 | +{ |
| 61 | + protected static function getFacadeAccessor(): string |
| 62 | + { |
| 63 | + return 'app.logger'; |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +You'll have to implement the `getFacadeAccessor()` method, which returns the identifier for service inside your DIC. |
| 69 | + |
| 70 | +Additionally, you have to "introduce" your DIC to the Facade. How to do this really depends in the framework you're using. In Symfony, a good opportunity to do so is to override the `boot()` method within `src/Kernel.php`. |
| 71 | + |
| 72 | +```php |
| 73 | +<?php |
| 74 | + |
| 75 | +namespace App; |
| 76 | + |
| 77 | +use GeekCell\Facade\Facade; |
| 78 | +use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
| 79 | +use Symfony\Component\HttpKernel\Kernel as BaseKernel; |
| 80 | + |
| 81 | +class Kernel extends BaseKernel |
| 82 | +{ |
| 83 | + use MicroKernelTrait; |
| 84 | + |
| 85 | + public function boot() |
| 86 | + { |
| 87 | + parent::boot(); |
| 88 | + |
| 89 | + // This is where the magic happens! |
| 90 | + Facade::setContainer($this->container); |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +To use the facade within any part of your application, just call the service as you would a static method. Behind the scenes, the call is delegated to the actual container service via `__callStatic`. |
| 96 | + |
| 97 | +```php |
| 98 | +<?php |
| 99 | + |
| 100 | +// ... |
| 101 | + |
| 102 | +use App\Support\Facade\Logger; |
| 103 | + |
| 104 | +class SomeClass |
| 105 | +{ |
| 106 | + public function doStuff() |
| 107 | + { |
| 108 | + Logger::log('Calling ' __CLASS__ . '::doStuff()', LogLevel::DEBUG); |
| 109 | + |
| 110 | + // The acutal method logic ... |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### Testing |
| 116 | + |
| 117 | +Although the above looks like an anti pattern, it's actually very testing friendly. During unit testing, you can use the `swapMock()` method to literally swap the real service with a Mockery mock. |
| 118 | + |
| 119 | +```php |
| 120 | +<?php |
| 121 | + |
| 122 | +// ... |
| 123 | + |
| 124 | +use App\Support\Facade\Logger; |
| 125 | +use PHPUnit\Framework\TestCase; |
| 126 | + |
| 127 | +class SomeClassTest extends TestCase |
| 128 | +{ |
| 129 | + public function tearDown(): void |
| 130 | + { |
| 131 | + Logger::clear(); |
| 132 | + } |
| 133 | + |
| 134 | + // ... |
| 135 | + |
| 136 | + public function testDoStuff(): void |
| 137 | + { |
| 138 | + // Swap real service with mock |
| 139 | + $loggerMock = Logger::swapMock(); |
| 140 | + |
| 141 | + // Set expectations for mock |
| 142 | + $loggerMock->shouldReceive('log')->once(); |
| 143 | + |
| 144 | + $out = new SomeClass(); |
| 145 | + $result = $out->doStuff(); // This will now call the mock! |
| 146 | + |
| 147 | + // Test assertions ... |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +**Hint:** You must call the `clear()` method to clear out the internally cached mock instance. For PHPUnit, you could use the `tearDown()` method to do so. |
| 153 | + |
| 154 | +## Examples |
| 155 | + |
| 156 | +See the `examples` directory for various sample projects with a minimal integration of this package. |
| 157 | + |
| 158 | +| Framwork | Sample project | |
| 159 | +| ----------- | ------------------------------------ | |
| 160 | +| Symfony | [examples/symfony](examples/symfony) | |
0 commit comments