Passed
Push — v5 ( 6adbf6...c0775f )
by Alexey
06:03
created

Template   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 243
rs 8.2857
c 0
b 0
f 0
wmc 39

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A setPage() 0 9 4
D getContentPaths() 0 37 12
A loadConfig() 0 6 2
B setContent() 0 12 8
A getPagePaths() 0 7 2
A setModule() 0 8 4
A setParams() 0 3 2
1
<?php
2
3
/**
4
 * Template
5
 *
6
 * Object for template
7
 *
8
 * @author Alexey Krupskiy <[email protected]>
9
 * @link http://inji.ru/
10
 * @copyright 2015 Alexey Krupskiy
11
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
12
 */
13
14
namespace Inji\View;
15
16
class Template extends \Inji\InjiObject {
17
18
    /**
19
     * App for template
20
     *
21
     * @var \App
22
     */
23
    public $app = null;
24
25
    /**
26
     * Template name
27
     *
28
     * @var string
29
     */
30
    public $name = 'default';
31
32
    /**
33
     * Template path
34
     *
35
     * @var string
36
     */
37
    public $path = '';
38
39
    /**
40
     * Template config path
41
     *
42
     * @var string
43
     */
44
    public $configPath = '';
45
46
    /**
47
     * Template config
48
     *
49
     * @var array
50
     */
51
    public $config = [];
52
53
    /**
54
     * Current template page for rendering
55
     *
56
     * @var string
57
     */
58
    public $page = 'index';
59
60
    /**
61
     * Current template page path for rendering
62
     *
63
     * @var string|boolean
64
     */
65
    public $pagePath = '';
66
67
    /**
68
     * Template module for content path finder
69
     *
70
     * @var \Module
71
     */
72
    public $module = null;
73
74
    /**
75
     * Current content file for rendering
76
     *
77
     * @var string
78
     */
79
    public $content = '';
80
81
    /**
82
     * Current content file path for rendering
83
     *
84
     * @var string|boolean
85
     */
86
    public $contentPath = '';
87
88
    /**
89
     * Setup template object
90
     *
91
     * @param array $params
92
     */
93
    public function __construct($params = []) {
94
        $this->setParams($params);
95
        if (!$this->path) {
96
            $this->path = $this->app->view->templatesPath . '/' . $this->name;
97
        }
98
        $this->loadConfig();
99
        $this->setPage();
100
        $this->setModule();
101
        $this->setContent();
102
    }
103
104
    /**
105
     * Load template config
106
     *
107
     * @param string $configPath
108
     */
109
    public function loadConfig($configPath = '') {
110
        if (!$configPath) {
111
            $configPath = $this->path . '/config.php';
112
        }
113
        $this->configPath = $configPath;
114
        $this->config = \Inji\Config::custom($this->configPath);
115
    }
116
117
    /**
118
     * Set params for template
119
     *
120
     * @param array $params
121
     */
122
    public function setParams($params) {
123
        foreach ($params as $param => $value) {
124
            $this->$param = $value;
125
        }
126
    }
127
128
    /**
129
     * Set page and page path for template
130
     *
131
     * @param string $page
132
     */
133
    public function setPage($page = '') {
134
        if (!$page) {
135
            $page = !empty($this->config['defaultPage']) ? $this->config['defaultPage'] : $this->page;
136
        }
137
        $this->page = $page;
138
        if (!$this->pagePath) {
139
            $this->pagePath = $this->path . '/' . $this->page . '.html';
140
        }
141
        $this->pagePath = \Inji\Tools::pathsResolve($this->getPagePaths(), $this->pagePath);
142
    }
143
144
    /**
145
     * Get posible paths for template page by name
146
     *
147
     * @param string $page
148
     * @return array
149
     */
150
    public function getPagePaths($page = '') {
151
        if (!$page) {
152
            $page = $this->page;
153
        }
154
        return [
155
            'template' => $this->path . '/' . $page . '.html',
156
            'defaultPage' => \Inji\Module::getModulePath('View') . '/templatePages/' . $page . '.html'
157
        ];
158
    }
159
160
    /**
161
     * Set module for content path finder
162
     *
163
     * @param \Inji\Module $module
164
     */
165
    public function setModule($module = null) {
166
        if (!$module && !$this->module) {
167
            $this->module = \Inji\Module::$cur;
0 ignored issues
show
Documentation Bug introduced by
It seems like Inji\Module::cur of type Inji\Module is incompatible with the declared type Module of property $module.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
168
        } else {
169
            $this->module = $module;
170
        }
171
        if (is_string($this->module)) {
172
            $this->module = $this->app->{$this->module};
173
        }
174
    }
175
176
    /**
177
     * Set content file for rendering
178
     *
179
     * @param string $content
180
     */
181
    public function setContent($content = '') {
182
        if ($content) {
183
            $this->content = $content;
184
        }
185
        if (\Inji\Controller::$cur && \Inji\Controller::$cur->run) {
186
            if (!$this->content) {
187
                $this->content = \Inji\Controller::$cur->method;
188
            }
189
            if ((!$this->contentPath || $content) && \Inji\Module::$cur) {
190
                $this->contentPath = \Inji\Module::$cur->path . '/' . \Inji\Module::$cur->app->type . "Controllers/content/{$this->content}.php";
191
            }
192
            $this->contentPath = \Inji\Tools::pathsResolve($this->getContentPaths(), $this->contentPath);
0 ignored issues
show
Bug introduced by
$this->getContentPaths() of type string is incompatible with the type array expected by parameter $paths of Inji\Tools::pathsResolve(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

192
            $this->contentPath = \Inji\Tools::pathsResolve(/** @scrutinizer ignore-type */ $this->getContentPaths(), $this->contentPath);
Loading history...
193
        }
194
    }
195
196
    /**
197
     * Return posible path for content file by name
198
     *
199
     * @param string $content
200
     * @return string
201
     */
202
    public function getContentPaths($content = '') {
203
        if (!$content) {
204
            $content = $this->content;
205
        }
206
        $paths = [];
207
        if ($this->module) {
208
            if (\Inji\Controller::$cur) {
209
                $paths['templateModuleController'] = $this->path . "/modules/{$this->module->name}/" . \Inji\Controller::$cur->name . "/{$content}.php";
210
            }
211
            $paths['templateModule'] = $this->path . "/modules/{$this->module->name}/{$content}.php";
212
        }
213
        if (\Inji\Module::$cur) {
214
            if (\Inji\Controller::$cur) {
215
                $paths['templateCurModuleController'] = $this->path . "/modules/" . \Inji\Module::$cur->name . "/" . \Inji\Controller::$cur->name . "/{$content}.php";
216
            }
217
            $paths['templateCurModule'] = $this->path . "/modules/" . \Inji\Module::$cur->name . "/{$content}.php";
218
        }
219
        if (\Inji\Controller::$cur) {
220
            $modulePaths = \Inji\Module::getModulePaths(\Inji\Controller::$cur->module->name);
221
            foreach ($modulePaths as $key => $modulePath) {
222
                $paths['module_' . $key . '_appType'] = $modulePath . '/views/' . \Inji\Controller::$cur->module->app->type . '/' . \Inji\Controller::$cur->name . "/{$content}.php";
223
                $paths['module_' . $key . '_appType_controllerName'] = $modulePath . '/views/' . \Inji\Controller::$cur->module->app->type . "/{$content}.php";
224
                $paths['module_' . $key] = $modulePath . '/views/' . "/{$content}.php";
225
            }
226
        }
227
228
        if ($this->module) {
229
            if (\Inji\Controller::$cur) {
230
                $paths['customModuleTemplateControllerContentController'] = $this->path . "/modules/" . $this->module->name . "/" . \Inji\Controller::$cur->name . "/{$content}.php";
231
            }
232
            $paths['customModuleTemplateControllerContent'] = $this->path . "/modules/" . $this->module->name . "/{$content}.php";
233
        }
234
        if ($this->module && \Inji\Controller::$cur) {
235
            $paths['customModuleControllerContentController'] = $this->module->path . '/' . \Inji\Controller::$cur->module->app->type . "Controllers/content/" . \Inji\Controller::$cur->name . "/{$content}.php";
236
            $paths['customModuleControllerContent'] = $this->module->path . '/' . \Inji\Controller::$cur->module->app->type . "Controllers/content/{$content}.php";
237
        }
238
        return $paths;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $paths returns the type string[]|array which is incompatible with the documented return type string.
Loading history...
239
    }
240
241
    /**
242
     * Retrn object of template by template name
243
     *
244
     * @param string $templateName
245
     * @param \Inji\App $app
246
     * @return \Inji\View\Template
247
     */
248
    public static function get($templateName, $app = null, $templatesPath = '') {
249
        if (!$app) {
250
            $app = \Inji\App::$cur;
251
        }
252
        if (!$templatesPath) {
253
            $templatesPath = $app->view->templatesPath;
0 ignored issues
show
Bug Best Practice introduced by
The property view does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
254
        }
255
        if (file_exists($templatesPath . '/' . $templateName)) {
256
            return new Template([
257
                'name' => $templateName,
258
                'path' => $templatesPath . '/' . $templateName,
259
                'app' => $app
260
            ]);
261
        }
262
    }
263
264
}
265