Passed
Pull Request — master (#39)
by Alexander
04:47 queued 02:15
created

ClassCache   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 18
dl 0
loc 99
ccs 18
cts 20
cp 0.9
rs 10
c 4
b 1
f 0
wmc 7

5 Methods

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