1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Proxy; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Yiisoft\Files\FileHelper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @internal |
13
|
|
|
* |
14
|
|
|
* Local file system based cache used to store and retrieve contents of proxy objects. See {@see ProxyManager} for |
15
|
|
|
* usage. |
16
|
|
|
*/ |
17
|
|
|
final class ClassCache |
18
|
|
|
{ |
19
|
12 |
|
public function __construct( |
20
|
|
|
/** |
21
|
|
|
* @var string Base directory for working with cache. It will be created automatically if it does not exist |
22
|
|
|
* ({@see getClassPath()}). |
23
|
|
|
*/ |
24
|
|
|
private string $cachePath |
25
|
|
|
) { |
26
|
12 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Writes proxy class contents to a file in {@see getClassPath()}. |
30
|
|
|
* |
31
|
|
|
* @param string $className The full name of user class or interface (with namespace). For example: |
32
|
|
|
* `Yiisoft\Proxy\Tests\Stub\GraphInterface` or `Yiisoft\Proxy\Tests\Stub\Graph`. You can use `::class` instead of |
33
|
|
|
* manually specifying a string. |
34
|
|
|
* @param string $baseProxyClassName The full name of {@see ObjectProxy} implementation (with namespace) which will |
35
|
|
|
* be the base class for proxy. For example: `MyProxy`. |
36
|
|
|
* @param string $classContents The whole class contents without opening PHP tag (it's prepended automatically). |
37
|
|
|
*/ |
38
|
7 |
|
public function set(string $className, string $baseProxyClassName, string $classContents): void |
39
|
|
|
{ |
40
|
7 |
|
file_put_contents($this->getClassPath($className, $baseProxyClassName), "<?php\n\n" . $classContents, LOCK_EX); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Reads proxy class contents. |
45
|
|
|
* |
46
|
|
|
* @param string $className The full name of user class or interface (with namespace). For example: `GraphInterface` |
47
|
|
|
* or `Graph`. You can use `::class` instead of manually specifying a string. |
48
|
|
|
* @param string $baseProxyClassName The full name of {@see ObjectProxy} implementation (with namespace) which will |
49
|
|
|
* be the base class for proxy. For example: `MyProxy`. |
50
|
|
|
* |
51
|
|
|
* @throws Exception When unable to write to a file in {@see getClassPath()}. |
52
|
|
|
* |
53
|
|
|
* @return string|null In case of presence data in cache the whole class contents (including PHP opening tag) |
54
|
|
|
* returned as a string. In case of its absence or other errors - `null` is returned. |
55
|
|
|
*/ |
56
|
7 |
|
public function get(string $className, string $baseProxyClassName): ?string |
57
|
|
|
{ |
58
|
7 |
|
$classPath = $this->getClassPath($className, $baseProxyClassName); |
59
|
7 |
|
if (!file_exists($classPath)) { |
60
|
6 |
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$content = file_get_contents($classPath); |
64
|
|
|
|
65
|
1 |
|
return $content === false ? null : $content; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets full path to a class. For example: `/tmp/Yiisoft/Tests/Stub/GraphInterface.MyProxy.php` or |
70
|
|
|
* `/tmp/Yiisoft/Tests/Stub/Graph.MyProxy.php`. Additionally, checks and prepares (if needed) {@see $cachePath} for |
71
|
|
|
* usage (@see FileHelper::ensureDirectory()}. |
72
|
|
|
* |
73
|
|
|
* @param string $className The full name of user class or interface (with namespace). For example: `GraphInterface` |
74
|
|
|
* or `Graph`. You can use `::class` instead of manually specifying a string. |
75
|
|
|
* @param string $baseProxyClassName The full name of {@see ObjectProxy} implementation (with namespace) which will |
76
|
|
|
* be the base class for proxy. For example: `MyProxy`. |
77
|
|
|
* |
78
|
|
|
* @throws RuntimeException In case when it's impossible to use or create {@see $cachePath}. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
8 |
|
public function getClassPath(string $className, string $baseProxyClassName): string |
83
|
|
|
{ |
84
|
8 |
|
[$classFileName, $classFilePath] = $this->getClassFileNameAndPath($className, $baseProxyClassName); |
85
|
|
|
|
86
|
|
|
try { |
87
|
8 |
|
FileHelper::ensureDirectory($classFilePath, 0777); |
88
|
|
|
} catch (RuntimeException) { |
89
|
|
|
throw new RuntimeException("Directory \"$classFilePath\" was not created."); |
90
|
|
|
} |
91
|
|
|
|
92
|
8 |
|
return $classFilePath . DIRECTORY_SEPARATOR . $classFileName; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Gets class file name and path as separate elements: |
97
|
|
|
* |
98
|
|
|
* - For name, a combination of both class name and base proxy class name is used. |
99
|
|
|
* - For path, {@see $cachePath} used as a base directory and class namespace for subdirectories. |
100
|
|
|
* |
101
|
|
|
* @param string $className The full name of user class or interface (with namespace). For example: `GraphInterface` |
102
|
|
|
* or `Graph`. You can use `::class` instead of manually specifying a string. |
103
|
|
|
* @param string $baseProxyClassName The full name of {@see ObjectProxy} implementation (with namespace) which will |
104
|
|
|
* be the base class for proxy. For example: `MyProxy`. |
105
|
|
|
* |
106
|
|
|
* @return string[] Array with two elements, the first one is a file name and the second one is a path. For example: |
107
|
|
|
* `[`/tmp/Yiisoft/Proxy/Tests/Stub`, `GraphInterface.MyProxy.php`]` or |
108
|
|
|
* `[`/tmp/Yiisoft/Proxy/Tests/Stub`, `Graph.MyProxy.php`]`. |
109
|
|
|
*/ |
110
|
8 |
|
private function getClassFileNameAndPath(string $className, string $baseProxyClassName): array |
111
|
|
|
{ |
112
|
8 |
|
$classParts = explode('\\', $className); |
113
|
8 |
|
if (count($classParts) === 1) { |
114
|
1 |
|
$classParts = ['Builtin', ...$classParts]; |
115
|
|
|
} |
116
|
|
|
|
117
|
8 |
|
$parentClassParts = explode('\\', $baseProxyClassName); |
118
|
8 |
|
$classFileName = array_pop($classParts) . '.' . array_pop($parentClassParts) . '.php'; |
119
|
8 |
|
$classFilePath = $this->cachePath . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $classParts); |
120
|
|
|
|
121
|
8 |
|
return [$classFileName, $classFilePath]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|