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
|
|
|
foreach (['index', 'README'] as $name) { |
43
|
|
|
$index = $this->find($page . '/' . $name); |
44
|
|
|
if ($index) { |
45
|
|
|
return $index; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($this->getStorage()->has($page)) { |
51
|
|
|
return $page; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
foreach (array_keys($this->handlers) as $extension) { |
55
|
|
|
$path = $page . '.' . $extension; |
56
|
|
|
if ($this->getStorage()->has($path)) { |
57
|
|
|
return $path; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function isDir($page) |
65
|
|
|
{ |
66
|
|
|
|
67
|
|
|
if (!$this->getStorage()->has($page)) { |
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
$meta = $this->getMetadata($page); |
71
|
|
|
|
72
|
|
|
return $meta['type'] === 'dir'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getMetadata($page) |
76
|
|
|
{ |
77
|
|
|
return $this->getStorage()->getMetadata($page); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Reads given path as array of already rtrimmed lines. |
82
|
|
|
*/ |
83
|
|
|
public function readArray($path) |
84
|
|
|
{ |
85
|
|
|
/// XXX: performance |
86
|
|
|
return preg_split("/((\r?\n)|(\r\n?))/", $this->getStorage()->read($path)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getLocalPath($path) |
90
|
|
|
{ |
91
|
|
|
/// XXX: works for Local Filesystem only |
92
|
|
|
/// TODO: implement copying for others |
93
|
|
|
return $this->getStorage()->path . '/' . $path; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setStorage($value) |
97
|
|
|
{ |
98
|
|
|
$this->_storage = $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getStorage() |
102
|
|
|
{ |
103
|
|
|
if (!is_object($this->_storage)) { |
104
|
|
|
$this->_storage = Yii::createObject($this->_storage); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->_storage; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|