1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Directory |
4
|
|
|
* |
5
|
|
|
* @filesource Directory.php |
6
|
|
|
* @created 03.03.2017 |
7
|
|
|
* @package chillerlan\Filereader |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Filereader; |
14
|
|
|
|
15
|
|
|
use chillerlan\Filereader\Drivers\FSDriverInterface; |
16
|
|
|
|
17
|
|
|
class Directory extends FSAbstract{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Directory constructor. |
21
|
|
|
* |
22
|
|
|
* @param \chillerlan\Filereader\Drivers\FSDriverInterface $driver |
23
|
|
|
* @param string $path |
24
|
|
|
*/ |
25
|
|
|
public function __construct(FSDriverInterface $driver, string $path){ |
26
|
|
|
parent::__construct($driver); |
27
|
|
|
|
28
|
|
|
$this->path = $path; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $path |
33
|
|
|
* |
34
|
|
|
* @return \chillerlan\Filereader\Directory |
35
|
|
|
*/ |
36
|
|
|
public function change(string $path):Directory{ |
37
|
|
|
$this->path = $path; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Reads a directory and returns the contents as an array of \stdClass |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
* @throws \chillerlan\Filereader\FilereaderException |
47
|
|
|
*/ |
48
|
|
|
public function read():array{ |
49
|
|
|
|
50
|
|
|
if(!$this->filereader->isDir($this->path)){ |
51
|
|
|
throw new FilereaderException('Directory not found: '.$this->path); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$dir = scandir($this->path); |
55
|
|
|
|
56
|
|
|
$filelist = []; |
57
|
|
|
|
58
|
|
|
if(is_array($dir)){ |
59
|
|
|
|
60
|
|
|
foreach($dir as $file){ |
61
|
|
|
|
62
|
|
|
if(in_array($file, ['.', '..'], true)){ |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$filelist[] = new File($this->filereader, $this, $file); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $filelist; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string|null $subdir |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function create(string $subdir = null):bool{ |
|
|
|
|
79
|
|
|
|
80
|
|
|
if($subdir && $this->filereader->isDir($this->path)){ |
|
|
|
|
81
|
|
|
return $this->filereader->makeDir($this->path.DIRECTORY_SEPARATOR.$subdir); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->filereader->makeDir($this->path); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string|null $subdir |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public function delete(string $subdir = null):bool{ |
|
|
|
|
93
|
|
|
|
94
|
|
|
if($subdir && $this->filereader->isDir($this->path)){ |
|
|
|
|
95
|
|
|
return $this->filereader->deleteDir($this->path.DIRECTORY_SEPARATOR.$subdir); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->filereader->deleteDir($this->path); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $newname |
103
|
|
|
* @param bool $overwrite |
104
|
|
|
* |
105
|
|
|
* @return \chillerlan\Filereader\Directory |
106
|
|
|
* @throws \chillerlan\Filereader\FilereaderException |
107
|
|
|
*/ |
108
|
|
|
public function rename(string $newname, bool $overwrite = true):Directory{ |
109
|
|
|
|
110
|
|
View Code Duplication |
if(!$this->filereader->rename($this->path, $newname, $overwrite)){ |
|
|
|
|
111
|
|
|
throw new FilereaderException('cannot rename '.$this->path.' to '.$newname); // @codeCoverageIgnore |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->change($newname); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.