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

ClassCache   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

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

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 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:
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 4
    public function set(string $className, string $baseProxyClassName, string $classContents): void
39
    {
40 4
        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 4
    public function get(string $className, string $baseProxyClassName): ?string
57
    {
58
        try {
59 4
            $content = file_get_contents($this->getClassPath($className, $baseProxyClassName));
60 3
        } catch (Exception) {
61 3
            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 5
    public function getClassPath(string $className, string $baseProxyClassName): string
82
    {
83 5
        [$classFileName, $classFilePath] = $this->getClassFileNameAndPath($className, $baseProxyClassName);
84
85
        try {
86 5
            FileHelper::ensureDirectory($classFilePath, 0777);
87
        } catch (RuntimeException) {
88
            throw new RuntimeException("Directory \"$classFilePath\" was not created.");
89
        }
90
91 5
        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 5
    private function getClassFileNameAndPath(string $className, string $baseProxyClassName): array
110
    {
111 5
        $classParts = explode('\\', $className);
112 5
        $parentClassParts = explode('\\', $baseProxyClassName);
113 5
        $classFileName = array_pop($classParts) . '.' . array_pop($parentClassParts) . '.php';
114 5
        $classFilePath = $this->cachePath . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $classParts);
115
116 5
        return [$classFileName, $classFilePath];
117
    }
118
}
119