1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class File |
4
|
|
|
* |
5
|
|
|
* @filesource File.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
|
|
|
/** |
18
|
|
|
* @property string $name |
19
|
|
|
* @property \chillerlan\Filereader\Directory $directory |
20
|
|
|
*/ |
21
|
|
|
class File extends FSAbstract{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \chillerlan\Filereader\Directory |
30
|
|
|
*/ |
31
|
|
|
protected $directory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* File constructor. |
35
|
|
|
* |
36
|
|
|
* @param \chillerlan\Filereader\Drivers\FSDriverInterface $driver |
37
|
|
|
* @param \chillerlan\Filereader\Directory $directory |
38
|
|
|
* @param string $name |
39
|
|
|
*/ |
40
|
|
|
public function __construct(FSDriverInterface $driver, Directory $directory, string $name){ |
41
|
|
|
parent::__construct($driver); |
42
|
|
|
|
43
|
|
|
$this->directory = $directory; |
44
|
|
|
$this->name = $name; |
45
|
|
|
$this->path = $this->directory->path.DIRECTORY_SEPARATOR.$this->name; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function exists():bool{ |
52
|
|
|
return $this->filereader->fileExists($this->path); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function content():string{ |
59
|
|
|
return $this->filereader->fileContents($this->path); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function getRequire(){ |
66
|
|
|
return $this->filereader->getRequire($this->path); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $destination |
71
|
|
|
* @param bool $overwrite |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function copy(string $destination, bool $overwrite = true):bool{ |
76
|
|
|
return $this->filereader->copyFile($this->path, $destination, $overwrite); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $newname |
81
|
|
|
* @param bool $overwrite |
82
|
|
|
* |
83
|
|
|
* @return \chillerlan\Filereader\File |
84
|
|
|
* @throws \chillerlan\Filereader\FilereaderException |
85
|
|
|
*/ |
86
|
|
|
public function rename(string $newname, bool $overwrite = true):File{ |
87
|
|
|
|
88
|
|
View Code Duplication |
if(!$this->filereader->rename($this->path, $newname, $overwrite)){ |
|
|
|
|
89
|
|
|
throw new FilereaderException('cannot rename '.$this->path.' to '.$newname); // @codeCoverageIgnore |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if(!$this->filereader->isFile($newname)){ |
93
|
|
|
throw new FilereaderException('file not found: '.$newname); // @codeCoverageIgnore |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->path = $newname; |
97
|
|
|
|
98
|
|
|
$info = pathinfo($this->path); |
99
|
|
|
|
100
|
|
|
$this->name = isset($info['filename']) ? $info['filename'] : ''; |
101
|
|
|
$this->name .= '.'.$info['extension']; |
102
|
|
|
$this->directory = new Directory($this->filereader, $info['dirname']); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function delete():bool{ |
111
|
|
|
return $this->filereader->deleteFile($this->path); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
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.