|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Yii2 Pages Module |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/yii2-module-pages |
|
6
|
|
|
* @package yii2-module-pages |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\yii2\modules\pages\storage; |
|
12
|
|
|
|
|
13
|
|
|
use creocoder\flysystem\Filesystem; |
|
14
|
|
|
use hiqdev\yii2\collection\BaseObject; |
|
15
|
|
|
use hiqdev\yii2\modules\pages\interfaces\StorageInterface; |
|
16
|
|
|
use hiqdev\yii2\modules\pages\models\AbstractPage; |
|
17
|
|
|
use hiqdev\yii2\modules\pages\models\PagesList; |
|
18
|
|
|
use Yii; |
|
19
|
|
|
|
|
20
|
|
|
class FileSystemStorage extends BaseObject implements StorageInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string|Filesystem */ |
|
23
|
|
|
private $fileSystem; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $path; |
|
27
|
|
|
|
|
28
|
|
|
private $pageClasses = [ |
|
29
|
|
|
'' => \hiqdev\yii2\modules\pages\models\OtherPage::class, |
|
30
|
|
|
'md' => \hiqdev\yii2\modules\pages\models\MarkdownPage::class, |
|
31
|
|
|
'php' => \hiqdev\yii2\modules\pages\models\PhpPage::class, |
|
32
|
|
|
'twig' => \hiqdev\yii2\modules\pages\models\TwigPage::class, |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $page |
|
37
|
|
|
* @return AbstractPage|null |
|
38
|
|
|
* @throws \yii\base\InvalidConfigException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getPage(string $page): ?AbstractPage |
|
41
|
|
|
{ |
|
42
|
|
|
if ($this->isDir($page)) { |
|
43
|
|
|
foreach (['index', 'README'] as $name) { |
|
44
|
|
|
$index = $this->getPage($page . '/' . $name); |
|
|
|
|
|
|
45
|
|
|
if ($index) { |
|
46
|
|
|
return $index; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
foreach (array_keys($this->pageClasses) as $extension) { |
|
52
|
|
|
$path = $page . '.' . $extension; |
|
53
|
|
|
if ($this->getFileSystem()->has($path)) { |
|
54
|
|
|
return AbstractPage::createFromFile($path, $this); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getList(?string $path): ?PagesList |
|
62
|
|
|
{ |
|
63
|
|
|
if (!is_null($path) && $this->isDir($path)) { |
|
64
|
|
|
return PagesList::createFromDir($path, $this); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $page |
|
72
|
|
|
* @return bool|null |
|
73
|
|
|
* @throws \yii\base\InvalidConfigException |
|
74
|
|
|
*/ |
|
75
|
|
|
public function isDir(string $page): ?bool |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$this->getFileSystem()->has($page)) { |
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
$meta = $this->getMetadata($page); |
|
81
|
|
|
|
|
82
|
|
|
return $meta['type'] === 'dir'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $page |
|
87
|
|
|
* @return array|false |
|
88
|
|
|
* @throws \yii\base\InvalidConfigException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getMetadata($page) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->getFileSystem()->getMetadata($page); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param mixed $fileSystem |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setFileSystem($fileSystem): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->fileSystem = $fileSystem; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $path |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setPath(string $path): void |
|
107
|
|
|
{ |
|
108
|
|
|
$this->path = $path; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getPath(): string |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->path; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $path |
|
121
|
|
|
* @return null|string |
|
122
|
|
|
* @throws \yii\base\InvalidConfigException |
|
123
|
|
|
*/ |
|
124
|
|
|
public function read(string $path): ?string |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->getFileSystem()->read($path); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $extension |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function findPageClass(string $extension): string |
|
134
|
|
|
{ |
|
135
|
|
|
if (empty($this->pageClasses[$extension])) { |
|
136
|
|
|
$extension = ''; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $this->pageClasses[$extension]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Reads given path as array of already rtrimmed lines. |
|
144
|
|
|
* @param string $path |
|
145
|
|
|
* @return false|string[] |
|
146
|
|
|
*/ |
|
147
|
|
|
public function readArray(string $path) |
|
148
|
|
|
{ |
|
149
|
|
|
return preg_split("/((\r?\n)|(\r\n?))/", $this->fileSystem->read($path)); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param $filePath |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getLocalPath($filePath): string |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->path . '/' . $filePath; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return Filesystem |
|
163
|
|
|
* @throws \yii\base\InvalidConfigException |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getFileSystem(): Filesystem |
|
166
|
|
|
{ |
|
167
|
|
|
if (!is_object($this->fileSystem)) { |
|
168
|
|
|
$this->fileSystem = Yii::createObject([ |
|
169
|
|
|
'class' => $this->fileSystem, |
|
170
|
|
|
'path' => $this->path, |
|
171
|
|
|
]); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $this->fileSystem; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.