8000 [PropertyAccess] Add a cache factory · symfony/symfony@1b4b8fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b4b8fd

Browse files
committed
[PropertyAccess] Add a cache factory
1 parent 2a0726e commit 1b4b8fd

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
1313

1414
use Doctrine\Common\Annotations\Reader;
15+
use Symfony\Component\Cache\Adapter\ApcuAdapter;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\ContainerInterface;
1718
use Symfony\Component\DependencyInjection\Definition;
@@ -1040,6 +1041,7 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
10401041
$version = substr(str_replace('/', '-', base64_encode(md5(uniqid(mt_rand(), true), true))), 0, -2);
10411042
$container->getDefinition('cache.adapter.apcu')->replaceArgument(2, $version);
10421043
$container->getDefinition('cache.adapter.system')->replaceArgument(2, $version);
1044+
$container->getDefinition('cache.property_access')->replaceArgument(2, $version);
10431045
$container->getDefinition('cache.adapter.filesystem')->replaceArgument(2, $config['directory']);
10441046

10451047
foreach (array('doctrine', 'psr6', 'redis') as $name) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@
2222
<tag name="cache.pool" />
2323
</service>
2424

25-
<service id="cache.property_accessor" parent="cache.system" public="false">
26-
<tag name="cache.pool" />
25+
<service id="cache.property_access" class="Symfony\Component\Cache\Adapter\AdapterInterface" public="false">
26+
<factory class="Symfony\Component\PropertyAccess\PropertyAccessor" method="createCache" />
27+
<tag name="cache.pool" clearer="cache.default_clearer" />
28+
<tag name="monolog.logger" channel="cache" />
29+
<argument /> <!-- namespace -->
30+
<argument /> <!-- default lifetime -->
31+
<argument /> <!-- version -->
32+
<argument type="service" id="logger" on-invalid="ignore" />
2733
</service>
2834

2935
<service id="cache.adapter.system" class="Symfony\Component\Cache\Adapter\AdapterInterface" abstract="true">

src/Symfony/Bundle/FrameworkBundle/Resources/config/property_access.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<service id="property_accessor" class="Symfony\Component\PropertyAccess\PropertyAccessor" >
99
<argument /> <!-- magicCall, set by the extension -->
1010
<argument /> <!-- throwExceptionOnInvalidIndex, set by the extension -->
11+
<argument type="service" id="cache.property_access" on-invalid="ignore" />
10000
1112
</service>
1213
</services>
1314
</container>

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Psr\Cache\CacheItemPoolInterface;
1515
use Psr\Log\LoggerInterface;
1616
use Symfony\Component\Cache\Adapter\ApcuAdapter;
17-
use Symfony\Component\Cache\Exception\CacheException;
1817
use Symfony\Component\Inflector\Inflector;
1918
use Symfony\Component\PropertyAccess\Exception\AccessException;
2019
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
@@ -161,7 +160,7 @@ public function __construct($magicCall = false, $throwExceptionOnInvalidIndex =
161160
{
162161
$this->magicCall = $magicCall;
163162
$this->ignoreInvalidIndices = !$throwExceptionOnInvalidIndex;
164-
$this->cacheItemPool = null === $cacheItemPool ? self::createCache() : $cacheItemPool;
163+
$this->cacheItemPool = $cacheItemPool;
165164
}
166165

167166
/**
@@ -897,29 +896,25 @@ private function getPropertyPath($propertyPath)
897896
}
898897

899898
/**
900-
* Creates an ApcuAdapter if possible or return null.
899+
* Creates the APCu adapter if applicable.
901900
*
902901
* @param string $namespace
903902
* @param int $defaultLifetime
903+
* @param string $version
904904
* @param LoggerInterface|null $logger
905905
*
906906
* @return ApcuAdapter|null
907907
*/
908-
public static function createCache($namespace = '', $defaultLifetime = 0, LoggerInterface $logger = null)
908+
public static function createCache($namespace, $defaultLifetime, $version, LoggerInterface $logger = null)
909909
{
910-
if (!class_exists('Symfony\Component\Cache\Adapter\ApcuAdapter')) {
910+
if (!class_exists('Symfony\Component\Cache\Adapter\ApcuAdapter') || !ApcuAdapter::isSupported()) {
911911
return;
912912
}
913913

914-
try {
915-
$apcu = new ApcuAdapter($namespace, $defaultLifetime / 5);
916-
if (null !== $logger) {
917-
$apcu->setLogger($logger);
918-
}
919-
920-
return $apcu;
921-
} catch (CacheException $e) {
922-
// return null
914+
$apcu = new ApcuAdapter($namespace, $defaultLifetime / 5, $version);
915+
if (null !== $logger) {
916+
$apcu->setLogger($logger);
923917
}
918+
return $apcu;
924919
}
925920
}

0 commit comments

Comments
 (0)
0