1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiqdev\yii2\modules\pages; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
|
7
|
|
|
class Module extends \yii\base\Module |
8
|
|
|
{ |
9
|
|
|
protected $_storage; |
10
|
|
|
|
11
|
|
|
public $handlers = [ |
12
|
|
|
'md' => \hiqdev\yii2\modules\pages\models\MarkdownPage::class, |
13
|
|
|
'php' => \hiqdev\yii2\modules\pages\models\PhpPage::class, |
14
|
|
|
'twig' => \hiqdev\yii2\modules\pages\models\TwigPage::class, |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This to use standard app pathes for views and layouts. |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public function getViewPath() |
22
|
|
|
{ |
23
|
|
|
return Yii::$app->getViewPath(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function find($page) |
27
|
|
|
{ |
28
|
|
|
if ($this->getStorage()->has($page)) { |
29
|
|
|
return $page; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
foreach (array_keys($this->handlers) as $extension) { |
33
|
|
|
$path = $page . '.' . $extension; |
34
|
|
|
if ($this->getStorage()->has($path)) { |
35
|
|
|
return $path; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getMetadata($page) |
43
|
|
|
{ |
44
|
|
|
return $this->getStorage()->getMetadata($page); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function localPath($path) |
48
|
|
|
{ |
49
|
|
|
/// XXX: works for Local Filesystem only |
50
|
|
|
/// TODO: for others copying to be implemented |
51
|
|
|
return $this->getStorage()->path . '/' . $path; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Reads given path as array of already rtrimmed lines. |
56
|
|
|
*/ |
57
|
|
|
public function readArray($path) |
58
|
|
|
{ |
59
|
|
|
/// XXX: performance |
60
|
|
|
return preg_split("/((\r?\n)|(\r\n?))/", $this->getStorage()->read($path)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setStorage($value) |
64
|
|
|
{ |
65
|
|
|
$this->_storage = $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getStorage() |
69
|
|
|
{ |
70
|
|
|
if (!is_object($this->_storage)) { |
71
|
|
|
$this->_storage = Yii::createObject($this->_storage); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->_storage; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|