Annotation
5 years ago
Definition
5 years ago
Invoker
5 years ago
Proxy
5 years ago
Reflection
5 years ago
Container.php
5 years ago
ContainerBuilder.php
5 years ago
Debug.php
5 years ago
DependencyException.php
5 years ago
FactoryInterface.php
5 years ago
InvokerInterface.php
5 years ago
NotFoundException.php
5 years ago
Scope.php
5 years ago
functions.php
5 years ago
ContainerBuilder.php
259 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PHP-DI |
| 4 | * |
| 5 | * @link http://php-di.org/ |
| 6 | * @copyright Matthieu Napoli (http://mnapoli.fr/) |
| 7 | * @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file) |
| 8 | */ |
| 9 | |
| 10 | namespace Cybot\Dependencies\DI; |
| 11 | |
| 12 | use Cybot\Dependencies\DI\Definition\Source\AnnotationReader; |
| 13 | use Cybot\Dependencies\DI\Definition\Source\DefinitionArray; |
| 14 | use Cybot\Dependencies\DI\Definition\Source\CachedDefinitionSource; |
| 15 | use Cybot\Dependencies\DI\Definition\Source\DefinitionSource; |
| 16 | use Cybot\Dependencies\DI\Definition\Source\DefinitionFile; |
| 17 | use Cybot\Dependencies\DI\Definition\Source\Autowiring; |
| 18 | use Cybot\Dependencies\DI\Definition\Source\SourceChain; |
| 19 | use Cybot\Dependencies\DI\Proxy\ProxyFactory; |
| 20 | use Doctrine\Common\Cache\Cache; |
| 21 | use Cybot\Dependencies\Interop\Container\ContainerInterface; |
| 22 | use InvalidArgumentException; |
| 23 | |
| 24 | /** |
| 25 | * Helper to create and configure a Container. |
| 26 | * |
| 27 | * With the default options, the container created is appropriate for the development environment. |
| 28 | * |
| 29 | * Example: |
| 30 | * |
| 31 | * $builder = new ContainerBuilder(); |
| 32 | * $container = $builder->build(); |
| 33 | * |
| 34 | * @since 3.2 |
| 35 | * @author Matthieu Napoli <matthieu@mnapoli.fr> |
| 36 | */ |
| 37 | class ContainerBuilder |
| 38 | { |
| 39 | /** |
| 40 | * Name of the container class, used to create the container. |
| 41 | * @var string |
| 42 | */ |
| 43 | private $containerClass; |
| 44 | |
| 45 | /** |
| 46 | * @var boolean |
| 47 | */ |
| 48 | private $useAutowiring = true; |
| 49 | |
| 50 | /** |
| 51 | * @var boolean |
| 52 | */ |
| 53 | private $useAnnotations = false; |
| 54 | |
| 55 | /** |
| 56 | * @var boolean |
| 57 | */ |
| 58 | private $ignorePhpDocErrors = false; |
| 59 | |
| 60 | /** |
| 61 | * @var Cache |
| 62 | */ |
| 63 | private $cache; |
| 64 | |
| 65 | /** |
| 66 | * If true, write the proxies to disk to improve performances. |
| 67 | * @var boolean |
| 68 | */ |
| 69 | private $writeProxiesToFile = false; |
| 70 | |
| 71 | /** |
| 72 | * Directory where to write the proxies (if $writeProxiesToFile is enabled). |
| 73 | * @var string |
| 74 | */ |
| 75 | private $proxyDirectory; |
| 76 | |
| 77 | /** |
| 78 | * If PHP-DI is wrapped in another container, this references the wrapper. |
| 79 | * @var ContainerInterface |
| 80 | */ |
| 81 | private $wrapperContainer; |
| 82 | |
| 83 | /** |
| 84 | * @var DefinitionSource[] |
| 85 | */ |
| 86 | private $definitionSources = []; |
| 87 | |
| 88 | /** |
| 89 | * Build a container configured for the dev environment. |
| 90 | * |
| 91 | * @return Container |
| 92 | */ |
| 93 | public static function buildDevContainer() |
| 94 | { |
| 95 | $builder = new self(); |
| 96 | return $builder->build(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param string $containerClass Name of the container class, used to create the container. |
| 101 | */ |
| 102 | public function __construct($containerClass = 'Cybot\Dependencies\DI\Container') |
| 103 | { |
| 104 | $this->containerClass = $containerClass; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Build and return a container. |
| 109 | * |
| 110 | * @return Container |
| 111 | */ |
| 112 | public function build() |
| 113 | { |
| 114 | $sources = array_reverse($this->definitionSources); |
| 115 | if ($this->useAnnotations) { |
| 116 | $sources[] = new AnnotationReader($this->ignorePhpDocErrors); |
| 117 | } elseif ($this->useAutowiring) { |
| 118 | $sources[] = new Autowiring(); |
| 119 | } |
| 120 | |
| 121 | $chain = new SourceChain($sources); |
| 122 | |
| 123 | if ($this->cache) { |
| 124 | $source = new CachedDefinitionSource($chain, $this->cache); |
| 125 | $chain->setRootDefinitionSource($source); |
| 126 | } else { |
| 127 | $source = $chain; |
| 128 | // Mutable definition source |
| 129 | $source->setMutableDefinitionSource(new DefinitionArray()); |
| 130 | } |
| 131 | |
| 132 | $proxyFactory = new ProxyFactory($this->writeProxiesToFile, $this->proxyDirectory); |
| 133 | |
| 134 | $containerClass = $this->containerClass; |
| 135 | |
| 136 | return new $containerClass($source, $proxyFactory, $this->wrapperContainer); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Enable or disable the use of autowiring to guess injections. |
| 141 | * |
| 142 | * Enabled by default. |
| 143 | * |
| 144 | * @param boolean $bool |
| 145 | * @return ContainerBuilder |
| 146 | */ |
| 147 | public function useAutowiring($bool) |
| 148 | { |
| 149 | $this->useAutowiring = $bool; |
| 150 | return $this; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Enable or disable the use of annotations to guess injections. |
| 155 | * |
| 156 | * Disabled by default. |
| 157 | * |
| 158 | * @param boolean $bool |
| 159 | * @return ContainerBuilder |
| 160 | */ |
| 161 | public function useAnnotations($bool) |
| 162 | { |
| 163 | $this->useAnnotations = $bool; |
| 164 | return $this; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Enable or disable ignoring phpdoc errors (non-existent classes in `@param` or `@var`) |
| 169 | * |
| 170 | * @param boolean $bool |
| 171 | * @return ContainerBuilder |
| 172 | */ |
| 173 | public function ignorePhpDocErrors($bool) |
| 174 | { |
| 175 | $this->ignorePhpDocErrors = $bool; |
| 176 | return $this; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Enables the use of a cache for the definitions. |
| 181 | * |
| 182 | * @param Cache $cache Cache backend to use |
| 183 | * @return ContainerBuilder |
| 184 | */ |
| 185 | public function setDefinitionCache(Cache $cache) |
| 186 | { |
| 187 | $this->cache = $cache; |
| 188 | return $this; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Configure the proxy generation |
| 193 | * |
| 194 | * For dev environment, use writeProxiesToFile(false) (default configuration) |
| 195 | * For production environment, use writeProxiesToFile(true, 'tmp/proxies') |
| 196 | * |
| 197 | * @param boolean $writeToFile If true, write the proxies to disk to improve performances |
| 198 | * @param string|null $proxyDirectory Directory where to write the proxies |
| 199 | * @return ContainerBuilder |
| 200 | * |
| 201 | * @throws InvalidArgumentException when writeToFile is set to true and the proxy directory is null |
| 202 | */ |
| 203 | public function writeProxiesToFile($writeToFile, $proxyDirectory = null) |
| 204 | { |
| 205 | $this->writeProxiesToFile = $writeToFile; |
| 206 | |
| 207 | if ($writeToFile && $proxyDirectory === null) { |
| 208 | throw new InvalidArgumentException( |
| 209 | "The proxy directory must be specified if you want to write proxies on disk" |
| 210 | ); |
| 211 | } |
| 212 | $this->proxyDirectory = $proxyDirectory; |
| 213 | |
| 214 | return $this; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * If PHP-DI's container is wrapped by another container, we can |
| 219 | * set this so that PHP-DI will use the wrapper rather than itself for building objects. |
| 220 | * |
| 221 | * @param ContainerInterface $otherContainer |
| 222 | * @return $this |
| 223 | */ |
| 224 | public function wrapContainer(ContainerInterface $otherContainer) |
| 225 | { |
| 226 | $this->wrapperContainer = $otherContainer; |
| 227 | |
| 228 | return $this; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Add definitions to the container. |
| 233 | * |
| 234 | * @param string|array|DefinitionSource $definitions Can be an array of definitions, the |
| 235 | * name of a file containing definitions |
| 236 | * or a DefinitionSource object. |
| 237 | * @return $this |
| 238 | */ |
| 239 | public function addDefinitions($definitions) |
| 240 | { |
| 241 | if (is_string($definitions)) { |
| 242 | // File |
| 243 | $definitions = new DefinitionFile($definitions); |
| 244 | } elseif (is_array($definitions)) { |
| 245 | $definitions = new DefinitionArray($definitions); |
| 246 | } elseif (! $definitions instanceof DefinitionSource) { |
| 247 | throw new InvalidArgumentException(sprintf( |
| 248 | '%s parameter must be a string, an array or a DefinitionSource object, %s given', |
| 249 | 'ContainerBuilder::addDefinitions()', |
| 250 | is_object($definitions) ? get_class($definitions) : gettype($definitions) |
| 251 | )); |
| 252 | } |
| 253 | |
| 254 | $this->definitionSources[] = $definitions; |
| 255 | |
| 256 | return $this; |
| 257 | } |
| 258 | } |
| 259 |