o2system /
filesystem
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Filesystem; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use O2System\Spl\Info\SplDirectoryInfo; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Class Directory |
||
| 22 | * |
||
| 23 | * @package O2System\Filesystem |
||
| 24 | */ |
||
| 25 | class Directory extends SplDirectoryInfo |
||
| 26 | {
|
||
| 27 | /** |
||
| 28 | * Directory::make |
||
| 29 | * |
||
| 30 | * Make a directory. |
||
| 31 | * |
||
| 32 | * @param string $dir Directory real path. |
||
| 33 | * @param int $mode Directory mode. |
||
| 34 | * @param bool $recursive Make directory creation recursive. |
||
| 35 | * |
||
| 36 | * @return bool|\O2System\Spl\Info\SplDirectoryInfo |
||
| 37 | */ |
||
| 38 | public function make($dir = null, $mode = 0777, $recursive = true) |
||
| 39 | {
|
||
| 40 | $dir = is_null($dir) ? $this->getPathName() : $dir; |
||
| 41 | |||
| 42 | if (is_dir($dir)) {
|
||
| 43 | return new SplDirectoryInfo($dir); |
||
| 44 | } elseif (null !== ($pathName = $this->getPathName())) {
|
||
| 45 | if (mkdir( |
||
| 46 | $makeDirectory = $pathName . DIRECTORY_SEPARATOR . str_replace( |
||
| 47 | ['\\', '/'], |
||
| 48 | DIRECTORY_SEPARATOR, |
||
| 49 | $dir |
||
| 50 | ), |
||
| 51 | $mode, |
||
| 52 | $recursive |
||
| 53 | )) {
|
||
| 54 | return new SplDirectoryInfo($makeDirectory); |
||
| 55 | } |
||
| 56 | } elseif (mkdir( |
||
| 57 | $makeDirectory = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $dir), |
||
| 58 | $mode, |
||
| 59 | $recursive |
||
| 60 | )) {
|
||
| 61 | return new SplDirectoryInfo($makeDirectory); |
||
| 62 | } |
||
| 63 | |||
| 64 | return false; |
||
| 65 | } |
||
| 66 | |||
| 67 | // ------------------------------------------------------------------------ |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Directory::delete |
||
| 71 | * |
||
| 72 | * Remove a directory. |
||
| 73 | * |
||
| 74 | * @param bool $fileOnly Remove files only and keep the directory structure. |
||
| 75 | * |
||
| 76 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 77 | */ |
||
| 78 | public function delete($fileOnly = false) |
||
| 79 | {
|
||
| 80 | return $this->recursiveDelete($this->getRealPath(), $fileOnly); |
||
| 81 | } |
||
| 82 | |||
| 83 | // ------------------------------------------------------------------------ |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Directory::recursiveDelete |
||
| 87 | * |
||
| 88 | * @param string $dir Directory path. |
||
| 89 | * @param bool $fileOnly Remove files only and keep the directory structure. |
||
| 90 | * |
||
| 91 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 92 | */ |
||
| 93 | private function recursiveDelete($dir, $fileOnly = false) |
||
| 94 | {
|
||
| 95 | $dir = realpath($dir); |
||
| 96 | |||
| 97 | if (is_dir($dir)) {
|
||
| 98 | $iterator = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); |
||
| 99 | $files = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST); |
||
| 100 | foreach ($files as $file) {
|
||
| 101 | if ($file->isDir()) {
|
||
| 102 | $this->recursiveDelete($file->getRealPath(), $fileOnly); |
||
| 103 | } elseif ($file->isFile()) {
|
||
| 104 | unlink($file->getRealPath()); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($fileOnly === false) {
|
||
| 109 | rmdir($dir); |
||
| 110 | } |
||
| 111 | |||
| 112 | return true; |
||
| 113 | } |
||
| 114 | |||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | // ------------------------------------------------------------------------ |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Directory::setGroup |
||
| 122 | * |
||
| 123 | * Attempts to change the group of the directory to group. |
||
| 124 | * |
||
| 125 | * Only the superuser may change the group of a directory arbitrarily; other users may change the group of a file |
||
| 126 | * to any group of which that user is a member. |
||
| 127 | * |
||
| 128 | * @param mixed $group A group name or number. |
||
| 129 | * |
||
| 130 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 131 | */ |
||
| 132 | public function setGroup($group) |
||
| 133 | {
|
||
| 134 | $params[] = $this->getRealPath(); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 135 | $params[] = $group; |
||
| 136 | |||
| 137 | return call_user_func_array('chgrp', $params);
|
||
| 138 | } |
||
| 139 | |||
| 140 | // ------------------------------------------------------------------------ |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Directory::setMode |
||
| 144 | * |
||
| 145 | * Attempts to change the mode of the specified file to that given in mode. |
||
| 146 | * |
||
| 147 | * @param int $mode The mode parameter consists of three octal number components specifying access restrictions for |
||
| 148 | * the owner, the user group in which the owner is in, and to everybody else in this order. One |
||
| 149 | * component can be computed by adding up the needed permissions for that target user base. Number |
||
| 150 | * 1 means that you grant execute rights, number 2 means that you make the directory writable, |
||
| 151 | * number |
||
| 152 | * 4 means that you make the directory readable. Add up these numbers to specify needed rights. |
||
| 153 | * You can also read more about modes on Unix systems with 'man 1 chmod' and 'man 2 chmod'. |
||
| 154 | * |
||
| 155 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 156 | */ |
||
| 157 | public function setMode($mode) |
||
| 158 | {
|
||
| 159 | $params[] = $this->getRealPath(); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 160 | $params[] = $mode; |
||
| 161 | |||
| 162 | return call_user_func_array('chmod', $params);
|
||
| 163 | } |
||
| 164 | |||
| 165 | // ------------------------------------------------------------------------ |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Directory::setOwner |
||
| 169 | * |
||
| 170 | * Attempts to change the owner of the directory to user user. |
||
| 171 | * Only the superuser may change the owner of a directory. |
||
| 172 | * |
||
| 173 | * @param mixed $user A user name or number. |
||
| 174 | * |
||
| 175 | * @return bool Returns TRUE on success or FALSE on failure. |
||
| 176 | */ |
||
| 177 | public function setOwner($user) |
||
| 178 | {
|
||
| 179 | $params[] = $this->getRealPath(); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 180 | $params[] = $user; |
||
| 181 | |||
| 182 | return call_user_func_array('chown', $params);
|
||
| 183 | } |
||
| 184 | |||
| 185 | // ------------------------------------------------------------------------ |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Directory::findFilesByExtension |
||
| 189 | * |
||
| 190 | * Find files by extension. |
||
| 191 | * |
||
| 192 | * @param string $extension |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public function findFilesByExtension($extension) |
||
| 197 | {
|
||
| 198 | $extension = trim($extension, '.'); |
||
| 199 | |||
| 200 | $directoryIterator = new \RecursiveIteratorIterator( |
||
| 201 | new \RecursiveDirectoryIterator($this->getRealPath()), |
||
| 202 | \RecursiveIteratorIterator::SELF_FIRST |
||
| 203 | ); |
||
| 204 | |||
| 205 | $result = []; |
||
| 206 | |||
| 207 | foreach ($directoryIterator as $directoryFile) {
|
||
| 208 | if ($directoryFile->isFile()) {
|
||
| 209 | if ($extension === '*') {
|
||
| 210 | array_push($result, $directoryFile->getFilename()); |
||
| 211 | } elseif (preg_match('/\.' . $extension . '$/ui', $directoryFile->getFilename())) {
|
||
| 212 | if ( ! in_array($directoryFile->getRealPath(), $result)) {
|
||
| 213 | array_push($result, $directoryFile->getRealPath()); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | return $result; |
||
| 220 | } |
||
| 221 | |||
| 222 | // ------------------------------------------------------------------------ |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Directory::findFilesByFilename |
||
| 226 | * |
||
| 227 | * Find Files By Filename |
||
| 228 | * |
||
| 229 | * @param string $filename |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | public function findFilesByFilename($filename) |
||
| 234 | {
|
||
| 235 | $directoryIterator = new \RecursiveIteratorIterator( |
||
| 236 | new \RecursiveDirectoryIterator($this->getRealPath()), |
||
| 237 | \RecursiveIteratorIterator::SELF_FIRST |
||
| 238 | ); |
||
| 239 | |||
| 240 | $result = []; |
||
| 241 | |||
| 242 | foreach ($directoryIterator as $directoryFile) {
|
||
| 243 | if ($directoryFile->isFile()) {
|
||
| 244 | if (preg_match('/\\' . $filename . '.*/ui', $directoryFile->getFilename()) OR
|
||
| 245 | preg_match('/\\' . ucfirst($filename) . '.*/ui', $directoryFile->getFilename())
|
||
| 246 | ) {
|
||
| 247 | if ( ! in_array($directoryFile->getRealPath(), $result)) {
|
||
| 248 | array_push($result, $directoryFile->getRealPath()); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | return $result; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 |