Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
TraitUsageGenerator.php
001 <?php
002 /**
003 * Zend Framework (http://framework.zend.com/)
004 *
005 * @link http://github.com/zendframework/zf2 for the canonical source repository
006 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
007 * @license http://framework.zend.com/license/new-bsd New BSD License
008 */
009 namespace Zend\Code\Generator;
010
011 use Reflection;
012 use ReflectionMethod;
013
014 class TraitUsageGenerator extends AbstractGenerator
015 {
016 /**
017 * @var ClassGenerator
018 */
019 protected $classGenerator;
020
021 /**
022 * @var array Array of trait names
023 */
024 protected $traits = array();
025
026 /**
027 * @var array Array of trait aliases
028 */
029 protected $traitAliases = array();
030
031 /**
032 * @var array Array of trait overrides
033 */
034 protected $traitOverrides = array();
035
036 /**
037 * @var array Array of string names
038 */
039 protected $uses = array();
040
041 public function __construct(ClassGenerator $classGenerator)
042 {
043 $this->classGenerator = $classGenerator;
044 }
045
046 /**
047 * @inherit Zend\Code\Generator\TraitUsageInterface
048 */
049 public function addUse($use, $useAlias = null)
050 {
051 if (! empty($useAlias)) {
052 $use .= ' as ' . $useAlias;
053 }
054
055 $this->uses[$use] = $use;
056 return $this;
057 }
058
059 /**
060 * @inherit Zend\Code\Generator\TraitUsageInterface
061 */
062 public function getUses()
063 {
064 return array_values($this->uses);
065 }
066
067 /**
068 * @inherit Zend\Code\Generator\TraitUsageInterface
069 */
070 public function addTrait($trait)
071 {
072 $traitName = $trait;
073 if (is_array($trait)) {
074 if (! array_key_exists('traitName', $trait)) {
075 throw new Exception\InvalidArgumentException('Missing required value for traitName');
076 }
077 $traitName = $trait['traitName'];
078
079 if (array_key_exists('aliases', $trait)) {
080 foreach ($trait['aliases'] as $alias) {
081 $this->addAlias($alias);
082 }
083 }
084
085 if (array_key_exists('insteadof', $trait)) {
086 foreach ($trait['insteadof'] as $insteadof) {
087 $this->addTraitOverride($insteadof);
088 }
089 }
090 }
091
092 if (! $this->hasTrait($traitName)) {
093 $this->traits[] = $traitName;
094 }
095
096 return $this;
097 }
098
099 /**
100 * @inherit Zend\Code\Generator\TraitUsageInterface
101 */
102 public function addTraits(array $traits)
103 {
104 foreach ($traits as $trait) {
105 $this->addTrait($trait);
106 }
107
108 return $this;
109 }
110
111 /**
112 * @inherit Zend\Code\Generator\TraitUsageInterface
113 */
114 public function hasTrait($traitName)
115 {
116 return in_array($traitName, $this->traits);
117 }
118
119 /**
120 * @inherit Zend\Code\Generator\TraitUsageInterface
121 */
122 public function getTraits()
123 {
124 return $this->traits;
125 }
126
127 /**
128 * @inherit Zend\Code\Generator\TraitUsageInterface
129 */
130 public function removeTrait($traitName)
131 {
132 $key = array_search($traitName, $this->traits);
133 if (false !== $key) {
134 unset($this->traits[$key]);
135 }
136
137 return $this;
138 }
139
140 /**
141 * @inherit Zend\Code\Generator\TraitUsageInterface
142 */
143 public function addTraitAlias($method, $alias, $visibility = null)
144 {
145 $traitAndMethod = $method;
146 if (is_array($method)) {
147 if (! array_key_exists('traitName', $method)) {
148 throw new Exception\InvalidArgumentException('Missing required argument "traitName" for $method');
149 }
150
151 if (! array_key_exists('method', $method)) {
152 throw new Exception\InvalidArgumentException('Missing required argument "method" for $method');
153 }
154
155 $traitAndMethod = $method['traitName'] . '::' . $method['method'];
156 }
157
158 // Validations
159 if (false === strpos($traitAndMethod, "::")) {
160 throw new Exception\InvalidArgumentException(
161 'Invalid Format: $method must be in the format of trait::method'
162 );
163 }
164 if (! is_string($alias)) {
165 throw new Exception\InvalidArgumentException('Invalid Alias: $alias must be a string or array.');
166 }
167 if ($this->classGenerator->hasMethod($alias)) {
168 throw new Exception\InvalidArgumentException('Invalid Alias: Method name already exists on this class.');
169 }
170 if (null !== $visibility
171 && $visibility !== ReflectionMethod::IS_PUBLIC
172 && $visibility !== ReflectionMethod::IS_PRIVATE
173 && $visibility !== ReflectionMethod::IS_PROTECTED
174 ) {
175 throw new Exception\InvalidArgumentException(
176 'Invalid Type: $visibility must of ReflectionMethod::IS_PUBLIC,'
177 . ' ReflectionMethod::IS_PRIVATE or ReflectionMethod::IS_PROTECTED'
178 );
179 }
180
181 list($trait, $method) = explode('::', $traitAndMethod);
182 if (! $this->hasTrait($trait)) {
183 throw new Exception\InvalidArgumentException('Invalid trait: Trait does not exists on this class');
184 }
185
186 $this->traitAliases[$traitAndMethod] = array(
187 'alias' => $alias,
188 'visibility' => $visibility
189 );
190
191 return $this;
192 }
193
194 /**
195 * @inherit Zend\Code\Generator\TraitUsageInterface
196 */
197 public function getTraitAliases()
198 {
199 return $this->traitAliases;
200 }
201
202 /**
203 * @inherit Zend\Code\Generator\TraitUsageInterface
204 */
205 public function addTraitOverride($method, $traitsToReplace)
206 {
207 if (false === is_array($traitsToReplace)) {
208 $traitsToReplace = array($traitsToReplace);
209 }
210
211 $traitAndMethod = $method;
212 if (is_array($method)) {
213 if (! array_key_exists('traitName', $method)) {
214 throw new Exception\InvalidArgumentException('Missing required argument "traitName" for $method');
215 }
216
217 if (! array_key_exists('method', $method)) {
218 throw new Exception\InvalidArgumentException('Missing required argument "method" for $method');
219 }
220
221 $traitAndMethod = (string) $method['traitName'] . '::' . (string) $method['method'];
222 }
223
224 // Validations
225 if (false === strpos($traitAndMethod, "::")) {
226 throw new Exception\InvalidArgumentException(
227 'Invalid Format: $method must be in the format of trait::method'
228 );
229 }
230
231 list($trait, $method) = explode("::", $traitAndMethod);
232 if (! $this->hasTrait($trait)) {
233 throw new Exception\InvalidArgumentException('Invalid trait: Trait does not exists on this class');
234 }
235
236 if (! array_key_exists($traitAndMethod, $this->traitOverrides)) {
237 $this->traitOverrides[$traitAndMethod] = array();
238 }
239
240 foreach ($traitsToReplace as $traitToReplace) {
241 if (! is_string($traitToReplace)) {
242 throw new Exception\InvalidArgumentException(
243 'Invalid Argument: $traitToReplace must be a string or array of strings'
244 );
245 }
246
247 if (! in_array($traitToReplace, $this->traitOverrides[$traitAndMethod])) {
248 $this->traitOverrides[$traitAndMethod][] = $traitToReplace;
249 }
250 }
251
252 return $this;
253 }
254
255 /**
256 * @inherit Zend\Code\Generator\TraitUsageInterface
257 */
258 public function removeTraitOverride($method, $overridesToRemove = null)
259 {
260 if (! array_key_exists($method, $this->traitOverrides)) {
261 return $this;
262 }
263
264 if (null === $overridesToRemove) {
265 unset($this->traitOverrides[$method]);
266 return $this;
267 }
268
269 $overridesToRemove = (! is_array($overridesToRemove))
270 ? array($overridesToRemove)
271 : $overridesToRemove;
272 foreach ($overridesToRemove as $traitToRemove) {
273 $key = array_search($traitToRemove, $this->traitOverrides[$method]);
274 if (false !== $key) {
275 unset($this->traitOverrides[$method][$key]);
276 }
277 }
278 return $this;
279 }
280
281 /**
282 * @inherit Zend\Code\Generator\TraitUsageInterface
283 */
284 public function getTraitOverrides()
285 {
286 return $this->traitOverrides;
287 }
288
289 /**
290 * @inherit Zend\Code\Generator\GeneratorInterface
291 */
292 public function generate()
293 {
294 $output = '';
295 $indent = $this->getIndentation();
296 $traits = $this->getTraits();
297
298 if (empty($traits)) {
299 return $output;
300 }
301
302 $output .= $indent . 'use ' . implode(', ', $traits);
303
304 $aliases = $this->getTraitAliases();
305 $overrides = $this->getTraitOverrides();
306 if (empty($aliases) && empty($overrides)) {
307 $output .= ";" . self::LINE_FEED . self::LINE_FEED;
308 return $output;
309 }
310
311 $output .= ' {' . self::LINE_FEED;
312 foreach ($aliases as $method => $alias) {
313 $visibility = (null !== $alias['visibility'])
314 ? current(Reflection::getModifierNames($alias['visibility'])) . ' '
315 : '';
316
317 // validation check
318 if ($this->classGenerator->hasMethod($alias['alias'])) {
319 throw new Exception\RuntimeException(sprintf(
320 'Generation Error: Aliased method %s already exists on this class',
321 $alias['alias']
322 ));
323 }
324
325 $output .=
326 $indent
327 . $indent
328 . $method
329 . ' as '
330 . $visibility
331 . $alias['alias']
332 . ';'
333 . self::LINE_FEED;
334 }
335
336 foreach ($overrides as $method => $insteadofTraits) {
337 foreach ($insteadofTraits as $insteadofTrait) {
338 $output .=
339 $indent
340 . $indent
341 . $method
342 . ' insteadof '
343 . $insteadofTrait
344 . ';'
345 . self::LINE_FEED;
346 }
347 }
348
349 $output .= self::LINE_FEED . $indent . '}' . self::LINE_FEED . self::LINE_FEED;
350
351 return $output;
352 }
353 }
354