-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add various plugin manager #4319
Changes from all commits
da79087
0a3650c
a647437
8a67357
d6b5798
1a6d521
4563312
f8f2892
ee439e3
0aaefd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
{ | ||
/** | ||
* @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')); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should probably check that if ($element instanceof InputFilter && $this->serviceLocator instanceof ServiceLocatorInterface) {
/* ... */
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't it always the case ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
)); | ||
} | ||
} |
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(); | ||
} |
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'; | ||
} |
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'; | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if this one should set the 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bakura10 maybe use aliases instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Ocramius Why? Other plugin managers use invokables as shortname/classname pairs... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
)); | ||
} | ||
} |
There was a problem hiding this comment.
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.