1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-cache\cache-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 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
|
1 |
|
public function __construct($proxyDirectory) |
30
|
|
|
{ |
31
|
1 |
|
$this->proxyDirectory = $proxyDirectory; |
32
|
1 |
|
} |
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
|
1 |
|
public function createProxy($class, &$proxyFile = null) |
43
|
|
|
{ |
44
|
1 |
|
$proxyClass = $this->getProxyClass($class); |
45
|
1 |
|
$class = '\\'.rtrim($class, '\\'); |
46
|
1 |
|
$proxyFile = $this->proxyDirectory.'/'.$proxyClass.'.php'; |
47
|
|
|
|
48
|
1 |
|
if (class_exists($proxyClass)) { |
49
|
|
|
return $proxyClass; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
if (file_exists($proxyFile)) { |
53
|
|
|
require $proxyFile; |
54
|
|
|
|
55
|
|
|
return $proxyClass; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$content = file_get_contents(dirname(__DIR__).'/Resources/proxy/template.php'); |
59
|
1 |
|
$content = str_replace('__TPL_CLASS__', $proxyClass, $content); |
60
|
1 |
|
$content = str_replace('__TPL_EXTENDS__', $class, $content); |
61
|
|
|
|
62
|
1 |
|
$this->checkProxyDirectory(); |
63
|
1 |
|
file_put_contents($proxyFile, $content); |
64
|
1 |
|
require $proxyFile; |
65
|
|
|
|
66
|
1 |
|
return $proxyClass; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
private function checkProxyDirectory() |
70
|
|
|
{ |
71
|
1 |
|
if (!is_dir($this->proxyDirectory)) { |
72
|
1 |
|
@mkdir($this->proxyDirectory, 0777, true); |
|
|
|
|
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
1 |
|
private function getProxyClass($namespace) |
77
|
|
|
{ |
78
|
1 |
|
return 'php_cache_proxy_'.str_replace('\\', '_', $namespace); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: