Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add MVC service factories for Filters and Validators #2315

Merged
merged 3 commits into from

3 participants

@cgmartin

This should be ready, but could use another set of eyes on the Zend/Mvc/Service changes. Cheers

@starJammer

I was just curious about the point of this. I had an issue with getting the doctrine entity manager into an form element or input I made. Would this help?

My question is here : http://stackoverflow.com/questions/12342189/how-to-create-form-inputs-elements-in-zf2

@weierophinney

@starJammer This is completely unrelated to the question you have.

The point of this patch is to allow managing the Filter plugin manager and Validator plugin manager at the application level. This allows developers to specify all custom filters and validators at that level, and then to inject these managers into appropriate objects as needed (such as ValidatorChain and FilterChain prototypes used with form and input filter factories).

@weierophinney weierophinney merged commit b57d117 into zendframework:master
@weierophinney

Merged to master -- thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
27 library/Zend/ModuleManager/Feature/FilterProviderInterface.php
@@ -0,0 +1,27 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_ModuleManager
+ */
+
+namespace Zend\ModuleManager\Feature;
+
+/**
+ * @category Zend
+ * @package Zend_ModuleManager
+ * @subpackage Feature
+ */
+interface FilterProviderInterface
+{
+ /**
+ * Expected to return \Zend\ServiceManager\Config object or array to
+ * seed such an object.
+ *
+ * @return array|\Zend\ServiceManager\Config
+ */
+ public function getFilterConfig();
+}
View
27 library/Zend/ModuleManager/Feature/ValidatorProviderInterface.php
@@ -0,0 +1,27 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_ModuleManager
+ */
+
+namespace Zend\ModuleManager\Feature;
+
+/**
+ * @category Zend
+ * @package Zend_ModuleManager
+ * @subpackage Feature
+ */
+interface ValidatorProviderInterface
+{
+ /**
+ * Expected to return \Zend\ServiceManager\Config object or array to
+ * seed such an object.
+ *
+ * @return array|\Zend\ServiceManager\Config
+ */
+ public function getValidatorConfig();
+}
View
36 library/Zend/Mvc/Service/FilterManagerFactory.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace Zend\Mvc\Service;
+
+use Zend\ServiceManager\ConfigInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ * @category Zend
+ * @package Zend_Mvc
+ * @subpackage Service
+ */
+class FilterManagerFactory extends AbstractPluginManagerFactory
+{
+ const PLUGIN_MANAGER_CLASS = 'Zend\Filter\FilterPluginManager';
+
+ /**
+ * Create and return the filter plugin manager
+ *
+ * @param ServiceLocatorInterface $serviceLocator
+ * @return FilterPluginManager
+ */
+ public function createService(ServiceLocatorInterface $serviceLocator)
+ {
+ $plugins = parent::createService($serviceLocator);
+ return $plugins;
+ }
+}
View
12 library/Zend/Mvc/Service/ModuleManagerFactory.php
@@ -74,6 +74,18 @@ public function createService(ServiceLocatorInterface $serviceLocator)
'Zend\ModuleManager\Feature\ViewHelperProviderInterface',
'getViewHelperConfig'
);
+ $serviceListener->addServiceManager(
+ 'ValidatorManager',
+ 'validators',
+ 'Zend\ModuleManager\Feature\ValidatorProviderInterface',
+ 'getValidatorConfig'
+ );
+ $serviceListener->addServiceManager(
+ 'FilterManager',
+ 'filters',
+ 'Zend\ModuleManager\Feature\FilterProviderInterface',
+ 'getFilterConfig'
+ );
$events = $serviceLocator->get('EventManager');
$events->attach($defaultListeners);
View
2  library/Zend/Mvc/Service/ServiceListenerFactory.php
@@ -52,10 +52,12 @@ class ServiceListenerFactory implements FactoryInterface
'ConsoleAdapter' => 'Zend\Mvc\Service\ConsoleAdapterFactory',
'ConsoleRouter' => 'Zend\Mvc\Service\RouterFactory',
'DependencyInjector' => 'Zend\Mvc\Service\DiFactory',
+ 'FilterManager' => 'Zend\Mvc\Service\FilterManagerFactory',
'HttpRouter' => 'Zend\Mvc\Service\RouterFactory',
'Request' => 'Zend\Mvc\Service\RequestFactory',
'Response' => 'Zend\Mvc\Service\ResponseFactory',
'Router' => 'Zend\Mvc\Service\RouterFactory',
+ 'ValidatorManager' => 'Zend\Mvc\Service\ValidatorManagerFactory',
'ViewHelperManager' => 'Zend\Mvc\Service\ViewHelperManagerFactory',
'ViewFeedRenderer' => 'Zend\Mvc\Service\ViewFeedRendererFactory',
'ViewFeedStrategy' => 'Zend\Mvc\Service\ViewFeedStrategyFactory',
View
36 library/Zend/Mvc/Service/ValidatorManagerFactory.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace Zend\Mvc\Service;
+
+use Zend\ServiceManager\ConfigInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ * @category Zend
+ * @package Zend_Mvc
+ * @subpackage Service
+ */
+class ValidatorManagerFactory extends AbstractPluginManagerFactory
+{
+ const PLUGIN_MANAGER_CLASS = 'Zend\Validator\ValidatorPluginManager';
+
+ /**
+ * Create and return the validator plugin manager
+ *
+ * @param ServiceLocatorInterface $serviceLocator
+ * @return ValidatorPluginManager
+ */
+ public function createService(ServiceLocatorInterface $serviceLocator)
+ {
+ $plugins = parent::createService($serviceLocator);
+ return $plugins;
+ }
+}
View
46 tests/ZendTest/Filter/FilterPluginManagerTest.php
@@ -0,0 +1,46 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Filter
+ */
+
+namespace ZendTest\Filter;
+
+use Zend\Filter\FilterPluginManager;
+
+/**
+ * @category Zend
+ * @package Zend_Filter
+ * @subpackage UnitTests
+ * @group Zend_Filter
+ */
+class FilterPluginManagerTest extends \PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ $this->filters = new FilterPluginManager();
+ }
+
+ public function testFilterSuccessfullyRetrieved()
+ {
+ $filter = $this->filters->get('int');
+ $this->assertInstanceOf('Zend\Filter\Int', $filter);
+ }
+
+ public function testRegisteringInvalidFilterRaisesException()
+ {
+ $this->setExpectedException('Zend\Filter\Exception\RuntimeException');
+ $this->filters->setService('test', $this);
+ }
+
+ public function testLoadingInvalidFilterRaisesException()
+ {
+ $this->filters->setInvokableClass('test', get_class($this));
+ $this->setExpectedException('Zend\Filter\Exception\RuntimeException');
+ $this->filters->get('test');
+ }
+}
View
1  tests/ZendTest/Validator/ValidatorChainTest.php
@@ -45,6 +45,7 @@ public function setUp()
public function tearDown()
{
+ AbstractValidator::setDefaultTranslator(null);
AbstractValidator::setMessageLength(-1);
}
View
76 tests/ZendTest/Validator/ValidatorPluginManagerTest.php
@@ -0,0 +1,76 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Validator
+ */
+
+namespace ZendTest\Validator;
+
+use Zend\Validator\ValidatorPluginManager;
+
+/**
+ * @category Zend
+ * @package Zend_Validator
+ * @subpackage UnitTests
+ * @group Zend_Validator
+ */
+class ValidatorPluginManagerTest extends \PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ $this->validators = new ValidatorPluginManager();
+ }
+
+ public function testAllowsInjectingTranslator()
+ {
+ $translator = $this->getMock("Zend\I18n\Translator\Translator");
+
+ $slContents = array(array('translator', $translator));
+ $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
+ $serviceLocator->expects($this->once())
+ ->method('get')
+ ->will($this->returnValueMap($slContents));
+ $serviceLocator->expects($this->once())
+ ->method('has')
+ ->with($this->equalTo('translator'))
+ ->will($this->returnValue(true));
+
+ $this->validators->setServiceLocator($serviceLocator);
+ $this->assertSame($serviceLocator, $this->validators->getServiceLocator());
+
+ $validator = $this->validators->get('notempty');
+ $this->assertSame($translator, $validator->getTranslator());
+ }
+
+ public function testNoTranslatorInjectedWhenTranslatorIsNotPresent()
+ {
+ $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
+ $serviceLocator->expects($this->once())
+ ->method('has')
+ ->with($this->equalTo('translator'))
+ ->will($this->returnValue(false));
+
+ $this->validators->setServiceLocator($serviceLocator);
+ $this->assertSame($serviceLocator, $this->validators->getServiceLocator());
+
+ $validator = $this->validators->get('notempty');
+ $this->assertNull($validator->getTranslator());
+ }
+
+ public function testRegisteringInvalidValidatorRaisesException()
+ {
+ $this->setExpectedException('Zend\Validator\Exception\RuntimeException');
+ $this->validators->setService('test', $this);
+ }
+
+ public function testLoadingInvalidValidatorRaisesException()
+ {
+ $this->validators->setInvokableClass('test', get_class($this));
+ $this->setExpectedException('Zend\Validator\Exception\RuntimeException');
+ $this->validators->get('test');
+ }
+}
Something went wrong with that request. Please try again.