|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @link http://github.com/zendframework/zend-form for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) |
| 5 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Zend\Form\Annotation; |
| 9 | + |
| 10 | +use Interop\Container\ContainerInterface; |
| 11 | +use Zend\EventManager\EventManagerInterface; |
| 12 | +use Zend\EventManager\ListenerAggregateInterface; |
| 13 | +use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
| 14 | +use Zend\ServiceManager\FactoryInterface; |
| 15 | +use Zend\ServiceManager\ServiceLocatorInterface; |
| 16 | + |
| 17 | +class AnnotationBuilderFactory implements FactoryInterface |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @param ContainerInterface $container |
| 21 | + * @param string $name |
| 22 | + * @param null|array $options |
| 23 | + * @return AnnotationBuilder |
| 24 | + * @throws ServiceNotCreatedException for invalid listener configuration. |
| 25 | + */ |
| 26 | + public function __invoke(ContainerInterface $container, $name, array $options = null) |
| 27 | + { |
| 28 | + //setup a form factory which can use custom form elements |
| 29 | + $annotationBuilder = new AnnotationBuilder(); |
| 30 | + $eventManager = $container->get('EventManager'); |
| 31 | + $annotationBuilder->setEventManager($eventManager); |
| 32 | + |
| 33 | + $formElementManager = $container->get('FormElementManager'); |
| 34 | + $formElementManager->injectFactory($container, $annotationBuilder); |
| 35 | + |
| 36 | + $config = $this->marshalConfig($container); |
| 37 | + if (isset($config['preserve_defined_order'])) { |
| 38 | + $annotationBuilder->setPreserveDefinedOrder($config['preserve_defined_order']); |
| 39 | + } |
| 40 | + |
| 41 | + $this->injectAnnotations($config, $annotationBuilder); |
| 42 | + $this->injectListeners($config, $eventManager, $container); |
| 43 | + |
| 44 | + return $annotationBuilder; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Create and return AnnotationBuilder instance |
| 49 | + * |
| 50 | + * For use with zend-servicemanager v2; proxies to __invoke(). |
| 51 | + * |
| 52 | + * @param ServiceLocatorInterface $container |
| 53 | + * @return AnnotationBuilder |
| 54 | + */ |
| 55 | + public function createService(ServiceLocatorInterface $container) |
| 56 | + { |
| 57 | + return $this($container, AnnotationBuilder::class); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Marshal annotation builder configuration, if any. |
| 62 | + * |
| 63 | + * Looks for the `config` service in the container, returning an empty array |
| 64 | + * if not found. |
| 65 | + * |
| 66 | + * If found, checks for a `form_annotation_builder` entry, returning an empty |
| 67 | + * array if not found or not an array. |
| 68 | + * |
| 69 | + * Otherwise, returns the `form_annotation_builder` array. |
| 70 | + * |
| 71 | + * @param ContainerInterface $container |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + private function marshalConfig(ContainerInterface $container) |
| 75 | + { |
| 76 | + if (! $container->has('config')) { |
| 77 | + return []; |
| 78 | + } |
| 79 | + |
| 80 | + $config = $container->get('config'); |
| 81 | + $config = isset($config['form_annotation_builder']) |
| 82 | + ? $config['form_annotation_builder'] |
| 83 | + : []; |
| 84 | + |
| 85 | + return is_array($config) ? $config : []; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Inject annotations from configuration, if any. |
| 90 | + * |
| 91 | + * @param array $config |
| 92 | + * @param AnnotationBuilder $builder |
| 93 | + * @return void |
| 94 | + */ |
| 95 | + private function injectAnnotations(array $config, AnnotationBuilder $builder) |
| 96 | + { |
| 97 | + if (! isset($config['annotations'])) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + $parser = $builder->getAnnotationParser(); |
| 102 | + foreach ($config['annotations'] as $fullyQualifiedClassName) { |
| 103 | + $parser->registerAnnotation($fullyQualifiedClassName); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Inject event listeners from configuration, if any. |
| 109 | + * |
| 110 | + * Loops through the 'listeners' array, and: |
| 111 | + * |
| 112 | + * - attempts to fetch it from the container |
| 113 | + * - if the fetched instance is not a `ListenerAggregate`, raises an exception |
| 114 | + * - otherwise attaches it to the event manager |
| 115 | + * |
| 116 | + * @param array $config |
| 117 | + * @param EventManagerInterface $events |
| 118 | + * @param ContainerInterface $container |
| 119 | + * @return void |
| 120 | + * @throws ServiceNotCreatedException if any listener is not an event listener |
| 121 | + * aggregate. |
| 122 | + */ |
| 123 | + private function injectListeners(array $config, EventManagerInterface $events, ContainerInterface $container) |
| 124 | + { |
| 125 | + if (! isset($config['listeners'])) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + foreach ($config['listeners'] as $listenerName) { |
| 130 | + $listener = $container->get($listenerName); |
| 131 | + |
| 132 | + if (! $listener instanceof ListenerAggregateInterface) { |
| 133 | + throw new ServiceNotCreatedException(sprintf('Invalid event listener (%s) provided', $listenerName)); |
| 134 | + } |
| 135 | + |
| 136 | + $listener->attach($events); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments