3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Code\Scanner
;
12 use Zend\Code\Exception
;
14 class DerivedClassScanner
extends ClassScanner
17 * @var DirectoryScanner
19 protected $directoryScanner = null;
24 protected $classScanner = null;
29 protected $parentClassScanners = array();
34 protected $interfaceClassScanners = array();
37 * @param ClassScanner $classScanner
38 * @param DirectoryScanner $directoryScanner
40 public function __construct(ClassScanner
$classScanner, DirectoryScanner
$directoryScanner)
42 $this->classScanner
= $classScanner;
43 $this->directoryScanner
= $directoryScanner;
45 $currentScannerClass = $classScanner;
47 while ($currentScannerClass && $currentScannerClass->hasParentClass()) {
48 $currentParentClassName = $currentScannerClass->getParentClass();
49 if ($directoryScanner->hasClass($currentParentClassName)) {
50 $currentParentClass = $directoryScanner->getClass($currentParentClassName);
51 $this->parentClassScanners
[$currentParentClassName] = $currentParentClass;
52 $currentScannerClass = $currentParentClass;
54 $currentScannerClass = false;
58 foreach ($interfaces = $this->classScanner
->getInterfaces() as $iName) {
59 if ($directoryScanner->hasClass($iName)) {
60 $this->interfaceClassScanners
[$iName] = $directoryScanner->getClass($iName);
68 public function getName()
70 return $this->classScanner
->getName();
76 public function getShortName()
78 return $this->classScanner
->getShortName();
84 public function isInstantiable()
86 return $this->classScanner
->isInstantiable();
92 public function isFinal()
94 return $this->classScanner
->isFinal();
100 public function isAbstract()
102 return $this->classScanner
->isAbstract();
108 public function isInterface()
110 return $this->classScanner
->isInterface();
116 public function getParentClasses()
118 return array_keys($this->parentClassScanners
);
124 public function hasParentClass()
126 return ($this->classScanner
->getParentClass() !== null);
130 * @return null|string
132 public function getParentClass()
134 return $this->classScanner
->getParentClass();
138 * @param bool $returnClassScanners
141 public function getInterfaces($returnClassScanners = false)
143 if ($returnClassScanners) {
144 return $this->interfaceClassScanners
;
147 $interfaces = $this->classScanner
->getInterfaces();
148 foreach ($this->parentClassScanners
as $pClassScanner) {
149 $interfaces = array_merge($interfaces, $pClassScanner->getInterfaces());
156 * Return a list of constant names
160 public function getConstantNames()
162 $constants = $this->classScanner
->getConstantNames();
163 foreach ($this->parentClassScanners
as $pClassScanner) {
164 $constants = array_merge($constants, $pClassScanner->getConstantNames());
171 * Return a list of constants
173 * @param bool $namesOnly Set false to return instances of ConstantScanner
174 * @return array|ConstantScanner[]
176 public function getConstants($namesOnly = true)
178 if (true === $namesOnly) {
179 trigger_error('Use method getConstantNames() instead', E_USER_DEPRECATED
);
180 return $this->getConstantNames();
183 $constants = $this->classScanner
->getConstants();
184 foreach ($this->parentClassScanners
as $pClassScanner) {
185 $constants = array_merge($constants, $pClassScanner->getConstants($namesOnly));
192 * Return a single constant by given name or index of info
194 * @param string|int $constantNameOrInfoIndex
195 * @throws Exception\InvalidArgumentException
196 * @return bool|ConstantScanner
198 public function getConstant($constantNameOrInfoIndex)
200 if ($this->classScanner
->hasConstant($constantNameOrInfoIndex)) {
201 return $this->classScanner
->getConstant($constantNameOrInfoIndex);
204 foreach ($this->parentClassScanners
as $pClassScanner) {
205 if ($pClassScanner->hasConstant($constantNameOrInfoIndex)) {
206 return $pClassScanner->getConstant($constantNameOrInfoIndex);
210 throw new Exception\
InvalidArgumentException(sprintf(
211 'Constant %s not found in %s',
212 $constantNameOrInfoIndex,
213 $this->classScanner
->getName()
218 * Verify if class or parent class has constant
220 * @param string $name
223 public function hasConstant($name)
225 if ($this->classScanner
->hasConstant($name)) {
228 foreach ($this->parentClassScanners
as $pClassScanner) {
229 if ($pClassScanner->hasConstant($name)) {
238 * Return a list of property names
242 public function getPropertyNames()
244 $properties = $this->classScanner
->getPropertyNames();
245 foreach ($this->parentClassScanners
as $pClassScanner) {
246 $properties = array_merge($properties, $pClassScanner->getPropertyNames());
253 * @param bool $returnScannerProperty
256 public function getProperties($returnScannerProperty = false)
258 $properties = $this->classScanner
->getProperties($returnScannerProperty);
259 foreach ($this->parentClassScanners
as $pClassScanner) {
260 $properties = array_merge($properties, $pClassScanner->getProperties($returnScannerProperty));
267 * Return a single property by given name or index of info
269 * @param string|int $propertyNameOrInfoIndex
270 * @throws Exception\InvalidArgumentException
271 * @return bool|PropertyScanner
273 public function getProperty($propertyNameOrInfoIndex)
275 if ($this->classScanner
->hasProperty($propertyNameOrInfoIndex)) {
276 return $this->classScanner
->getProperty($propertyNameOrInfoIndex);
279 foreach ($this->parentClassScanners
as $pClassScanner) {
280 if ($pClassScanner->hasProperty($propertyNameOrInfoIndex)) {
281 return $pClassScanner->getProperty($propertyNameOrInfoIndex);
285 throw new Exception\
InvalidArgumentException(sprintf(
286 'Property %s not found in %s',
287 $propertyNameOrInfoIndex,
288 $this->classScanner
->getName()
293 * Verify if class or parent class has property
295 * @param string $name
298 public function hasProperty($name)
300 if ($this->classScanner
->hasProperty($name)) {
303 foreach ($this->parentClassScanners
as $pClassScanner) {
304 if ($pClassScanner->hasProperty($name)) {
315 public function getMethodNames()
317 $methods = $this->classScanner
->getMethodNames();
318 foreach ($this->parentClassScanners
as $pClassScanner) {
319 $methods = array_merge($methods, $pClassScanner->getMethodNames());
326 * @return MethodScanner[]
328 public function getMethods()
330 $methods = $this->classScanner
->getMethods();
331 foreach ($this->parentClassScanners
as $pClassScanner) {
332 $methods = array_merge($methods, $pClassScanner->getMethods());
339 * @param int|string $methodNameOrInfoIndex
340 * @return MethodScanner
341 * @throws Exception\InvalidArgumentException
343 public function getMethod($methodNameOrInfoIndex)
345 if ($this->classScanner
->hasMethod($methodNameOrInfoIndex)) {
346 return $this->classScanner
->getMethod($methodNameOrInfoIndex);
349 foreach ($this->parentClassScanners
as $pClassScanner) {
350 if ($pClassScanner->hasMethod($methodNameOrInfoIndex)) {
351 return $pClassScanner->getMethod($methodNameOrInfoIndex);
355 throw new Exception\
InvalidArgumentException(sprintf(
356 'Method %s not found in %s',
357 $methodNameOrInfoIndex,
358 $this->classScanner
->getName()
363 * Verify if class or parent class has method by given name
365 * @param string $name
368 public function hasMethod($name)
370 if ($this->classScanner
->hasMethod($name)) {
373 foreach ($this->parentClassScanners
as $pClassScanner) {
374 if ($pClassScanner->hasMethod($name)) {