Facade.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of the File_Iterator package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Façade implementation that uses File_Iterator_Factory to create a
  12. * File_Iterator that operates on an AppendIterator that contains an
  13. * RecursiveDirectoryIterator for each given path. The list of unique
  14. * files is returned as an array.
  15. *
  16. * @since Class available since Release 1.3.0
  17. */
  18. class File_Iterator_Facade
  19. {
  20. /**
  21. * @param array|string $paths
  22. * @param array|string $suffixes
  23. * @param array|string $prefixes
  24. * @param array $exclude
  25. * @param bool $commonPath
  26. * @return array
  27. */
  28. public function getFilesAsArray($paths, $suffixes = '', $prefixes = '', array $exclude = array(), $commonPath = FALSE)
  29. {
  30. if (is_string($paths)) {
  31. $paths = array($paths);
  32. }
  33. $factory = new File_Iterator_Factory;
  34. $iterator = $factory->getFileIterator(
  35. $paths, $suffixes, $prefixes, $exclude
  36. );
  37. $files = array();
  38. foreach ($iterator as $file) {
  39. $file = $file->getRealPath();
  40. if ($file) {
  41. $files[] = $file;
  42. }
  43. }
  44. foreach ($paths as $path) {
  45. if (is_file($path)) {
  46. $files[] = realpath($path);
  47. }
  48. }
  49. $files = array_unique($files);
  50. sort($files);
  51. if ($commonPath) {
  52. return array(
  53. 'commonPath' => $this->getCommonPath($files),
  54. 'files' => $files
  55. );
  56. } else {
  57. return $files;
  58. }
  59. }
  60. /**
  61. * Returns the common path of a set of files.
  62. *
  63. * @param array $files
  64. * @return string
  65. */
  66. protected function getCommonPath(array $files)
  67. {
  68. $count = count($files);
  69. if ($count == 0) {
  70. return '';
  71. }
  72. if ($count == 1) {
  73. return dirname($files[0]) . DIRECTORY_SEPARATOR;
  74. }
  75. $_files = array();
  76. foreach ($files as $file) {
  77. $_files[] = $_fileParts = explode(DIRECTORY_SEPARATOR, $file);
  78. if (empty($_fileParts[0])) {
  79. $_fileParts[0] = DIRECTORY_SEPARATOR;
  80. }
  81. }
  82. $common = '';
  83. $done = FALSE;
  84. $j = 0;
  85. $count--;
  86. while (!$done) {
  87. for ($i = 0; $i < $count; $i++) {
  88. if ($_files[$i][$j] != $_files[$i+1][$j]) {
  89. $done = TRUE;
  90. break;
  91. }
  92. }
  93. if (!$done) {
  94. $common .= $_files[0][$j];
  95. if ($j > 0) {
  96. $common .= DIRECTORY_SEPARATOR;
  97. }
  98. }
  99. $j++;
  100. }
  101. return DIRECTORY_SEPARATOR . $common;
  102. }
  103. }