Completed
Push — master ( 29aa5c...85c009 )
by Andrii
02:22
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
    public static function getInstance()
18
    {
19
        return Yii::$app->getModule('pages');
20
    }
21
22
    /**
23
     * This to use standard app pathes for views and layouts.
24
     * @return string
25
     */
26
    public function getViewPath()
27
    {
28
        return Yii::$app->getViewPath();
29
    }
30
31
    public function find($page)
32
    {
33
        if ($this->getStorage()->has($page)) {
34
            return $page;
35
        }
36
37
        foreach (array_keys($this->handlers) as $extension) {
38
            $path = $page . '.' . $extension;
39
            if ($this->getStorage()->has($path)) {
40
                return $path;
41
            }
42
        }
43
44
        return null;
45
    }
46
47
    public function getMetadata($page)
48
    {
49
        return $this->getStorage()->getMetadata($page);
50
    }
51
52
    /**
53
     * Reads given path as array of already rtrimmed lines.
54
     */
55
    public function readArray($path)
56
    {
57
        /// XXX: performance
58
        return preg_split("/((\r?\n)|(\r\n?))/", $this->getStorage()->read($path));
59
    }
60
61
    public function getLocalPath($path)
62
    {
63
        /// XXX: works for Local Filesystem only
64
        /// TODO: implement copying for others
65
        return $this->getStorage()->path . '/' . $path;
66
    }
67
68
    public function setStorage($value)
69
    {
70
        $this->_storage = $value;
71
    }
72
73
    public function getStorage()
74
    {
75
        if (!is_object($this->_storage)) {
76
            $this->_storage = Yii::createObject($this->_storage);
77
        }
78
79
        return $this->_storage;
80
    }
81
}
82