Skip to content

Commit 4464a3a

Browse files
committed
Add better way to manage filesystem using filesystem entities classes.
1 parent 6214207 commit 4464a3a

File tree

4 files changed

+431
-0
lines changed

4 files changed

+431
-0
lines changed

src/FireFS/File.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/**
4+
* FireFS - Easily manage your filesystem, through PHP
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* @category Library
25+
* @package FireFS
26+
* @author Axel Nana <[email protected]>
27+
* @copyright 2018 Aliens Group, Inc.
28+
* @license MIT <https://github.com/ElementaryFramework/FireFS/blob/master/LICENSE>
29+
* @version GIT: 0.0.1
30+
* @link http://firefs.na2axl.tk
31+
*/
32+
33+
namespace ElementaryFramework\FireFS;
34+
35+
class File extends FileSystemEntity
36+
{
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function create(): bool
41+
{
42+
return $this->_fs->mkfile($this->_path, true);
43+
}
44+
45+
/**
46+
* Gets the extension of this file.
47+
*
48+
* @return string
49+
*/
50+
public function getExtension(): string
51+
{
52+
return $this->_fs->extension($this->_path);
53+
}
54+
55+
/**
56+
* Checks if the file is binary or not.
57+
*
58+
* @return bool
59+
*/
60+
public function isBinary(): bool
61+
{
62+
return $this->_fs->isBinary($this->_path);
63+
}
64+
65+
/**
66+
* Gets the mime type of this file.
67+
*
68+
* @param int $index The mime type index from the registry.
69+
*
70+
* @return string
71+
*/
72+
public function getMimeType(int $index = 0): string
73+
{
74+
return $this->_fs->mimeType($this->_path, $index);
75+
}
76+
77+
/**
78+
* Read the file and return the content as string.
79+
*
80+
* @return string
81+
*/
82+
public function read(): string
83+
{
84+
return $this->_fs->read($this->_path);
85+
}
86+
87+
/**
88+
* Writes data into the file.
89+
*
90+
* @param string $data The data to write into the file.
91+
* @param bool $append Define if the data have to be appended at the
92+
* end of the file (true), or overwrite the file
93+
* content (false).
94+
*
95+
* @return bool
96+
*/
97+
public function write(string $data, bool $append = false): bool
98+
{
99+
return $this->_fs->write($this->_path, $data, $append);
100+
}
101+
}

src/FireFS/FileSystemEntity.php

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<?php
2+
3+
/**
4+
* FireFS - Easily manage your filesystem, through PHP
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* @category Library
25+
* @package FireFS
26+
* @author Axel Nana <[email protected]>
27+
* @copyright 2018 Aliens Group, Inc.
28+
* @license MIT <https://github.com/ElementaryFramework/FireFS/blob/master/LICENSE>
29+
* @version GIT: 0.0.1
30+
* @link http://firefs.na2axl.tk
31+
*/
32+
33+
namespace ElementaryFramework\FireFS;
34+
35+
use ElementaryFramework\FireFS\Exceptions\FileSystemEntityNotFoundException;
36+
37+
abstract class FileSystemEntity
38+
{
39+
/**
40+
* @var string
41+
*/
42+
protected $_path;
43+
44+
/**
45+
* @var FireFS
46+
*/
47+
protected $_fs;
48+
49+
/**
50+
* FileSystemEntity constructor.
51+
*
52+
* @param string $path The path to the FS entity.
53+
* @param FireFS $fs The filesystem instance.
54+
*
55+
* @throws FileSystemEntityNotFoundException When the entity was not found on the given filesystem.
56+
*/
57+
public function __construct(string $path, FireFS &$fs)
58+
{
59+
$this->_path = $path;
60+
$this->_fs =& $fs;
61+
62+
if (!$this->_fs->exists($this->_path)) {
63+
throw new FileSystemEntityNotFoundException($this->_path);
64+
}
65+
}
66+
67+
/**
68+
* Gets the path to this entity.
69+
*
70+
* @return string
71+
*/
72+
public function getPath(): string
73+
{
74+
return $this->_path;
75+
}
76+
77+
/**
78+
* Gets the name of this entity.
79+
*
80+
* @return string
81+
*/
82+
public function getName(): string
83+
{
84+
return $this->_fs->basename($this->_path);
85+
}
86+
87+
/**
88+
* Creates this entity on the file system.
89+
*
90+
* @return bool
91+
*/
92+
public abstract function create(): bool;
93+
94+
/**
95+
* Deletes this entity from the file system.
96+
*
97+
* @return bool
98+
*/
99+
public function delete(): bool
100+
{
101+
return $this->_fs->delete($this->_path, true);
102+
}
103+
104+
/**
105+
* Renames the entity.
106+
*
107+
* @param string $newName The new name of the entity.
108+
*
109+
* @return bool
110+
*/
111+
public function rename(string $newName): bool
112+
{
113+
return $this->_fs->rename($this->_path, $newName);
114+
}
115+
116+
/**
117+
* Creates a copy of this entity into the given folder.
118+
*
119+
* @param Folder $folder The folder which will contain the copy;
120+
*
121+
* @return bool
122+
*/
123+
public function copyToFolder(Folder $folder): bool
124+
{
125+
return $this->copyToPath($folder->_path);
126+
}
127+
128+
/**
129+
* Creates a copy of this entity.
130+
*
131+
* @param string $path The path of the copy of the entity
132+
*
133+
* @return bool
134+
*/
135+
public function copyToPath(string $path): bool
136+
{
137+
return $this->_fs->copy($this->_path, $path);
138+
}
139+
140+
/**
141+
* Moves this entity into another folder.
142+
*
143+
* @param Folder $folder The new folder of the entity.
144+
*
145+
* @return bool
146+
*/
147+
public function moveToFolder(Folder $folder): bool
148+
{
149+
return $this->moveToPath($folder->_path);
150+
}
151+
152+
/**
153+
* Moves this entity to another path.
154+
*
155+
* @param string $path The new path of the entity
156+
*
157+
* @return bool
158+
*/
159+
public function moveToPath(string $path): bool
160+
{
161+
return $this->_fs->move($this->_path, $path);
162+
}
163+
164+
/**
165+
* Gets the internal path to this entity.
166+
*
167+
* @return string
168+
*/
169+
public function getInternalPath(): string
170+
{
171+
return $this->_fs->toInternalPath($this->_path);
172+
}
173+
174+
/**
175+
* Gets the external path to this entity.
176+
*
177+
* @return string
178+
*/
179+
public function getExternalPath(): string
180+
{
181+
return $this->_fs->toExternalPath($this->_path);
182+
}
183+
184+
/**
185+
* Gets the filesystem path to this entity.
186+
*
187+
* @return string
188+
*/
189+
public function getFileSystemPath(): string
190+
{
191+
return $this->_fs->toFileSystemPath($this->_path);
192+
}
193+
194+
/**
195+
* Gets the parent folder which contains this entity.
196+
*
197+
* @throws FileSystemEntityNotFoundException
198+
*
199+
* @return Folder
200+
*/
201+
public function getParent(): Folder
202+
{
203+
return new Folder($this->_fs->dirname($this->_path), $this->_fs);
204+
}
205+
206+
public function getSize(): int
207+
{
208+
return $this->_fs->size($this->_path);
209+
}
210+
211+
public function getSizeInOctets(): string
212+
{
213+
return $this->_fs->sizeInOctets($this->_path);
214+
}
215+
216+
public function getLastModificationTime(): int
217+
{
218+
return $this->_fs->lastModTime($this->_path);
219+
}
220+
221+
public function getLastAccessTime(): int
222+
{
223+
return $this->_fs->lastAccessTime($this->_path);
224+
}
225+
226+
/**
227+
* Checks if the current entity is stored
228+
* in a remote filesystem.
229+
*
230+
* @return bool
231+
*/
232+
public function isRemote(): bool
233+
{
234+
return $this->_fs->isRemote($this->_path);
235+
}
236+
}

src/FireFS/FireFS.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
namespace ElementaryFramework\FireFS;
3434

35+
use ElementaryFramework\FireFS\Exceptions\FileSystemEntityNotFoundException;
36+
3537
/**
3638
* FireFS - Filesystem Manager Class
3739
*
@@ -1082,4 +1084,22 @@ public function removeHostFromPath(string $path): string
10821084
{
10831085
return parse_url($path, PHP_URL_PATH);
10841086
}
1087+
1088+
/**
1089+
* Gets the file entity at the given path.
1090+
*
1091+
* @param string $path
1092+
*
1093+
* @return File|Folder
1094+
*
1095+
* @throws Exceptions\FileSystemEntityNotFoundException
1096+
*/
1097+
public function getEntity(string $path)
1098+
{
1099+
if ($this->exists($path)) {
1100+
return $this->isDir($path) ? new Folder($path, $this) : new File($path, $this);
1101+
} else {
1102+
throw new FileSystemEntityNotFoundException($path);
1103+
}
1104+
}
10851105
}

0 commit comments

Comments
 (0)