3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Service
;
12 use Interop\Container\ContainerInterface
;
13 use Zend\EventManager\ListenerAggregateInterface
;
14 use Zend\Form\Annotation\AnnotationBuilder
;
15 use Zend\ServiceManager\Exception\ServiceNotCreatedException
;
16 use Zend\ServiceManager\FactoryInterface
;
17 use Zend\ServiceManager\ServiceLocatorInterface
;
19 class FormAnnotationBuilderFactory
implements FactoryInterface
24 * @param ContainerInterface $container
26 * @param null|array $options
27 * @return AnnotationBuilder
28 * @throws ServiceNotCreatedException for invalid listener configuration.
30 public function __invoke(ContainerInterface
$container, $name, array $options = null)
32 //setup a form factory which can use custom form elements
33 $annotationBuilder = new AnnotationBuilder();
34 $eventManager = $container->get('EventManager');
35 $annotationBuilder->setEventManager($eventManager);
37 $formElementManager = $container->get('FormElementManager');
38 $formElementManager->injectFactory($container, $annotationBuilder);
40 $config = $container->get('config');
41 if (isset($config['form_annotation_builder'])) {
42 $config = $config['form_annotation_builder'];
44 if (isset($config['annotations'])) {
45 foreach ((array) $config['annotations'] as $fullyQualifiedClassName) {
46 $annotationBuilder->getAnnotationParser()->registerAnnotation($fullyQualifiedClassName);
50 if (isset($config['listeners'])) {
51 foreach ((array) $config['listeners'] as $listenerName) {
52 $listener = $container->get($listenerName);
53 if (!($listener instanceof ListenerAggregateInterface
)) {
54 throw new ServiceNotCreatedException(sprintf('Invalid event listener (%s) provided', $listenerName));
56 $listener->attach($eventManager);
60 if (isset($config['preserve_defined_order'])) {
61 $annotationBuilder->setPreserveDefinedOrder($config['preserve_defined_order']);
65 return $annotationBuilder;
69 * Create and return AnnotationBuilder instance
71 * For use with zend-servicemanager v2; proxies to __invoke().
73 * @param ServiceLocatorInterface $container
74 * @return AnnotationBuilder
76 public function createService(ServiceLocatorInterface
$container)
78 return $this($container, AnnotationBuilder
::class);