PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Sifo/Dir.php

http://github.com/alombarte/SIFO
PHP | 140 lines | 72 code | 17 blank | 51 comment | 13 complexity | d5d30add7d52f190cc0a35375d0a2b0a MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, GPL-2.0, BSD-3-Clause
  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. namespace Sifo;
  21. /**
  22. * Old-school Directory class.
  23. *
  24. * TODO: Migrate to SPL when PHP 5.3 is found easily on shared hostings.
  25. * Directory interaction for those not being able to use RecursiveDirectoryIterator
  26. * @deprecated Use Directory.php for new implementations.
  27. */
  28. class Dir
  29. {
  30. /**
  31. * List of files and dires skipped in scans.
  32. *
  33. * @var array
  34. */
  35. protected $ignored_files = array( '.', '..', '.svn', '.git', '.DS_Store', 'Thumbs.db', '_smarty' );
  36. /**
  37. * Returns an array containing a list of files found recursively for a given path.
  38. *
  39. * Every item in the array has a subarray containing the following keys:
  40. * "name" => Filename or Dirname
  41. * "relative" => Path relative to the starting point
  42. * "absolute" => Absolute path
  43. * "folder" => Parent folder
  44. *
  45. * @param string $base_path Path to starting directory.
  46. * @param string $relative_path Relative path added to base_path. If you have /path/to/dir and specify "dir" as relative_path then will list files in /path/to as dir/file1, dir/file2 ...
  47. */
  48. public function getFileListRecursive( $base_path, $relative_path = "" )
  49. {
  50. // If base path ends in / remove it.
  51. if ( substr( $base_path, -1, 1 ) == "/" )
  52. {
  53. $base_path = substr( $base_path, 0, -1 );
  54. }
  55. // If relative path starts in / remove it.
  56. if ( substr( $relative_path, 0, 1 ) == "/" )
  57. {
  58. $relative_path = substr( $relative_path, 1 );
  59. }
  60. $path = $base_path . "/" . $relative_path;
  61. if ( !is_dir( $path ) )
  62. {
  63. return false;
  64. }
  65. $list = array();
  66. $directory = opendir( "$path" );
  67. while ( $file = readdir( $directory ) )
  68. {
  69. if ( !in_array( $file, $this->ignored_files ) )
  70. {
  71. $f = $path . "/" . $file;
  72. $f = preg_replace( '/(\/){2,}/', '/', $f ); // Replace double slashes.
  73. if ( is_file( $f ) )
  74. {
  75. $list[] = array( "filename" => $file, "relative" => $relative_path . "/$file", "absolute" => $f, "folder"=> $relative_path );
  76. }
  77. if ( is_dir( $f ) ) // Ignore _smarty dir
  78. {
  79. $list = array_merge( $list, $this->getFileListRecursive( $base_path, $relative_path . "/$file" ) ); // Recursive call.
  80. }
  81. }
  82. }
  83. closedir( $directory );
  84. sort( $list );
  85. return $list ;
  86. }
  87. /**
  88. * Get subdirs
  89. *
  90. * @param string $path
  91. * @param string $relative_path
  92. * @return array
  93. */
  94. public function getDirs( $path )
  95. {
  96. if ( !is_dir( $path ) )
  97. {
  98. return false;
  99. }
  100. $list = array();
  101. $directory = opendir( "$path" );
  102. while ( $file = readdir( $directory ) )
  103. {
  104. if ( !in_array( $file, $this->ignored_files ) )
  105. {
  106. $f = $path . "/" . $file;
  107. $f = preg_replace( '/(\/){2,}/', '/', $f ); // Replace double slashes.
  108. if ( is_dir( $f ) )
  109. {
  110. $list[] = $file;
  111. }
  112. }
  113. }
  114. closedir( $directory );
  115. sort( $list );
  116. return $list;
  117. }
  118. public function setIgnore( Array $ignored_files )
  119. {
  120. $this->ignored_files = $ignored_files;
  121. }
  122. }
  123. ?>