Passed
Push — develop ( 8daac1...a2b8ae )
by Mykola
04:44
created

Loader::view()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 33
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 33
rs 8.8333
c 0
b 0
f 0
cc 7
nc 4
nop 2
1
<?php
2
/* 	Divine CMS - Open source CMS for widespread use.
3
    Copyright (c) 2019 Mykola Burakov ([email protected])
4
5
    See SOURCE.txt for other and additional information.
6
7
    This file is part of Divine CMS.
8
9
    This program is free software: you can redistribute it and/or modify
10
    it under the terms of the GNU General Public License as published by
11
    the Free Software Foundation, either version 3 of the License, or
12
    (at your option) any later version.
13
14
    This program is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
    GNU General Public License for more details.
18
19
    You should have received a copy of the GNU General Public License
20
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22
namespace Divine\Engine\Core;
23
24
final class Loader
25
{
26
    protected $registry;
27
28
    public function __construct($registry)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
29
    {
30
        $this->registry = $registry;
31
    }
32
33
    public function model($route)
34
    {
35
        // Sanitize the call
36
        $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string) $route);
37
38
        if (!$this->registry->has('model_' . str_replace(array('/', '-', '.'), array('_', '', ''), $route))) {
39
            $file  = SR_APPLICATION . 'model/' . $route . '.php';
40
            $class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', $route);
41
42
            if (is_file($file)) {
43
                include_once($file);
44
45
                $proxy = new \Divine\Engine\Core\Proxy();
46
47
                foreach (get_class_methods($class) as $method) {
48
                    $proxy->{$method} = $this->callback($this->registry, $route . '/' . $method);
49
                }
50
51
                $this->registry->set(
52
                    'model_' . str_replace(array('/', '-', '.'), array('_', '', ''), (string) $route),
53
                    $proxy
54
                );
55
            } else {
56
                throw new \Exception('Error: Could not load model ' . $route . '!');
57
            }
58
        }
59
    }
60
61
    public function controller($route, $data = array())
62
    {
63
        // Sanitize the call
64
        $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string) $route);
65
66
        $output = null;
67
68
        if (!$output) {
0 ignored issues
show
introduced by
$output is of type null, thus it always evaluated to false.
Loading history...
69
            $action = new \Divine\Engine\Core\Action($route);
70
            $output = $action->execute($this->registry, array(&$data));
71
        }
72
73
        return $output;
74
    }
75
76
    public function view($route, $data = array())
77
    {
78
        $output = null;
79
80
        // Sanitize the call
81
        $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string) $route);
82
83
        if (!$output) {
0 ignored issues
show
introduced by
$output is of type null, thus it always evaluated to false.
Loading history...
84
            $template = new \Divine\Engine\Library\Template();
85
86
            foreach ($data as $key => $value) {
87
                $template->set(
88
                    $key,
89
                    $value
90
                );
91
            }
92
93
            $output = $template->render($route);
94
95
            // Tracy Debugger
96
            $_SESSION['_tracy']['templates_log'][] = $route;
97
            //
98
99
            // Template paths
100
            if (defined('SR_TEMPLATE_PATHS_DEBUG') && true == SR_TEMPLATE_PATHS_DEBUG) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
The condition true == Divine\Engine\Core\SR_TEMPLATE_PATHS_DEBUG is always false.
Loading history...
101
                if (!empty($_SESSION[$_COOKIE['default']]['user_id']) && basename(SR_APPLICATION) == 'application') {
102
                    $output = '<span class="cc_template_path uk-label uk-background-secondary" id="cc_template_path">' . $route . '</span>' . $output . '';
103
                }
104
            }
105
            //
106
        }
107
108
        return $output;
109
    }
110
111
    public function library($route)
112
    {
113
        // Sanitize the call
114
        $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string) $route);
115
116
        $file = $_SERVER['DOCUMENT_ROOT'] . '/engine/library/' . $route . '.php';
117
        $class = str_replace('/', '\\', $route);
118
119
        if (is_file($file)) {
120
            include_once($file);
121
122
            $this->registry->set(
123
                basename($route),
124
                new $class($this->registry)
125
            );
126
        } else {
127
            throw new \Exception('Error: Could not load library ' . $route . '!');
128
        }
129
    }
130
131
    public function config($route)
132
    {
133
        $this->registry->get('config')->load($route);
134
    }
135
136
    public function language($route)
137
    {
138
        $output = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
139
140
        $output = $this->registry->get('language')->load($route);
141
142
        return $output;
143
    }
144
145
    protected function callback($registry, $route)
146
    {
147
        return function ($args) use ($registry, &$route) {
148
            static $model = array();
149
150
            $output = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
151
152
            // Store the model object
153
            if (!isset($model[$route])) {
154
                $file = SR_APPLICATION . 'model/' .  substr($route, 0, strrpos($route, '/')) . '.php';
155
                $class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', substr($route, 0, strrpos($route, '/')));
156
157
                if (is_file($file)) {
158
                    include_once($file);
159
160
                    $model[$route] = new $class($registry);
161
                } else {
162
                    throw new \Exception('Error: Could not load model ' . substr($route, 0, strrpos($route, '/')) . '!');
163
                }
164
            }
165
166
            $method = substr($route, strrpos($route, '/') + 1);
167
168
            $callable = array($model[$route], $method);
169
170
            if (is_callable($callable)) {
171
                $output = call_user_func_array($callable, $args);
172
            } else {
173
                throw new \Exception('Error: Could not call model/' . $route . '!');
174
            }
175
176
            return $output;
177
        };
178
    }
179
}
180