Completed
Push — master ( 4905bd...af6d05 )
by Andrii
03:36
created

Module   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 19
lcom 1
cbo 3
dl 0
loc 104
ccs 0
cts 67
cp 0
rs 10
c 4
b 1
f 2

10 Methods

Rating   Name   Duplication   Size   Complexity  
A findPageClass() 0 8 2
A getInstance() 0 4 1
A getViewPath() 0 4 1
C find() 0 24 7
A isDir() 0 10 2
A getMetadata() 0 4 1
A readArray() 0 5 1
A getLocalPath() 0 6 1
A setStorage() 0 4 1
A getStorage() 0 8 2
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;
12
13
use Yii;
14
use yii\base\InvalidConfigException;
15
16
class Module extends \yii\base\Module
17
{
18
    protected $_storage;
19
20
    public $pageClasses = [
21
        'md'    => \hiqdev\yii2\modules\pages\models\MarkdownPage::class,
22
        'php'   => \hiqdev\yii2\modules\pages\models\PhpPage::class,
23
        'twig'  => \hiqdev\yii2\modules\pages\models\TwigPage::class,
24
    ];
25
26
    public function findPageClass($extension)
27
    {
28
        if (!isset($this->pageClasses[$extension])) {
29
            throw new InvalidConfigException('not handled extension:' . $extension);
30
        }
31
32
        return $this->pageClasses[$extension];
33
    }
34
35
    public static function getInstance()
36
    {
37
        return Yii::$app->getModule('pages');
38
    }
39
40
    /**
41
     * This to use standard app pathes for views and layouts.
42
     * @return string
43
     */
44
    public function getViewPath()
45
    {
46
        return Yii::$app->getViewPath();
47
    }
48
49
    public function find($page)
50
    {
51
        if ($this->isDir($page)) {
52
            foreach (['index', 'README'] as $name) {
53
                $index = $this->find($page . '/' . $name);
54
                if ($index) {
55
                    return $index;
56
                }
57
            }
58
        }
59
60
        if ($this->getStorage()->has($page)) {
61
            return $page;
62
        }
63
64
        foreach (array_keys($this->pageClasses) as $extension) {
65
            $path = $page . '.' . $extension;
66
            if ($this->getStorage()->has($path)) {
67
                return $path;
68
            }
69
        }
70
71
        return null;
72
    }
73
74
    public function isDir($page)
75
    {
76
77
        if (!$this->getStorage()->has($page)) {
78
            return null;
79
        }
80
        $meta = $this->getMetadata($page);
81
82
        return $meta['type'] === 'dir';
83
    }
84
85
    public function getMetadata($page)
86
    {
87
        return $this->getStorage()->getMetadata($page);
88
    }
89
90
    /**
91
     * Reads given path as array of already rtrimmed lines.
92
     */
93
    public function readArray($path)
94
    {
95
        /// XXX: performance
96
        return preg_split("/((\r?\n)|(\r\n?))/", $this->getStorage()->read($path));
97
    }
98
99
    public function getLocalPath($path)
100
    {
101
        /// XXX: works for Local Filesystem only
102
        /// TODO: implement copying for others
103
        return $this->getStorage()->path . '/' . $path;
104
    }
105
106
    public function setStorage($value)
107
    {
108
        $this->_storage = $value;
109
    }
110
111
    public function getStorage()
112
    {
113
        if (!is_object($this->_storage)) {
114
            $this->_storage = Yii::createObject($this->_storage);
115
        }
116
117
        return $this->_storage;
118
    }
119
}
120