Passed
Pull Request — master (#46)
by
unknown
05:40 queued 03:35
created

ClassCache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 20
c 5
b 1
f 0
dl 0
loc 104
ccs 20
cts 22
cp 0.9091
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getClassPath() 0 11 2
A set() 0 3 1
A get() 0 9 2
A getClassFileNameAndPath() 0 12 2
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 11
    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
    }
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 5
    public function set(string $className, string $baseProxyClassName, string $classContents): void
39
    {
40 5
        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 5
    public function get(string $className, string $baseProxyClassName): ?string
57
    {
58
        try {
59 5
            $content = file_get_contents($this->getClassPath($className, $baseProxyClassName));
60 4
        } catch (Exception) {
61 4
            return null;
62
        }
63
64 1
        return $content;
65
    }
66
67
    /**
68
     * Gets full path to a class. For example: `/tmp/Yiisoft/Tests/Stub/GraphInterface.MyProxy.php` or
69
     * `/tmp/Yiisoft/Tests/Stub/Graph.MyProxy.php`. Additionally, checks and prepares (if needed) {@see $cachePath} for
70
     * usage (@see FileHelper::ensureDirectory()}.
71
     *
72
     * @param string $className The full name of user class or interface (with namespace). For example: `GraphInterface`
73
     * or `Graph`. You can use `::class` instead of manually specifying a string.
74
     * @param string $baseProxyClassName The full name of {@see ObjectProxy} implementation (with namespace) which will
75
     * be the base class for proxy. For example: `MyProxy`.
76
     *
77
     * @throws RuntimeException In case when it's impossible to use or create {@see $cachePath}.
78
     *
79
     * @return string
80
     */
81 6
    public function getClassPath(string $className, string $baseProxyClassName): string
82
    {
83 6
        [$classFileName, $classFilePath] = $this->getClassFileNameAndPath($className, $baseProxyClassName);
84
85
        try {
86 6
            FileHelper::ensureDirectory($classFilePath, 0777);
87
        } catch (RuntimeException) {
88
            throw new RuntimeException("Directory \"$classFilePath\" was not created.");
89
        }
90
91 6
        return $classFilePath . DIRECTORY_SEPARATOR . $classFileName;
92
    }
93
94
    /**
95
     * Gets class file name and path as separate elements:
96
     *
97
     * - For name, a combination of both class name and base proxy class name is used.
98
     * - For path, {@see $cachePath} used as a base directory and class namespace for subdirectories.
99
     *
100
     * @param string $className The full name of user class or interface (with namespace). For example: `GraphInterface`
101
     * or `Graph`. You can use `::class` instead of manually specifying a string.
102
     * @param string $baseProxyClassName The full name of {@see ObjectProxy} implementation (with namespace) which will
103
     * be the base class for proxy. For example: `MyProxy`.
104
     *
105
     * @return string[] Array with two elements, the first one is a file name and the second one is a path. For example:
106
     * `[`/tmp/Yiisoft/Proxy/Tests/Stub`, `GraphInterface.MyProxy.php`]` or
107
     * `[`/tmp/Yiisoft/Proxy/Tests/Stub`, `Graph.MyProxy.php`]`.
108
     */
109 6
    private function getClassFileNameAndPath(string $className, string $baseProxyClassName): array
110
    {
111 6
        $classParts = explode('\\', $className);
112 6
        if (count($classParts) === 1) {
113 1
            $classParts = ['Builtin', ...$classParts];
114
        }
115
116 6
        $parentClassParts = explode('\\', $baseProxyClassName);
117 6
        $classFileName = array_pop($classParts) . '.' . array_pop($parentClassParts) . '.php';
118 6
        $classFilePath = $this->cachePath . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $classParts);
119
120 6
        return [$classFileName, $classFilePath];
121
    }
122
}
123