|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Cubiche package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Cubiche |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Cubiche\Core\Metadata\Driver; |
|
12
|
|
|
|
|
13
|
|
|
use Cubiche\Core\Metadata\ClassMetadataInterface; |
|
14
|
|
|
use Cubiche\Core\Metadata\Exception\MappingException; |
|
15
|
|
|
use Cubiche\Core\Metadata\Locator\FileLocatorInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* AbstractFileDriver class. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractFileDriver implements DriverInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var FileLocatorInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $locator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $classCache; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $fileName; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* AbstractFileDriver constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param FileLocatorInterface $locator |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(FileLocatorInterface $locator) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->locator = $locator; |
|
47
|
|
|
$this->classCache = []; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function loadMetadataForClass($className) |
|
54
|
|
|
{ |
|
55
|
|
|
if (null === $path = $this->locator->findMappingFile($className, $this->getExtension())) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->fileName = $path; |
|
60
|
|
|
|
|
61
|
|
|
return $this->loadMetadataFromFile($className, $path); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getAllClassNames() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->locator->getAllClassNames($this->getExtension()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Gets the element of schema meta data for the class from the mapping file. |
|
74
|
|
|
* This will lazily load the mapping file if it is not loaded yet. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $className |
|
77
|
|
|
* @param string $file |
|
78
|
|
|
* |
|
79
|
|
|
* @return mixed |
|
80
|
|
|
* |
|
81
|
|
|
* @throws MappingException |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getElement($className, $file) |
|
84
|
|
|
{ |
|
85
|
|
|
if (isset($this->classCache[$className])) { |
|
86
|
|
|
return $this->classCache[$className]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$result = $this->loadMappingFile($file); |
|
90
|
|
|
if (!isset($result[$className])) { |
|
91
|
|
|
throw MappingException::invalidMapping($className, $file); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->classCache = array_merge($this->classCache, $result); |
|
95
|
|
|
|
|
96
|
|
|
return $result[$className]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Parses the content of the file, and converts it to the desired metadata. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $className |
|
103
|
|
|
* @param string $file |
|
104
|
|
|
* |
|
105
|
|
|
* @return ClassMetadataInterface |
|
106
|
|
|
*/ |
|
107
|
|
|
abstract protected function loadMetadataFromFile($className, $file); |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the extension of the file. |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
abstract protected function getExtension(); |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Loads a mapping file with the given name and returns a map |
|
118
|
|
|
* from class/entity names to their corresponding file driver elements. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $file The mapping file to load. |
|
121
|
|
|
* |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
abstract protected function loadMappingFile($file); |
|
125
|
|
|
} |
|
126
|
|
|
|