Completed
Push — master ( 2038e8...a5b302 )
by Andrii
01:52
created

AbstractPage::createFromFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 6
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()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
40
    {
41
        /// XXX think
42
        return Yii::$app->getModule('pages');
43
    }
44
45
    static public function createFromFile($path)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
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