1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of PHP DNS Server. |
5
|
|
|
* |
6
|
|
|
* (c) Yif Swery <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace yswery\DNS\Filesystem; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; |
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
16
|
|
|
use yswery\DNS\Resolver\JsonFileSystemResolver; |
17
|
|
|
|
18
|
|
|
class FilesystemManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Filesystem |
22
|
|
|
*/ |
23
|
|
|
protected $filesystem; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $basePath; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $zonePath; |
34
|
|
|
|
35
|
|
|
public function __construct($basePath = null, $zonePath = null) |
36
|
|
|
{ |
37
|
|
|
if ($basePath) { |
38
|
|
|
$this->setBasePath($basePath); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($zonePath) { |
42
|
|
|
$this->setZonePath($zonePath); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->registerFilesystem(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function registerFilesystem() |
49
|
|
|
{ |
50
|
|
|
$this->filesystem = new Filesystem(); |
51
|
|
|
|
52
|
|
|
// make sure our directories exist |
53
|
|
|
if (!$this->filesystem->exists($this->zonePath())) { |
54
|
|
|
try { |
55
|
|
|
$this->filesystem->mkdir($this->zonePath(), 0700); |
56
|
|
|
} catch (IOExceptionInterface $e) { |
57
|
|
|
// todo: implement logging functions |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setBasePath($basePath) |
63
|
|
|
{ |
64
|
|
|
$this->basePath = rtrim($basePath, '\/'); |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function basePath() |
70
|
|
|
{ |
71
|
|
|
return $this->basePath; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function setZonePath($zonePath) |
75
|
|
|
{ |
76
|
|
|
$this->zonePath = rtrim($zonePath, '\/'); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function zonePath() |
82
|
|
|
{ |
83
|
|
|
return $this->zonePath ?: $this->basePath.DIRECTORY_SEPARATOR.'zones'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $zone |
88
|
|
|
* |
89
|
|
|
* @return JsonFileSystemResolver |
90
|
|
|
* |
91
|
|
|
* @throws \yswery\DNS\UnsupportedTypeException |
92
|
|
|
*/ |
93
|
|
|
public function getZone(string $zone) |
94
|
|
|
{ |
95
|
|
|
$zoneFile = $this->basePath().DIRECTORY_SEPARATOR.$zone.'.json'; |
96
|
|
|
|
97
|
|
|
return new JsonFileSystemResolver($zoneFile); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|