|
| 1 | +# Proxy generator for Symfony's DIC |
| 2 | + |
| 3 | +[](https://packagist.org/packages/olvlvl/symfony-dependency-injection-proxy) |
| 4 | +[](http://travis-ci.org/olvlvl/symfony-dependency-injection-proxy) |
| 5 | +[](https://scrutinizer-ci.com/g/olvlvl/symfony-dependency-injection-proxy) |
| 6 | +[](https://coveralls.io/r/olvlvl/symfony-dependency-injection-proxy) |
| 7 | +[](https://packagist.org/packages/olvlvl/symfony-dependency-injection-proxy) |
| 8 | + |
| 9 | +This package provides a proxy generator for [Symfony's dependency injection component][1] that generates super tiny, |
| 10 | +super simple proxies, especially when [compared to Symphony's default implementation][2]. |
| 11 | + |
| 12 | +> If you're not familiar with proxy services, better have a look at [Symfony's documentation][3] before going any |
| 13 | +> further. |
| 14 | +
|
| 15 | +The generator works with the following assumptions: the service we want to proxy implements an interface and services |
| 16 | +using that service expect that interface too. Pretty normal stuff. Consider the following code, where an |
| 17 | +`ExceptionHandler` service requires a logger implementing `LogInterface`: |
| 18 | + |
| 19 | +```php |
| 20 | +<?php |
| 21 | + |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | + |
| 24 | +class ExceptionHandler |
| 25 | +{ |
| 26 | + private $logger; |
| 27 | + |
| 28 | + public function __construct(LoggerInterface $logger) |
| 29 | + { |
| 30 | + $this->logger = $logger; |
| 31 | + } |
| 32 | + |
| 33 | + // … |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Now imagine we're using [Monolog](https://github.com/Seldaek/monolog) as a logger, and we have an expansive stream to |
| 38 | +setup. Why waste time building that logger for every request when it's seldom used? That's when we mark our service as |
| 39 | +_lazy_. |
| 40 | + |
| 41 | +The following example demonstrates how we can mark our `Psr\Log\LoggerInterface` service as lazy (we could use PHP code |
| 42 | +or XML just the same): |
| 43 | + |
| 44 | +```yaml |
| 45 | +services: |
| 46 | + |
| 47 | + Psr\Log\LoggerInterface: |
| 48 | + class: Monolog\Logger |
| 49 | + lazy: true |
| 50 | + # … |
| 51 | +``` |
| 52 | + |
| 53 | +The service can also use a factory: |
| 54 | + |
| 55 | +```yaml |
| 56 | +services: |
| 57 | + |
| 58 | + Psr\Log\LoggerInterface: |
| 59 | + factory: 'LoggerFactory::build' |
| 60 | + lazy: true |
| 61 | + # … |
| 62 | +``` |
| 63 | + |
| 64 | +> We don't have to define our service with a class, we could use `logger` instead of `Psr\Log\LoggerInterface` just |
| 65 | +> the same, except we would have to define `class` for the factory one. |
| 66 | +
|
| 67 | +Now let's see how to build our container. |
| 68 | + |
| 69 | +## Building the dependency injection container |
| 70 | + |
| 71 | +The following code demonstrates how to build, compile, and dump a container: |
| 72 | + |
| 73 | +```php |
| 74 | +<?php |
| 75 | + |
| 76 | +use olvlvl\SymfonyDependencyInjectionProxy\FactoryRenderer; |
| 77 | +use olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver\BasicInterfaceResolver; |
| 78 | +use olvlvl\SymfonyDependencyInjectionProxy\MethodRenderer; |
| 79 | +use olvlvl\SymfonyDependencyInjectionProxy\ProxyDumper; |
| 80 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 81 | +use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
| 82 | + |
| 83 | +$builder = new ContainerBuilder(); |
| 84 | + |
| 85 | +// … |
| 86 | +// Here we load our config, or build the container using clever PHP calls. |
| 87 | +// We might event have some compile passes to add. |
| 88 | +// … |
| 89 | + |
| 90 | +$builder->compile(); |
| 91 | + |
| 92 | +$dumper = new PhpDumper($builder); |
| 93 | +$dumper->setProxyDumper(new ProxyDumper( |
| 94 | + new BasicInterfaceResolver(), |
| 95 | + new FactoryRenderer(new MethodRenderer) |
| 96 | +)); |
| 97 | + |
| 98 | +/* @var string $containerFile */ |
| 99 | + |
| 100 | +file_put_contents($containerFile, $dumper->dump()); |
| 101 | +``` |
| 102 | + |
| 103 | +There you have it. We can use our container as usual and everything is awesome and cute. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +### What if my lazy service implements multiple interfaces? |
| 110 | + |
| 111 | +The basic interface resolver will have a hard time figuring out which interface to implement if a service implements |
| 112 | +many. For instance, if a service was an instance of `ArrayObject` the following exception would be raised: |
| 113 | + |
| 114 | +``` |
| 115 | +Don't know which interface to choose from for ArrayObject: IteratorAggregate, Traversable, ArrayAccess, Serializable, Countable. |
| 116 | +``` |
| 117 | + |
| 118 | +We can help by decorating the basic interface resolver with a map, and specify which interface to implement for which |
| 119 | +class: |
| 120 | + |
| 121 | +```php |
| 122 | +<?php |
| 123 | + |
| 124 | +use olvlvl\SymfonyDependencyInjectionProxy\FactoryRenderer; |
| 125 | +use olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver\BasicInterfaceResolver; |
| 126 | +use olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver\MapInterfaceResolver; |
| 127 | +use olvlvl\SymfonyDependencyInjectionProxy\MethodRenderer; |
| 128 | +use olvlvl\SymfonyDependencyInjectionProxy\ProxyDumper; |
| 129 | +use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
| 130 | + |
| 131 | +/* @var PhpDumper $dumper */ |
| 132 | + |
| 133 | +$dumper->setProxyDumper(new ProxyDumper( |
| 134 | + new MapInterfaceResolver(new BasicInterfaceResolver(), [ |
| 135 | + ArrayObject::class => ArrayAccess::class, |
| 136 | + ]), |
| 137 | + new FactoryRenderer(new MethodRenderer) |
| 138 | +)); |
| 139 | +``` |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +---------- |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +## Requirements |
| 152 | + |
| 153 | +The package requires PHP 7.1 or later. |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +## Installation |
| 160 | + |
| 161 | +The recommended way to install this package is through [Composer](http://getcomposer.org/): |
| 162 | + |
| 163 | + $ composer require olvlvl/symfony-dependency-injection-proxy |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +### Cloning the repository |
| 170 | + |
| 171 | +The package is [available on GitHub](https://github.com/olvlvl/symfony-dependency-injection-proxy), |
| 172 | +its repository can be cloned with the following command line: |
| 173 | + |
| 174 | + $ git clone https://github.com/olvlvl/symfony-dependency-injection-proxy.git |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +## Testing |
| 181 | + |
| 182 | +The test suite is ran with the `make test` command. [PHPUnit](https://phpunit.de/) and |
| 183 | +[Composer](http://getcomposer.org/) need to be globally available to run the suite. The command |
| 184 | +installs dependencies as required. The `make test-coverage` command runs test suite and also creates |
| 185 | +an HTML coverage report in `build/coverage`. If your environment doesn't meet the requirements you can run the tests |
| 186 | +with a container, run `make test-container` to create it. |
| 187 | + |
| 188 | +The package is continuously tested by [Travis CI](http://about.travis-ci.org/). |
| 189 | + |
| 190 | +[](http://travis-ci.org/olvlvl/symfony-dependency-injection-proxy) |
| 191 | +[](https://coveralls.io/r/olvlvl/symfony-dependency-injection-proxy) |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +## License |
| 198 | + |
| 199 | +**olvlvl/symfony-dependency-injection-proxy** is licensed under the New BSD License - See the [LICENSE](LICENSE) file for details. |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | +[1]: https://symfony.com/doc/current/components/dependency_injection.html |
| 207 | +[2]: https://github.com/olvlvl/symfony-dependency-injection-proxy/wiki/Comparing-olvlvl's-proxy-generator-with-Symphony's |
| 208 | +[3]: https://symfony.com/doc/current/service_container/lazy_services.html |
0 commit comments