Skip to content

Commit 56ae528

Browse files
authored
Support for simple cache (#75)
* Support for simple cache * Applied changes from StyleCI * Fixed stuff * Minor * Fixes * Moved template to a separate class * typo * Applied changes from StyleCI
1 parent 3a0f534 commit 56ae528

File tree

12 files changed

+219
-355
lines changed

12 files changed

+219
-355
lines changed

Diff for: composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
"php": "^5.6 || ^7.0",
2828
"symfony/framework-bundle": "^2.7 || ^3.0",
2929
"cache/taggable-cache": "^0.5",
30-
"cache/session-handler": "^0.2"
30+
"cache/session-handler": "^0.2",
31+
"nyholm/nsa": "^1.1"
3132
},
3233
"require-dev": {
3334
"phpunit/phpunit": "^5.7.17",
3435
"symfony/symfony": "^2.7 || ^3.0",
3536
"cache/psr-6-doctrine-bridge": "^3.0",
3637
"cache/array-adapter": "^0.5",
37-
"nyholm/symfony-bundle-test": "^1.0.1",
38+
"nyholm/symfony-bundle-test": "^1.2",
3839
"matthiasnoback/symfony-dependency-injection-test": "^1.0"
3940
},
4041
"suggest": {

Diff for: src/Cache/Recording/Factory.php

-74
This file was deleted.

Diff for: src/Cache/Recording/TaggablePool.php

-48
This file was deleted.

Diff for: src/DataCollector/CacheDataCollector.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Cache\CacheBundle\DataCollector;
1313

14-
use Cache\CacheBundle\Cache\Recording\CachePool;
1514
use Cache\CacheBundle\Cache\Recording\TraceableAdapterEvent;
1615
use Symfony\Component\HttpFoundation\Request;
1716
use Symfony\Component\HttpFoundation\Response;
@@ -26,15 +25,15 @@
2625
class CacheDataCollector extends DataCollector
2726
{
2827
/**
29-
* @type CachePool[]
28+
* @type CacheProxy[]
3029
*/
3130
private $instances = [];
3231

3332
/**
34-
* @param string $name
35-
* @param CachePool $instance
33+
* @param string $name
34+
* @param CacheProxy $instance
3635
*/
37-
public function addInstance($name, CachePool $instance)
36+
public function addInstance($name, CacheProxy $instance)
3837
{
3938
$this->instances[$name] = $instance;
4039
}
@@ -47,7 +46,7 @@ public function collect(Request $request, Response $response, \Exception $except
4746
$empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []];
4847
$this->data = ['instances' => $empty, 'total' => $empty];
4948
foreach ($this->instances as $name => $instance) {
50-
$this->data['instances']['calls'][$name] = $instance->getCalls();
49+
$this->data['instances']['calls'][$name] = $instance->__getCalls();
5150
}
5251

5352
$this->data['instances']['statistics'] = $this->calculateStatistics();

Diff for: src/Cache/Recording/HierarchyAndTaggablePool.php renamed to src/DataCollector/CacheProxy.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Cache\CacheBundle\Cache\Recording;
13-
14-
use Cache\Hierarchy\HierarchicalPoolInterface;
12+
namespace Cache\CacheBundle\DataCollector;
1513

1614
/**
17-
* @author Tobias Nyholm <[email protected]>
15+
* An interface for a cache proxy. A cache proxy is created when we profile a cache pool.
1816
*
19-
* @internal
17+
* @author Tobias Nyholm <[email protected]>
2018
*/
21-
class HierarchyAndTaggablePool extends TaggablePool implements HierarchicalPoolInterface
19+
interface CacheProxy
2220
{
21+
public function __getCalls();
22+
23+
public function __setName($name);
2324
}

Diff for: src/DataCollector/DecoratingFactory.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\CacheBundle\DataCollector;
13+
14+
use Nyholm\NSA;
15+
use Psr\Cache\CacheItemPoolInterface;
16+
17+
/**
18+
* A factory that decorates another factory to be able to use the proxy cache.
19+
*
20+
* @author Tobias Nyholm <[email protected]>
21+
*/
22+
class DecoratingFactory
23+
{
24+
/**
25+
* @type ProxyFactory
26+
*/
27+
private $proxyFactory;
28+
29+
/**
30+
* @param ProxyFactory $proxyFactory
31+
*/
32+
public function __construct(ProxyFactory $proxyFactory)
33+
{
34+
$this->proxyFactory = $proxyFactory;
35+
}
36+
37+
/**
38+
* @param CacheItemPoolInterface $originalObject original class
39+
*
40+
* @return CacheProxy|CacheItemPoolInterface
41+
*/
42+
public function create($originalObject)
43+
{
44+
$proxyClass = $this->proxyFactory->createProxy(get_class($originalObject));
45+
$rc = new \ReflectionClass($proxyClass);
46+
$pool = $rc->newInstanceWithoutConstructor();
47+
48+
// Copy properties from original pool to new
49+
foreach (NSA::getProperties($originalObject) as $property) {
50+
NSA::setProperty($pool, $property, NSA::getProperty($originalObject, $property));
51+
}
52+
53+
return $pool;
54+
}
55+
}

Diff for: src/DataCollector/ProxyFactory.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\CacheBundle\DataCollector;
13+
14+
/**
15+
* Generate proxies over your cache pool. This should only be used in development.
16+
*
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class ProxyFactory
20+
{
21+
/**
22+
* @type string
23+
*/
24+
private $proxyDirectory;
25+
26+
/**
27+
* @param string $proxyDirectory
28+
*/
29+
public function __construct($proxyDirectory)
30+
{
31+
$this->proxyDirectory = $proxyDirectory;
32+
}
33+
34+
/**
35+
* Create a proxy that handles data collecting better.
36+
*
37+
* @param string $class
38+
* @param string &$proxyFile where we store the proxy class
39+
*
40+
* @return string the name of a much much better class
41+
*/
42+
public function createProxy($class, &$proxyFile = null)
43+
{
44+
$proxyClass = $this->getProxyClass($class);
45+
$class = '\\'.rtrim($class, '\\');
46+
$proxyFile = $this->proxyDirectory.'/'.$proxyClass.'.php';
47+
48+
if (class_exists($proxyClass)) {
49+
return $proxyClass;
50+
}
51+
52+
$content = file_get_contents(dirname(__DIR__).'/Resources/proxy/template.php');
53+
$content = str_replace('__TPL_CLASS__', $proxyClass, $content);
54+
$content = str_replace('__TPL_EXTENDS__', $class, $content);
55+
56+
$this->checkProxyDirectory();
57+
file_put_contents($proxyFile, $content);
58+
require $proxyFile;
59+
60+
return $proxyClass;
61+
}
62+
63+
private function checkProxyDirectory()
64+
{
65+
if (!is_dir($this->proxyDirectory)) {
66+
@mkdir($this->proxyDirectory, 0777, true);
67+
}
68+
}
69+
70+
private function getProxyClass($namespace)
71+
{
72+
return 'php_cache_proxy_'.str_replace('\\', '_', $namespace);
73+
}
74+
}

Diff for: src/Cache/Recording/HierarchyPool.php renamed to src/DataCollector/TraceableAdapterEvent.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Cache\CacheBundle\Cache\Recording;
13-
14-
use Cache\Hierarchy\HierarchicalPoolInterface;
12+
namespace Cache\CacheBundle\DataCollector;
1513

1614
/**
17-
* @author Tobias Nyholm <[email protected]>
18-
*
1915
* @internal
2016
*/
21-
class HierarchyPool extends CachePool implements HierarchicalPoolInterface
17+
class TraceableAdapterEvent
2218
{
19+
public $name;
20+
public $argument;
21+
public $start;
22+
public $end;
23+
public $result;
24+
public $hits = 0;
25+
public $misses = 0;
2326
}

0 commit comments

Comments
 (0)