FileUtil.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. //-----------------------------------------------------------------------------
  3. // Copyright (c) 2012 GarageGames, LLC
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. class FileUtil
  24. {
  25. // Turn foo/../bar/../baz/ into baz/
  26. static function collapsePath( $p )
  27. {
  28. $p = explode( '/', $p );
  29. $o = array();
  30. for( $i = 0; $i < sizeof( $p ); $i++ )
  31. {
  32. // Skip meaningless . or empty terms.
  33. if( $p[ $i ] == '' || $p[ $i ] == '.' )
  34. continue;
  35. // Consider if we can pop something off the list.
  36. if( $p[ $i ] == '..' && $i > 0 && $o[ sizeof( $o ) - 1 ] != '..' )
  37. {
  38. array_pop( $o );
  39. continue;
  40. }
  41. array_push( $o, $p[ $i ] );
  42. }
  43. return implode( '/', $o );
  44. }
  45. // We apply some cleanup to the file list, specifically to remove
  46. // any groupings with no siblings.
  47. static function trimFileList( &$list )
  48. {
  49. // Consider each child...
  50. foreach( $list as $k => $v )
  51. {
  52. // Only consider folders...
  53. if( !is_array( $v ) )
  54. continue;
  55. // If child has only one child, then collapse it
  56. reset( $v );
  57. if( count( $v ) == 1 && is_array( current( $v ) ) )
  58. {
  59. //echo("Trying to collapse $k\n");
  60. $list[ $k ] = current( $v );
  61. // Reset our walk through the list...
  62. reset( $list );
  63. }
  64. }
  65. // Now recurse on all children.
  66. foreach( $list as $k => $v )
  67. {
  68. // Only consider folders...
  69. if( !is_array( $v ) )
  70. continue;
  71. FileUtil::trimFileList( $list[ $k ] );
  72. }
  73. }
  74. static function prepareOutputDir( $outdir )
  75. {
  76. if( !file_exists( $outdir ) )
  77. {
  78. echo( " Directory " . $outdir . " doesn't exist, attempting to create it.\n" );
  79. if( !mkdir_r( $outdir, 0777 ) )
  80. {
  81. echo( " Couldn't create directory.\n" );
  82. return false;
  83. }
  84. }
  85. if( !chdir( $outdir ) )
  86. {
  87. echo( "Couldn't change directory\n" );
  88. return false;;
  89. }
  90. return true;
  91. }
  92. static function normalizeSlashes( $path )
  93. {
  94. return str_replace( '\\', '/', $path );
  95. }
  96. static function isAbsolutePath( $path )
  97. {
  98. // This looks complex, but its really fairly simple.
  99. //
  100. // We're detecting existing absolute paths in the include
  101. // by converting it to absolute, normalizing the slashes,
  102. // and comparing the results.
  103. //
  104. // If we get an absolute path we just don't prepend the
  105. // project relative path part.
  106. //
  107. $orgPath = FileUtil::normalizeSlashes( $path );
  108. $absPath = FileUtil::normalizeSlashes( realpath( $path ) );
  109. return strcasecmp( $orgPath, $absPath ) == 0;
  110. }
  111. }
  112. ?>