Skip to content

Commit

Permalink
Merge pull request #129 from zf-fr/fix-cache
Browse files Browse the repository at this point in the history
Properly wakeup ClassMetadata
  • Loading branch information
bakura10 committed Feb 22, 2014
2 parents 0ec97cc + 8f36976 commit 4cb0af6
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 25 deletions.
2 changes: 1 addition & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
'factories' => [
/* Factories that do not map to a class */
'ZfrRest\Cache' => 'ZfrRest\Factory\CacheFactory',
'ZfrRest\Resource\Metadata\ResourceMetadataFactory' => 'ZfrRest\Factory\ResourceMetadataFactoryFactory',
'ZfrRest\View\Renderer\ResourceRenderer' => 'ZfrRest\Factory\SimpleResourceRendererFactory',

/* Factories that map to a class */
'ZfrRest\Mvc\HttpExceptionListener' => 'ZfrRest\Factory\HttpExceptionListenerFactory',
'ZfrRest\Mvc\Controller\MethodHandler\MethodHandlerPluginManager' => 'ZfrRest\Factory\MethodHandlerPluginManagerFactory',
'ZfrRest\Options\ModuleOptions' => 'ZfrRest\Factory\ModuleOptionsFactory',
'ZfrRest\Resource\Metadata\ResourceMetadataFactory' => 'ZfrRest\Factory\ResourceMetadataFactoryFactory',
'ZfrRest\Router\Http\Matcher\AssociationSubPathMatcher' => 'ZfrRest\Factory\AssociationSubPathMatcherFactory',
'ZfrRest\Router\Http\Matcher\BaseSubPathMatcher' => 'ZfrRest\Factory\BaseSubPathMatcherFactory',
'ZfrRest\View\Strategy\ResourceStrategy' => 'ZfrRest\Factory\ResourceStrategyFactory'
Expand Down
10 changes: 4 additions & 6 deletions src/ZfrRest/Factory/ResourceMetadataFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Metadata\Driver\DriverChain;
use Metadata\MetadataFactory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfrRest\Exception\RuntimeException;
use ZfrRest\Resource\Metadata\Driver\AnnotationDriver;
use ZfrRest\Resource\Metadata\ResourceMetadataFactory;

/**
* @author Michaël Gallego <mic.gallego@gmail.com>
Expand All @@ -47,18 +47,16 @@ public function createService(ServiceLocatorInterface $serviceLocator)
$doctrineMetadataFactory = $objectManager->getMetadataFactory();

$driverChain = new DriverChain();
$resourceMetadataFactory = new MetadataFactory($driverChain);
$resourceMetadataFactory = new ResourceMetadataFactory($doctrineMetadataFactory, $driverChain);

$drivers = $moduleOptions->getDrivers();
foreach ($drivers as $driverOptions) {
$driver = null;

switch($driverOptions->getClass()) {
case 'ZfrRest\Resource\Metadata\Driver\AnnotationDriver':
$driver = new AnnotationDriver(
new AnnotationReader(),
$doctrineMetadataFactory
);
$driver = new AnnotationDriver(new AnnotationReader());

break;
default:
throw new RuntimeException(sprintf(
Expand Down
21 changes: 4 additions & 17 deletions src/ZfrRest/Resource/Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,14 @@ class AnnotationDriver implements DriverInterface
*/
protected $annotationReader;

/**
* @var ClassMetadataFactory
*/
protected $classMetadataFactory;

/**
* Constructor
*
* @param Reader $annotationReader
* @param ClassMetadataFactory $classMetadataFactory
* @param Reader $annotationReader
*/
public function __construct(
Reader $annotationReader,
ClassMetadataFactory $classMetadataFactory
) {
$this->annotationReader = $annotationReader;
$this->classMetadataFactory = $classMetadataFactory;
public function __construct(Reader $annotationReader)
{
$this->annotationReader = $annotationReader;
}

/**
Expand All @@ -64,12 +55,8 @@ public function __construct(
public function loadMetadataForClass(ReflectionClass $class)
{
$className = $class->getName();

$classMetadata = $this->classMetadataFactory->getMetadataFor($className);
$resourceMetadata = new ResourceMetadata($className);

$resourceMetadata->propertyMetadata['classMetadata'] = $classMetadata;

// Process class level annotations
$classAnnotations = $this->annotationReader->getClassAnnotations($class);
$this->processMetadata($resourceMetadata, $classAnnotations);
Expand Down
14 changes: 14 additions & 0 deletions src/ZfrRest/Resource/Metadata/ResourceMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace ZfrRest\Resource\Metadata;

use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Metadata\ClassMetadata;
use ZfrRest\Resource\Exception;
use ZfrRest\Resource\Resource;
Expand Down Expand Up @@ -109,4 +111,16 @@ public function getAssociationMetadata($association)

return $this->propertyMetadata['associations'][$association];
}

/**
* We don't want to serialize ourself the Doctrine class metadata, because it needs
* special handing that is performed by the class metadata factory
*
* @return string
*/
public function serialize()
{
unset($this->propertyMetadata['classMetadata']);
return parent::serialize();
}
}
57 changes: 57 additions & 0 deletions src/ZfrRest/Resource/Metadata/ResourceMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\Metadata;

use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
use Metadata\Driver\DriverInterface;
use Metadata\MetadataFactory;

/**
* @author Michaël Gallego <mic.gallego@gmail.com>
* @licence MIT
*/
class ResourceMetadataFactory extends MetadataFactory
{
/**
* @var ClassMetadataFactory
*/
protected $classMetadataFactory;

/**
* @param ClassMetadataFactory $classMetadataFactory
* @param DriverInterface $driver
*/
public function __construct(ClassMetadataFactory $classMetadataFactory, DriverInterface $driver)
{
$this->classMetadataFactory = $classMetadataFactory;
parent::__construct($driver);
}

/**
* @param string $className
* @return ResourceMetadataInterface
*/
public function getMetadataForClass($className)
{
$resourceMetadata = parent::getMetadataForClass($className);
$resourceMetadata->propertyMetadata['classMetadata'] = $this->classMetadataFactory->getMetadataFor($className);

return $resourceMetadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ public function testCreateFromFactory($driverConfig, $throwException)
$factory = new ResourceMetadataFactoryFactory();
$result = $factory->createService($serviceManager);

$this->assertInstanceOf('Metadata\MetadataFactory', $result);
$this->assertInstanceOf('ZfrRest\Resource\Metadata\ResourceMetadataFactory', $result);
}
}
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\Resource\Metadata;

use ZfrRest\Resource\Metadata\ResourceMetadataFactory;

/**
* @licence MIT
* @author Michaël Gallego <mic.gallego@gmail.com>
*
* @group Coverage
* @covers \ZfrRest\Resource\Metadata\ResourceMetadataFactory
*/
class ResourceMetadataFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testAssertDoctrineClassMetadataIsFilled()
{
$classMetadataFactory = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadataFactory');
$driver = $this->getMock('Metadata\Driver\DriverInterface');

$metadataFactory = new ResourceMetadataFactory($classMetadataFactory, $driver);

$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');

$classMetadataFactory->expects($this->once())
->method('getMetadataFor')
->will($this->returnValue($classMetadata));

$resourceMetadata = $metadataFactory->getMetadataForClass('stdClass');

$this->assertSame($classMetadata, $resourceMetadata->propertyMetadata['classMetadata']);
}
}

0 comments on commit 4cb0af6

Please sign in to comment.