From 6d86e4c0ad68eddf16e27e4a7eb47213972941c1 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 16 Jul 2017 20:25:06 +0200 Subject: [PATCH 1/8] Support for simple cache --- composer.json | 5 +- src/Cache/Recording/CachePool.php | 263 ------------------ src/Cache/Recording/Factory.php | 74 ----- .../Recording/HierarchyAndTaggablePool.php | 23 -- src/Cache/Recording/HierarchyPool.php | 23 -- src/Cache/Recording/TaggablePool.php | 48 ---- src/DataCollector/CacheDataCollector.php | 43 ++- src/DataCollector/CacheProxy.php | 15 + src/DataCollector/DecoratingFactory.php | 47 ++++ src/DataCollector/ProxyFactory.php | 233 ++++++++++++++++ src/DataCollector/TraceableAdapterEvent.php | 17 ++ .../Compiler/DataCollectorCompilerPass.php | 40 +-- src/Resources/config/data-collector.yml | 4 + 13 files changed, 360 insertions(+), 475 deletions(-) delete mode 100644 src/Cache/Recording/CachePool.php delete mode 100644 src/Cache/Recording/Factory.php delete mode 100644 src/Cache/Recording/HierarchyAndTaggablePool.php delete mode 100644 src/Cache/Recording/HierarchyPool.php delete mode 100644 src/Cache/Recording/TaggablePool.php create mode 100644 src/DataCollector/CacheProxy.php create mode 100644 src/DataCollector/DecoratingFactory.php create mode 100644 src/DataCollector/ProxyFactory.php create mode 100644 src/DataCollector/TraceableAdapterEvent.php diff --git a/composer.json b/composer.json index e867685..45a599d 100644 --- a/composer.json +++ b/composer.json @@ -27,14 +27,15 @@ "php": "^5.6 || ^7.0", "symfony/framework-bundle": "^2.7 || ^3.0", "cache/taggable-cache": "^0.5", - "cache/session-handler": "^0.2" + "cache/session-handler": "^0.2", + "nyholm/nsa": "^1.1" }, "require-dev": { "phpunit/phpunit": "^5.7.17", "symfony/symfony": "^2.7 || ^3.0", "cache/psr-6-doctrine-bridge": "^3.0", "cache/array-adapter": "^0.5", - "nyholm/symfony-bundle-test": "^1.0.1", + "nyholm/symfony-bundle-test": "^1.2", "matthiasnoback/symfony-dependency-injection-test": "^1.0" }, "suggest": { diff --git a/src/Cache/Recording/CachePool.php b/src/Cache/Recording/CachePool.php deleted file mode 100644 index 9ba58b6..0000000 --- a/src/Cache/Recording/CachePool.php +++ /dev/null @@ -1,263 +0,0 @@ -, Tobias Nyholm - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Cache\CacheBundle\Cache\Recording; - -use Psr\Cache\CacheItemInterface; -use Psr\Cache\CacheItemPoolInterface; -use Psr\Log\LoggerInterface; - -/** - * A pool that logs and collects all your cache calls. - * - * @author Aaron Scherer - * @author Tobias Nyholm - * @author Nicolas Grekas - * - * @internal - */ -class CachePool implements CacheItemPoolInterface -{ - /** - * @type CacheItemPoolInterface - */ - protected $pool; - - /** - * @type LoggerInterface - */ - private $logger; - - /** - * @type string - */ - private $name; - - /** - * @type string - */ - private $level = 'info'; - - /** - * @type array calls - */ - private $calls = []; - - /** - * @param CacheItemPoolInterface $pool - */ - public function __construct(CacheItemPoolInterface $pool) - { - $this->pool = $pool; - } - - /** - * {@inheritdoc} - */ - public function getItem($key) - { - $event = $this->start(__FUNCTION__, $key); - try { - $item = $this->pool->getItem($key); - } finally { - $event->end = microtime(true); - } - if ($item->isHit()) { - ++$event->hits; - } else { - ++$event->misses; - } - $event->result = $item->get(); - - return $item; - } - - /** - * {@inheritdoc} - */ - public function hasItem($key) - { - $event = $this->start(__FUNCTION__, $key); - try { - $event->result = $this->pool->hasItem($key); - } finally { - $event->end = microtime(true); - } - - if (!$event->result) { - ++$event->misses; - } - - return $event->result; - } - - /** - * {@inheritdoc} - */ - public function deleteItem($key) - { - $event = $this->start(__FUNCTION__, $key); - try { - return $event->result = $this->pool->deleteItem($key); - } finally { - $event->end = microtime(true); - } - } - - /** - * {@inheritdoc} - */ - public function save(CacheItemInterface $item) - { - $event = $this->start(__FUNCTION__, $item); - try { - return $event->result = $this->pool->save($item); - } finally { - $event->end = microtime(true); - } - } - - /** - * {@inheritdoc} - */ - public function saveDeferred(CacheItemInterface $item) - { - $event = $this->start(__FUNCTION__, $item); - try { - return $event->result = $this->pool->saveDeferred($item); - } finally { - $event->end = microtime(true); - } - } - - /** - * {@inheritdoc} - */ - public function getItems(array $keys = []) - { - $event = $this->start(__FUNCTION__, $keys); - try { - $result = $this->pool->getItems($keys); - } finally { - $event->end = microtime(true); - } - $f = function () use ($result, $event) { - $event->result = []; - foreach ($result as $key => $item) { - if ($item->isHit()) { - ++$event->hits; - } else { - ++$event->misses; - } - $event->result[$key] = $item->get(); - yield $key => $item; - } - }; - - return $f(); - } - - /** - * {@inheritdoc} - */ - public function clear() - { - $event = $this->start(__FUNCTION__); - try { - return $event->result = $this->pool->clear(); - } finally { - $event->end = microtime(true); - } - } - - /** - * {@inheritdoc} - */ - public function deleteItems(array $keys) - { - $event = $this->start(__FUNCTION__, $keys); - try { - return $event->result = $this->pool->deleteItems($keys); - } finally { - $event->end = microtime(true); - } - } - - /** - * {@inheritdoc} - */ - public function commit() - { - $event = $this->start(__FUNCTION__); - try { - return $event->result = $this->pool->commit(); - } finally { - $event->end = microtime(true); - } - } - - public function getCalls() - { - return $this->calls; - } - - protected function start($name, $argument = null) - { - $this->calls[] = $event = new TraceableAdapterEvent(); - $event->name = $name; - $event->argument = $argument; - $event->start = microtime(true); - - return $event; - } - - /** - * @param LoggerInterface $logger - */ - public function setLogger(LoggerInterface $logger = null) - { - $this->logger = $logger; - } - - /** - * @param string $name - */ - public function setName($name) - { - $this->name = $name; - - return $this; - } - - /** - * @param string $level - */ - public function setLevel($level) - { - $this->level = $level; - - return $this; - } -} - -/** - * @internal - */ -class TraceableAdapterEvent -{ - public $name; - public $argument; - public $start; - public $end; - public $result; - public $hits = 0; - public $misses = 0; -} diff --git a/src/Cache/Recording/Factory.php b/src/Cache/Recording/Factory.php deleted file mode 100644 index a7a7cb1..0000000 --- a/src/Cache/Recording/Factory.php +++ /dev/null @@ -1,74 +0,0 @@ -, Tobias Nyholm - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Cache\CacheBundle\Cache\Recording; - -use Cache\Hierarchy\HierarchicalPoolInterface; -use Cache\TagInterop\TaggableCacheItemPoolInterface; -use Psr\Cache\CacheItemPoolInterface; -use Psr\Log\LoggerInterface; - -/** - * Create a recording CachePool. - * - * @author Tobias Nyholm - * - * @internal - */ -class Factory -{ - /** - * @type int|string - */ - private $level; - - /** - * @type LoggerInterface - */ - private $logger; - - /** - * @param LoggerInterface $logger - * @param string|int $level - */ - public function __construct(LoggerInterface $logger = null, $level = null) - { - $this->level = $level; - $this->logger = $logger; - } - - /** - * Decorate a CachePool with a recorder. Make sure we use a recorder that implements the same functionality - * as the underling pool. - * - * @param CacheItemPoolInterface $pool - * - * @return CachePool|HierarchyAndTaggablePool|HierarchyPool|TaggablePool - */ - public function create($name, CacheItemPoolInterface $pool) - { - if ($pool instanceof TaggableCacheItemPoolInterface && $pool instanceof HierarchicalPoolInterface) { - $recorder = new HierarchyAndTaggablePool($pool); - } elseif ($pool instanceof TaggableCacheItemPoolInterface) { - $recorder = new TaggablePool($pool); - } elseif ($pool instanceof HierarchicalPoolInterface) { - $recorder = new HierarchyPool($pool); - } else { - $recorder = new CachePool($pool); - } - - $recorder->setName($name); - $recorder->setLevel($this->level); - $recorder->setLogger($this->logger); - - return $recorder; - } -} diff --git a/src/Cache/Recording/HierarchyAndTaggablePool.php b/src/Cache/Recording/HierarchyAndTaggablePool.php deleted file mode 100644 index 718251c..0000000 --- a/src/Cache/Recording/HierarchyAndTaggablePool.php +++ /dev/null @@ -1,23 +0,0 @@ -, Tobias Nyholm - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Cache\CacheBundle\Cache\Recording; - -use Cache\Hierarchy\HierarchicalPoolInterface; - -/** - * @author Tobias Nyholm - * - * @internal - */ -class HierarchyAndTaggablePool extends TaggablePool implements HierarchicalPoolInterface -{ -} diff --git a/src/Cache/Recording/HierarchyPool.php b/src/Cache/Recording/HierarchyPool.php deleted file mode 100644 index a33a17c..0000000 --- a/src/Cache/Recording/HierarchyPool.php +++ /dev/null @@ -1,23 +0,0 @@ -, Tobias Nyholm - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Cache\CacheBundle\Cache\Recording; - -use Cache\Hierarchy\HierarchicalPoolInterface; - -/** - * @author Tobias Nyholm - * - * @internal - */ -class HierarchyPool extends CachePool implements HierarchicalPoolInterface -{ -} diff --git a/src/Cache/Recording/TaggablePool.php b/src/Cache/Recording/TaggablePool.php deleted file mode 100644 index 0057537..0000000 --- a/src/Cache/Recording/TaggablePool.php +++ /dev/null @@ -1,48 +0,0 @@ -, Tobias Nyholm - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Cache\CacheBundle\Cache\Recording; - -use Cache\TagInterop\TaggableCacheItemPoolInterface; - -/** - * @author Tobias Nyholm - * - * @internal - */ -class TaggablePool extends CachePool implements TaggableCacheItemPoolInterface -{ - /** - * {@inheritdoc} - */ - public function invalidateTag($tag) - { - $event = $this->start(__FUNCTION__, $tag); - try { - return $event->result = $this->pool->invalidateTag($tag); - } finally { - $event->end = microtime(true); - } - } - - /** - * {@inheritdoc} - */ - public function invalidateTags(array $tags) - { - $event = $this->start(__FUNCTION__, $tags); - try { - return $event->result = $this->pool->invalidateTags($tags); - } finally { - $event->end = microtime(true); - } - } -} diff --git a/src/DataCollector/CacheDataCollector.php b/src/DataCollector/CacheDataCollector.php index d371383..e374d11 100644 --- a/src/DataCollector/CacheDataCollector.php +++ b/src/DataCollector/CacheDataCollector.php @@ -11,7 +11,6 @@ namespace Cache\CacheBundle\DataCollector; -use Cache\CacheBundle\Cache\Recording\CachePool; use Cache\CacheBundle\Cache\Recording\TraceableAdapterEvent; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -26,15 +25,15 @@ class CacheDataCollector extends DataCollector { /** - * @type CachePool[] + * @var CacheProxy[] */ private $instances = []; /** - * @param string $name - * @param CachePool $instance + * @param string $name + * @param CacheProxy $instance */ - public function addInstance($name, CachePool $instance) + public function addInstance($name, CacheProxy $instance) { $this->instances[$name] = $instance; } @@ -44,14 +43,14 @@ public function addInstance($name, CachePool $instance) */ public function collect(Request $request, Response $response, \Exception $exception = null) { - $empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []]; + $empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []]; $this->data = ['instances' => $empty, 'total' => $empty]; foreach ($this->instances as $name => $instance) { - $this->data['instances']['calls'][$name] = $instance->getCalls(); + $this->data['instances']['calls'][$name] = $instance->__getCalls(); } $this->data['instances']['statistics'] = $this->calculateStatistics(); - $this->data['total']['statistics'] = $this->calculateTotalStatistics(); + $this->data['total']['statistics'] = $this->calculateTotalStatistics(); } /** @@ -100,15 +99,15 @@ private function calculateStatistics() $statistics = []; foreach ($this->data['instances']['calls'] as $name => $calls) { $statistics[$name] = [ - 'calls' => 0, - 'time' => 0, - 'reads' => 0, - 'writes' => 0, + 'calls' => 0, + 'time' => 0, + 'reads' => 0, + 'writes' => 0, 'deletes' => 0, - 'hits' => 0, - 'misses' => 0, + 'hits' => 0, + 'misses' => 0, ]; - /** @type TraceableAdapterEvent $call */ + /** @var TraceableAdapterEvent $call */ foreach ($calls as $call) { $statistics[$name]['calls'] += 1; $statistics[$name]['time'] += $call->end - $call->start; @@ -153,14 +152,14 @@ private function calculateStatistics() private function calculateTotalStatistics() { $statistics = $this->getStatistics(); - $totals = [ - 'calls' => 0, - 'time' => 0, - 'reads' => 0, - 'writes' => 0, + $totals = [ + 'calls' => 0, + 'time' => 0, + 'reads' => 0, + 'writes' => 0, 'deletes' => 0, - 'hits' => 0, - 'misses' => 0, + 'hits' => 0, + 'misses' => 0, ]; foreach ($statistics as $name => $values) { foreach ($totals as $key => $value) { diff --git a/src/DataCollector/CacheProxy.php b/src/DataCollector/CacheProxy.php new file mode 100644 index 0000000..30a886a --- /dev/null +++ b/src/DataCollector/CacheProxy.php @@ -0,0 +1,15 @@ + + */ +interface CacheProxy +{ + public function __getCalls(); + + public function __setName($name); +} diff --git a/src/DataCollector/DecoratingFactory.php b/src/DataCollector/DecoratingFactory.php new file mode 100644 index 0000000..fdd6ed0 --- /dev/null +++ b/src/DataCollector/DecoratingFactory.php @@ -0,0 +1,47 @@ + + */ +class DecoratingFactory +{ + /** + * @var ProxyFactory + */ + private $proxyFactory; + + /** + * @var mixed cache pool + */ + private $originalObject; + + /** + * @param ProxyFactory $proxyFactory + * @param mixed $originalObject + */ + public function __construct(ProxyFactory $proxyFactory, $originalObject) + { + $this->proxyFactory = $proxyFactory; + $this->originalObject = $originalObject; + } + + public function create() + { + $proxyClass = $this->proxyFactory->createProxy(get_class($this->originalObject)); + $rc = new \ReflectionClass($proxyClass); + $pool = $rc->newInstanceWithoutConstructor(); + + // Copy properties from original pool to new + foreach (NSA::getProperties($this->originalObject) as $property) { + NSA::setProperty($pool, $property, NSA::getProperty($this->originalObject, $property)); + } + + return $pool; + } +} diff --git a/src/DataCollector/ProxyFactory.php b/src/DataCollector/ProxyFactory.php new file mode 100644 index 0000000..cbbc219 --- /dev/null +++ b/src/DataCollector/ProxyFactory.php @@ -0,0 +1,233 @@ + + */ +class ProxyFactory +{ + /** + * @var string + */ + private $proxyDirectory; + + /** + * @param string $proxyDirectory + */ + public function __construct($proxyDirectory) + { + $this->proxyDirectory = $proxyDirectory; + } + + /** + * Create a proxy that handles logging better. + * + * @param string $class + * @param string $file where we store the proxy class + * + * @return string the name of a much much better class + */ + public function createProxy($class, &$proxyFile = null) + { + $proxyClass = $this->getProxyClass($class); + $class = '\\'.rtrim($class, '\\'); + $proxyFile = $this->proxyDirectory.'/'.$proxyClass.'.php'; + + if (class_exists($proxyClass)) { + return $proxyClass; + } + + $content = <<start(__FUNCTION__, \$key); + try { + \$item = parent::getItem(\$key); + } finally { + \$event->end = microtime(true); + } + if (\$item->isHit()) { + ++\$event->hits; + } else { + ++\$event->misses; + } + \$event->result = \$item->get(); + + return \$item; + } + + public function hasItem(\$key) + { + \$event = \$this->start(__FUNCTION__, \$key); + try { + \$event->result = parent::hasItem(\$key); + } finally { + \$event->end = microtime(true); + } + + if (!\$event->result) { + ++\$event->misses; + } + + return \$event->result; + } + + public function deleteItem(\$key) + { + \$event = \$this->start(__FUNCTION__, \$key); + try { + return \$event->result = parent::deleteItem(\$key); + } finally { + \$event->end = microtime(true); + } + } + + public function save(CacheItemInterface \$item) + { + \$event = \$this->start(__FUNCTION__, \$item); + try { + return \$event->result = parent::save(\$item); + } finally { + \$event->end = microtime(true); + } + } + + public function saveDeferred(CacheItemInterface \$item) + { + \$event = \$this->start(__FUNCTION__, \$item); + try { + return \$event->result = parent::saveDeferred(\$item); + } finally { + \$event->end = microtime(true); + } + } + + public function getItems(array \$keys = []) + { + \$event = \$this->start(__FUNCTION__, \$keys); + try { + \$result = parent::getItems(\$keys); + } finally { + \$event->end = microtime(true); + } + \$f = function () use (\$result, \$event) { + \$event->result = []; + foreach (\$result as \$key => \$item) { + if (\$item->isHit()) { + ++\$event->hits; + } else { + ++\$event->misses; + } + \$event->result[\$key] = \$item->get(); + yield \$key => \$item; + } + }; + + return \$f(); + } + + public function clear() + { + \$event = \$this->start(__FUNCTION__); + try { + return \$event->result = parent::clear(); + } finally { + \$event->end = microtime(true); + } + } + + public function deleteItems(array \$keys) + { + \$event = \$this->start(__FUNCTION__, \$keys); + try { + return \$event->result = parent::deleteItems(\$keys); + } finally { + \$event->end = microtime(true); + } + } + + public function commit() + { + \$event = \$this->start(__FUNCTION__); + try { + return \$event->result = parent::commit(); + } finally { + \$event->end = microtime(true); + } + } + + public function invalidateTag(\$tag) + { + \$event = \$this->start(__FUNCTION__, \$tag); + try { + return \$event->result = parent::invalidateTag(\$tag); + } finally { + \$event->end = microtime(true); + } + } + + public function invalidateTags(array \$tags) + { + \$event = \$this->start(__FUNCTION__, \$tags); + try { + return \$event->result = parent::invalidateTags(\$tags); + } finally { + \$event->end = microtime(true); + } + } + + public function __getCalls() + { + return \$this->__calls; + } + + public function __setName(\$name) + { + \$this->__name = \$name; + } + + private function start(\$name, \$argument = null) + { + \$this->__calls[] = \$event = new TraceableAdapterEvent(); + \$event->name = \$name; + \$event->argument = \$argument; + \$event->start = microtime(true); + + return \$event; + } +} +PROXY; + + $this->checkProxyDirectory(); + file_put_contents($proxyFile, $content); + require $proxyFile; + + return $proxyClass; + } + + private function checkProxyDirectory() + { + if (!is_dir($this->proxyDirectory)) { + @mkdir($this->proxyDirectory, 0777, true); + } + } + + protected function getProxyClass($namespace) + { + return 'php_cache_proxy_'.str_replace('\\', '_', $namespace); + } +} diff --git a/src/DataCollector/TraceableAdapterEvent.php b/src/DataCollector/TraceableAdapterEvent.php new file mode 100644 index 0000000..26510b9 --- /dev/null +++ b/src/DataCollector/TraceableAdapterEvent.php @@ -0,0 +1,17 @@ +register($factoryId, Factory::class); - // Check if logging support is enabled - if ($container->hasParameter('cache.logging')) { - $config = $container->getParameter('cache.logging'); - $factory->addArgument(new Reference($config['logger'])); - $factory->addArgument($config['level']); - } - + $proxyFactory = $container->get('cache.proxy_factory'); $collectorDefinition = $container->getDefinition('cache.data_collector'); $serviceIds = $container->findTaggedServiceIds('cache.provider'); @@ -51,15 +42,24 @@ public function process(ContainerBuilder $container) // Get the pool definition and rename it. $poolDefinition = $container->getDefinition($id); - $poolDefinition->setPublic(false); - $container->setDefinition($id.'.inner', $poolDefinition); + if (null === $poolDefinition->getFactory()) { + // Just replace the class + $proxyClass = $proxyFactory->createProxy($poolDefinition->getClass(), $file); + $poolDefinition->setClass($proxyClass); + $poolDefinition->setFile($file); + $poolDefinition->addMethodCall('__setName', [$id]); + } else { + // Create a new ID for the original service + $innerId = $id.'.inner'; + $container->setDefinition($innerId, $poolDefinition); - // Create a recording pool with a factory - $recorderDefinition = $container->register($id, DummyAdapter::class); - $recorderDefinition->setFactory([new Reference($factoryId), 'create']); - $recorderDefinition->addArgument($id); - $recorderDefinition->addArgument(new Reference($id.'.inner')); - $recorderDefinition->setTags($poolDefinition->getTags()); + // Create a new definition. + $decoratingFactory = new Definition(DecoratingFactory::class, [new Reference('cache.proxy_factory'), new Reference($innerId)]); + $decoratedPool = new Definition($poolDefinition->getClass()); + $decoratedPool->setFactory([$decoratingFactory, 'create']); + $container->setDefinition($id, $decoratedPool); + $decoratedPool->addMethodCall('__setName', [$id]); + } // Tell the collector to add the new instance $collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]); diff --git a/src/Resources/config/data-collector.yml b/src/Resources/config/data-collector.yml index 48d9fc3..c4b537e 100644 --- a/src/Resources/config/data-collector.yml +++ b/src/Resources/config/data-collector.yml @@ -3,3 +3,7 @@ services: class: Cache\CacheBundle\DataCollector\CacheDataCollector tags: - { name: data_collector, template: 'CacheBundle:Collector:cache.html.twig', id: 'php-cache' } + + cache.proxy_factory: + class: Cache\CacheBundle\DataCollector\ProxyFactory + arguments: ['%kernel.cache_dir%/phpcache/proxy'] From 6cb33db08cd0e7207d82f1b78e207ff0c5f77af5 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 16 Jul 2017 20:26:50 +0200 Subject: [PATCH 2/8] Applied changes from StyleCI --- src/DataCollector/CacheDataCollector.php | 34 +++++++++---------- src/DataCollector/CacheProxy.php | 9 +++++ src/DataCollector/DecoratingFactory.php | 19 ++++++++--- src/DataCollector/ProxyFactory.php | 15 ++++++-- src/DataCollector/TraceableAdapterEvent.php | 11 +++++- .../Compiler/DataCollectorCompilerPass.php | 4 +-- 6 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/DataCollector/CacheDataCollector.php b/src/DataCollector/CacheDataCollector.php index e374d11..54b1148 100644 --- a/src/DataCollector/CacheDataCollector.php +++ b/src/DataCollector/CacheDataCollector.php @@ -25,7 +25,7 @@ class CacheDataCollector extends DataCollector { /** - * @var CacheProxy[] + * @type CacheProxy[] */ private $instances = []; @@ -43,14 +43,14 @@ public function addInstance($name, CacheProxy $instance) */ public function collect(Request $request, Response $response, \Exception $exception = null) { - $empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []]; + $empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []]; $this->data = ['instances' => $empty, 'total' => $empty]; foreach ($this->instances as $name => $instance) { $this->data['instances']['calls'][$name] = $instance->__getCalls(); } $this->data['instances']['statistics'] = $this->calculateStatistics(); - $this->data['total']['statistics'] = $this->calculateTotalStatistics(); + $this->data['total']['statistics'] = $this->calculateTotalStatistics(); } /** @@ -99,15 +99,15 @@ private function calculateStatistics() $statistics = []; foreach ($this->data['instances']['calls'] as $name => $calls) { $statistics[$name] = [ - 'calls' => 0, - 'time' => 0, - 'reads' => 0, - 'writes' => 0, + 'calls' => 0, + 'time' => 0, + 'reads' => 0, + 'writes' => 0, 'deletes' => 0, - 'hits' => 0, - 'misses' => 0, + 'hits' => 0, + 'misses' => 0, ]; - /** @var TraceableAdapterEvent $call */ + /** @type TraceableAdapterEvent $call */ foreach ($calls as $call) { $statistics[$name]['calls'] += 1; $statistics[$name]['time'] += $call->end - $call->start; @@ -152,14 +152,14 @@ private function calculateStatistics() private function calculateTotalStatistics() { $statistics = $this->getStatistics(); - $totals = [ - 'calls' => 0, - 'time' => 0, - 'reads' => 0, - 'writes' => 0, + $totals = [ + 'calls' => 0, + 'time' => 0, + 'reads' => 0, + 'writes' => 0, 'deletes' => 0, - 'hits' => 0, - 'misses' => 0, + 'hits' => 0, + 'misses' => 0, ]; foreach ($statistics as $name => $values) { foreach ($totals as $key => $value) { diff --git a/src/DataCollector/CacheProxy.php b/src/DataCollector/CacheProxy.php index 30a886a..759f5e8 100644 --- a/src/DataCollector/CacheProxy.php +++ b/src/DataCollector/CacheProxy.php @@ -1,5 +1,14 @@ , Tobias Nyholm + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace Cache\CacheBundle\DataCollector; /** diff --git a/src/DataCollector/DecoratingFactory.php b/src/DataCollector/DecoratingFactory.php index fdd6ed0..75a09dd 100644 --- a/src/DataCollector/DecoratingFactory.php +++ b/src/DataCollector/DecoratingFactory.php @@ -1,5 +1,14 @@ , Tobias Nyholm + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace Cache\CacheBundle\DataCollector; use Nyholm\NSA; @@ -12,12 +21,12 @@ class DecoratingFactory { /** - * @var ProxyFactory + * @type ProxyFactory */ private $proxyFactory; /** - * @var mixed cache pool + * @type mixed cache pool */ private $originalObject; @@ -27,15 +36,15 @@ class DecoratingFactory */ public function __construct(ProxyFactory $proxyFactory, $originalObject) { - $this->proxyFactory = $proxyFactory; + $this->proxyFactory = $proxyFactory; $this->originalObject = $originalObject; } public function create() { $proxyClass = $this->proxyFactory->createProxy(get_class($this->originalObject)); - $rc = new \ReflectionClass($proxyClass); - $pool = $rc->newInstanceWithoutConstructor(); + $rc = new \ReflectionClass($proxyClass); + $pool = $rc->newInstanceWithoutConstructor(); // Copy properties from original pool to new foreach (NSA::getProperties($this->originalObject) as $property) { diff --git a/src/DataCollector/ProxyFactory.php b/src/DataCollector/ProxyFactory.php index cbbc219..70bd942 100644 --- a/src/DataCollector/ProxyFactory.php +++ b/src/DataCollector/ProxyFactory.php @@ -1,5 +1,14 @@ , Tobias Nyholm + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace Cache\CacheBundle\DataCollector; /** @@ -10,7 +19,7 @@ class ProxyFactory { /** - * @var string + * @type string */ private $proxyDirectory; @@ -33,8 +42,8 @@ public function __construct($proxyDirectory) public function createProxy($class, &$proxyFile = null) { $proxyClass = $this->getProxyClass($class); - $class = '\\'.rtrim($class, '\\'); - $proxyFile = $this->proxyDirectory.'/'.$proxyClass.'.php'; + $class = '\\'.rtrim($class, '\\'); + $proxyFile = $this->proxyDirectory.'/'.$proxyClass.'.php'; if (class_exists($proxyClass)) { return $proxyClass; diff --git a/src/DataCollector/TraceableAdapterEvent.php b/src/DataCollector/TraceableAdapterEvent.php index 26510b9..ab3c799 100644 --- a/src/DataCollector/TraceableAdapterEvent.php +++ b/src/DataCollector/TraceableAdapterEvent.php @@ -1,5 +1,14 @@ , Tobias Nyholm + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace Cache\CacheBundle\DataCollector; /** @@ -12,6 +21,6 @@ class TraceableAdapterEvent public $start; public $end; public $result; - public $hits = 0; + public $hits = 0; public $misses = 0; } diff --git a/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php b/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php index 0a55523..18ebac2 100644 --- a/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php +++ b/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php @@ -34,7 +34,7 @@ public function process(ContainerBuilder $container) return; } - $proxyFactory = $container->get('cache.proxy_factory'); + $proxyFactory = $container->get('cache.proxy_factory'); $collectorDefinition = $container->getDefinition('cache.data_collector'); $serviceIds = $container->findTaggedServiceIds('cache.provider'); @@ -55,7 +55,7 @@ public function process(ContainerBuilder $container) // Create a new definition. $decoratingFactory = new Definition(DecoratingFactory::class, [new Reference('cache.proxy_factory'), new Reference($innerId)]); - $decoratedPool = new Definition($poolDefinition->getClass()); + $decoratedPool = new Definition($poolDefinition->getClass()); $decoratedPool->setFactory([$decoratingFactory, 'create']); $container->setDefinition($id, $decoratedPool); $decoratedPool->addMethodCall('__setName', [$id]); From dbcea78d70a6eca21e9640c77ae21253204f2859 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 16 Jul 2017 20:40:15 +0200 Subject: [PATCH 3/8] Fixed stuff --- .../DataCollectorCompilerPassTest.php | 70 ------------------- 1 file changed, 70 deletions(-) delete mode 100644 tests/Unit/DependencyInjection/DataCollectorCompilerPassTest.php diff --git a/tests/Unit/DependencyInjection/DataCollectorCompilerPassTest.php b/tests/Unit/DependencyInjection/DataCollectorCompilerPassTest.php deleted file mode 100644 index 835090b..0000000 --- a/tests/Unit/DependencyInjection/DataCollectorCompilerPassTest.php +++ /dev/null @@ -1,70 +0,0 @@ -, Tobias Nyholm - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Cache\CacheBundle\Tests\Unit\DependencyInjection; - -use Cache\CacheBundle\DependencyInjection\Compiler\DataCollectorCompilerPass; -use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Definition; -use Symfony\Component\DependencyInjection\Reference; - -class DataCollectorCompilerPassTest extends AbstractCompilerPassTestCase -{ - protected function registerCompilerPass(ContainerBuilder $container) - { - $container->addCompilerPass(new DataCollectorCompilerPass()); - } - - public function testWithLogger() - { - $collector = new Definition(); - $this->setDefinition('cache.data_collector', $collector); - - $this->setParameter('cache.logging', ['logger' => 'foo_logger', 'level' => 'bar']); - $this->compile(); - - $this->assertContainerBuilderHasServiceDefinitionWithArgument( - 'cache.recorder_factory', - 0, - new Reference('foo_logger') - ); - $this->assertContainerBuilderHasServiceDefinitionWithArgument( - 'cache.recorder_factory', - 1, - 'bar' - ); - } - - public function testFactory() - { - $collector = new Definition(); - $this->setDefinition('cache.data_collector', $collector); - - $collectedService = new Definition(); - $collectedService->addTag('cache.provider'); - $this->setDefinition('collected_pool', $collectedService); - - $this->compile(); - - $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( - 'cache.data_collector', - 'addInstance', - [ - 'collected_pool', - new Reference('collected_pool'), - ] - ); - - $this->assertContainerBuilderHasService('collected_pool.inner'); - $this->assertContainerBuilderHasServiceDefinitionWithTag('collected_pool', 'cache.provider'); - } -} From 9b7ba2e1639203406cd93fc619be0a2bbd7fe374 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 30 Jul 2017 15:26:05 +0200 Subject: [PATCH 4/8] Minor --- src/DataCollector/ProxyFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DataCollector/ProxyFactory.php b/src/DataCollector/ProxyFactory.php index 70bd942..6443fa4 100644 --- a/src/DataCollector/ProxyFactory.php +++ b/src/DataCollector/ProxyFactory.php @@ -35,7 +35,7 @@ public function __construct($proxyDirectory) * Create a proxy that handles logging better. * * @param string $class - * @param string $file where we store the proxy class + * @param string &$proxyFile where we store the proxy class * * @return string the name of a much much better class */ @@ -235,7 +235,7 @@ private function checkProxyDirectory() } } - protected function getProxyClass($namespace) + private function getProxyClass($namespace) { return 'php_cache_proxy_'.str_replace('\\', '_', $namespace); } From 39c706bf21e165460a3fb47cb9a8220cd494f6a2 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 30 Jul 2017 15:36:21 +0200 Subject: [PATCH 5/8] Fixes --- src/DataCollector/DecoratingFactory.php | 25 +++++++++---------- .../Compiler/DataCollectorCompilerPass.php | 6 ++--- src/Resources/config/data-collector.yml | 5 ++++ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/DataCollector/DecoratingFactory.php b/src/DataCollector/DecoratingFactory.php index 75a09dd..dfccc96 100644 --- a/src/DataCollector/DecoratingFactory.php +++ b/src/DataCollector/DecoratingFactory.php @@ -12,6 +12,7 @@ namespace Cache\CacheBundle\DataCollector; use Nyholm\NSA; +use Psr\Cache\CacheItemPoolInterface; /** * A factory that decorates another factory to be able to use the proxy cache. @@ -25,30 +26,28 @@ class DecoratingFactory */ private $proxyFactory; - /** - * @type mixed cache pool - */ - private $originalObject; - /** * @param ProxyFactory $proxyFactory - * @param mixed $originalObject */ - public function __construct(ProxyFactory $proxyFactory, $originalObject) + public function __construct(ProxyFactory $proxyFactory) { - $this->proxyFactory = $proxyFactory; - $this->originalObject = $originalObject; + $this->proxyFactory = $proxyFactory; } - public function create() + /** + * @param CacheItemPoolInterface $originalObject original class + * + * @return CacheProxy|CacheItemPoolInterface + */ + public function create($originalObject) { - $proxyClass = $this->proxyFactory->createProxy(get_class($this->originalObject)); + $proxyClass = $this->proxyFactory->createProxy(get_class($originalObject)); $rc = new \ReflectionClass($proxyClass); $pool = $rc->newInstanceWithoutConstructor(); // Copy properties from original pool to new - foreach (NSA::getProperties($this->originalObject) as $property) { - NSA::setProperty($pool, $property, NSA::getProperty($this->originalObject, $property)); + foreach (NSA::getProperties($originalObject) as $property) { + NSA::setProperty($pool, $property, NSA::getProperty($originalObject, $property)); } return $pool; diff --git a/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php b/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php index 18ebac2..a24c004 100644 --- a/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php +++ b/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php @@ -54,9 +54,9 @@ public function process(ContainerBuilder $container) $container->setDefinition($innerId, $poolDefinition); // Create a new definition. - $decoratingFactory = new Definition(DecoratingFactory::class, [new Reference('cache.proxy_factory'), new Reference($innerId)]); - $decoratedPool = new Definition($poolDefinition->getClass()); - $decoratedPool->setFactory([$decoratingFactory, 'create']); + $decoratedPool = new Definition($poolDefinition->getClass()); + $decoratedPool->setFactory([new Reference('cache.decorating_factory'), 'create']); + $decoratedPool->setArguments([new Reference($innerId)]); $container->setDefinition($id, $decoratedPool); $decoratedPool->addMethodCall('__setName', [$id]); } diff --git a/src/Resources/config/data-collector.yml b/src/Resources/config/data-collector.yml index c4b537e..3c02faa 100644 --- a/src/Resources/config/data-collector.yml +++ b/src/Resources/config/data-collector.yml @@ -7,3 +7,8 @@ services: cache.proxy_factory: class: Cache\CacheBundle\DataCollector\ProxyFactory arguments: ['%kernel.cache_dir%/phpcache/proxy'] + + cache.decorating_factory: + class: Cache\CacheBundle\DataCollector\DecoratingFactory + arguments: ["@cache.proxy_factory"] + public: false From 2db53dc3186351a07d03e82c1b4dce4c6f5e3c35 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 30 Jul 2017 18:38:12 +0200 Subject: [PATCH 6/8] Moved template to a separate class --- src/DataCollector/ProxyFactory.php | 174 +---------------------------- src/Resources/proxy/template.php | 169 ++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+), 171 deletions(-) create mode 100644 src/Resources/proxy/template.php diff --git a/src/DataCollector/ProxyFactory.php b/src/DataCollector/ProxyFactory.php index 6443fa4..c74c61c 100644 --- a/src/DataCollector/ProxyFactory.php +++ b/src/DataCollector/ProxyFactory.php @@ -49,177 +49,9 @@ public function createProxy($class, &$proxyFile = null) return $proxyClass; } - $content = <<start(__FUNCTION__, \$key); - try { - \$item = parent::getItem(\$key); - } finally { - \$event->end = microtime(true); - } - if (\$item->isHit()) { - ++\$event->hits; - } else { - ++\$event->misses; - } - \$event->result = \$item->get(); - - return \$item; - } - - public function hasItem(\$key) - { - \$event = \$this->start(__FUNCTION__, \$key); - try { - \$event->result = parent::hasItem(\$key); - } finally { - \$event->end = microtime(true); - } - - if (!\$event->result) { - ++\$event->misses; - } - - return \$event->result; - } - - public function deleteItem(\$key) - { - \$event = \$this->start(__FUNCTION__, \$key); - try { - return \$event->result = parent::deleteItem(\$key); - } finally { - \$event->end = microtime(true); - } - } - - public function save(CacheItemInterface \$item) - { - \$event = \$this->start(__FUNCTION__, \$item); - try { - return \$event->result = parent::save(\$item); - } finally { - \$event->end = microtime(true); - } - } - - public function saveDeferred(CacheItemInterface \$item) - { - \$event = \$this->start(__FUNCTION__, \$item); - try { - return \$event->result = parent::saveDeferred(\$item); - } finally { - \$event->end = microtime(true); - } - } - - public function getItems(array \$keys = []) - { - \$event = \$this->start(__FUNCTION__, \$keys); - try { - \$result = parent::getItems(\$keys); - } finally { - \$event->end = microtime(true); - } - \$f = function () use (\$result, \$event) { - \$event->result = []; - foreach (\$result as \$key => \$item) { - if (\$item->isHit()) { - ++\$event->hits; - } else { - ++\$event->misses; - } - \$event->result[\$key] = \$item->get(); - yield \$key => \$item; - } - }; - - return \$f(); - } - - public function clear() - { - \$event = \$this->start(__FUNCTION__); - try { - return \$event->result = parent::clear(); - } finally { - \$event->end = microtime(true); - } - } - - public function deleteItems(array \$keys) - { - \$event = \$this->start(__FUNCTION__, \$keys); - try { - return \$event->result = parent::deleteItems(\$keys); - } finally { - \$event->end = microtime(true); - } - } - - public function commit() - { - \$event = \$this->start(__FUNCTION__); - try { - return \$event->result = parent::commit(); - } finally { - \$event->end = microtime(true); - } - } - - public function invalidateTag(\$tag) - { - \$event = \$this->start(__FUNCTION__, \$tag); - try { - return \$event->result = parent::invalidateTag(\$tag); - } finally { - \$event->end = microtime(true); - } - } - - public function invalidateTags(array \$tags) - { - \$event = \$this->start(__FUNCTION__, \$tags); - try { - return \$event->result = parent::invalidateTags(\$tags); - } finally { - \$event->end = microtime(true); - } - } - - public function __getCalls() - { - return \$this->__calls; - } - - public function __setName(\$name) - { - \$this->__name = \$name; - } - - private function start(\$name, \$argument = null) - { - \$this->__calls[] = \$event = new TraceableAdapterEvent(); - \$event->name = \$name; - \$event->argument = \$argument; - \$event->start = microtime(true); - - return \$event; - } -} -PROXY; + $content = file_get_contents(dirname(__DIR__).'/Resources/proxy/template.php'); + $content = str_replace('__TPL_CLASS__', $proxyClass, $content); + $content = str_replace('__TPL_EXTENDS__', $class, $content); $this->checkProxyDirectory(); file_put_contents($proxyFile, $content); diff --git a/src/Resources/proxy/template.php b/src/Resources/proxy/template.php new file mode 100644 index 0000000..a99cf95 --- /dev/null +++ b/src/Resources/proxy/template.php @@ -0,0 +1,169 @@ +start(__FUNCTION__, $key); + try { + $item = parent::getItem($key); + } finally { + $event->end = microtime(true); + } + if ($item->isHit()) { + ++$event->hits; + } else { + ++$event->misses; + } + $event->result = $item->get(); + + return $item; + } + + public function hasItem($key) + { + $event = $this->start(__FUNCTION__, $key); + try { + $event->result = parent::hasItem($key); + } finally { + $event->end = microtime(true); + } + + if (!$event->result) { + ++$event->misses; + } + + return $event->result; + } + + public function deleteItem($key) + { + $event = $this->start(__FUNCTION__, $key); + try { + return $event->result = parent::deleteItem($key); + } finally { + $event->end = microtime(true); + } + } + + public function save(CacheItemInterface $item) + { + $event = $this->start(__FUNCTION__, $item); + try { + return $event->result = parent::save($item); + } finally { + $event->end = microtime(true); + } + } + + public function saveDeferred(CacheItemInterface $item) + { + $event = $this->start(__FUNCTION__, $item); + try { + return $event->result = parent::saveDeferred($item); + } finally { + $event->end = microtime(true); + } + } + + public function getItems(array $keys = []) + { + $event = $this->start(__FUNCTION__, $keys); + try { + $result = parent::getItems($keys); + } finally { + $event->end = microtime(true); + } + $f = function () use ($result, $event) { + $event->result = []; + foreach ($result as $key => $item) { + if ($item->isHit()) { + ++$event->hits; + } else { + ++$event->misses; + } + $event->result[$key] = $item->get(); + yield $key => $item; + } + }; + + return $f(); + } + + public function clear() +{ + $event = $this->start(__FUNCTION__); + try { + return $event->result = parent::clear(); + } finally { + $event->end = microtime(true); + } + } + + public function deleteItems(array $keys) + { + $event = $this->start(__FUNCTION__, $keys); + try { + return $event->result = parent::deleteItems($keys); + } finally { + $event->end = microtime(true); + } + } + + public function commit() +{ + $event = $this->start(__FUNCTION__); + try { + return $event->result = parent::commit(); + } finally { + $event->end = microtime(true); + } + } + + public function invalidateTag($tag) + { + $event = $this->start(__FUNCTION__, $tag); + try { + return $event->result = parent::invalidateTag($tag); + } finally { + $event->end = microtime(true); + } + } + + public function invalidateTags(array $tags) + { + $event = $this->start(__FUNCTION__, $tags); + try { + return $event->result = parent::invalidateTags($tags); + } finally { + $event->end = microtime(true); + } + } + + public function __getCalls() +{ + return $this->__calls; + } + + public function __setName($name) + { + $this->__name = $name; + } + + private function start($name, $argument = null) + { + $this->__calls[] = $event = new TraceableAdapterEvent(); + $event->name = $name; + $event->argument = $argument; + $event->start = microtime(true); + + return $event; + } +} From 93f3e9fb58f5e07f65dae2f55b26ab05c333f91e Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 30 Jul 2017 18:41:43 +0200 Subject: [PATCH 7/8] typo --- src/DataCollector/ProxyFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DataCollector/ProxyFactory.php b/src/DataCollector/ProxyFactory.php index c74c61c..c115753 100644 --- a/src/DataCollector/ProxyFactory.php +++ b/src/DataCollector/ProxyFactory.php @@ -32,7 +32,7 @@ public function __construct($proxyDirectory) } /** - * Create a proxy that handles logging better. + * Create a proxy that handles data collecting better. * * @param string $class * @param string &$proxyFile where we store the proxy class From 88d5e019da02ab129907872ed7e8f30754321437 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 30 Jul 2017 18:56:07 +0200 Subject: [PATCH 8/8] Applied changes from StyleCI --- src/DataCollector/ProxyFactory.php | 2 +- .../Compiler/DataCollectorCompilerPass.php | 1 - src/Resources/proxy/template.php | 41 +++++++++++-------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/DataCollector/ProxyFactory.php b/src/DataCollector/ProxyFactory.php index c115753..986cf0e 100644 --- a/src/DataCollector/ProxyFactory.php +++ b/src/DataCollector/ProxyFactory.php @@ -35,7 +35,7 @@ public function __construct($proxyDirectory) * Create a proxy that handles data collecting better. * * @param string $class - * @param string &$proxyFile where we store the proxy class + * @param string &$proxyFile where we store the proxy class * * @return string the name of a much much better class */ diff --git a/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php b/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php index a24c004..b82b952 100644 --- a/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php +++ b/src/DependencyInjection/Compiler/DataCollectorCompilerPass.php @@ -11,7 +11,6 @@ namespace Cache\CacheBundle\DependencyInjection\Compiler; -use Cache\CacheBundle\DataCollector\DecoratingFactory; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; diff --git a/src/Resources/proxy/template.php b/src/Resources/proxy/template.php index a99cf95..91b69fd 100644 --- a/src/Resources/proxy/template.php +++ b/src/Resources/proxy/template.php @@ -1,5 +1,14 @@ , Tobias Nyholm + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + use Cache\CacheBundle\DataCollector\CacheProxy; use Cache\CacheBundle\DataCollector\TraceableAdapterEvent; use Psr\Cache\CacheItemInterface; @@ -18,9 +27,9 @@ public function getItem($key) $event->end = microtime(true); } if ($item->isHit()) { - ++$event->hits; + ++$event->hits; } else { - ++$event->misses; + ++$event->misses; } $event->result = $item->get(); @@ -37,7 +46,7 @@ public function hasItem($key) } if (!$event->result) { - ++$event->misses; + ++$event->misses; } return $event->result; @@ -82,12 +91,12 @@ public function getItems(array $keys = []) $event->end = microtime(true); } $f = function () use ($result, $event) { - $event->result = []; + $event->result = []; foreach ($result as $key => $item) { - if ($item->isHit()) { - ++$event->hits; + if ($item->isHit()) { + ++$event->hits; } else { - ++$event->misses; + ++$event->misses; } $event->result[$key] = $item->get(); yield $key => $item; @@ -98,8 +107,8 @@ public function getItems(array $keys = []) } public function clear() -{ - $event = $this->start(__FUNCTION__); + { + $event = $this->start(__FUNCTION__); try { return $event->result = parent::clear(); } finally { @@ -118,8 +127,8 @@ public function deleteItems(array $keys) } public function commit() -{ - $event = $this->start(__FUNCTION__); + { + $event = $this->start(__FUNCTION__); try { return $event->result = parent::commit(); } finally { @@ -148,8 +157,8 @@ public function invalidateTags(array $tags) } public function __getCalls() -{ - return $this->__calls; + { + return $this->__calls; } public function __setName($name) @@ -160,9 +169,9 @@ public function __setName($name) private function start($name, $argument = null) { $this->__calls[] = $event = new TraceableAdapterEvent(); - $event->name = $name; - $event->argument = $argument; - $event->start = microtime(true); + $event->name = $name; + $event->argument = $argument; + $event->start = microtime(true); return $event; }