1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiqdev\yii2\modules\pages\models; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
6
|
|
|
use Yii; |
7
|
|
|
|
8
|
|
|
abstract class AbstractPage extends \yii\base\Object |
9
|
|
|
{ |
10
|
|
|
public $layout; |
11
|
|
|
|
12
|
|
|
public $title; |
13
|
|
|
|
14
|
|
|
protected $data = []; |
15
|
|
|
|
16
|
|
|
protected $text; |
17
|
|
|
|
18
|
|
|
public function setData(array $data) |
19
|
|
|
{ |
20
|
|
|
$this->data = $data; |
21
|
|
|
foreach (['title', 'layout'] as $key) { |
22
|
|
|
if (isset($data[$key])) { |
23
|
|
|
$this->{$key} = $data[$key]; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getData() |
29
|
|
|
{ |
30
|
|
|
return $this->data; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function __construct(array $data, $text) |
34
|
|
|
{ |
35
|
|
|
$this->setData($data); |
36
|
|
|
$this->text = $text; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
static public function getModule() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
/// XXX think |
42
|
|
|
return Yii::$app->getModule('pages'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
static public function createFromFile($path) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$extension = pathinfo($path)['extension']; |
48
|
|
|
|
49
|
|
|
if (!isset(static::getModule()->handlers[$extension])) { |
50
|
|
|
throw new InvalidConfigException('not handled extension:' . $extension); |
51
|
|
|
} |
52
|
|
|
$class = static::getModule()->handlers[$extension]; |
53
|
|
|
|
54
|
|
|
list($data, $text) = static::extractData($path); |
55
|
|
|
|
56
|
|
|
return new $class($data, $text); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
static public function extractData($path) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$lines = static::getModule()->readArray($path); |
62
|
|
|
$marker = "---"; |
63
|
|
|
$line = array_shift($lines); |
64
|
|
|
if ($line === $marker) { |
65
|
|
|
$meta = ''; |
66
|
|
|
while (true) { |
67
|
|
|
$line = array_shift($lines); |
68
|
|
|
if ($line === $marker) { |
69
|
|
|
break; |
70
|
|
|
} |
71
|
|
|
$meta .= "\n" . $line; |
72
|
|
|
} |
73
|
|
|
$line = ''; |
74
|
|
|
$data = Yaml::parse($meta); |
75
|
|
|
} else { |
76
|
|
|
$data = []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return [$data, $line . join("\n", $lines)]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Renders the page with given params. |
84
|
|
|
* |
85
|
|
|
* @param array $params |
86
|
|
|
* @abstract |
87
|
|
|
* @access public |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
abstract public function render(array $params = []); |
91
|
|
|
} |
92
|
|
|
|