ClassFileLocator.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_File
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Locate files containing PHP classes, interfaces, or abstracts
  22. *
  23. * @package Zend_File
  24. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license New BSD {@link http://framework.zend.com/license/new-bsd}
  26. */
  27. class Zend_File_ClassFileLocator extends FilterIterator
  28. {
  29. /**
  30. * Create an instance of the locator iterator
  31. *
  32. * Expects either a directory, or a DirectoryIterator (or its recursive variant)
  33. * instance.
  34. *
  35. * @param string|DirectoryIterator $dirOrIterator
  36. */
  37. public function __construct($dirOrIterator = '.')
  38. {
  39. if (is_string($dirOrIterator)) {
  40. if (!is_dir($dirOrIterator)) {
  41. throw new InvalidArgumentException('Expected a valid directory name');
  42. }
  43. $dirOrIterator = new RecursiveDirectoryIterator($dirOrIterator);
  44. }
  45. if (!$dirOrIterator instanceof DirectoryIterator) {
  46. throw new InvalidArgumentException('Expected a DirectoryIterator');
  47. }
  48. if ($dirOrIterator instanceof RecursiveIterator) {
  49. $iterator = new RecursiveIteratorIterator($dirOrIterator);
  50. } else {
  51. $iterator = $dirOrIterator;
  52. }
  53. parent::__construct($iterator);
  54. $this->setInfoClass('Zend_File_PhpClassFile');
  55. // Forward-compat with PHP 5.3
  56. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  57. if (!defined('T_NAMESPACE')) {
  58. define('T_NAMESPACE', 'namespace');
  59. }
  60. if (!defined('T_NS_SEPARATOR')) {
  61. define('T_NS_SEPARATOR', '\\');
  62. }
  63. }
  64. }
  65. /**
  66. * Filter for files containing PHP classes, interfaces, or abstracts
  67. *
  68. * @return bool
  69. */
  70. public function accept()
  71. {
  72. $file = $this->getInnerIterator()->current();
  73. // If we somehow have something other than an SplFileInfo object, just
  74. // return false
  75. if (!$file instanceof SplFileInfo) {
  76. return false;
  77. }
  78. // If we have a directory, it's not a file, so return false
  79. if (!$file->isFile()) {
  80. return false;
  81. }
  82. // If not a PHP file, skip
  83. if ($file->getBasename('.php') == $file->getBasename()) {
  84. return false;
  85. }
  86. $contents = file_get_contents($file->getRealPath());
  87. $tokens = token_get_all($contents);
  88. $count = count($tokens);
  89. $t_trait = defined('T_TRAIT') ? T_TRAIT : -1; // For preserve PHP 5.3 compatibility
  90. for ($i = 0; $i < $count; $i++) {
  91. $token = $tokens[$i];
  92. if (!is_array($token)) {
  93. // single character token found; skip
  94. $i++;
  95. continue;
  96. }
  97. switch ($token[0]) {
  98. case T_NAMESPACE:
  99. // Namespace found; grab it for later
  100. $namespace = '';
  101. for ($i++; $i < $count; $i++) {
  102. $token = $tokens[$i];
  103. if (is_string($token)) {
  104. if (';' === $token) {
  105. $saveNamespace = false;
  106. break;
  107. }
  108. if ('{' === $token) {
  109. $saveNamespace = true;
  110. break;
  111. }
  112. continue;
  113. }
  114. list($type, $content, $line) = $token;
  115. switch ($type) {
  116. case T_STRING:
  117. case T_NS_SEPARATOR:
  118. $namespace .= $content;
  119. break;
  120. }
  121. }
  122. if ($saveNamespace) {
  123. $savedNamespace = $namespace;
  124. }
  125. break;
  126. case $t_trait:
  127. case T_CLASS:
  128. case T_INTERFACE:
  129. // Abstract class, class, interface or trait found
  130. // Get the classname
  131. for ($i++; $i < $count; $i++) {
  132. $token = $tokens[$i];
  133. if (is_string($token)) {
  134. continue;
  135. }
  136. list($type, $content, $line) = $token;
  137. if (T_STRING == $type) {
  138. // If a classname was found, set it in the object, and
  139. // return boolean true (found)
  140. if (!isset($namespace) || null === $namespace) {
  141. if (isset($saveNamespace) && $saveNamespace) {
  142. $namespace = $savedNamespace;
  143. } else {
  144. $namespace = null;
  145. }
  146. }
  147. $class = (null === $namespace) ? $content : $namespace . '\\' . $content;
  148. $file->addClass($class);
  149. $namespace = null;
  150. break;
  151. }
  152. }
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. $classes = $file->getClasses();
  159. if (!empty($classes)) {
  160. return true;
  161. }
  162. // No class-type tokens found; return false
  163. return false;
  164. }
  165. }