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

ClassCache::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
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
         */
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: `GraphInterface`
32
     * or `Graph`. You can use `::class` instead of manually specifying a string.
33
     * @param string $baseProxyClassName The full name of {@see ObjectProxy} implementation (with namespace) which will
34
     * be the base class for proxy. For example: `MyProxy`.
35
     * @param string $classContents The whole class contents without opening PHP tag (it's prepended automatically).
36
     */
37 4
    public function set(string $className, string $baseProxyClassName, string $classContents): void
38
    {
39 4
        file_put_contents($this->getClassPath($className, $baseProxyClassName), "<?php\n\n" . $classContents, LOCK_EX);
40
    }
41
42
    /**
43
     * Reads proxy class contents.
44
     *
45
     * @param string $className {@see set()}
46
     * @param string $baseProxyClassName {@see set())
47
     *
48
     * @throws Exception When unable to write to a file in {@see getClassPath()}.
49
     *
50
     * @return string|null In case of presence data in cache the whole class contents (including PHP opening tag)
51
     * returned as a string. In case of its absence or other errors - `null` is returned.
52
     */
53 4
    public function get(string $className, string $baseProxyClassName): ?string
54
    {
55
        try {
56 4
            $content = file_get_contents($this->getClassPath($className, $baseProxyClassName));
57 3
        } catch (Exception) {
58 3
            return null;
59
        }
60
61 1
        return $content;
62
    }
63
64
    /**
65
     * Gets full path to a class. For example: `/tmp/Yiisoft/Tests/Stub/GraphInterface.MyProxy.php` or
66
     * `/tmp/Yiisoft/Tests/Stub/Graph.MyProxy.php`. Additionally, checks and prepares (if needed) {@see $cachePath} for
67
     * usage (@see FileHelper::ensureDirectory()}.
68
     *
69
     * @param string $className {@see set()}
70
     * @param string $baseProxyClassName {@see set()}
71
     *
72
     * @throws RuntimeException In case when it's impossible to use or create {@see $cachePath}.
73
     *
74
     * @return string
75
     */
76 5
    public function getClassPath(string $className, string $baseProxyClassName): string
77
    {
78 5
        [$classFileName, $classFilePath] = $this->getClassFileNameAndPath($className, $baseProxyClassName);
79
80
        try {
81 5
            FileHelper::ensureDirectory($classFilePath, 0777);
82
        } catch (RuntimeException) {
83
            throw new RuntimeException("Directory \"$classFilePath\" was not created");
84
        }
85
86 5
        return $classFilePath . DIRECTORY_SEPARATOR . $classFileName;
87
    }
88
89
    /**
90
     * Gets class file name and path as separate elements:
91
     *
92
     * - For name, a combination of both class name and base proxy class name is used.
93
     * - For path, {@see $cachePath} used as a base directory and class namespace for subdirectories.
94
     *
95
     * @param string $className {@see set()}
96
     * @param string $baseProxyClassName {@see set()}
97
     *
98
     * @return string[] Array with two elements, the first one is a file name and the second one is a path. For example:
99
     * `[`/tmp/Yiisoft/Proxy/Tests/Stub`, `GraphInterface.MyProxy.php`]` or
100
     * `[`/tmp/Yiisoft/Proxy/Tests/Stub`, `Graph.MyProxy.php`]`.
101
     */
102 5
    private function getClassFileNameAndPath(string $className, string $baseProxyClassName): array
103
    {
104 5
        $classParts = explode('\\', $className);
105 5
        $parentClassParts = explode('\\', $baseProxyClassName);
106 5
        $classFileName = array_pop($classParts) . '.' . array_pop($parentClassParts) . '.php';
107 5
        $classFilePath = $this->cachePath . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $classParts);
108
109 5
        return [$classFileName, $classFilePath];
110
    }
111
}
112