No Description

Facade.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * php-file-iterator
  4. *
  5. * Copyright (c) 2009-2013, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package File
  38. * @author Sebastian Bergmann <sebastian@phpunit.de>
  39. * @copyright 2009-2013 Sebastian Bergmann <sebastian@phpunit.de>
  40. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  41. * @since File available since Release 1.3.0
  42. */
  43. /**
  44. * Façade implementation that uses File_Iterator_Factory to create a
  45. * File_Iterator that operates on an AppendIterator that contains an
  46. * RecursiveDirectoryIterator for each given path. The list of unique
  47. * files is returned as an array.
  48. *
  49. * @author Sebastian Bergmann <sebastian@phpunit.de>
  50. * @copyright 2009-2013 Sebastian Bergmann <sebastian@phpunit.de>
  51. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  52. * @version Release: @package_version@
  53. * @link http://github.com/sebastianbergmann/php-file-iterator/tree
  54. * @since Class available since Release 1.3.0
  55. */
  56. class File_Iterator_Facade
  57. {
  58. /**
  59. * @param array|string $paths
  60. * @param array|string $suffixes
  61. * @param array|string $prefixes
  62. * @param array $exclude
  63. * @param boolean $commonPath
  64. * @return array
  65. */
  66. public function getFilesAsArray($paths, $suffixes = '', $prefixes = '', array $exclude = array(), $commonPath = FALSE)
  67. {
  68. if (is_string($paths)) {
  69. $paths = array($paths);
  70. }
  71. $factory = new File_Iterator_Factory;
  72. $iterator = $factory->getFileIterator(
  73. $paths, $suffixes, $prefixes, $exclude
  74. );
  75. $files = array();
  76. foreach ($iterator as $file) {
  77. $file = $file->getRealPath();
  78. if ($file) {
  79. $files[] = $file;
  80. }
  81. }
  82. foreach ($paths as $path) {
  83. if (is_file($path)) {
  84. $files[] = realpath($path);
  85. }
  86. }
  87. $files = array_unique($files);
  88. sort($files);
  89. if ($commonPath) {
  90. return array(
  91. 'commonPath' => $this->getCommonPath($files),
  92. 'files' => $files
  93. );
  94. } else {
  95. return $files;
  96. }
  97. }
  98. /**
  99. * Returns the common path of a set of files.
  100. *
  101. * @param array $files
  102. * @return string
  103. */
  104. protected function getCommonPath(array $files)
  105. {
  106. $count = count($files);
  107. if ($count == 0) {
  108. return '';
  109. }
  110. if ($count == 1) {
  111. return dirname($files[0]) . DIRECTORY_SEPARATOR;
  112. }
  113. $_files = array();
  114. foreach ($files as $file) {
  115. $_files[] = $_fileParts = explode(DIRECTORY_SEPARATOR, $file);
  116. if (empty($_fileParts[0])) {
  117. $_fileParts[0] = DIRECTORY_SEPARATOR;
  118. }
  119. }
  120. $common = '';
  121. $done = FALSE;
  122. $j = 0;
  123. $count--;
  124. while (!$done) {
  125. for ($i = 0; $i < $count; $i++) {
  126. if ($_files[$i][$j] != $_files[$i+1][$j]) {
  127. $done = TRUE;
  128. break;
  129. }
  130. }
  131. if (!$done) {
  132. $common .= $_files[0][$j];
  133. if ($j > 0) {
  134. $common .= DIRECTORY_SEPARATOR;
  135. }
  136. }
  137. $j++;
  138. }
  139. return DIRECTORY_SEPARATOR . $common;
  140. }
  141. }