Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 3d3b658

Browse files
committed
Merge branch 'feature/config-provider' into develop
Close #53
2 parents 511cf66 + d4b9e94 commit 3d3b658

10 files changed

+829
-263
lines changed

CHANGELOG.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,30 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- Nothing.
9+
- [#53](https://github.com/zendframework/zend-form/pull/53) adds
10+
`Zend\Form\FormElementManagerFactory`, for creating and returning instances of
11+
`Zend\Form\FormElementManager`. This factory was ported from zend-mvc, and
12+
will replace it for version 3 of that component.
13+
- [#53](https://github.com/zendframework/zend-form/pull/53) adds
14+
`Zend\Form\Annotation\AnnotationBuilderFactory`, for creating and returning
15+
instances of `Zend\Form\Annotation\AnnotationBuilder`. This factory was ported
16+
from zend-mvc, and will replace it for version 3 of that component.
17+
- [#53](https://github.com/zendframework/zend-form/pull/53) exposes the package
18+
as a config-provider and ZF component, by adding:
19+
- `ConfigProvider`, which maps the `FormElementsManager` and
20+
`FormAnnotationBuilder` servies previously provided by zend-mvc; the form
21+
abstract factory as previously registered by zend-mvc; and all view helper
22+
configuration.
23+
- `Module`, which maps services and view helpers per the `ConfigProvider`, and
24+
provides configuration to the zend-modulemanager `ServiceLocator` in order
25+
for modules to provide form and form element configuration.
1026

1127
### Deprecated
1228

13-
- Nothing.
29+
- [#53](https://github.com/zendframework/zend-form/pull/53) deprecates
30+
`Zend\Form\View\HelperConfig`; the functionality is made obsolete by
31+
`ConfigProvider`. It now consumes the latter in order to provide view helper
32+
configuration.
1433

1534
### Removed
1635

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
"branch-alias": {
5353
"dev-master": "2.7-dev",
5454
"dev-develop": "2.8-dev"
55+
},
56+
"zf": {
57+
"component": "Zend\\Form",
58+
"config-provider": "Zend\\Form\\ConfigProvider"
5559
}
5660
},
5761
"autoload-dev": {
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)