Overview

Namespaces

  • None
  • Peg
    • Cli
      • Commands
    • Config
    • Custom
      • Command
        • Action
          • Generate
          • Parse
      • CommandLine
      • Config
      • Localization
      • Utilities
    • Lib
      • Definitions
        • Element
      • Generator
      • Lexers
      • Plugins
      • Signals
        • Data
          • Definitions
          • Lexers
        • Type
  • PHP

Classes

  • FileSystem
  • Json
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: /**
  3:  * @author Jefferson González
  4:  * @license MIT
  5:  * @link http://github.com/peg-org/peg-custom Source code.
  6:  */
  7: 
  8: namespace Peg\Custom\Utilities;
  9: 
 10: /**
 11:  * Function to manage files and directories
 12:  */
 13: class FileSystem
 14: {
 15: 
 16:     // Disable constructor
 17:     private function __construct(){}
 18: 
 19:     /**
 20:      * Get all the files and directories available on a specified path.
 21:      * @param string $path
 22:      * @return array List of files found.
 23:      */
 24:     public static function GetDirContent($path)
 25:     {
 26:         $files = array();
 27:         $directory = opendir($path);
 28: 
 29:         while(($file = readdir($directory)) !== false)
 30:         {
 31:             $full_path = $path . "/" . $file;
 32: 
 33:             if(is_file($full_path))
 34:             {
 35:                 $files[] = $full_path;
 36:             }
 37:             elseif($file != "." && $file != ".." && is_dir($full_path))
 38:             {
 39:                 $files[] = $full_path;
 40:                 $files = array_merge($files, self::GetDirContent($full_path));
 41:             }
 42:         }
 43: 
 44:         closedir($directory);
 45: 
 46:         return $files;
 47:     }
 48: 
 49:     /**
 50:      * Same as php mkdir() but adds Operating system check and replaces
 51:      * every / by \ on windows.
 52:      * @param string $directory The directory to create.
 53:      * @param integer $mode the permissions granted to the directory.
 54:      * @param bool $recursive Recurse in to the path creating neccesary directories.
 55:      * @return bool true on success false on fail.
 56:      */
 57:     public static function MakeDir($directory, $mode = 0755, $recursive = false)
 58:     {
 59:         if("" . strpos(PHP_OS, "WIN") . "" != "")
 60:         {
 61:             $directory = str_replace("/", "\\", $directory);
 62:         }
 63: 
 64:         return mkdir($directory, $mode, $recursive);
 65:     }
 66: 
 67:     /**
 68:      * Copy a directory and its content to another directory replacing any file
 69:      * on the target directory if already exist.
 70:      * @param string $source The directory to copy.
 71:      * @param string $target The copy destination.
 72:      * @return bool true on success or false on fail.
 73:      */
 74:     public static function RecursiveCopyDir($source, $target)
 75:     {
 76:         $source_dir = opendir($source);
 77: 
 78:         //Check if source directory exists
 79:         if(!$source_dir)
 80:         {
 81:             return false;
 82:         }
 83: 
 84:         //Create target directory in case it doesnt exist
 85:         if(!file_exists($target))
 86:         {
 87:             self::MakeDir($target, 0755, true);
 88:         }
 89: 
 90:         while(($item = readdir($source_dir)) !== false)
 91:         {
 92:             $source_full_path = $source . "/" . $item;
 93:             $target_full_path = $target . "/" . $item;
 94: 
 95:             if($item != "." && $item != "..")
 96:             {
 97:                 //copy source files
 98:                 if(is_file($source_full_path))
 99:                 {
100:                     if(!copy($source_full_path, $target_full_path))
101:                     {
102:                         return false;
103:                     }
104:                 }
105:                 else if(is_dir($source_full_path))
106:                 {
107:                     self::RecursiveCopyDir($source_full_path, $target_full_path);
108:                 }
109:             }
110:         }
111: 
112:         closedir($source_dir);
113: 
114:         return true;
115:     }
116: 
117:     /**
118:      * Remove a directory that is not empty by deleting all its content.
119:      * @param string $directory The directory to delete with all its content.
120:      * @param string $empty Removes all directory contents keeping only itself.
121:      * @return bool True on success or false.
122:      */
123:     public static function RecursiveRemoveDir($directory, $empty = false)
124:     {
125:         // if the path has a slash at the end we remove it here
126:         if(substr($directory, -1) == '/')
127:         {
128:             $directory = substr($directory, 0, -1);
129:         }
130: 
131:         // if the path is not valid or is not a directory ...
132:         if(!file_exists($directory) || !is_dir($directory))
133:         {
134:             return false;
135: 
136:             // ... if the path is not readable
137:         }
138:         elseif(!is_readable($directory))
139:         {
140:             return false;
141:         }
142:         else
143:         {
144:             $handle = opendir($directory);
145: 
146:             while(false !== ($item = readdir($handle)))
147:             {
148:                 if($item != '.' && $item != '..')
149:                 {
150:                     // we build the new path to delete
151:                     $path = $directory . '/' . $item;
152: 
153:                     // if the new path is a directory
154:                     if(is_dir($path))
155:                     {
156:                         self::RecursiveRemoveDir($path);
157: 
158:                         // if the new path is a file
159:                     }
160:                     else
161:                     {
162:                         if(!unlink($path))
163:                         {
164:                             return false;
165:                         }
166:                     }
167:                 }
168:             }
169: 
170:             closedir($handle);
171: 
172:             if($empty == false)
173:             {
174:                 if(!rmdir($directory))
175:                 {
176:                     return false;
177:                 }
178:             }
179: 
180:             return true;
181:         }
182:     }
183:     
184:     /**
185:      * Only saves content to a file if the new content is not the same
186:      * as the original. This is helpful to prevent an unneccesary timestamp 
187:      * modification which is used by compilers to decide wether the file
188:      * needs recompilation.
189:      * @param string $file Path to file.
190:      * @param string $content New content of file.
191:      * @return bool True on success or false if file content is the same.
192:      */
193:     function WriteFileIfDifferent($file, &$contents)
194:     {
195:         $actual_file_content = "";
196: 
197:         if(file_exists($file))
198:             $actual_file_content = file_get_contents($file);
199: 
200:         if(crc32($actual_file_content) != crc32($contents))
201:         {
202:             file_put_contents($file, $contents);
203: 
204:             return true;
205:         }
206: 
207:         print false;
208:     }
209: 
210: }
PEG Api API documentation generated by ApiGen 2.8.0