1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Metadata\Factory; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Metadata\Driver\DriverInterface; |
15
|
|
|
use Cubiche\Core\Metadata\Locator\FileLocatorInterface; |
16
|
|
|
use Doctrine\Common\Annotations\Reader; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DriverFactory class. |
20
|
|
|
* |
21
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class DriverFactory |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var DriverFactory |
27
|
|
|
*/ |
28
|
|
|
protected static $instance = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $drivers = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $driver |
37
|
|
|
*/ |
38
|
|
|
public static function registerAnnotationDriver($driver) |
39
|
|
|
{ |
40
|
|
|
static::instance()->addAnnotationDriver($driver); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $driver |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
protected function addAnnotationDriver($driver) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$this->checkDriver($driver); |
49
|
|
|
|
50
|
|
|
if (!isset($this->drivers['annotation'])) { |
51
|
|
|
$this->drivers['annotation'] = array(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->drivers['annotation'][] = $driver; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $driver |
59
|
|
|
*/ |
60
|
|
|
public static function registerYamlDriver($driver) |
61
|
|
|
{ |
62
|
|
|
static::instance()->addYamlDriver($driver); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $driver |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
protected function addYamlDriver($driver) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$this->checkDriver($driver); |
71
|
|
|
|
72
|
|
|
if (!isset($this->drivers['yaml'])) { |
73
|
|
|
$this->drivers['yaml'] = array(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->drivers['yaml'][] = $driver; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $driver |
81
|
|
|
*/ |
82
|
|
|
public static function registerXmlDriver($driver) |
83
|
|
|
{ |
84
|
|
|
static::instance()->addXmlDriver($driver); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $driver |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
protected function addXmlDriver($driver) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$this->checkDriver($driver); |
93
|
|
|
|
94
|
|
|
if (!isset($this->drivers['xml'])) { |
95
|
|
|
$this->drivers['xml'] = array(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->drivers['xml'][] = $driver; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Reader $reader |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function createAnnotationDriver(Reader $reader) |
107
|
|
|
{ |
108
|
|
|
$drivers = array(); |
109
|
|
|
foreach ($this->drivers['annotation'] as $driver) { |
110
|
|
|
$drivers[] = new $driver($reader); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $drivers; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param FileLocatorInterface $locator |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function createYamlDriver(FileLocatorInterface $locator) |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$drivers = array(); |
124
|
|
|
foreach ($this->drivers['yaml'] as $driver) { |
125
|
|
|
$drivers[] = new $driver($locator); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $drivers; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param FileLocatorInterface $locator |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function createXmlDriver(FileLocatorInterface $locator) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$drivers = array(); |
139
|
|
|
foreach ($this->drivers['xml'] as $driver) { |
140
|
|
|
$drivers[] = new $driver($locator); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $drivers; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $driver |
148
|
|
|
*/ |
149
|
|
|
private function checkDriver($driver) |
150
|
|
|
{ |
151
|
|
|
$reflector = new \ReflectionClass($driver); |
152
|
|
|
if (!$reflector->implementsInterface(DriverInterface::class)) { |
153
|
|
|
throw new \InvalidArgumentException(sprintf( |
154
|
|
|
'The object must be an instance of %s. Instance of %s given', |
155
|
|
|
DriverInterface::class, |
156
|
|
|
$driver |
157
|
|
|
)); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return DriverFactory |
163
|
|
|
*/ |
164
|
|
|
public static function instance() |
165
|
|
|
{ |
166
|
|
|
if (static::$instance === null) { |
167
|
|
|
static::$instance = new static(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return static::$instance; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.