Passed
Push — master ( 1f7fc5...40eec0 )
by Darío
02:01
created

Layout::setBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Mvc;
12
13
use Drone\Mvc\AbstractController;
14
use Drone\Mvc\Exception;
15
16
/**
17
 * Layout class
18
 *
19
 * This class manages templates from views
20
 */
21
class Layout
22
{
23
    use \Drone\Util\ParamTrait;
24
25
    /**
26
     * Controller instance
27
     *
28
     * @var AbstractController
29
     */
30
    private $controller;
31
32
    /**
33
     * View path
34
     *
35
     * @var string
36
     */
37
    private $view;
38
39
    /**
40
     * Document title
41
     *
42
     * @var string
43
     */
44
    private $title;
45
46
    /**
47
     * Document description
48
     *
49
     * @var string
50
     */
51
    private $description;
52
53
    /**
54
     * Document image
55
     *
56
     * @var string
57
     */
58
    private $image;
59
60
    /**
61
     * Returns the instance of current controller
62
     *
63
     * @return AbstractController
64
     */
65
    public function getController()
66
    {
67
        return $this->controller;
68
    }
69
70
    /**
71
     * Returns the view
72
     *
73
     * @return string
74
     */
75
    public function getView()
76
    {
77
        return $this->view;
78
    }
79
80
    /**
81
     * Returns the document title
82
     *
83
     * @return string
84
     */
85
    public function getTitle()
86
    {
87
        return $this->title;
88
    }
89
90
    /**
91
     * Returns the document description
92
     *
93
     * @return string
94
     */
95
    public function getDescription()
96
    {
97
        return $this->description;
98
    }
99
100
    /**
101
     * Returns the document image
102
     *
103
     * @return string
104
     */
105
    public function getImage()
106
    {
107
        return $this->image;
108
    }
109
110
    /**
111
     * Sets the document title
112
     *
113
     * @param string $title
114
     *
115
     * @return null
116
     */
117
    public function setTitle($title)
118
    {
119
        $this->title = $title;
120
    }
121
122
    /**
123
     * Sets the document description
124
     *
125
     * @param string $description
126
     *
127
     * @return null
128
     */
129
    public function setDescription($description)
130
    {
131
        $this->description = $description;
132
    }
133
134
    /**
135
     * Sets the document image
136
     *
137
     * @param string $image
138
     *
139
     * @return null
140
     */
141
    public function setImage($image)
142
    {
143
        $this->image = $image;
144
    }
145
146
    /**
147
     * Sets the view
148
     *
149
     * @param AbstractionModule $module
0 ignored issues
show
Bug introduced by
The type Drone\Mvc\AbstractionModule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
150
     * @param string $view
151
     *
152
     * @return null
153
     */
154
    public function setView($module, $view)
155
    {
156
        $config = $module->getConfig();
157
158
        if (!array_key_exists($view, $config["view_manager"]["view_map"]) || !file_exists($config["view_manager"]["view_map"][$view]))
159
            throw new Exception\ViewNotFoundException("The 'view' template " . $view . " does not exists");
160
161
        $this->view = $config["view_manager"]["view_map"][$view];
162
    }
163
164
    /**
165
     * Constructor
166
     *
167
     * All modifiable attributes (i.e. with setter method) can be passed as key
168
     *
169
     * @param array $params
170
     *
171
     * @throws Exception\PageNotFoundException
172
     */
173
    public function __construct(array $params = [])
174
    {
175
        foreach ($params as $param => $value)
176
        {
177
            if (property_exists(__CLASS__, strtolower($param)) && method_exists($this, 'set'.$param))
178
                $this->{'set'.$param}($value);
179
        }
180
    }
181
182
    /**
183
     * Loads a view from a controller
184
     *
185
     * @throws Exception\PageNotFoundException
186
     *
187
     * @param AbstractController
188
     *
189
     * @return null
190
     */
191
    public function fromController(AbstractController $controller)
192
    {
193
        $this->setParams($controller->getParams());
194
        $this->controller = $controller;
195
196
        if (is_null($controller->getModule()))
197
            throw new \RuntimeException("No module instance found in controller '" . get_class($controller) . "'");
198
199
        if ($controller->getShowView())
200
            $this->view =
201
                $controller->getModule()->getModulePath() .'/'. $controller->getModule()->getModuleName() .'/'.
202
                $controller->getModule()->getViewPath()                .'/'.
203
                basename(str_replace('\\','/',get_class($controller))) .'/'.
204
                $controller->getMethod() . '.phtml';
205
206
        if ($controller->getTerminal())
207
        {
208
            if (file_exists($this->view))
209
                include $this->view;
210
        }
211
        else
212
        {
213
            if (!is_null($this->view) && !file_exists($this->view))
0 ignored issues
show
introduced by
The condition is_null($this->view) is always false.
Loading history...
214
                throw new Exception\ViewNotFoundException("The 'view' template " . $this->view . " does not exists");
215
216
            $config = $controller->getModule()->getConfig();
217
218
            $layout = $controller->getLayout();
219
220
            if (!array_key_exists($controller->getLayout(), $config["view_manager"]["template_map"]))
221
                throw new Exception\PageNotFoundException("The 'template' " . $layout . " was not defined in module.config.php");
222
223
            $template = $config["view_manager"]["template_map"][$controller->getLayout()];
224
225
            if (!file_exists($template))
226
                throw new Exception\PageNotFoundException("The 'template' " . $template . " does not exists");
227
228
            include $template;
229
        }
230
    }
231
232
    /**
233
     * Loads a view from a template file
234
     *
235
     * @throws Exception\PageNotFoundException
236
     *
237
     * @param AbstractionModule $module
238
     * @param string $template
239
     *
240
     * @return null
241
     */
242
    public function fromTemplate($module, $template)
243
    {
244
        $config = $module->getConfig();
245
        include $config["view_manager"]["template_map"][$template];
246
    }
247
248
    /**
249
     * Includes the file view
250
     *
251
     * @return null
252
     */
253
    public function content()
254
    {
255
        if (!file_exists($this->view))
256
            throw new Exception\ViewNotFoundException("The 'view' template " . $this->view . " does not exists");
257
258
        include $this->view;
259
    }
260
261
    /**
262
     * Alias to return the base path of the application
263
     *
264
     * @return string
265
     */
266
    public function basePath()
267
    {
268
        return $this->controller->getModule()->getRouter()->getBasePath();
269
    }
270
}