IncludePathLoader.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_Tool
  17. * @subpackage Framework
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Tool_Framework_Loader_Abstract
  24. */
  25. require_once 'Zend/Tool/Framework/Loader/Abstract.php';
  26. /**
  27. * @see Zend_Tool_Framework_Loader_IncludePathLoader_RecursiveFilterIterator
  28. */
  29. require_once 'Zend/Tool/Framework/Loader/IncludePathLoader/RecursiveFilterIterator.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Tool
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Tool_Framework_Loader_IncludePathLoader extends Zend_Tool_Framework_Loader_Abstract
  37. {
  38. /**
  39. * _getFiles()
  40. *
  41. * @return array Array of files to load
  42. */
  43. protected function _getFiles()
  44. {
  45. $paths = explode(PATH_SEPARATOR, get_include_path());
  46. // used for checking similarly named files
  47. $relativeItems = array();
  48. $files = array();
  49. $isZendTraversed = false;
  50. foreach ($paths as $path) {
  51. // default patterns to use
  52. $filterDenyDirectoryPattern = '.*(/|\\\\).svn';
  53. $filterAcceptFilePattern = '.*(?:Manifest|Provider)\.php$';
  54. if (!file_exists($path) || $path[0] == '.') {
  55. continue;
  56. }
  57. $realIncludePath = realpath($path);
  58. // ensure that we only traverse a single version of Zend Framework on all include paths
  59. if (file_exists($realIncludePath . '/Zend/Tool/Framework/Loader/IncludePathLoader.php')) {
  60. if ($isZendTraversed === false) {
  61. $isZendTraversed = true;
  62. } else {
  63. // use the deny directory pattern that includes the path to 'Zend', it will not be accepted
  64. $filterDenyDirectoryPattern = '.*((/|\\\\).svn|' . preg_quote($realIncludePath . DIRECTORY_SEPARATOR) . 'Zend)';
  65. }
  66. }
  67. // create recursive directory iterator
  68. $rdi = new RecursiveDirectoryIterator($path);
  69. // pass in the RecursiveDirectoryIterator & the patterns
  70. $filter = new Zend_Tool_Framework_Loader_IncludePathLoader_RecursiveFilterIterator(
  71. $rdi,
  72. $filterDenyDirectoryPattern,
  73. $filterAcceptFilePattern
  74. );
  75. // build the rii with the filter
  76. $iterator = new RecursiveIteratorIterator($filter);
  77. // iterate over the accepted items
  78. foreach ($iterator as $item) {
  79. $file = (string)$item;
  80. if($this->_fileIsBlacklisted($file)) {
  81. continue;
  82. }
  83. // ensure that the same named file from separate include_paths is not loaded
  84. $relativeItem = preg_replace('#^' . preg_quote($realIncludePath . DIRECTORY_SEPARATOR, '#') . '#', '', $item->getRealPath());
  85. // no links allowed here for now
  86. if ($item->isLink()) {
  87. continue;
  88. }
  89. // no items that are relavitely the same are allowed
  90. if (in_array($relativeItem, $relativeItems)) {
  91. continue;
  92. }
  93. $relativeItems[] = $relativeItem;
  94. $files[] = $item->getRealPath();
  95. }
  96. }
  97. return $files;
  98. }
  99. /**
  100. *
  101. * @param string $file
  102. * @return bool
  103. */
  104. protected function _fileIsBlacklisted($file)
  105. {
  106. $blacklist = array(
  107. "PHPUnit".DIRECTORY_SEPARATOR."Framework",
  108. "Zend".DIRECTORY_SEPARATOR."OpenId".DIRECTORY_SEPARATOR."Provider"
  109. );
  110. foreach($blacklist AS $blacklitedPattern) {
  111. if(strpos($file, $blacklitedPattern) !== false) {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. }