ClassFileLocator.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. $this->rewind();
  56. // Forward-compat with PHP 5.3
  57. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  58. if (!defined('T_NAMESPACE')) {
  59. define('T_NAMESPACE', 'namespace');
  60. }
  61. if (!defined('T_NS_SEPARATOR')) {
  62. define('T_NS_SEPARATOR', '\\');
  63. }
  64. }
  65. }
  66. /**
  67. * Filter for files containing PHP classes, interfaces, or abstracts
  68. *
  69. * @return bool
  70. */
  71. public function accept()
  72. {
  73. $file = $this->getInnerIterator()->current();
  74. // If we somehow have something other than an SplFileInfo object, just
  75. // return false
  76. if (!$file instanceof SplFileInfo) {
  77. return false;
  78. }
  79. // If we have a directory, it's not a file, so return false
  80. if (!$file->isFile()) {
  81. return false;
  82. }
  83. // If not a PHP file, skip
  84. if ($file->getBasename('.php') == $file->getBasename()) {
  85. return false;
  86. }
  87. $contents = file_get_contents($file->getRealPath());
  88. $tokens = token_get_all($contents);
  89. $count = count($tokens);
  90. $i = 0;
  91. while ($i < $count) {
  92. $token = $tokens[$i];
  93. if (!is_array($token)) {
  94. // single character token found; skip
  95. $i++;
  96. continue;
  97. }
  98. list($id, $content, $line) = $token;
  99. switch ($id) {
  100. case T_NAMESPACE:
  101. // Namespace found; grab it for later
  102. $namespace = '';
  103. $done = false;
  104. do {
  105. ++$i;
  106. $token = $tokens[$i];
  107. if (is_string($token)) {
  108. if (';' === $token) {
  109. $done = true;
  110. }
  111. continue;
  112. }
  113. list($type, $content, $line) = $token;
  114. switch ($type) {
  115. case T_STRING:
  116. case T_NS_SEPARATOR:
  117. $namespace .= $content;
  118. break;
  119. }
  120. } while (!$done && $i < $count);
  121. // Set the namespace of this file in the object
  122. $file->namespace = $namespace;
  123. break;
  124. case T_CLASS:
  125. case T_INTERFACE:
  126. // Abstract class, class, or interface found
  127. // Get the classname
  128. $class = '';
  129. do {
  130. ++$i;
  131. $token = $tokens[$i];
  132. if (is_string($token)) {
  133. continue;
  134. }
  135. list($type, $content, $line) = $token;
  136. switch ($type) {
  137. case T_STRING:
  138. $class = $content;
  139. break;
  140. }
  141. } while (empty($class) && $i < $count);
  142. // If a classname was found, set it in the object, and
  143. // return boolean true (found)
  144. if (!empty($class)) {
  145. $file->classname = $class;
  146. return true;
  147. }
  148. break;
  149. default:
  150. break;
  151. }
  152. ++$i;
  153. }
  154. // No class-type tokens found; return false
  155. return false;
  156. }
  157. }