/
DecoratingFactory.php
55 lines (47 loc) · 1.4 KB
/
DecoratingFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/*
* This file is part of php-cache\cache-bundle package.
*
* (c) 2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* 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;
use Psr\Cache\CacheItemPoolInterface;
/**
* A factory that decorates another factory to be able to use the proxy cache.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class DecoratingFactory
{
/**
* @type ProxyFactory
*/
private $proxyFactory;
/**
* @param ProxyFactory $proxyFactory
*/
public function __construct(ProxyFactory $proxyFactory)
{
$this->proxyFactory = $proxyFactory;
}
/**
* @param CacheItemPoolInterface $originalObject original class
*
* @return CacheProxyInterface
*/
public function create($originalObject)
{
$proxyClass = $this->proxyFactory->createProxy(get_class($originalObject));
$reflection = new \ReflectionClass($proxyClass);
$pool = $reflection->newInstanceWithoutConstructor();
// Copy properties from original pool to new
foreach (NSA::getProperties($originalObject) as $property) {
NSA::setProperty($pool, $property, NSA::getProperty($originalObject, $property));
}
return $pool;
}
}