|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
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 Sonata\EasyExtendsBundle\Bundle; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
|
15
|
|
|
|
|
16
|
|
|
class BundleMetadata |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var BundleInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $bundle; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string|bool |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $vendor = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $valid = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $namespace; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $name; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var bool |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $extendedDirectory = false; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var bool |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $extendedNamespace = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $configuration = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var OrmMetadata |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $ormMetadata = null; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var OdmMetadata |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $odmMetadata = null; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var PhpcrMetadata |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $phpcrMetadata = null; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var string |
|
75
|
|
|
*/ |
|
76
|
|
|
private $application; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param BundleInterface $bundle |
|
80
|
|
|
* @param array $configuration |
|
81
|
|
|
*/ |
|
82
|
|
|
public function __construct(BundleInterface $bundle, array $configuration = []) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->bundle = $bundle; |
|
85
|
|
|
$this->configuration = $configuration; |
|
86
|
|
|
|
|
87
|
|
|
$this->buildInformation(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public function isExtendable() |
|
94
|
|
|
{ |
|
95
|
|
|
// does not extends Application bundle ... |
|
96
|
|
|
return !( |
|
97
|
|
|
0 === strpos($this->getClass(), $this->configuration['namespace']) |
|
98
|
|
|
|| 0 === strpos($this->getClass(), 'Symfony') |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getClass() |
|
106
|
|
|
{ |
|
107
|
|
|
return get_class($this->bundle); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
public function isValid() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->valid; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getExtendedDirectory() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->extendedDirectory; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getVendor() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->vendor; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getExtendedNamespace() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->extendedNamespace; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getNamespace() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->namespace; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* return the bundle name. |
|
152
|
|
|
* |
|
153
|
|
|
* @return string return the bundle name |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getName() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->name; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getApplication() |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->application; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return BundleInterface |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getBundle() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->bundle; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return OdmMetadata |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getOdmMetadata() |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->odmMetadata; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return OrmMetadata |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getOrmMetadata() |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->ormMetadata; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @return PhpcrMetadata |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getPhpcrMetadata() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->phpcrMetadata; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* build basic information and check if the bundle respect the following convention |
|
202
|
|
|
* Vendor/BundleNameBundle/VendorBundleNameBundle. |
|
203
|
|
|
* |
|
204
|
|
|
* if the bundle does not respect this convention then the easy extends command will ignore |
|
205
|
|
|
* this bundle |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function buildInformation() |
|
208
|
|
|
{ |
|
209
|
|
|
$information = explode('\\', $this->getClass()); |
|
210
|
|
|
|
|
211
|
|
|
if (!$this->isExtendable()) { |
|
212
|
|
|
$this->valid = false; |
|
213
|
|
|
|
|
214
|
|
|
return; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
if (3 != count($information)) { |
|
218
|
|
|
$this->valid = false; |
|
219
|
|
|
|
|
220
|
|
|
return; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
if ($information[0].$information[1] != $information[2]) { |
|
224
|
|
|
$this->valid = false; |
|
225
|
|
|
|
|
226
|
|
|
return; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
$this->name = $information[count($information) - 1]; |
|
230
|
|
|
$this->vendor = $information[0]; |
|
231
|
|
|
$this->namespace = sprintf('%s\\%s', $this->vendor, $information[1]); |
|
232
|
|
|
$this->extendedDirectory = |
|
233
|
|
|
str_replace(':vendor', $this->vendor, $this->configuration['application_dir']). |
|
|
|
|
|
|
234
|
|
|
DIRECTORY_SEPARATOR. |
|
235
|
|
|
$information[1]; |
|
236
|
|
|
$this->extendedNamespace = sprintf( |
|
|
|
|
|
|
237
|
|
|
'%s%s\\%s', |
|
238
|
|
|
$this->configuration['namespace_prefix'], |
|
239
|
|
|
str_replace(':vendor', $this->vendor, $this->configuration['namespace']), |
|
240
|
|
|
$information[1] |
|
241
|
|
|
); |
|
242
|
|
|
$this->application = explode('\\', $this->configuration['namespace'])[0]; |
|
243
|
|
|
$this->valid = true; |
|
244
|
|
|
|
|
245
|
|
|
$this->ormMetadata = new OrmMetadata($this); |
|
246
|
|
|
$this->odmMetadata = new OdmMetadata($this); |
|
247
|
|
|
$this->phpcrMetadata = new PhpcrMetadata($this); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.