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

Add various plugin manager #4319

Merged
merged 10 commits into from
Apr 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion library/Zend/InputFilter/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,30 @@
use ArrayAccess;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\InitializableInterface;

/**
* @todo How should we deal with required input when data is missing?
* should a message be returned? if so, what message?
*/
class BaseInputFilter implements InputFilterInterface, UnknownInputsCapableInterface
class BaseInputFilter implements InputFilterInterface, UnknownInputsCapableInterface, InitializableInterface
{
protected $data;
protected $inputs = array();
protected $invalidInputs;
protected $validationGroup;
protected $validInputs;

/**
* This function is automatically called when creating element with factory. It
* allows to perform various operations (add elements...)
*
* @return void
*/
public function init()
{
}

/**
* Countable: number of inputs in this input filter
*
Expand Down Expand Up @@ -469,6 +480,7 @@ public function getMessages()
foreach ($this->getInvalidInput() as $name => $input) {
$messages[$name] = $input->getMessages();
}

return $messages;
}

Expand Down
6 changes: 6 additions & 0 deletions library/Zend/InputFilter/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class Factory
protected $defaultFilterChain;
protected $defaultValidatorChain;

public function __construct()
{
$this->defaultFilterChain = new FilterChain();
$this->defaultValidatorChain = new ValidatorChain();
}

/**
* Set default filter chain to use
*
Expand Down
67 changes: 67 additions & 0 deletions library/Zend/InputFilter/InputFilterPluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\InputFilter;

use Zend\InputFilter\Exception;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\InitializableInterface;

/**
* Plugin manager implementation for input filters.
*/
class InputFilterPluginManager extends AbstractPluginManager
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized this one should be marked sharedByDefault == false as well (as input filters are rarely if ever shared). I'll do that on merge.

/**
* @param ConfigInterface $configuration
*/
public function __construct(ConfigInterface $configuration = null)
{
parent::__construct($configuration);

$this->addInitializer(array($this, 'populateFactory'));
}

/**
* Populate the factory with filter chain and validator chain
*
* @param $element
*/
public function populateFactory($element)
{
if ($element instanceof InputFilter && $this->serviceLocator instanceof ServiceLocatorInterface) {
$factory = $element->getFactory();
$factory->getDefaultFilterChain()->setPluginManager($this->serviceLocator->get('FilterManager'));
$factory->getDefaultValidatorChain()->setPluginManager($this->serviceLocator->get('ValidatorManager'));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably check that $this->serviceLocator is indeed an instance before calling get() on it. You could do it in the initial conditional:

if ($element instanceof InputFilter && $this->serviceLocator instanceof ServiceLocatorInterface) {
    /* ... */
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it always the case ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's always a remote possibility that somebody will instantiate it themselves. :)

}

/**
* {@inheritDoc}
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof InputFilterInterface) {
// Hook to perform various initialization, when the element is not created through the factory
if ($plugin instanceof InitializableInterface) {
$plugin->init();
}

// we're okay
return;
}

throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement Zend\InputFilter\InputFilterInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
));
}
}
3 changes: 3 additions & 0 deletions library/Zend/InputFilter/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"zendframework/zend-validator": "self.version",
"zendframework/zend-stdlib": "self.version"
},
"suggest": {
"zendframework/zend-servicemanager": "To support plugin manager support"
},
"extra": {
"branch-alias": {
"dev-master": "2.1-dev",
Expand Down
21 changes: 21 additions & 0 deletions library/Zend/ModuleManager/Feature/HydratorProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\ModuleManager\Feature;

interface HydratorProviderInterface
{
/**
* Expected to return \Zend\ServiceManager\Config object or array to
* seed such an object.
*
* @return array|\Zend\ServiceManager\Config
*/
public function getHydratorConfig();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\ModuleManager\Feature;

interface InputFilterProviderInterface
{
/**
* Expected to return \Zend\ServiceManager\Config object or array to
* seed such an object.
*
* @return array|\Zend\ServiceManager\Config
*/
public function getInputFilterConfig();
}
18 changes: 18 additions & 0 deletions library/Zend/Mvc/Service/HydratorManagerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Mvc\Service;

use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class HydratorManagerFactory extends AbstractPluginManagerFactory
{
const PLUGIN_MANAGER_CLASS = 'Zend\Stdlib\Hydrator\HydratorPluginManager';
}
18 changes: 18 additions & 0 deletions library/Zend/Mvc/Service/InputFilterManagerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Mvc\Service;

use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class InputFilterManagerFactory extends AbstractPluginManagerFactory
{
const PLUGIN_MANAGER_CLASS = 'Zend\InputFilter\InputFilterPluginManager';
}
18 changes: 18 additions & 0 deletions library/Zend/Mvc/Service/ModuleManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ public function createService(ServiceLocatorInterface $serviceLocator)
'Zend\ModuleManager\Feature\RouteProviderInterface',
'getRouteConfig'
);
$serviceListener->addServiceManager(
'SerializerAdapterManager',
'serializers',
'Zend\ModuleManager\Feature\SerializerProviderInterface',
'getSerializerConfig'
);
$serviceListener->addServiceManager(
'HydratorManager',
'hydrators',
'Zend\ModuleManager\Feature\HydratorProviderInterface',
'getHydratorConfig'
);
$serviceListener->addServiceManager(
'InputFilterManager',
'input_filters',
'Zend\ModuleManager\Feature\InputFilterProviderInterface',
'getInputFilterConfig'
);

$events = $serviceLocator->get('EventManager');
$events->attach($defaultListeners);
Expand Down
23 changes: 0 additions & 23 deletions library/Zend/Mvc/Service/SerializerAdapterPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,7 @@

namespace Zend\Mvc\Service;

use Zend\ServiceManager\ServiceLocatorInterface;

class SerializerAdapterPluginManagerFactory extends AbstractPluginManagerFactory
{
const PLUGIN_MANAGER_CLASS = 'Zend\Serializer\AdapterPluginManager';


/**
* {@inheritDoc}
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
/** @var $serviceListener \Zend\ModuleManager\Listener\ServiceListener */
$serviceListener = $serviceLocator->get('ServiceListener');

// This will allow to register new serializers easily, either by implementing the SerializerProviderInterface
// in your Module.php file, or by adding the "serializers" key in your module.config.php file
$serviceListener->addServiceManager(
$serviceLocator,
'serializers',
'Zend\ModuleManager\Feature\SerializerProviderInterface',
'getSerializerConfig'
);

return parent::createService($serviceLocator);
}
}
3 changes: 3 additions & 0 deletions library/Zend/Mvc/Service/ServiceListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ class ServiceListenerFactory implements FactoryInterface
'FilterManager' => 'Zend\Mvc\Service\FilterManagerFactory',
'FormElementManager' => 'Zend\Mvc\Service\FormElementManagerFactory',
'HttpRouter' => 'Zend\Mvc\Service\RouterFactory',
'HydratorManager' => 'Zend\Mvc\Service\HydratorManagerFactory',
'InputFilterManager' => 'Zend\Mvc\Service\InputFilterManagerFactory',
'PaginatorPluginManager' => 'Zend\Mvc\Service\PaginatorPluginManagerFactory',
'Request' => 'Zend\Mvc\Service\RequestFactory',
'Response' => 'Zend\Mvc\Service\ResponseFactory',
'Router' => 'Zend\Mvc\Service\RouterFactory',
'RoutePluginManager' => 'Zend\Mvc\Service\RoutePluginManagerFactory',
'SerializerAdapterManager' => 'Zend\Mvc\Service\SerializerAdapterPluginManagerFactory',
'ValidatorManager' => 'Zend\Mvc\Service\ValidatorManagerFactory',
'ViewHelperManager' => 'Zend\Mvc\Service\ViewHelperManagerFactory',
'ViewFeedRenderer' => 'Zend\Mvc\Service\ViewFeedRendererFactory',
Expand Down
3 changes: 3 additions & 0 deletions library/Zend/Serializer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"zendframework/zend-json": "self.version",
"zendframework/zend-math": "self.version"
},
"suggest": {
"zendframework/zend-servicemanager": "To support plugin manager support"
},
"extra": {
"branch-alias": {
"dev-master": "2.1-dev",
Expand Down
56 changes: 56 additions & 0 deletions library/Zend/Stdlib/Hydrator/HydratorPluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Hydrator;

use Zend\ServiceManager\AbstractPluginManager;
use Zend\Stdlib\Exception;

/**
* Plugin manager implementation for hydrators.
*
* Enforces that adapters retrieved are instances of HydratorInterface
*/
class HydratorPluginManager extends AbstractPluginManager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this one should set the shareByDefault flag to false.

The reason I mention this is because the ClassMethods hydrator and the SerializableStrategy both take options, and those may be different based on the object type you want to hydrate/extract.

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmhhh... I don't know what is the preferred use case here... On more heavy hydrators like DoctrineModule's one I think we'd prefer to share... I tend to think that you use the same options within your application, so shared may be the safest solution... But I'm not sure on this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking about situations when you have multiple 3rd party modules, each with different strategies. There may not be cohesion in such cases.

{
/**
* Whether or not to share by default
*
* @var bool
*/
protected $shareByDefault = false;

/**
* Default set of adapters
*
* @var array
*/
protected $invokableClasses = array(
'arrayserializable' => 'Zend\Stdlib\Hydrator\ArraySerializable',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bakura10 maybe use aliases instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius Why? Other plugin managers use invokables as shortname/classname pairs...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Furthremore all plugin managers are configured to autoadd any invokables classes.

'classmethods' => 'Zend\Stdlib\Hydrator\ClassMethods',
'objectproperty' => 'Zend\Stdlib\Hydrator\ObjectProperty',
'reflection' => 'Zend\Stdlib\Hydrator\Reflection'
);

/**
* {@inheritDoc}
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof HydratorInterface) {
// we're okay
return;
}

throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement Zend\Stdlib\Hydrator\HydratorInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
));
}
}
3 changes: 2 additions & 1 deletion library/Zend/Stdlib/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
},
"target-dir": "Zend/Stdlib",
"suggest": {
"pecl-weakref": "Implementation of weak references for Stdlib\\CallbackHandler"
"pecl-weakref": "Implementation of weak references for Stdlib\\CallbackHandler",
"zendframework/zend-servicemanager": "To support hydrator plugin manager usage"
},
"require": {
"php": ">=5.3.3"
Expand Down
Loading