|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @link http://github.com/zendframework/zend-mvc for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com) |
| 5 | + * @license https://github.com/zendframework/zend-mvc/blob/master/LICENSE.md New BSD License |
| 6 | + * @SuppressWarnings(PHPMD) |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace Zend\Mvc\Controller; |
| 12 | + |
| 13 | +use Interop\Container\ContainerInterface; |
| 14 | +use ReflectionClass; |
| 15 | +use ReflectionParameter; |
| 16 | +use Zend\Console\Adapter\AdapterInterface as ConsoleAdapterInterface; |
| 17 | +use Zend\Filter\FilterPluginManager; |
| 18 | +use Zend\Hydrator\HydratorPluginManager; |
| 19 | +use Zend\InputFilter\InputFilterPluginManager; |
| 20 | +use Zend\Log\FilterPluginManager as LogFilterManager; |
| 21 | +use Zend\Log\FormatterPluginManager as LogFormatterManager; |
| 22 | +use Zend\Log\ProcessorPluginManager as LogProcessorManager; |
| 23 | +use Zend\Log\WriterPluginManager as LogWriterManager; |
| 24 | +use Zend\Serializer\AdapterPluginManager as SerializerAdapterManager; |
| 25 | +use Zend\ServiceManager\AbstractFactoryInterface; |
| 26 | +use Zend\ServiceManager\Exception\ServiceNotFoundException; |
| 27 | +use Zend\ServiceManager\ServiceLocatorInterface; |
| 28 | +use Zend\Stdlib\DispatchableInterface; |
| 29 | +use Zend\Validator\ValidatorPluginManager; |
| 30 | + |
| 31 | +/** |
| 32 | + * Reflection-based factory for controllers. |
| 33 | + * |
| 34 | + * To ease development, this factory may be used for controllers with |
| 35 | + * type-hinted arguments that resolve to services in the application |
| 36 | + * container; this allows omitting the step of writing a factory for |
| 37 | + * each controller. |
| 38 | + * |
| 39 | + * You may use it as either an abstract factory: |
| 40 | + * |
| 41 | + * <code> |
| 42 | + * 'controllers' => [ |
| 43 | + * 'abstract_factories' => [ |
| 44 | + * LazyControllerAbstractFactory::class, |
| 45 | + * ], |
| 46 | + * ], |
| 47 | + * </code> |
| 48 | + * |
| 49 | + * Or as a factory, mapping a controller class name to it: |
| 50 | + * |
| 51 | + * <code> |
| 52 | + * 'controllers' => [ |
| 53 | + * 'factories' => [ |
| 54 | + * MyControllerWithDependencies::class => LazyControllerAbstractFactory::class, |
| 55 | + * ], |
| 56 | + * ], |
| 57 | + * </code> |
| 58 | + * |
| 59 | + * The latter approach is more explicit, and also more performant. |
| 60 | + * |
| 61 | + * The factory has the following constraints/features: |
| 62 | + * |
| 63 | + * - A parameter named `$config` typehinted as an array will receive the |
| 64 | + * application "config" service (i.e., the merged configuration). |
| 65 | + * - Parameters type-hinted against array, but not named `$config` will |
| 66 | + * be injected with an empty array. |
| 67 | + * - Scalar parameters will be resolved as null values. |
| 68 | + * - If a service cannot be found for a given typehint, the factory will |
| 69 | + * raise an exception detailing this. |
| 70 | + * - Some services provided by Zend Framework components do not have |
| 71 | + * entries based on their class name (for historical reasons); the |
| 72 | + * factory contains a map of these class/interface names to the |
| 73 | + * corresponding service name to allow them to resolve. |
| 74 | + * |
| 75 | + * `$options` passed to the factory are ignored in all cases, as we cannot |
| 76 | + * make assumptions about which argument(s) they might replace. |
| 77 | + */ |
| 78 | +class LazyControllerAbstractFactory implements AbstractFactoryInterface |
| 79 | +{ |
| 80 | + /** |
| 81 | + * Maps known classes/interfaces to the service that provides them; only |
| 82 | + * required for those services with no entry based on the class/interface |
| 83 | + * name. |
| 84 | + * |
| 85 | + * Extend the class if you wish to add to the list. |
| 86 | + * |
| 87 | + * @var string[] |
| 88 | + */ |
| 89 | + protected $aliases = [ |
| 90 | + ConsoleAdapterInterface::class => 'ConsoleAdapter', |
| 91 | + FilterPluginManager::class => 'FilterManager', |
| 92 | + HydratorPluginManager::class => 'HydratorManager', |
| 93 | + InputFilterPluginManager::class => 'InputFilterManager', |
| 94 | + LogFilterManager::class => 'LogFilterManager', |
| 95 | + LogFormatterManager::class => 'LogFormatterManager', |
| 96 | + LogProcessorManager::class => 'LogProcessorManager', |
| 97 | + LogWriterManager::class => 'LogWriterManager', |
| 98 | + SerializerAdapterManager::class => 'SerializerAdapterManager', |
| 99 | + ValidatorPluginManager::class => 'ValidatorManager', |
| 100 | + ]; |
| 101 | + |
| 102 | + /** |
| 103 | + * {@inheritDoc} |
| 104 | + * |
| 105 | + * @return DispatchableInterface |
| 106 | + */ |
| 107 | + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
| 108 | + { |
| 109 | + $reflectionClass = new ReflectionClass($requestedName); |
| 110 | + |
| 111 | + if (null === ($constructor = $reflectionClass->getConstructor())) { |
| 112 | + return new $requestedName(); |
| 113 | + } |
| 114 | + |
| 115 | + $reflectionParameters = $constructor->getParameters(); |
| 116 | + |
| 117 | + if (empty($reflectionParameters)) { |
| 118 | + return new $requestedName(); |
| 119 | + } |
| 120 | + |
| 121 | + $parameters = array_map( |
| 122 | + $this->resolveParameter($container->getServiceLocator(), $requestedName), |
| 123 | + $reflectionParameters |
| 124 | + ); |
| 125 | + |
| 126 | + return new $requestedName(...$parameters); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * {@inheritDoc} |
| 131 | + */ |
| 132 | + public function canCreate(ContainerInterface $container, $requestedName) |
| 133 | + { |
| 134 | + if (! class_exists($requestedName)) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + return in_array(DispatchableInterface::class, class_implements($requestedName), true); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Resolve a parameter to a value. |
| 143 | + * |
| 144 | + * Returns a callback for resolving a parameter to a value. |
| 145 | + * |
| 146 | + * @param ContainerInterface $container |
| 147 | + * @param string $requestedName |
| 148 | + * @return callable |
| 149 | + */ |
| 150 | + private function resolveParameter(ContainerInterface $container, $requestedName) |
| 151 | + { |
| 152 | + /** |
| 153 | + * @param ReflectionClass $parameter |
| 154 | + * @return mixed |
| 155 | + * @throws ServiceNotFoundException If type-hinted parameter cannot be |
| 156 | + * resolved to a service in the container. |
| 157 | + */ |
| 158 | + return function (ReflectionParameter $parameter) use ($container, $requestedName) { |
| 159 | + if ($parameter->isArray() |
| 160 | + && $parameter->getName() === 'config' |
| 161 | + && $container->has('config') |
| 162 | + ) { |
| 163 | + return $container->get('config'); |
| 164 | + } |
| 165 | + |
| 166 | + if ($parameter->isArray()) { |
| 167 | + return []; |
| 168 | + } |
| 169 | + |
| 170 | + if (! $parameter->getClass()) { |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + $type = $parameter->getClass()->getName(); |
| 175 | + $type = isset($this->aliases[$type]) ? $this->aliases[$type] : $type; |
| 176 | + |
| 177 | + if (! $container->has($type)) { |
| 178 | + throw new ServiceNotFoundException(sprintf( |
| 179 | + 'Unable to create controller "%s"; unable to resolve parameter "%s" using type hint "%s"', |
| 180 | + $requestedName, |
| 181 | + $parameter->getName(), |
| 182 | + $type |
| 183 | + )); |
| 184 | + } |
| 185 | + |
| 186 | + return $container->get($type); |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Determine if we can create a service with name |
| 192 | + * |
| 193 | + * @param ServiceLocatorInterface $serviceLocator |
| 194 | + * @param $name |
| 195 | + * @param $requestedName |
| 196 | + * @return bool |
| 197 | + * @SuppressWarnings("unused") |
| 198 | + */ |
| 199 | + public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
| 200 | + { |
| 201 | + return $this->canCreate($serviceLocator, $requestedName); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Create service with name |
| 206 | + * |
| 207 | + * @param ServiceLocatorInterface $serviceLocator |
| 208 | + * @param $name |
| 209 | + * @param $requestedName |
| 210 | + * @return mixed |
| 211 | + * @SuppressWarnings("unused") |
| 212 | + */ |
| 213 | + public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
| 214 | + { |
| 215 | + return $this($serviceLocator, $requestedName); |
| 216 | + } |
| 217 | +} |
0 commit comments