Passed
Pull Request — master (#47)
by
unknown
02:47
created

ClassCache::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 10
    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
         * Warning: Do not use stream like "php://memory" in production! It can cause file name collisions.
24
         */
25
        private string $cachePath
26
    ) {
27
    }
28
29
    /**
30
     * Writes proxy class contents to a file in {@see getClassPath()}.
31
     *
32
     * @param string $className The full name of user class or interface (with namespace). For example:
33
     * `Yiisoft\Proxy\Tests\Stub\GraphInterface` or `Yiisoft\Proxy\Tests\Stub\Graph`. You can use `::class` instead of
34
     * manually specifying a string.
35
     * @param string $baseProxyClassName The full name of {@see ObjectProxy} implementation (with namespace) which will
36
     * be the base class for proxy. For example: `MyProxy`.
37
     * @param string $classContents The whole class contents without opening PHP tag (it's prepended automatically).
38
     */
39 4
    public function set(string $className, string $baseProxyClassName, string $classContents): void
40
    {
41 4
        file_put_contents($this->getClassPath($className, $baseProxyClassName), "<?php\n\n" . $classContents, LOCK_EX);
42
    }
43
44
    /**
45
     * Reads proxy class contents.
46
     *
47
     * @param string $className The full name of user class or interface (with namespace). For example: `GraphInterface`
48
     * or `Graph`. You can use `::class` instead of manually specifying a string.
49
     * @param string $baseProxyClassName The full name of {@see ObjectProxy} implementation (with namespace) which will
50
     * be the base class for proxy. For example: `MyProxy`.
51
     *
52
     * @throws Exception When unable to write to a file in {@see getClassPath()}.
53
     *
54
     * @return string|null In case of presence data in cache the whole class contents (including PHP opening tag)
55
     * returned as a string. In case of its absence or other errors - `null` is returned.
56
     */
57 4
    public function get(string $className, string $baseProxyClassName): ?string
58
    {
59
        try {
60 4
            $content = file_get_contents($this->getClassPath($className, $baseProxyClassName));
61 3
        } catch (Exception) {
62 3
            return null;
63
        }
64
65 1
        return $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 5
    public function getClassPath(string $className, string $baseProxyClassName): string
83
    {
84 5
        [$classFileName, $classFilePath] = $this->getClassFileNameAndPath($className, $baseProxyClassName);
85
86 5
        if ($this->cachePath === 'php://memory') {
87 3
            return $classFileName;
88
        }
89
90
        try {
91 2
            FileHelper::ensureDirectory($classFilePath, 0777);
92
        } catch (RuntimeException) {
93
            throw new RuntimeException("Directory \"$classFilePath\" was not created.");
94
        }
95
96 2
        return $classFilePath . DIRECTORY_SEPARATOR . $classFileName;
97
    }
98
99
    /**
100
     * Gets class file name and path as separate elements:
101
     *
102
     * - For name, a combination of both class name and base proxy class name is used.
103
     * - For path, {@see $cachePath} used as a base directory and class namespace for subdirectories.
104
     *
105
     * @param string $className The full name of user class or interface (with namespace). For example: `GraphInterface`
106
     * or `Graph`. You can use `::class` instead of manually specifying a string.
107
     * @param string $baseProxyClassName The full name of {@see ObjectProxy} implementation (with namespace) which will
108
     * be the base class for proxy. For example: `MyProxy`.
109
     *
110
     * @return string[] Array with two elements, the first one is a file name and the second one is a path. For example:
111
     * `[`/tmp/Yiisoft/Proxy/Tests/Stub`, `GraphInterface.MyProxy.php`]` or
112
     * `[`/tmp/Yiisoft/Proxy/Tests/Stub`, `Graph.MyProxy.php`]`.
113
     */
114 5
    private function getClassFileNameAndPath(string $className, string $baseProxyClassName): array
115
    {
116 5
        $classParts = explode('\\', $className);
117 5
        $parentClassParts = explode('\\', $baseProxyClassName);
118 5
        $classFileName = array_pop($classParts) . '.' . array_pop($parentClassParts) . '.php';
119 5
        $classFilePath = $this->cachePath . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $classParts);
120
121 5
        return [$classFileName, $classFilePath];
122
    }
123
}
124