Completed
Push — master ( fbb4c3...48aefa )
by Andrii
04:08
created

BaseHandler::readArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\handlers;
12
13
use hidev\helpers\FileHelper;
14
use Yii;
15
use yii\base\InvalidConfigException;
16
use yii\helpers\ArrayHelper;
17
18
/**
19
 * Base Handler.
20
 * Knows how to parse and render it's file type.
21
 */
22
class BaseHandler extends \yii\base\Object
23
{
24
    /**
25
     * @var Goal
26
     */
27
    public $goal;
28
29
    /**
30
     * @var string template file name to be used for rendering
31
     */
32
    public $template;
33
34
    /**
35
     * @var View object
36
     */
37
    protected $_view;
38
39
    /**
40
     * Returns the view object that can be used to render views or view files.
41
     * If not set, it will default to the "view" application component.
42
     * @return View the view object that can be used to render views or view files
43
     */
44
    public function getView()
45
    {
46
        if ($this->_view === null) {
47
            $this->_view = Yii::$app->getView();
48
        }
49
50
        return $this->_view;
51
    }
52
53
    /**
54
     * Renders prepared data.
55
     * Must be redefined in child.
56
     *
57
     * @param array $data
58
     *
59
     * @throws InvalidConfigException
60
     *
61
     * @return string file content
62
     */
63
    public function renderPrepared(array $data)
64
    {
65
        throw new InvalidConfigException('Render not available');
66
    }
67
68
    /**
69
     * Renders raw data.
70
     * Prepares data with ArrayHelper::toArray.
71
     *
72
     * @param mixed $data
73
     *
74
     * @return string file content
75
     */
76
    public function render($data)
77
    {
78
        return $this->renderPrepared($this->prepareData($data));
79
    }
80
81
    public function prepareData($data)
82
    {
83
        return ArrayHelper::toArray($data, [], false);
84
        //return $data;
85
    }
86
87
    /**
88
     * Renders file content using given data.
89
     * Converts to array with ArrayHelper::toArray.
90
     *
91
     * @param mixed $data
92
     *
93
     * @return string file content
94
     */
95 3
    public function renderPath($path, $data)
96
    {
97 3
        return $this->write($path, $this->render($data));
98
    }
99
100
    public function parsePath($path, $minimal = null)
101
    {
102
        return $this->parse($this->read($path));
0 ignored issues
show
Bug introduced by
It seems like $this->read($path) targeting hidev\handlers\BaseHandler::read() can also be of type array; however, hidev\handlers\BaseHandler::parse() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103
    }
104
105
    /**
106
     * Parses string input. To be redefined in real handlers.
107
     * @param string $input to parse
108
     * @return array
109
     */
110
    public function parse($input)
111
    {
112
        return [];
113
    }
114
115
    /**
116
     * Writes given content to the file.
117
     * Doesn't touch file if it has exactly same content.
118
     * Creates intermediate directories when necessary.
119
     * @param string $path
120
     * @param string $content
121
     * @return bool true if file was changed
122
     */
123 3
    public function write($path, $content)
124
    {
125 3
        return FileHelper::write($path, $content);
126
    }
127
128
    /**
129
     * Creates directory if not exists.
130
     * @param string $path
131
     * @return bool true if directory did not exist and was created
132
     */
133
    public function mkdir($path)
134
    {
135
        return FileHelper::mkdir($path);
136
    }
137
138
    /**
139
     * Read file into a string or array.
140
     * @param string $path
141
     * @param bool $asArray
142
     * @return string|array
143
     */
144
    public function read($path, $asArray = false)
145
    {
146
        if (file_exists($path)) {
147
            Yii::info('Read file: ' . $path, 'file');
148
149
            return $asArray ? file($path) : file_get_contents($path);
150
        } else {
151
            Yii::error('Couldn\'t read file: ' . $path, 'file');
152
153
            return;
154
        }
155
    }
156
157
    /**
158
     * Read file into array of strings.
159
     * @param string $path
160
     * @return array
161
     */
162
    public function readArray($path)
163
    {
164
        return $this->read($path, true);
165
    }
166
}
167