/libs/SEOframework/DirectoryList.php

https://github.com/quimateur/SIFO · PHP · 205 lines · 73 code · 21 blank · 111 comment · 6 complexity · 6867a605885dbfe95ec2785cd65b4c21 MD5 · raw file

  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * Copyright 2010 Albert Lombarte
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. */
  20. /**
  21. * Basic listings of directories using SPL.
  22. *
  23. * For complex and very specific needs you should use and extend your own iterators.
  24. * This class replaces "Dir" although there is no backwards compatibility.
  25. *
  26. * ------
  27. * Examples of usage:
  28. *
  29. * $dir = new DirectoryList();
  30. *
  31. * // Recursive list of all files including dirs:
  32. * $iterator = $dir->getRecursiveList( $path );
  33. *
  34. * // Recursive list of PHP and HTML files (hidding dirs)
  35. * $iterator = $dir->getRecursiveList( $path, false, array( 'php', 'html' ) );
  36. *
  37. * // List of immediate files and dirs (non recursive):
  38. * $iterator = $dir->getList( $path );
  39. *
  40. *
  41. * Then you can iterate the result $iterator in a foreach ( $path => $DirectoryIterator )
  42. * where $DirectoryIterator has many interesting methods such as:
  43. * ->getATime()
  44. * ->getBasename ( $ending_part_to_remove )
  45. * ->getCTime()
  46. * ->getExtension()
  47. * ->getFilename()
  48. * ->getGroup()
  49. * ->getInode()
  50. * ->getMTime()
  51. * ->getOwner()
  52. * ->getPath()
  53. * ->getPathname()
  54. * ->getPerms()
  55. * ->getSize()
  56. * ->getType()
  57. * ->isDir()
  58. * ->isDot()
  59. * ->isExecutable()
  60. * ->isFile()
  61. * ->isLink()
  62. * ->isReadable()
  63. * ->isWritable()
  64. * ->valid()
  65. *
  66. * @see http://www.php.net/manual/en/spl.iterators.php
  67. */
  68. class DirectoryList
  69. {
  70. /**
  71. * Returns the list of *immediate* files [isFile()] and folders [isDir()] on the given path.
  72. *
  73. * Non-recursive function.
  74. *
  75. * @param string $path Directory where you want to start parsing.
  76. * @return IteratorIterator
  77. */
  78. public function getList( $path, $valid_extensions = array() )
  79. {
  80. if ( !empty ( $valid_extensions ) )
  81. {
  82. return new FilterFilesByExtensionIterator(
  83. new IteratorIterator( new RecursiveDirectoryIterator( $path ) ),
  84. $valid_extensions );
  85. }
  86. else
  87. {
  88. // Using RecursiveDirectoryIterator because compared to DirectoryIterator this one removes dot folders:
  89. return new IteratorIterator( new RecursiveDirectoryIterator( $path ) );
  90. }
  91. }
  92. /**
  93. * Recursive listing of directory, files and dirs. If a set of extensions is
  94. * passed only matching files will be returned (do not pass the dot in the extensions)
  95. *
  96. * @param string $path Directory where you want to start parsing.
  97. * @param array $valid_extensions List of accepted extensions in the list, empty array for no filtering.
  98. * @return RecursiveIteratorIterator of RecursiveDirectoryIterator
  99. */
  100. public function getRecursiveList( $path, $accept_directories = true, $valid_extensions = array() )
  101. {
  102. $mode = $this->_getIteratorMode( $accept_directories );
  103. $dir_iterator = new RecursiveDirectoryIterator( $path );
  104. if ( !empty ( $valid_extensions ) )
  105. {
  106. return new FilterFilesByExtensionIterator(
  107. new RecursiveIteratorIterator(
  108. $dir_iterator,
  109. $mode, RecursiveIteratorIterator::CATCH_GET_CHILD
  110. ),
  111. $valid_extensions );
  112. }
  113. else
  114. {
  115. return new RecursiveIteratorIterator( $dir_iterator, $mode, RecursiveIteratorIterator::CATCH_GET_CHILD );
  116. }
  117. }
  118. /**
  119. * Returns a recursive list of files matching the given extension.
  120. *
  121. * @param string $path Filesystem path.
  122. *
  123. * @return FilterFilesIterator
  124. */
  125. public function getRecursiveListWithExtensions( $path, Array $valid_extensions, $accept_directories = false )
  126. {
  127. }
  128. private function _getIteratorMode( $accept_directories )
  129. {
  130. /**
  131. * Available modes for RecursiveIteratorIterator:
  132. *
  133. * RecursiveIteratorIterator::LEAVES_ONLY (don't show folders, default)
  134. * RecursiveIteratorIterator::SELF_FIRST (show parent folders before children)
  135. * RecursiveIteratorIterator::CHILD_FIRST (show parent folders after children, unusual)
  136. *
  137. * Flags (set in flags, not in mode):
  138. * RecursiveIteratorIterator::CATCH_GET_CHILD = Catches exceptions in getChildren() call
  139. */
  140. return ( $accept_directories ?
  141. RecursiveIteratorIterator::SELF_FIRST :
  142. RecursiveIteratorIterator::LEAVES_ONLY
  143. );
  144. }
  145. }
  146. /**
  147. * Filters out unwanted files from the iterator. Pass an array with the list of
  148. * extensions you want to accept.
  149. */
  150. class FilterFilesByExtensionIterator extends FilterIterator
  151. {
  152. protected $allowed_extensions;
  153. public function __construct( Iterator $iterator, Array $allowed_extensions )
  154. {
  155. $this->allowed_extensions = $allowed_extensions;
  156. parent::__construct( $iterator );
  157. }
  158. /**
  159. * Checks if current file matches the allowed extension or not. If directories
  160. * are passed will be accepted.
  161. *
  162. * @return boolean
  163. */
  164. public function accept()
  165. {
  166. // If directories are passed, we accept them. Is duty of the calling function:
  167. if ( $this->current()->isDir() )
  168. {
  169. return true;
  170. }
  171. if ( !empty( $this->allowed_extensions ) )
  172. {
  173. foreach ( $this->allowed_extensions as $ext )
  174. {
  175. if ( pathinfo( $this->getBaseName(), PATHINFO_EXTENSION ) == $ext )
  176. {
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. return true;
  183. }
  184. }