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 #138 from zf-fr/repository-manager
Add an object repository plugin manager
- Loading branch information
Showing
10 changed files
with
307 additions
and
58 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
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
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
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,49 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. | ||
*/ | ||
|
||
namespace ZfrRest\Factory; | ||
|
||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use ZfrRest\Resource\ResourcePluginManager; | ||
|
||
/** | ||
* Factory to create an ResourcePluginManager | ||
* | ||
* @author Michaël Gallego <mic.gallego@gmail.com> | ||
* @licence MIT | ||
*/ | ||
class ResourcePluginManagerFactory implements FactoryInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
/* @var \ZfrRest\Options\ModuleOptions $moduleOptions */ | ||
$moduleOptions = $serviceLocator->get('ZfrRest\Options\ModuleOptions'); | ||
|
||
/* @var \Doctrine\Common\Persistence\ObjectManager $objectManager */ | ||
$objectManager = $serviceLocator->get($moduleOptions->getObjectManager()); | ||
|
||
$pluginManager = new ResourcePluginManager($objectManager); | ||
$pluginManager->setServiceLocator($serviceLocator); | ||
|
||
return $pluginManager; | ||
} | ||
} |
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,88 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. | ||
*/ | ||
|
||
namespace ZfrRest\Resource; | ||
|
||
use Doctrine\Common\Persistence\ObjectManager; | ||
use Zend\ServiceManager\AbstractPluginManager; | ||
use Zend\ServiceManager\Exception; | ||
use ZfrRest\Exception\RuntimeException; | ||
|
||
/** | ||
* Plugin manager that allows to create object repository, without having to define a factory | ||
* for each entity. This is used also to improve performance, so that the router can lazy-load | ||
* the creation of repositories | ||
* | ||
* @author Michaël Gallego <mic.gallego@gmail.com> | ||
* @licence MIT | ||
*/ | ||
class ResourcePluginManager extends AbstractPluginManager | ||
{ | ||
/** | ||
* @var ObjectManager | ||
*/ | ||
protected $objectManager; | ||
|
||
/** | ||
* @param ObjectManager $objectManager | ||
*/ | ||
public function __construct(ObjectManager $objectManager) | ||
{ | ||
$this->objectManager = $objectManager; | ||
} | ||
|
||
/** | ||
* Get the repository | ||
* | ||
* @param string $name | ||
* @return object | ||
*/ | ||
public function get($name) | ||
{ | ||
// First check if an explicit resource was set | ||
if ($this->has($name)) { | ||
return parent::get($name); | ||
} | ||
|
||
// Otherwise, fallback to getting an object repository | ||
return $this->objectManager->getRepository($name); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function validatePlugin($plugin) | ||
{ | ||
if (is_object($plugin)) { | ||
return; // we're okay | ||
} | ||
|
||
throw new RuntimeException(sprintf( | ||
'An object was expected; "%s" given', | ||
gettype($plugin) | ||
)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function canonicalizeName($name) | ||
{ | ||
return $name; | ||
} | ||
} |
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
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
49 changes: 49 additions & 0 deletions
49
tests/ZfrRestTest/Factory/ResourcePluginManagerFactoryTest.php
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,49 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. | ||
*/ | ||
|
||
namespace ZfrRestTest\Factory; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
use Zend\ServiceManager\ServiceManager; | ||
use ZfrRest\Factory\ResourcePluginManagerFactory; | ||
use ZfrRest\Options\ModuleOptions; | ||
|
||
/** | ||
* @licence MIT | ||
* @author Michaël Gallego <mic.gallego@gmail.com> | ||
* | ||
* @group Coverage | ||
* @covers \ZfrRest\Factory\ResourcePluginManagerFactory | ||
*/ | ||
class ResourcePluginManagerFactoryTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testCreateFromFactory() | ||
{ | ||
$moduleOptions = new ModuleOptions(['object_manager' => 'my_object_manager']); | ||
|
||
$serviceManager = new ServiceManager(); | ||
$serviceManager->setService('ZfrRest\Options\ModuleOptions', $moduleOptions); | ||
$serviceManager->setService('my_object_manager', $this->getMock('Doctrine\Common\Persistence\ObjectManager')); | ||
|
||
$factory = new ResourcePluginManagerFactory(); | ||
$result = $factory->createService($serviceManager); | ||
|
||
$this->assertInstanceOf('ZfrRest\Resource\ResourcePluginManager', $result); | ||
$this->assertSame($serviceManager, $result->getServiceLocator()); | ||
} | ||
} |
Oops, something went wrong.