Skip to content
This repository

Add MVC service factories for Filters and Validators #2315

Merged
merged 3 commits into from over 1 year ago

3 participants

Christopher Martin starJammer Matthew Weier O'Phinney
Christopher Martin

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

Matthew Weier O'Phinney

@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).

Matthew Weier O'Phinney weierophinney merged commit b57d117 into from September 13, 2012
Matthew Weier O'Phinney weierophinney closed this September 13, 2012
Matthew Weier O'Phinney

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.
27  library/Zend/ModuleManager/Feature/FilterProviderInterface.php
... ...
@@ -0,0 +1,27 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_ModuleManager
  9
+ */
  10
+
  11
+namespace Zend\ModuleManager\Feature;
  12
+
  13
+/**
  14
+ * @category   Zend
  15
+ * @package    Zend_ModuleManager
  16
+ * @subpackage Feature
  17
+ */
  18
+interface FilterProviderInterface
  19
+{
  20
+    /**
  21
+     * Expected to return \Zend\ServiceManager\Config object or array to
  22
+     * seed such an object.
  23
+     *
  24
+     * @return array|\Zend\ServiceManager\Config
  25
+     */
  26
+    public function getFilterConfig();
  27
+}
27  library/Zend/ModuleManager/Feature/ValidatorProviderInterface.php
... ...
@@ -0,0 +1,27 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_ModuleManager
  9
+ */
  10
+
  11
+namespace Zend\ModuleManager\Feature;
  12
+
  13
+/**
  14
+ * @category   Zend
  15
+ * @package    Zend_ModuleManager
  16
+ * @subpackage Feature
  17
+ */
  18
+interface ValidatorProviderInterface
  19
+{
  20
+    /**
  21
+     * Expected to return \Zend\ServiceManager\Config object or array to
  22
+     * seed such an object.
  23
+     *
  24
+     * @return array|\Zend\ServiceManager\Config
  25
+     */
  26
+    public function getValidatorConfig();
  27
+}
36  library/Zend/Mvc/Service/FilterManagerFactory.php
... ...
@@ -0,0 +1,36 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Mvc
  9
+ */
  10
+
  11
+namespace Zend\Mvc\Service;
  12
+
  13
+use Zend\ServiceManager\ConfigInterface;
  14
+use Zend\ServiceManager\ServiceLocatorInterface;
  15
+
  16
+/**
  17
+ * @category   Zend
  18
+ * @package    Zend_Mvc
  19
+ * @subpackage Service
  20
+ */
  21
+class FilterManagerFactory extends AbstractPluginManagerFactory
  22
+{
  23
+    const PLUGIN_MANAGER_CLASS = 'Zend\Filter\FilterPluginManager';
  24
+
  25
+    /**
  26
+     * Create and return the filter plugin manager
  27
+     *
  28
+     * @param  ServiceLocatorInterface $serviceLocator
  29
+     * @return FilterPluginManager
  30
+     */
  31
+    public function createService(ServiceLocatorInterface $serviceLocator)
  32
+    {
  33
+        $plugins = parent::createService($serviceLocator);
  34
+        return $plugins;
  35
+    }
  36
+}
12  library/Zend/Mvc/Service/ModuleManagerFactory.php
@@ -74,6 +74,18 @@ public function createService(ServiceLocatorInterface $serviceLocator)
74 74
             'Zend\ModuleManager\Feature\ViewHelperProviderInterface',
75 75
             'getViewHelperConfig'
76 76
         );
  77
+        $serviceListener->addServiceManager(
  78
+            'ValidatorManager',
  79
+            'validators',
  80
+            'Zend\ModuleManager\Feature\ValidatorProviderInterface',
  81
+            'getValidatorConfig'
  82
+        );
  83
+        $serviceListener->addServiceManager(
  84
+            'FilterManager',
  85
+            'filters',
  86
+            'Zend\ModuleManager\Feature\FilterProviderInterface',
  87
+            'getFilterConfig'
  88
+        );
77 89
 
78 90
         $events = $serviceLocator->get('EventManager');
79 91
         $events->attach($defaultListeners);
2  library/Zend/Mvc/Service/ServiceListenerFactory.php
@@ -52,10 +52,12 @@ class ServiceListenerFactory implements FactoryInterface
52 52
             'ConsoleAdapter'          => 'Zend\Mvc\Service\ConsoleAdapterFactory',
53 53
             'ConsoleRouter'           => 'Zend\Mvc\Service\RouterFactory',
54 54
             'DependencyInjector'      => 'Zend\Mvc\Service\DiFactory',
  55
+            'FilterManager'           => 'Zend\Mvc\Service\FilterManagerFactory',
55 56
             'HttpRouter'              => 'Zend\Mvc\Service\RouterFactory',
56 57
             'Request'                 => 'Zend\Mvc\Service\RequestFactory',
57 58
             'Response'                => 'Zend\Mvc\Service\ResponseFactory',
58 59
             'Router'                  => 'Zend\Mvc\Service\RouterFactory',
  60
+            'ValidatorManager'        => 'Zend\Mvc\Service\ValidatorManagerFactory',
59 61
             'ViewHelperManager'       => 'Zend\Mvc\Service\ViewHelperManagerFactory',
60 62
             'ViewFeedRenderer'        => 'Zend\Mvc\Service\ViewFeedRendererFactory',
61 63
             'ViewFeedStrategy'        => 'Zend\Mvc\Service\ViewFeedStrategyFactory',
36  library/Zend/Mvc/Service/ValidatorManagerFactory.php
... ...
@@ -0,0 +1,36 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Mvc
  9
+ */
  10
+
  11
+namespace Zend\Mvc\Service;
  12
+
  13
+use Zend\ServiceManager\ConfigInterface;
  14
+use Zend\ServiceManager\ServiceLocatorInterface;
  15
+
  16
+/**
  17
+ * @category   Zend
  18
+ * @package    Zend_Mvc
  19
+ * @subpackage Service
  20
+ */
  21
+class ValidatorManagerFactory extends AbstractPluginManagerFactory
  22
+{
  23
+    const PLUGIN_MANAGER_CLASS = 'Zend\Validator\ValidatorPluginManager';
  24
+
  25
+    /**
  26
+     * Create and return the validator plugin manager
  27
+     *
  28
+     * @param  ServiceLocatorInterface $serviceLocator
  29
+     * @return ValidatorPluginManager
  30
+     */
  31
+    public function createService(ServiceLocatorInterface $serviceLocator)
  32
+    {
  33
+        $plugins = parent::createService($serviceLocator);
  34
+        return $plugins;
  35
+    }
  36
+}
46  tests/ZendTest/Filter/FilterPluginManagerTest.php
... ...
@@ -0,0 +1,46 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Filter
  9
+ */
  10
+
  11
+namespace ZendTest\Filter;
  12
+
  13
+use Zend\Filter\FilterPluginManager;
  14
+
  15
+/**
  16
+ * @category   Zend
  17
+ * @package    Zend_Filter
  18
+ * @subpackage UnitTests
  19
+ * @group      Zend_Filter
  20
+ */
  21
+class FilterPluginManagerTest extends \PHPUnit_Framework_TestCase
  22
+{
  23
+    public function setUp()
  24
+    {
  25
+        $this->filters = new FilterPluginManager();
  26
+    }
  27
+
  28
+    public function testFilterSuccessfullyRetrieved()
  29
+    {
  30
+        $filter = $this->filters->get('int');
  31
+        $this->assertInstanceOf('Zend\Filter\Int', $filter);
  32
+    }
  33
+
  34
+    public function testRegisteringInvalidFilterRaisesException()
  35
+    {
  36
+        $this->setExpectedException('Zend\Filter\Exception\RuntimeException');
  37
+        $this->filters->setService('test', $this);
  38
+    }
  39
+
  40
+    public function testLoadingInvalidFilterRaisesException()
  41
+    {
  42
+        $this->filters->setInvokableClass('test', get_class($this));
  43
+        $this->setExpectedException('Zend\Filter\Exception\RuntimeException');
  44
+        $this->filters->get('test');
  45
+    }
  46
+}
1  tests/ZendTest/Validator/ValidatorChainTest.php
@@ -45,6 +45,7 @@ public function setUp()
45 45
 
46 46
     public function tearDown()
47 47
     {
  48
+        AbstractValidator::setDefaultTranslator(null);
48 49
         AbstractValidator::setMessageLength(-1);
49 50
     }
50 51
 
76  tests/ZendTest/Validator/ValidatorPluginManagerTest.php
... ...
@@ -0,0 +1,76 @@
  1
+<?php
  2
+/**
  3
+ * Zend Framework (http://framework.zend.com/)
  4
+ *
  5
+ * @link      http://github.com/zendframework/zf2 for the canonical source repository
  6
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7
+ * @license   http://framework.zend.com/license/new-bsd New BSD License
  8
+ * @package   Zend_Validator
  9
+ */
  10
+
  11
+namespace ZendTest\Validator;
  12
+
  13
+use Zend\Validator\ValidatorPluginManager;
  14
+
  15
+/**
  16
+ * @category   Zend
  17
+ * @package    Zend_Validator
  18
+ * @subpackage UnitTests
  19
+ * @group      Zend_Validator
  20
+ */
  21
+class ValidatorPluginManagerTest extends \PHPUnit_Framework_TestCase
  22
+{
  23
+    public function setUp()
  24
+    {
  25
+        $this->validators = new ValidatorPluginManager();
  26
+    }
  27
+
  28
+    public function testAllowsInjectingTranslator()
  29
+    {
  30
+        $translator = $this->getMock("Zend\I18n\Translator\Translator");
  31
+
  32
+        $slContents = array(array('translator', $translator));
  33
+        $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
  34
+        $serviceLocator->expects($this->once())
  35
+            ->method('get')
  36
+            ->will($this->returnValueMap($slContents));
  37
+        $serviceLocator->expects($this->once())
  38
+            ->method('has')
  39
+            ->with($this->equalTo('translator'))
  40
+            ->will($this->returnValue(true));
  41
+
  42
+        $this->validators->setServiceLocator($serviceLocator);
  43
+        $this->assertSame($serviceLocator, $this->validators->getServiceLocator());
  44
+
  45
+        $validator = $this->validators->get('notempty');
  46
+        $this->assertSame($translator, $validator->getTranslator());
  47
+    }
  48
+
  49
+    public function testNoTranslatorInjectedWhenTranslatorIsNotPresent()
  50
+    {
  51
+        $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
  52
+        $serviceLocator->expects($this->once())
  53
+            ->method('has')
  54
+            ->with($this->equalTo('translator'))
  55
+            ->will($this->returnValue(false));
  56
+
  57
+        $this->validators->setServiceLocator($serviceLocator);
  58
+        $this->assertSame($serviceLocator, $this->validators->getServiceLocator());
  59
+
  60
+        $validator = $this->validators->get('notempty');
  61
+        $this->assertNull($validator->getTranslator());
  62
+    }
  63
+
  64
+    public function testRegisteringInvalidValidatorRaisesException()
  65
+    {
  66
+        $this->setExpectedException('Zend\Validator\Exception\RuntimeException');
  67
+        $this->validators->setService('test', $this);
  68
+    }
  69
+
  70
+    public function testLoadingInvalidValidatorRaisesException()
  71
+    {
  72
+        $this->validators->setInvokableClass('test', get_class($this));
  73
+        $this->setExpectedException('Zend\Validator\Exception\RuntimeException');
  74
+        $this->validators->get('test');
  75
+    }
  76
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.