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\models; |
12
|
|
|
|
13
|
|
|
use hiqdev\yii2\modules\pages\storage\FileSystemStorage; |
14
|
|
|
use hiqdev\yii2\modules\pages\interfaces\StorageInterface; |
15
|
|
|
use Symfony\Component\Yaml\Yaml; |
16
|
|
|
use Yii; |
17
|
|
|
|
18
|
|
|
abstract class AbstractPage extends \yii\base\BaseObject |
19
|
|
|
{ |
20
|
|
|
/** @var \yii\web\View */ |
21
|
|
|
protected $view; |
22
|
|
|
|
23
|
|
|
public $layout; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
public $title; |
27
|
|
|
|
28
|
|
|
/** @var null|string */ |
29
|
|
|
protected $path; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $text; |
33
|
|
|
|
34
|
|
|
/** @var array */ |
35
|
|
|
protected $data = []; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
protected $url; |
39
|
|
|
|
40
|
|
|
/** @var StorageInterface */ |
41
|
|
|
protected $storage; |
42
|
|
|
|
43
|
|
|
public function __construct($path = null, StorageInterface $storage = null, $config = []) |
44
|
|
|
{ |
45
|
|
|
if ($path) { |
46
|
|
|
$this->storage = $storage; |
47
|
|
|
list($data, $text) = $this->extractData($path); |
48
|
|
|
|
49
|
|
|
$this->path = $path; |
50
|
|
|
$this->text = $text; |
51
|
|
|
$this->setData($data); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->view = Yii::$app->view; |
55
|
|
|
parent::__construct($config); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setData($data) |
59
|
|
|
{ |
60
|
|
|
if (!is_array($data)) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
$this->data = $data; |
64
|
|
|
foreach (['title', 'layout', 'url'] as $key) { |
65
|
|
|
if (isset($data[$key])) { |
66
|
|
|
$this->{$key} = $data[$key]; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getData() |
72
|
|
|
{ |
73
|
|
|
return $this->data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getPath() |
77
|
|
|
{ |
78
|
|
|
return $this->path; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getDate() |
82
|
|
|
{ |
83
|
|
|
return $this->data['date']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getUrl() |
87
|
|
|
{ |
88
|
|
|
return $this->url ?: ['/pages/render/index', 'page' => $this->getPath()]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public static function createFromFile($path, FileSystemStorage $storage) |
92
|
|
|
{ |
93
|
|
|
$extension = pathinfo($path)['extension']; |
94
|
|
|
$class = $storage->findPageClass($extension); |
95
|
|
|
|
96
|
|
|
return new $class($path, $storage); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function extractData($path) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$lines = $this->storage->readArray($path); |
|
|
|
|
102
|
|
|
$yaml = $this->readQuotedLines($lines, '/^---$/', '/^---$/'); |
103
|
|
|
if (empty($yaml)) { |
104
|
|
|
$data = []; |
105
|
|
|
$text = $lines; |
106
|
|
|
} else { |
107
|
|
|
$data = $this->readYaml($yaml); |
108
|
|
|
$text = array_slice($lines, count($yaml)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return [$data, implode("\n", $text)]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function readFrontMatter($lines) |
115
|
|
|
{ |
116
|
|
|
$yaml = $this->readQuotedLines($lines, '/^---$/', '/^---$/'); |
117
|
|
|
if (empty($yaml)) { |
118
|
|
|
return []; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return empty($yaml) ? [] : $this->readYaml($yaml); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function readYaml($lines) |
125
|
|
|
{ |
126
|
|
|
$data = Yaml::parse(implode("\n", $lines)); |
127
|
|
|
if (is_int($data['date'])) { |
128
|
|
|
$data['date'] = date('c', $data['date']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $data; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function readQuotedLines($lines, $headMarker, $tailMarker) |
135
|
|
|
{ |
136
|
|
|
$line = array_shift($lines); |
137
|
|
|
if (!preg_match($headMarker, $line, $matches)) { |
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
$res[] = ltrim(substr($line, strlen($matches[0]))); |
|
|
|
|
141
|
|
|
while ($line) { |
142
|
|
|
$line = array_shift($lines); |
143
|
|
|
if (preg_match($tailMarker, $line, $matches)) { |
144
|
|
|
$res[] = rtrim(substr($line, 0, -strlen($matches[0]))); |
145
|
|
|
break; |
146
|
|
|
} |
147
|
|
|
$res[] = $line; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $res; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Renders the page with given params. |
155
|
|
|
* |
156
|
|
|
* @param array $params |
157
|
|
|
* @abstract |
158
|
|
|
*/ |
159
|
|
|
abstract public function render(array $params = []); |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $text |
163
|
|
|
*/ |
164
|
|
|
public function setText(string $text): void |
165
|
|
|
{ |
166
|
|
|
$this->text = $text; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $url |
171
|
|
|
*/ |
172
|
|
|
public function setUrl(string $url): void |
173
|
|
|
{ |
174
|
|
|
$this->url = $url; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.