|
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; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
|
|
15
|
|
|
class Module extends \yii\base\Module |
|
16
|
|
|
{ |
|
17
|
|
|
protected $_storage; |
|
18
|
|
|
|
|
19
|
|
|
public $handlers = [ |
|
20
|
|
|
'md' => \hiqdev\yii2\modules\pages\models\MarkdownPage::class, |
|
21
|
|
|
'php' => \hiqdev\yii2\modules\pages\models\PhpPage::class, |
|
22
|
|
|
'twig' => \hiqdev\yii2\modules\pages\models\TwigPage::class, |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
public static function getInstance() |
|
26
|
|
|
{ |
|
27
|
|
|
return Yii::$app->getModule('pages'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* This to use standard app pathes for views and layouts. |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getViewPath() |
|
35
|
|
|
{ |
|
36
|
|
|
return Yii::$app->getViewPath(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function find($page) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($this->isDir($page)) { |
|
42
|
|
|
$index = $this->find($page . '/index'); |
|
43
|
|
|
if ($index) { |
|
44
|
|
|
return $index; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($this->getStorage()->has($page)) { |
|
49
|
|
|
return $page; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
foreach (array_keys($this->handlers) as $extension) { |
|
53
|
|
|
$path = $page . '.' . $extension; |
|
54
|
|
|
if ($this->getStorage()->has($path)) { |
|
55
|
|
|
return $path; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function isDir($page) |
|
63
|
|
|
{ |
|
64
|
|
|
|
|
65
|
|
|
if (!$this->getStorage()->has($page)) { |
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
$meta = $this->getMetadata($page); |
|
69
|
|
|
|
|
70
|
|
|
return $meta['type'] === 'dir'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getMetadata($page) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->getStorage()->getMetadata($page); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Reads given path as array of already rtrimmed lines. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function readArray($path) |
|
82
|
|
|
{ |
|
83
|
|
|
/// XXX: performance |
|
84
|
|
|
return preg_split("/((\r?\n)|(\r\n?))/", $this->getStorage()->read($path)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getLocalPath($path) |
|
88
|
|
|
{ |
|
89
|
|
|
/// XXX: works for Local Filesystem only |
|
90
|
|
|
/// TODO: implement copying for others |
|
91
|
|
|
return $this->getStorage()->path . '/' . $path; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function setStorage($value) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->_storage = $value; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getStorage() |
|
100
|
|
|
{ |
|
101
|
|
|
if (!is_object($this->_storage)) { |
|
102
|
|
|
$this->_storage = Yii::createObject($this->_storage); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->_storage; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|