1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Drest package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Lee Davis |
9
|
|
|
* @copyright Copyright (c) Lee Davis <@leedavis81> |
10
|
|
|
* @link https://github.com/leedavis81/drest/blob/master/LICENSE |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT X License (MIT) |
12
|
|
|
*/ |
13
|
|
|
namespace Drest\Mapping; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\Cache\Cache; |
16
|
|
|
use Drest\DrestException; |
17
|
|
|
use Drest\Mapping\Driver\DriverInterface; |
18
|
|
|
|
19
|
|
|
class MetadataFactory |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Driver attached to this meta data factory |
24
|
|
|
* @var \Drest\Mapping\Driver\DriverInterface $driver |
25
|
|
|
*/ |
26
|
|
|
private $driver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Cache used to prevent metadata reloading |
30
|
|
|
* @var \Doctrine\Common\Cache\Cache $cache |
31
|
|
|
*/ |
32
|
|
|
private $cache; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* A prefix string to use when interacting with a Doctrine cache object |
36
|
|
|
* @var string $cache_prefix |
37
|
|
|
*/ |
38
|
|
|
private $cache_prefix = '_drest_'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Metadata that has already been loaded by the driver |
42
|
|
|
* @var array $loadedMetadata |
43
|
|
|
*/ |
44
|
|
|
private $loadedMetadata = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param DriverInterface $driver |
48
|
|
|
*/ |
49
|
51 |
|
public function __construct(DriverInterface $driver) |
50
|
|
|
{ |
51
|
51 |
|
$this->driver = $driver; |
52
|
51 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the doctrine cache drive to be user |
56
|
|
|
* @param Cache $cache |
57
|
|
|
*/ |
58
|
31 |
|
public function setCache(Cache $cache) |
59
|
|
|
{ |
60
|
31 |
|
$this->cache = $cache; |
61
|
31 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get all class names |
65
|
|
|
* @return array class names registered to this driver |
66
|
|
|
*/ |
67
|
2 |
|
public function getAllClassNames() |
68
|
|
|
{ |
69
|
2 |
|
return $this->driver->getAllClassNames(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get metadata for a certain class - loads once and caches |
74
|
|
|
* @param string $className |
75
|
|
|
* @throws \Drest\DrestException |
76
|
|
|
* @return ClassMetaData $metaData |
77
|
|
|
*/ |
78
|
50 |
|
public function getMetadataForClass($className) |
79
|
|
|
{ |
80
|
50 |
|
if (isset($this->loadedMetadata[$className])) { |
81
|
4 |
|
return $this->loadedMetadata[$className]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// check the cache |
85
|
50 |
|
if ($this->cache !== null) { |
86
|
31 |
|
$classMetadata = $this->cache->fetch($this->cache_prefix . $className); |
87
|
31 |
|
if ($classMetadata instanceof ClassMetaData) { |
88
|
|
|
if ($classMetadata->expired()) { |
89
|
|
|
$this->cache->delete($this->cache_prefix . $className); |
90
|
|
|
} else { |
91
|
|
|
$this->loadedMetadata[$className] = $classMetadata; |
92
|
|
|
|
93
|
|
|
return $classMetadata; |
94
|
|
|
} |
95
|
|
|
} |
96
|
31 |
|
} |
97
|
|
|
|
98
|
50 |
|
$classMetadata = $this->driver->loadMetadataForClass($className); |
99
|
41 |
|
if ($classMetadata !== null) { |
100
|
39 |
|
$this->loadedMetadata[$className] = $classMetadata; |
101
|
39 |
|
if ($this->cache !== null) { |
102
|
31 |
|
$this->cache->save($this->cache_prefix . $className, $classMetadata); |
103
|
31 |
|
} |
104
|
|
|
|
105
|
39 |
|
return $classMetadata; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
if (!isset($this->loadedMetadata[$className])) { |
109
|
2 |
|
throw DrestException::unableToLoadMetaDataFromDriver(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->loadedMetadata[$className]; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|