PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/BackOffice/ClientGenerator/Util.php

https://github.com/silexlabs/amfphp-2.0
PHP | 166 lines | 73 code | 19 blank | 74 comment | 29 complexity | d1c3e0f4d0861ea6f62dd4b0bb5d338a MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of amfPHP
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the license that is bundled
  8. * with this package in the file license.txt.
  9. * @package Amfphp__BackOffice_ClientGenerator
  10. *
  11. */
  12. /**
  13. * common utilities for generators
  14. *
  15. * @author Ariel Sommeria-klein
  16. * @package Amfphp__BackOffice_ClientGenerator
  17. */
  18. class Amfphp_BackOffice_ClientGenerator_Util {
  19. /**
  20. * recursively copies one folder to another.
  21. * @param string $src
  22. * @param string $dst must not exist yet
  23. */
  24. public static function recurseCopy($src, $dst) {
  25. $dir = opendir($src);
  26. if(!file_exists($dst)){
  27. mkdir($dst);
  28. }
  29. while (false !== ( $file = readdir($dir))) {
  30. if (( $file != '.' ) && ( $file != '..' )) {
  31. if (is_dir($src . '/' . $file)) {
  32. self::recurseCopy($src . '/' . $file, $dst . '/' . $file);
  33. } else {
  34. copy($src . '/' . $file, $dst . '/' . $file);
  35. }
  36. }
  37. }
  38. closedir($dir);
  39. }
  40. /**
  41. * looks if the server has the necessary zip functions.
  42. * @return boolean
  43. */
  44. public static function serverCanZip(){
  45. return class_exists('ZipArchive');
  46. }
  47. /**
  48. * zip folder
  49. * @param type $sourcefolder
  50. * @param type $zipfilename
  51. * @param type $removeFromLocalName use to reduce paths inside zip
  52. */
  53. public static function zipFolder($sourcefolder, $zipfilename, $removeFromLocalName) {
  54. // instantate an iterator (before creating the zip archive, just
  55. // in case the zip file is created inside the source folder)
  56. // and traverse the directory to get the file list.
  57. $dirlist = new RecursiveDirectoryIterator($sourcefolder);
  58. $filelist = new RecursiveIteratorIterator($dirlist);
  59. // instantate object
  60. $zip = new ZipArchive();
  61. // create and open the archive
  62. if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) {
  63. throw new Exception("Could not open archive");
  64. }
  65. // add each file in the file list to the archive
  66. foreach ($filelist as $value) {
  67. if($value == '.'){
  68. continue;
  69. }
  70. if($value == '..'){
  71. continue;
  72. }
  73. $localName = str_replace($removeFromLocalName, '', $value);
  74. if(!$zip->addFile(realpath($value), $localName)){
  75. throw new Exception("ERROR: Could not add file: $key");
  76. }
  77. }
  78. // close the archive
  79. $zip->close();
  80. }
  81. // ------------ lixlpixel recursive PHP functions -------------
  82. // recursive_remove_directory( directory to delete, empty )
  83. // expects path to directory and optional TRUE / FALSE to empty
  84. // of course PHP has to have the rights to delete the directory
  85. // you specify and all files and folders inside the directory
  86. // ------------------------------------------------------------
  87. // to use this function to totally remove a directory, write:
  88. // recursive_remove_directory('path/to/directory/to/delete');
  89. // to use this function to empty a directory, write:
  90. // recursive_remove_directory('path/to/full_directory',TRUE);
  91. /**
  92. * unused for now.
  93. * @param string $directory
  94. * @param boolean $empty
  95. * @return boolean
  96. */
  97. public static function recursive_remove_directory($directory, $empty=FALSE) {
  98. // if the path has a slash at the end we remove it here
  99. if (substr($directory, -1) == '/') {
  100. $directory = substr($directory, 0, -1);
  101. }
  102. // if the path is not valid or is not a directory ...
  103. if (!file_exists($directory) || !is_dir($directory)) {
  104. // ... we return false and exit the function
  105. return FALSE;
  106. // ... if the path is not readable
  107. } elseif (!is_readable($directory)) {
  108. // ... we return false and exit the function
  109. return FALSE;
  110. // ... else if the path is readable
  111. } else {
  112. // we open the directory
  113. $handle = opendir($directory);
  114. // and scan through the items inside
  115. while (FALSE !== ($item = readdir($handle))) {
  116. // if the filepointer is not the current directory
  117. // or the parent directory
  118. if ($item != '.' && $item != '..') {
  119. // we build the new path to delete
  120. $path = $directory . '/' . $item;
  121. // if the new path is a directory
  122. if (is_dir($path)) {
  123. // we call this function with the new path
  124. self::recursive_remove_directory($path);
  125. // if the new path is a file
  126. } else {
  127. // we remove the file
  128. unlink($path);
  129. }
  130. }
  131. }
  132. // close the directory
  133. closedir($handle);
  134. // if the option to empty is not set to true
  135. if ($empty == FALSE) {
  136. // try to delete the now empty directory
  137. if (!rmdir($directory)) {
  138. // return false if not possible
  139. return FALSE;
  140. }
  141. }
  142. // return success
  143. return TRUE;
  144. }
  145. }
  146. }
  147. ?>