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

Module::find()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 1 Features 1
Metric Value
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 9.2
c 2
b 1
f 1
cc 4
eloc 8
nc 4
nop 1
crap 20
1
<?php
2
3
namespace hiqdev\yii2\modules\pages;
4
5
use Yii;
6
7
class Module extends \yii\base\Module
8
{
9
    protected $_storage;
10
11
    public $handlers = [
12
        'md'    => \hiqdev\yii2\modules\pages\models\MarkdownPage::class,
13
        'php'   => \hiqdev\yii2\modules\pages\models\PhpPage::class,
14
        'twig'  => \hiqdev\yii2\modules\pages\models\TwigPage::class,
15
    ];
16
17
    /**
18
     * This to use standard app pathes for views and layouts.
19
     * @return string
20
     */
21
    public function getViewPath()
22
    {
23
        return Yii::$app->getViewPath();
24
    }
25
26
    public function find($page)
27
    {
28
        if ($this->getStorage()->has($page)) {
29
            return $page;
30
        }
31
32
        foreach (array_keys($this->handlers) as $extension) {
33
            $path = $page . '.' . $extension;
34
            if ($this->getStorage()->has($path)) {
35
                return $path;
36
            }
37
        }
38
39
        return null;
40
    }
41
42
    public function getMetadata($page)
43
    {
44
        return $this->getStorage()->getMetadata($page);
45
    }
46
47
    public function localPath($path)
48
    {
49
        /// XXX: works for Local Filesystem only
50
        /// TODO: for others copying to be implemented
51
        return $this->getStorage()->path . '/' . $path;
52
    }
53
54
    /**
55
     * Reads given path as array of already rtrimmed lines.
56
     */
57
    public function readArray($path)
58
    {
59
        /// XXX: performance
60
        return preg_split("/((\r?\n)|(\r\n?))/", $this->getStorage()->read($path));
61
    }
62
63
    public function setStorage($value)
64
    {
65
        $this->_storage = $value;
66
    }
67
68
    public function getStorage()
69
    {
70
        if (!is_object($this->_storage)) {
71
            $this->_storage = Yii::createObject($this->_storage);
72
        }
73
74
        return $this->_storage;
75
    }
76
}
77