/Zend/Tool/Framework/Loader/IncludePathLoader/IncludePathLoader.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 144 lines · 66 code · 22 blank · 56 comment · 10 complexity · 42ee9b0fcef8cec8a7bf269086c2ffbb MD5 · raw file

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