This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zendframework/zendframework#4319 from bakura10/hyd…
…rator-plugin-manager Add various plugin manager
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
/** | ||
* 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', | ||
'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)) | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?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 ZendTest\Stdlib\Hydrator; | ||
|
||
use Zend\ServiceManager\ServiceManager; | ||
use Zend\Stdlib\Hydrator\HydratorPluginManager; | ||
|
||
/** | ||
* @group Zend_Stdlib | ||
*/ | ||
class HydratorManagerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var HydratorPluginManager | ||
*/ | ||
protected $manager; | ||
|
||
public function setUp() | ||
{ | ||
$this->manager = new HydratorPluginManager(); | ||
} | ||
|
||
public function testRegisteringInvalidElementRaisesException() | ||
{ | ||
$this->setExpectedException('Zend\Stdlib\Exception\RuntimeException'); | ||
$this->manager->setService('test', $this); | ||
} | ||
|
||
public function testLoadingInvalidElementRaisesException() | ||
{ | ||
$this->manager->setInvokableClass('test', get_class($this)); | ||
$this->setExpectedException('Zend\Stdlib\Exception\RuntimeException'); | ||
$this->manager->get('test'); | ||
} | ||
} |