This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
930df91
commit 7b7a637
Showing
4 changed files
with
288 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?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. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Alias; | ||
|
||
/** | ||
* Symfony bridge adpter | ||
* | ||
* @author Kinn Coelho Julião <kinncj@php.net> | ||
* @author Fabio B. Silva <fabio.bat.silva@gmail.com> | ||
*/ | ||
class SymfonyBridgeAdapter | ||
{ | ||
/** | ||
* @var \Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\DoctrineCacheExtension | ||
*/ | ||
protected $cacheExtension; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $objectManagerName; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $mappingResourceName; | ||
|
||
/** | ||
* @param \Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\DoctrineCacheExtension $cacheExtension | ||
* @param string $objectManagerName | ||
* @param string $mappingResourceName | ||
*/ | ||
public function __construct(DoctrineCacheExtension $cacheExtension, $objectManagerName, $mappingResourceName) | ||
{ | ||
$this->cacheExtension = $cacheExtension; | ||
$this->objectManagerName = $objectManagerName; | ||
$this->mappingResourceName = $mappingResourceName; | ||
} | ||
|
||
/** | ||
* @param string $cacheName | ||
* @param string $objectManagerName | ||
* @param array $cacheDriver | ||
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container | ||
* | ||
* @return string | ||
*/ | ||
public function loadCacheDriver($cacheName, $objectManagerName, array $cacheDriver, ContainerBuilder $container) | ||
{ | ||
$id = $this->getObjectManagerElementName($objectManagerName . '_' . $cacheName); | ||
$host = isset($cacheDriver['host']) ? $cacheDriver['host'] : null; | ||
$port = isset($cacheDriver['port']) ? $cacheDriver['port'] : null; | ||
$type = $cacheDriver['type']; | ||
|
||
if ($type == 'service') { | ||
$container->setAlias($id, new Alias($cacheDriver['id'], false)); | ||
|
||
return $id; | ||
} | ||
|
||
$config = array( | ||
'aliases' => array($id), | ||
$type => array(), | ||
'type' => $type, | ||
'namespace' => null, | ||
); | ||
|
||
if ( ! isset($cacheDriver['namespace'])) { | ||
// generate a unique namespace for the given application | ||
$environment = $container->getParameter('kernel.root_dir').$container->getParameter('kernel.environment'); | ||
$hash = hash('sha256', $environment); | ||
$namespace = 'sf2' . $this->mappingResourceName .'_' . $objectManagerName . '_' . $hash; | ||
|
||
$config['namespace'] = $namespace; | ||
} | ||
|
||
if (in_array($type, array('memcache', 'memcached'))) { | ||
$config[$type]['servers'][$host] = array( | ||
'host' => $host, | ||
'port' => $port, | ||
); | ||
} | ||
|
||
if ($type === 'redis') { | ||
$config[$type] = array( | ||
'host' => $host, | ||
'port' => $port, | ||
); | ||
} | ||
|
||
$this->cacheExtension->loadCacheProvider($cacheName, $config, $container); | ||
|
||
return $id; | ||
} | ||
|
||
/** | ||
* @param array $objectManager | ||
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container | ||
* @param string $cacheName | ||
*/ | ||
public function loadObjectManagerCacheDriver(array $objectManager, ContainerBuilder $container, $cacheName) | ||
{ | ||
$this->loadCacheDriver($cacheName, $objectManager['name'], $objectManager[$cacheName.'_driver'], $container); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return string | ||
*/ | ||
protected function getObjectManagerElementName($name) | ||
{ | ||
return $this->objectManagerName . '.' . $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?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. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\Bundle\DoctrineCacheBundle\Tests\DependencyInjection; | ||
|
||
use Doctrine\Bundle\DoctrineCacheBundle\Tests\TestCase; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\SymfonyBridgeAdapter; | ||
use Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\DoctrineCacheExtension; | ||
|
||
/** | ||
* @group Extension | ||
* @group SymfonyBridge | ||
* | ||
* @author Kinn Coelho Julião <kinncj@php.net> | ||
* @author Fabio B. Silva <fabio.bat.silva@gmail.com> | ||
*/ | ||
class SymfonyBridgeAdpterTest extends TestCase | ||
{ | ||
/** | ||
* @var \Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\SymfonyBridgeAdapter | ||
*/ | ||
private $adapter; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->adapter = new SymfonyBridgeAdapter(new DoctrineCacheExtension(), 'doctrine.orm', 'orm'); | ||
} | ||
|
||
public function providerBasicDrivers() | ||
{ | ||
return array( | ||
array('%doctrine_cache.apc.class%', array('type' => 'apc')), | ||
array('%doctrine_cache.array.class%', array('type' => 'array')), | ||
array('%doctrine_cache.xcache.class%', array('type' => 'xcache')), | ||
array('%doctrine_cache.wincache.class%', array('type' => 'wincache')), | ||
array('%doctrine_cache.zenddata.class%', array('type' => 'zenddata')), | ||
array('%doctrine_cache.redis.class%', array('type' => 'redis'), array('setRedis')), | ||
array('%doctrine_cache.memcache.class%', array('type' => 'memcache'), array('setMemcache')), | ||
array('%doctrine_cache.memcached.class%', array('type' => 'memcached'), array('setMemcached')), | ||
); | ||
} | ||
|
||
/** | ||
* @param string $class | ||
* @param array $config | ||
* | ||
* @dataProvider providerBasicDrivers | ||
*/ | ||
public function testLoadBasicCacheDriver($class, array $config, array $expectedCalls = array()) | ||
{ | ||
$container = $this->createServiceContainer(); | ||
$cacheName = 'metadata_cache'; | ||
$objectManager = array( | ||
'name' => 'default', | ||
'metadata_cache_driver' => $config | ||
); | ||
|
||
$this->adapter->loadObjectManagerCacheDriver($objectManager, $container, $cacheName); | ||
$this->assertTrue($container->hasAlias('doctrine.orm.default_metadata_cache')); | ||
|
||
$alias = $container->getAlias('doctrine.orm.default_metadata_cache'); | ||
$decorator = $container->getDefinition($alias); | ||
$definition = $container->getDefinition($decorator->getParent()); | ||
$defCalls = $decorator->getMethodCalls(); | ||
$expectedCalls[] = 'setNamespace'; | ||
$actualCalls = array_map(function ($call) { | ||
return $call[0]; | ||
}, $defCalls); | ||
|
||
$this->assertEquals($class, $definition->getClass()); | ||
|
||
foreach (array_unique($expectedCalls) as $call) { | ||
$this->assertContains($call, $actualCalls); | ||
} | ||
} | ||
|
||
public function testServiceCacheDriver() | ||
{ | ||
$cacheName = 'metadata_cache'; | ||
$container = $this->createServiceContainer(); | ||
$definition = new Definition('%doctrine.orm.cache.apc.class%'); | ||
$objectManager = array( | ||
'name' => 'default', | ||
'metadata_cache_driver' => array( | ||
'type' => 'service', | ||
'id' => 'service_driver' | ||
) | ||
); | ||
|
||
$container->setDefinition('service_driver', $definition); | ||
|
||
$this->adapter->loadObjectManagerCacheDriver($objectManager, $container, $cacheName); | ||
|
||
$this->assertTrue($container->hasAlias('doctrine.orm.default_metadata_cache')); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedExceptionMessage "unrecognized_type" is an unrecognized Doctrine cache driver. | ||
*/ | ||
public function testUnrecognizedCacheDriverException() | ||
{ | ||
$cacheName = 'metadata_cache'; | ||
$container = $this->createServiceContainer(); | ||
$objectManager = array( | ||
'name' => 'default', | ||
'metadata_cache_driver' => array( | ||
'type' => 'unrecognized_type' | ||
) | ||
); | ||
|
||
$this->adapter->loadObjectManagerCacheDriver($objectManager, $container, $cacheName); | ||
} | ||
} |
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