ClassFileLocator.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. * @return void
  37. */
  38. public function __construct($dirOrIterator = '.')
  39. {
  40. if (is_string($dirOrIterator)) {
  41. if (!is_dir($dirOrIterator)) {
  42. throw new InvalidArgumentException('Expected a valid directory name');
  43. }
  44. $dirOrIterator = new RecursiveDirectoryIterator($dirOrIterator);
  45. }
  46. if (!$dirOrIterator instanceof DirectoryIterator) {
  47. throw new InvalidArgumentException('Expected a DirectoryIterator');
  48. }
  49. if ($dirOrIterator instanceof RecursiveIterator) {
  50. $iterator = new RecursiveIteratorIterator($dirOrIterator);
  51. } else {
  52. $iterator = $dirOrIterator;
  53. }
  54. parent::__construct($iterator);
  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. $i = 0;
  90. while ($i < $count) {
  91. $token = $tokens[$i];
  92. if (!is_array($token)) {
  93. // single character token found; skip
  94. $i++;
  95. continue;
  96. }
  97. list($id, $content, $line) = $token;
  98. switch ($id) {
  99. case T_NAMESPACE:
  100. // Namespace found; grab it for later
  101. $namespace = '';
  102. $done = false;
  103. do {
  104. ++$i;
  105. $token = $tokens[$i];
  106. if (is_string($token)) {
  107. if (';' === $token) {
  108. $done = true;
  109. }
  110. continue;
  111. }
  112. list($type, $content, $line) = $token;
  113. switch ($type) {
  114. case T_STRING:
  115. case T_NS_SEPARATOR:
  116. $namespace .= $content;
  117. break;
  118. }
  119. } while (!$done && $i < $count);
  120. // Set the namespace of this file in the object
  121. $file->namespace = $namespace;
  122. break;
  123. case T_CLASS:
  124. case T_INTERFACE:
  125. // Abstract class, class, or interface found
  126. // Get the classname
  127. $class = '';
  128. do {
  129. ++$i;
  130. $token = $tokens[$i];
  131. if (is_string($token)) {
  132. continue;
  133. }
  134. list($type, $content, $line) = $token;
  135. switch ($type) {
  136. case T_STRING:
  137. $class = $content;
  138. break;
  139. }
  140. } while (empty($class) && $i < $count);
  141. // If a classname was found, set it in the object, and
  142. // return boolean true (found)
  143. if (!empty($class)) {
  144. $file->classname = $class;
  145. return true;
  146. }
  147. break;
  148. default:
  149. break;
  150. }
  151. ++$i;
  152. }
  153. // No class-type tokens found; return false
  154. return false;
  155. }
  156. }