-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DoctrineBundle] Implement Proxy CacheWarmer
- Loading branch information
Showing
7 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/Symfony/Bundle/DoctrineBundle/CacheWarmer/ProxyCacheWarmer.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,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\DoctrineBundle\CacheWarmer; | ||
|
||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
|
||
/** | ||
* The proxy generator cache warmer generates all entity proxies. | ||
* | ||
* In the process of generating proxies the cache for all the metadata is primed also, | ||
* since this information is necessary to build the proxies in the first place. | ||
* | ||
* @author Benjamin Eberlei <kontakt@beberlei.de> | ||
*/ | ||
class ProxyCacheWarmer implements CacheWarmerInterface | ||
{ | ||
/** | ||
* @var Container | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @param Container $container | ||
*/ | ||
public function __construct(Container $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* This cache warmer is not optional, without proxies fatal error occour! | ||
* | ||
* @return false | ||
*/ | ||
public function isOptional() | ||
{ | ||
return false; | ||
} | ||
|
||
public function warmUp($cacheDir) | ||
{ | ||
$entityManagers = $this->container->getParameter('doctrine.orm.entity_managers'); | ||
foreach ($entityManagers AS $entityManagerName) { | ||
$em = $this->container->get(sprintf('doctrine.orm.%s_entity_manager', $entityManagerName)); | ||
/* @var $em Doctrine\ORM\EntityManager */ | ||
$classes = $em->getMetadataFactory()->getAllMetadata(); | ||
$em->getProxyFactory()->generateProxyClasses($classes); | ||
} | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
src/Symfony/Bundle/DoctrineBundle/Tests/CacheWarmer/ProxyCacheWarmerTest.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,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\DoctrineBundle\Tests\CacheWarmer; | ||
|
||
use Symfony\Bundle\DoctrineBundle\CacheWarmer\ProxyCacheWarmer; | ||
|
||
class ProxyCacheWarmerTest extends \Symfony\Bundle\DoctrineBundle\Tests\TestCase | ||
{ | ||
/** | ||
* This is not necessarily a good test, it doesnt generate any proxies | ||
* because there are none in the AnnotationsBundle. However that is | ||
* rather a task of doctrine to test. We touch the lines here and | ||
* verify that the container is called correctly for the relevant information. | ||
*/ | ||
public function testWarmCache() | ||
{ | ||
$testManager = $this->createTestEntityManager(array( | ||
__DIR__ . "/../DependencyInjection/Fixtures/Bundles/AnnotationsBundle/Entity") | ||
); | ||
$container = $this->getMock('Symfony\Component\DependencyInjection\Container'); | ||
$container->expects($this->at(0)) | ||
->method('getParameter') | ||
->with($this->equalTo('doctrine.orm.entity_managers')) | ||
->will($this->returnValue(array('default', 'foo'))); | ||
$container->expects($this->at(1)) | ||
->method('get') | ||
->with($this->equalTo('doctrine.orm.default_entity_manager')) | ||
->will($this->returnValue($testManager)); | ||
$container->expects($this->at(2)) | ||
->method('get') | ||
->with($this->equalTo('doctrine.orm.foo_entity_manager')) | ||
->will($this->returnValue($testManager)); | ||
|
||
$cacheWarmer = new ProxyCacheWarmer($container); | ||
$cacheWarmer->warmUp(sys_get_temp_dir()); | ||
} | ||
|
||
public function testProxyCacheWarmingIsNotOptional() | ||
{ | ||
$container = $this->getMock('Symfony\Component\DependencyInjection\Container'); | ||
$cacheWarmer = new ProxyCacheWarmer($container); | ||
|
||
$this->assertFalse($cacheWarmer->isOptional()); | ||
} | ||
} |
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