|
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
|
|
|
public static function getInstance() |
|
18
|
|
|
{ |
|
19
|
|
|
return Yii::$app->getModule('pages'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* This to use standard app pathes for views and layouts. |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getViewPath() |
|
27
|
|
|
{ |
|
28
|
|
|
return Yii::$app->getViewPath(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function find($page) |
|
32
|
|
|
{ |
|
33
|
|
|
if ($this->getStorage()->has($page)) { |
|
34
|
|
|
return $page; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
foreach (array_keys($this->handlers) as $extension) { |
|
38
|
|
|
$path = $page . '.' . $extension; |
|
39
|
|
|
if ($this->getStorage()->has($path)) { |
|
40
|
|
|
return $path; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return null; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getMetadata($page) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->getStorage()->getMetadata($page); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Reads given path as array of already rtrimmed lines. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function readArray($path) |
|
56
|
|
|
{ |
|
57
|
|
|
/// XXX: performance |
|
58
|
|
|
return preg_split("/((\r?\n)|(\r\n?))/", $this->getStorage()->read($path)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getLocalPath($path) |
|
62
|
|
|
{ |
|
63
|
|
|
/// XXX: works for Local Filesystem only |
|
64
|
|
|
/// TODO: implement copying for others |
|
65
|
|
|
return $this->getStorage()->path . '/' . $path; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function setStorage($value) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->_storage = $value; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getStorage() |
|
74
|
|
|
{ |
|
75
|
|
|
if (!is_object($this->_storage)) { |
|
76
|
|
|
$this->_storage = Yii::createObject($this->_storage); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->_storage; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|