|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\Service\Mapping; |
|
4
|
|
|
|
|
5
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Service\Mapping\Driver\ModelConfigurationDriverInterface; |
|
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* ClassMetadataFactory builds a metadata object from a driver or cache. |
|
10
|
|
|
* |
|
11
|
|
|
* @internal This code is part of the internal API to gather the appropriate model information and shouldn't be used for else use-cases |
|
12
|
|
|
*/ |
|
13
|
|
|
final class ClassMetadataFactory |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ModelConfigurationDriverInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $driver; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Filesystem |
|
22
|
|
|
*/ |
|
23
|
|
|
private $filesystem; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var bool |
|
27
|
|
|
*/ |
|
28
|
|
|
private $isCacheEnabled; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $cacheFile; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ModelConfigurationDriverInterface $driver |
|
39
|
|
|
* @param Filesystem $filesystem |
|
40
|
|
|
* @param bool $isCacheEnabled |
|
41
|
|
|
* @param string $cacheFile |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(ModelConfigurationDriverInterface $driver, Filesystem $filesystem, $isCacheEnabled, $cacheFile) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->driver = $driver; |
|
46
|
|
|
$this->filesystem = $filesystem; |
|
47
|
|
|
$this->isCacheEnabled = (bool) $isCacheEnabled; |
|
48
|
|
|
$this->cacheFile = $cacheFile; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Creates the class metadata object. |
|
53
|
|
|
* |
|
54
|
|
|
* @return ClassMetadata |
|
55
|
|
|
*/ |
|
56
|
|
|
public function createMetadataObject() |
|
57
|
|
|
{ |
|
58
|
|
|
return new ClassMetadata($this->resolveProperties()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Resolves the class properties. |
|
63
|
|
|
* |
|
64
|
|
|
* @return \ReflectionProperty[] |
|
65
|
|
|
*/ |
|
66
|
|
|
private function resolveProperties() |
|
67
|
|
|
{ |
|
68
|
|
|
if ($this->isCacheEnabled) { |
|
69
|
|
|
return $this->resolveCache(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->driver->getMetadataForUser(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Fetches the data from the cache. |
|
77
|
|
|
* |
|
78
|
|
|
* @return \ReflectionProperty[] |
|
79
|
|
|
*/ |
|
80
|
|
|
private function resolveCache() |
|
81
|
|
|
{ |
|
82
|
|
|
if (!$this->filesystem->exists($this->cacheFile)) { |
|
83
|
|
|
throw new \RuntimeException(sprintf( |
|
84
|
|
|
'The file "%s" can\'t be parsed!', |
|
85
|
|
|
$this->cacheFile |
|
86
|
|
|
)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return unserialize(file_get_contents($this->cacheFile)); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|