1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<?php
/**
* Directory Helper.
*
* @package Silla.IO
* @subpackage Core\Helpers;
* @author Plamen Nikolov <plamen@athlonsofia.com>
* @copyright Copyright (c) 2015, Silla.io
* @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3.0 (GPLv3)
*/
namespace Core\Helpers;
use Core;
/**
* Contains helper methods concerned with directory manipulation.
*/
class Directory
{
/**
* Creates a directory and subdirectories, if not present.
*
* @param string $path Full or relative path to directory.
*
* @throws \DomainException If directory already exists.
* @uses File::getFullPath To format path to file.
*
* @return boolean If the directory was successfully created.
*/
public static function create($path)
{
$fullPath = File::getFullPath($path);
if (is_dir($fullPath)) {
throw new \DomainException('Directory exists.');
}
/* 0777 default mode with recursive creation */
$created = mkdir($fullPath, 0777, true);
return $created;
}
/**
* Deletes a directory and all its subdirectories and files.
*
* @param string $path Full or relative path to directory.
*
* @throws \InvalidArgumentException If path is empty or is not a directory.
* @uses File::getFullPath To format path to file.
* @uses self::delete To recursively delete subdirectories.
* @uses File::delete To delete files in directories.
*
* @return boolean Result of the operation.
*/
public static function delete($path)
{
$fullPath = File::getFullPath($path);
if (empty($path) || !is_dir($fullPath)) {
throw new \InvalidArgumentException('Path is empty or is not a directory.');
}
/* Open a directory handle. */
$handle = opendir($fullPath);
/* Read entries from the directory handle. */
while (($entry = readdir($handle)) !== false) {
/* Skip directory handles for current and previous directories. */
if ($entry == '.' || $entry == '..') {
continue;
}
/* Check whether the current entry is a directory and is not a symbolic link */
if (is_dir($fullPath . DIRECTORY_SEPARATOR . $entry) && !is_link($fullPath)) {
self::delete($fullPath . DIRECTORY_SEPARATOR . $entry);
} else {
File::delete($fullPath . DIRECTORY_SEPARATOR . $entry);
}
}
/* Close directory handle */
closedir($handle);
$deleted = rmdir($fullPath);
return $deleted;
}
/**
* Copies a directory to another destination, recursively.
*
* @param string $from Full or relative path to directory.
* @param string $to Full or relative path to destination directory.
*
* @uses File::getFullPath To format path to file.
* @uses self::create To create a directory.
* @uses self::copy To recursively copy subdirectories.
* @uses File::copy To copy files to the destination.
*
* @return boolean If the directory and its contents were copied successfully.
*/
public static function copy($from, $to)
{
$from = File::getFullPath($from);
/* Create destination directory. */
$copied = self::create($to);
/* Open a directory handle. */
$handle = opendir($from);
/* Read entries from the directory handle. */
while (($entry = readdir($handle)) !== false) {
/* Skip directory handles for current and previous directories. */
if ($entry == '.' || $entry == '..') {
continue;
}
/* Check whether the current entry is a directory and is not a symbolic link */
if (is_dir($from . DIRECTORY_SEPARATOR . $entry) && !is_link($from)) {
$copied = self::copy(
$from . DIRECTORY_SEPARATOR . $entry,
$to . DIRECTORY_SEPARATOR . $entry
);
} else {
$copied = File::copy(
$from . DIRECTORY_SEPARATOR . $entry,
$to . DIRECTORY_SEPARATOR . $entry
);
}
}
/* Close directory handle */
closedir($handle);
return $copied;
}
}