4
4
5
5
namespace ClassTransformer \CacheGenerator ;
6
6
7
- use ClassTransformer \Attributes \FieldAlias ;
8
- use ClassTransformer \Attributes \WritingStyle ;
9
7
use ReflectionClass ;
10
8
use RuntimeException ;
11
- use ReflectionException ;
12
- use ReflectionNamedType ;
13
9
use ClassTransformer \HydratorConfig ;
10
+ use ClassTransformer \Attributes \FieldAlias ;
14
11
use ClassTransformer \Reflection \CacheReflectionClass ;
15
12
use ClassTransformer \Exceptions \ClassNotFoundException ;
16
13
use ClassTransformer \Reflection \CacheReflectionProperty ;
28
25
*
29
26
* @template TClass
30
27
*/
31
- class CacheGenerator
28
+ final class CacheGenerator
32
29
{
30
+ /**
31
+ *
32
+ */
33
33
private const DIR_PERMISSION = 0777 ;
34
34
35
+ /**
36
+ * @var HydratorConfig
37
+ */
35
38
private HydratorConfig $ config ;
36
39
37
40
/** @psalm-param class-string<TClass> $class */
38
41
private string $ class ;
39
- private string $ cacheFile ;
42
+
43
+ /** @var string Path to cache file */
40
44
private string $ path ;
41
45
42
46
/**
@@ -46,12 +50,14 @@ public function __construct(string $class, HydratorConfig $config = null)
46
50
{
47
51
$ this ->config = $ config ?? new HydratorConfig ();
48
52
$ this ->class = $ class ;
49
- $ this -> cacheFile = str_replace ('\\' , '_ ' , $ this ->class );
50
- $ this ->path = $ this ->config ->cachePath . DIRECTORY_SEPARATOR . $ this -> cacheFile . '.cache.php ' ;
53
+ $ cacheFile = str_replace ('\\' , '_ ' , $ this ->class );
54
+ $ this ->path = $ this ->config ->cachePath . DIRECTORY_SEPARATOR . $ cacheFile . '.cache.php ' ;
51
55
}
52
56
53
57
/**
54
- * @param class-string<TClass> $class
58
+ * @template T
59
+ *
60
+ * @param class-string<T> $class
55
61
*/
56
62
public static function create (string $ class , HydratorConfig $ config = null ): CacheGenerator
57
63
{
@@ -60,11 +66,8 @@ public static function create(string $class, HydratorConfig $config = null): Cac
60
66
61
67
62
68
/**
63
- * @param string $class
64
- *
65
69
* @return CacheReflectionClass
66
- * @throws ReflectionException
67
- * @throws ClassNotFoundException
70
+ * @throws ClassNotFoundException|RuntimeException
68
71
*/
69
72
public function getClass (): CacheReflectionClass
70
73
{
@@ -79,12 +82,16 @@ public function getClass(): CacheReflectionClass
79
82
80
83
/**
81
84
* @return array{properties: array<CacheReflectionProperty>}
82
- * @throws ReflectionException |RuntimeException
85
+ * @throws ClassNotFoundException |RuntimeException
83
86
*/
84
87
public function generate (): array
85
88
{
86
89
$ this ->makeCacheDir ();
87
90
91
+ if (!class_exists ($ this ->class )) {
92
+ throw new ClassNotFoundException ("Class $ this ->class not found. Please check the class path you specified. " );
93
+ }
94
+
88
95
$ refInstance = new ReflectionClass ($ this ->class );
89
96
90
97
$ properties = $ refInstance ->getProperties ();
@@ -94,6 +101,7 @@ public function generate(): array
94
101
];
95
102
96
103
file_put_contents ($ this ->path , serialize ($ cache ));
104
+
97
105
return $ cache ;
98
106
}
99
107
@@ -119,9 +127,11 @@ private function convertToCacheProperty(RuntimeReflectionProperty $property): Ca
119
127
}
120
128
121
129
/**
122
- * @return array<string>
130
+ * @param array $args
131
+ *
132
+ * @return array<array-key,string>
123
133
*/
124
- public function getAliases ($ args ): array
134
+ public function getAliases (array $ args = [] ): array
125
135
{
126
136
$ aliases = $ args [FieldAlias::class] ?? null ;
127
137
@@ -169,18 +179,13 @@ public function cacheExists(): bool
169
179
}
170
180
171
181
/**
172
- * @param string|null $path
173
- *
174
182
* @return void
175
183
* @throws RuntimeException
176
184
*/
177
185
private function makeCacheDir (): void
178
186
{
179
187
if (
180
- (
181
- !file_exists ($ this ->config ->cachePath ) &&
182
- !mkdir ($ this ->config ->cachePath , self ::DIR_PERMISSION , true )
183
- )
188
+ (!file_exists ($ this ->config ->cachePath ) && !mkdir ($ concurrentDirectory = $ this ->config ->cachePath , self ::DIR_PERMISSION , true ) && !is_dir ($ concurrentDirectory ))
184
189
||
185
190
!is_dir ($ this ->config ->cachePath )
186
191
) {
0 commit comments