1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of byrokrat\giroapp. |
4
|
|
|
* |
5
|
|
|
* byrokrat\giroapp is free software: you can redistribute it and/or |
6
|
|
|
* modify it under the terms of the GNU General Public License as published |
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* byrokrat\giroapp is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
* |
18
|
|
|
* Copyright 2016-18 Hannes Forsgård |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
22
|
|
|
|
23
|
|
|
namespace byrokrat\giroapp\Utils; |
24
|
|
|
|
25
|
|
|
use byrokrat\giroapp\Exception\UnableToReadFileException; |
26
|
|
|
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem; |
27
|
|
|
use Symfony\Component\Finder\Finder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Wrapper class to access to file system |
31
|
|
|
*/ |
32
|
|
|
class Filesystem |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $basePath; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var SymfonyFilesystem |
41
|
|
|
*/ |
42
|
|
|
private $fs; |
43
|
|
|
|
44
|
|
|
public function __construct(string $basePath, SymfonyFilesystem $fs) |
45
|
|
|
{ |
46
|
|
|
$this->basePath = $basePath; |
47
|
|
|
$this->fs = $fs; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getAbsolutePath(string $path = ''): string |
51
|
|
|
{ |
52
|
|
|
return $this->fs->isAbsolutePath($path) ? $path : $this->basePath . DIRECTORY_SEPARATOR . $path; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function exists(string $path = ''): bool |
56
|
|
|
{ |
57
|
|
|
return $this->fs->exists($this->getAbsolutePath($path)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function mkdir(string $path = ''): void |
61
|
|
|
{ |
62
|
|
|
$this->fs->mkdir($this->getAbsolutePath($path)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function isFile(string $path): bool |
66
|
|
|
{ |
67
|
|
|
$path = $this->getAbsolutePath($path); |
68
|
|
|
return $this->fs->exists($path) && is_file($path) && is_readable($path); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws UnableToReadFileException if file does not exist |
73
|
|
|
*/ |
74
|
|
|
public function readFile(string $path): File |
75
|
|
|
{ |
76
|
|
|
if (!$this->isFile($path)) { |
77
|
|
|
throw new UnableToReadFileException("Unable to read {$path}"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new File( |
81
|
|
|
$path, |
82
|
|
|
(string)file_get_contents($this->getAbsolutePath($path)) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function dumpFile(string $path, string $content): void |
87
|
|
|
{ |
88
|
|
|
$this->fs->dumpFile($this->getAbsolutePath($path), $content); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getFinder(string $path = ''): Finder |
92
|
|
|
{ |
93
|
|
|
return (new Finder)->in($this->getAbsolutePath($path)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|