Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
FileSystem.inc
1 <?php
22 {
23 
29  const HASH_DIR_FILE_COUNT = 10000;
30 
36  const FILE_SIZE_BYTE = 'B';
37  const FILE_SIZE_KILOBYTE = 'kB';
38  const FILE_SIZE_MEGABYTE = 'MB';
39  const FILE_SIZE_GIGABYTE = 'GB';
40 
41 
60  public static function clearDirectory($path, $deleteRoot=FALSE, array $omitFiles=array())
61  {
62  if (file_exists($path) === FALSE) {
63  return FALSE;
64  }
65 
66  $dir = new DirectoryIterator($path);
67  while ($dir->valid() === TRUE) {
68  if ($dir->isDot() === TRUE) {
69  // This is '.' or '..'. Not interested.
70  $dir->next();
71  continue;
72  }
73 
74  if ($dir->isDir() === TRUE) {
75  // If $deleteRoot is FALSE, then we don't want to delete
76  // .svn entries, as with directories like the oven directory,
77  // this will break updates on parent directories.
78  if ($deleteRoot === FALSE) {
79  if ($dir->getFileName() === '.svn') {
80  $dir->next();
81  continue;
82  }
83  }
84 
85  // Recursively call this method to clear nested directories.
86  if (self::clearDirectory($dir->getPathName(), FALSE) === FALSE) {
87  return FALSE;
88  }
89 
90  // Directory is now clear, so we can remove the directory.
91  if (rmdir($dir->getPathName()) === FALSE) {
92  // Unable to delete the directory. Throw Exception, as the
93  // operation will ultimately fail.
94  $error = 'Unable to delete directory: '.$dir->getPathName;
95  throw new RuntimeException($error);
96  }
97 
98  $dir->next();
99  continue;
100  }//end if $dir->isDir()
101 
102  // We have a file or symbolic link, we need to unlink it.
103  if (in_array($dir->getFileName(), $omitFiles) === FALSE) {
104  if (unlink($dir->getPathName()) === FALSE) {
105  // Problem deleting the file, throw exception.
106  $error = 'Unable to delete file: '.$dir->getPathName;
107  throw new RuntimeException($error);
108  }
109  }
110 
111  $dir->next();
112  }//end while
113 
114  if ($deleteRoot === TRUE) {
115  if (file_exists($path) === TRUE) {
116  if (rmdir($path) === FALSE) {
117  $error = 'Unable to delete directory: '.$dir->getPathName;
118  throw new RuntimeException($error);
119  }
120  }
121  }
122 
123  return TRUE;
124 
125  }//end clearDirectory()
126 
127 
144  public static function listDirectory($path, array $extensions=array(), $nested=TRUE, $fullPath=TRUE)
145  {
146  $files = array();
147  if (file_exists($path) === FALSE) {
148  return $files;
149  }
150 
151  $dir = new DirectoryIterator($path);
152  while ($dir->valid() === TRUE) {
153  if ($dir->isDot() === TRUE) {
154  // This is '.' or '..'. Not interested.
155  $dir->next();
156  continue;
157  }
158 
159  if ($dir->isDir() === TRUE && $nested === TRUE) {
160  if ($dir->getFileName() === '.svn') {
161  $dir->next();
162  continue;
163  }
164 
165  // Recursively call this method to list nested directories.
166  $nestedDirectory = self::listDirectory($dir->getPathName(), $extensions, $nested, $fullPath);
167  // Merge the files from the subdirectory with ours.
168  $files = array_merge($files, $nestedDirectory);
169  $dir->next();
170  continue;
171  }
172 
173  // We have a file, so we need to determine if it fits the specified
174  // criteria.
175  if ($dir->isFile() === TRUE) {
176 
177  $addFile = FALSE;
178  $filename = $dir->getFilename();
179 
180  // Skip hidden files.
181  if ($filename{0} !== '.') {
182 
183  if (empty($extensions) === TRUE) {
184  // No extensions, every file should be added.
185  $addFile = TRUE;
186  } else {
187  // Determine if the file has the right extension.
188  $fileName = $dir->getFileName();
189  $dotPos = strrpos($fileName, '.');
190  $fileExt = substr($fileName, $dotPos);
191  if (in_array($fileExt, $extensions) === TRUE) {
192  // Correct extension. Set our flag so it's added below.
193  $addFile = TRUE;
194  }
195  }
196 
197  if ($addFile === TRUE) {
198  // File fits our criteria, now we only need to work out
199  // which details to add to our array.
200  if ($fullPath === TRUE) {
201  $files[] = $dir->getPathName();
202  } else {
203  $files[] = $dir->getFileName();
204  }
205  }
206  }//end if
207 
208  }//end if isFile()
209 
210  $dir->next();
211  }//end while valid()
212 
213  return $files;
214 
215  }//end listDirectory()
216 
217 
230  public static function findDirectories($path, $name)
231  {
232  $dir = new RecursiveDirectoryIterator($path);
233 
234  $paths = array();
235  foreach ($dir as $file) {
236  if ($file->isDir() === TRUE) {
237  if ($file->getFilename() === $name) {
238  $paths[] = $file->getPathname();
239  } else {
240  $paths = array_merge($paths, self::findDirectories($file->getPathName(), $name));
241  }
242  }
243  }
244 
245  return $paths;
246 
247  }//end findDirectories()
248 
249 
259  public static function getHashDir($id)
260  {
261  $id = (int) $id;
262  if ($id <= 0) {
263  throw new InvalidArgumentException('$id must be positive');
264  }
265 
266  $endId = (ceil(($id / self::HASH_DIR_FILE_COUNT)) * self::HASH_DIR_FILE_COUNT);
267  $startId = ($endId - self::HASH_DIR_FILE_COUNT + 1);
268 
269  return $startId.'-'.$endId;
270 
271  }//end getHashDir()
272 
273 
283  public static function readableSize($path, $unit=NULL)
284  {
285  $size = @filesize($path);
286  if ($size === FALSE) {
287  return FALSE;
288  }
289 
290  $units = array(
291  self::FILE_SIZE_BYTE,
292  self::FILE_SIZE_KILOBYTE,
293  self::FILE_SIZE_MEGABYTE,
294  self::FILE_SIZE_GIGABYTE,
295  );
296  $maxUnit = (count($units) - 1);
297 
298  // Accept units as a parameter, maybe...
299  $unit = array_search($unit, $units);
300  if ($unit === NULL || $unit === FALSE) {
301  $unit = $maxUnit;
302  }
303 
304  $factor = 0;
305  while ($unit !== $factor && $size >= 1000 && $factor < $maxUnit) {
306  $size = ($size / 1000);
307  $factor++;
308  }
309 
310  return sprintf('%01.2f%s', $size, $units[$factor]);
311 
312  }//end readableSize()
313 
314 
323  public static function getBaseName($path)
324  {
325  $fileName = preg_replace( '/^.+[\\\\\\/]/', '', $path);
326  return $fileName;
327 
328  }//end getBaseName()
329 
330 
331 }//end class
332 
333 ?>