Skip to content

Commit

Permalink
Merge pull request zendframework#40 from kynx/sm-em-v2-v3
Browse files Browse the repository at this point in the history
WIP: ServiceManager and EventManager v2-v3 compatibility
  • Loading branch information
weierophinney committed Feb 18, 2016
2 parents afa5739 + aa8f4d9 commit f141318
Show file tree
Hide file tree
Showing 17 changed files with 885 additions and 321 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ matrix:
- php: 5.5
env:
- EXECUTE_CS_CHECK=true
- SERVICE_MANAGER_V2=true
- php: 5.6
env:
- EXECUTE_TEST_COVERALLS=true
Expand All @@ -41,7 +42,8 @@ before_install:
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls ; fi

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs
- if [[ $SERVICE_MANAGER_V2 != 'true' ]]; then travis_retry composer install --no-interaction --ignore-platform-reqs ; fi
- if [[ $SERVICE_MANAGER_V2 == 'true' ]]; then travis_retry composer update --no-interaction --ignore-platform-reqs --prefer-lowest ; fi

script:
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi
Expand Down
59 changes: 36 additions & 23 deletions book/zend.form.advanced-use-of-forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ class Phone extends Element implements InputProviderInterface
protected $validator;

/**
* Get a validator if none has been set.
*
* @return ValidatorInterface
*/
* Get a validator if none has been set.
*
* @return ValidatorInterface
*/
public function getValidator()
{
if (null === $this->validator) {
Expand Down Expand Up @@ -160,34 +160,42 @@ First, add the custom element to the plugin manager, in your `Module.php` class:
```php
namespace Application;

use Application\Form\Element\Phone;
use Zend\Form\ElementFactory;
use Zend\ModuleManager\Feature\FormElementProviderInterface;

class Module implements FormElementProviderInterface
{
public function getFormElementConfig()
{
return array(
'invokables' => array(
'phone' => 'Application\Form\Element\Phone'
)
);
return [
'aliases' => [
'phone' => Phone::class,
],
'factories' => [
Phone::class => ElementFactory::class,
],
];
}
}
```

Or, you can do the same in your `module.config.php` file:

```php
return array(
'form_elements' => array(
'invokables' => array(
'phone' => 'Application\Form\Element\Phone'
)
)
);
return [
'form_elements' => [
'aliases' => [
'phone' => Phone::class,
],
'factories' => [
Phone::class => ElementFactory::class,
],
],
];
```

You can use a factory instead of an invokable in order to handle dependencies in your
`ElementFactory` is the default factory for form elements. Use your own if you need to handle dependencies in your
elements/fieldsets/forms.

**And now comes the first catch.**
Expand Down Expand Up @@ -240,22 +248,27 @@ same key as the element you want to replace:
```php
namespace Application;

use Application\Form\Element\MyEmail;
use Zend\Form\ElementFactory;
use Zend\ModuleManager\Feature\FormElementProviderInterface;

class Module implements FormElementProviderInterface
{
public function getFormElementConfig()
{
return array(
'invokables' => array(
'Email' => 'Application\Form\Element\MyEmail'
)
);
return [
'aliases' => [
'email' => MyEmail::class,
],
'factories' => [
MyEmail::class => ElementFactory::class,
],
];
}
}
```

Now, whenever you'll create an element whose `type` is 'Email', it will create the custom Email
Now, whenever you'll create an element whose `type` is 'email', it will create the custom Email
element instead of the built-in one.

> ## Note
Expand Down
20 changes: 10 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@
},
"require": {
"php": ">=5.5",
"zendframework/zend-inputfilter": "~2.5",
"zendframework/zend-inputfilter": "^2.6",
"zendframework/zend-hydrator": "~1.0",
"zendframework/zend-stdlib": "~2.7"
"zendframework/zend-stdlib": "^2.7 || ^3.0"
},
"require-dev": {
"doctrine/annotations": "~1.0",
"zendframework/zend-cache": "~2.5",
"zendframework/zend-cache": "^2.6",
"zendframework/zend-captcha": "~2.5",
"zendframework/zend-code": "~2.5",
"zendframework/zend-di": "~2.5",
"zendframework/zend-eventmanager": "~2.5",
"zendframework/zend-filter": "~2.5",
"zendframework/zend-i18n": "~2.5",
"zendframework/zend-eventmanager": "^2.6.2 || ^3.0",
"zendframework/zend-filter": "^2.6",
"zendframework/zend-i18n": "^2.6",
"zendframework/zend-mvc": "~2.5",
"zendframework/zend-servicemanager": "~2.5",
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
"zendframework/zend-session": "~2.5",
"zendframework/zend-text": "~2.5",
"zendframework/zend-validator": "~2.5",
"zendframework/zend-view": "~2.5",
"zendframework/zend-text": "^2.6",
"zendframework/zend-validator": "^2.6",
"zendframework/zend-view": "^2.6",
"zendframework/zendservice-recaptcha": "*",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
Expand Down
49 changes: 27 additions & 22 deletions src/Annotation/AnnotationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public function setEventManager(EventManagerInterface $events)
__CLASS__,
get_class($this),
]);
$events->attach(new ElementAnnotationsListener());
$events->attach(new FormAnnotationsListener());
(new ElementAnnotationsListener())->attach($events);
(new FormAnnotationsListener())->attach($events);
$this->events = $events;
return $this;
}
Expand Down Expand Up @@ -318,31 +318,30 @@ protected function configureElement($annotations, $reflection, $formSpec, $filte
'name' => $name,
]);

$event = new Event();
$event->setParams([
$params = [
'name' => $name,
'elementSpec' => $elementSpec,
'inputSpec' => $inputSpec,
'formSpec' => $formSpec,
'filterSpec' => $filterSpec,
]);
];
foreach ($annotations as $annotation) {
$event->setParam('annotation', $annotation);
$events->trigger(__FUNCTION__, $this, $event);
$params['annotation'] = $annotation;
$events->trigger(__FUNCTION__, $this, $params);
}

// Since "type" is a reserved name in the filter specification,
// we need to add the specification without the name as the key.
// In all other cases, though, the name is fine.
if ($event->getParam('inputSpec')->count() > 1) {
if ($params['inputSpec']->count() > 1) {
if ($name === 'type') {
$filterSpec[] = $event->getParam('inputSpec');
$filterSpec[] = $params['inputSpec'];
} else {
$filterSpec[$name] = $event->getParam('inputSpec');
$filterSpec[$name] = $params['inputSpec'];
}
}

$elementSpec = $event->getParam('elementSpec');
$elementSpec = $params['elementSpec'];
$type = (isset($elementSpec['spec']['type']))
? $elementSpec['spec']['type']
: 'Zend\Form\Element';
Expand Down Expand Up @@ -389,12 +388,15 @@ public function preserveDefinedOrder()
*/
protected function discoverName($annotations, $reflection)
{
$results = $this->getEventManager()->trigger('discoverName', $this, [
'annotations' => $annotations,
'reflection' => $reflection,
], function ($r) {
return (is_string($r) && !empty($r));
});
$results = $this->getEventManager()->triggerUntil(function ($r) {
return (is_string($r) && !empty($r));
},
'discoverName',
$this, [
'annotations' => $annotations,
'reflection' => $reflection,
]
);
return $results->last();
}

Expand All @@ -406,11 +408,14 @@ protected function discoverName($annotations, $reflection)
*/
protected function checkForExclude($annotations)
{
$results = $this->getEventManager()->trigger('checkForExclude', $this, [
'annotations' => $annotations,
], function ($r) {
return (true === $r);
});
$results = $this->getEventManager()->triggerUntil(function ($r) {
return (true === $r);
},
'checkForExclude',
$this, [
'annotations' => $annotations,
]
);
return (bool) $results->last();
}

Expand Down
40 changes: 20 additions & 20 deletions src/Annotation/ElementAnnotationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@ class ElementAnnotationsListener extends AbstractAnnotationsListener
/**
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events)
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach('configureElement', [$this, 'handleAllowEmptyAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleAttributesAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleComposedObjectAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleContinueIfEmptyAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleErrorMessageAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleFilterAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleFlagsAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleHydratorAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleInputAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleObjectAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleOptionsAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleRequiredAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleTypeAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleValidatorAnnotation']);

$this->listeners[] = $events->attach('discoverName', [$this, 'handleNameAnnotation']);
$this->listeners[] = $events->attach('discoverName', [$this, 'discoverFallbackName']);

$this->listeners[] = $events->attach('checkForExclude', [$this, 'handleExcludeAnnotation']);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleAllowEmptyAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleAttributesAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleComposedObjectAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleContinueIfEmptyAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleErrorMessageAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleFilterAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleFlagsAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleHydratorAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleInputAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleObjectAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleOptionsAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleRequiredAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleTypeAnnotation'], $priority);
$this->listeners[] = $events->attach('configureElement', [$this, 'handleValidatorAnnotation'], $priority);

$this->listeners[] = $events->attach('discoverName', [$this, 'handleNameAnnotation'], $priority);
$this->listeners[] = $events->attach('discoverName', [$this, 'discoverFallbackName'], $priority);

$this->listeners[] = $events->attach('checkForExclude', [$this, 'handleExcludeAnnotation'], $priority);
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/Annotation/FormAnnotationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ class FormAnnotationsListener extends AbstractAnnotationsListener
* @param EventManagerInterface $events
* @return void
*/
public function attach(EventManagerInterface $events)
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach('configureForm', [$this, 'handleAttributesAnnotation']);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleFlagsAnnotation']);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleHydratorAnnotation']);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleInputFilterAnnotation']);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleObjectAnnotation']);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleOptionsAnnotation']);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleTypeAnnotation']);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleValidationGroupAnnotation']);

$this->listeners[] = $events->attach('discoverName', [$this, 'handleNameAnnotation']);
$this->listeners[] = $events->attach('discoverName', [$this, 'discoverFallbackName']);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleAttributesAnnotation'], $priority);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleFlagsAnnotation'], $priority);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleHydratorAnnotation'], $priority);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleInputFilterAnnotation'], $priority);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleObjectAnnotation'], $priority);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleOptionsAnnotation'], $priority);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleTypeAnnotation'], $priority);
$this->listeners[] = $events->attach('configureForm', [$this, 'handleValidationGroupAnnotation'], $priority);

$this->listeners[] = $events->attach('discoverName', [$this, 'handleNameAnnotation'], $priority);
$this->listeners[] = $events->attach('discoverName', [$this, 'discoverFallbackName'], $priority);
}

/**
Expand Down
Loading

0 comments on commit f141318

Please sign in to comment.