Ajde_Application::getRoute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
class Ajde_Application extends Ajde_Object_Singleton
4
{
5
    protected $_timers = [];
6
    protected $_timerLevel = 0;
7
8
    /**
9
     * @staticvar Ajde_Application $instance
10
     *
11
     * @return Ajde_Application
12
     */
13
    public static function getInstance()
14
    {
15
        static $instance;
16
17
        return $instance === null ? $instance = new self() : $instance;
18
    }
19
20
    /**
21
     * @return Ajde_Application
22
     */
23
    public static function app()
24
    {
25
        return self::getInstance();
26
    }
27
28
    /**
29
     * @return Ajde_Application
30
     */
31
    public static function create()
32
    {
33
        return self::getInstance();
34
    }
35
36
    public function addTimer($description)
37
    {
38
        $this->_timers[] = [
39
            'description' => $description,
40
            'level'       => $this->_timerLevel,
41
            'start'       => microtime(true),
42
            'end'         => null,
43
            'total'       => null,
44
        ];
45
        $this->_timerLevel++;
46
47
        return $this->getLastTimerKey();
48
    }
49
50
    public function getLastTimerKey()
51
    {
52
        end($this->_timers);
53
54
        return key($this->_timers);
55
    }
56
57
    public function endTimer($key)
58
    {
59
        $this->_timerLevel--;
60
        $this->_timers[$key]['end'] = $end = microtime(true);
61
        $this->_timers[$key]['total'] = round(($end - $this->_timers[$key]['start']) * 1000, 0);
62
63
        return $this->_timers[$key]['total'];
64
    }
65
66
    public function getTimers()
67
    {
68
        return $this->_timers;
69
    }
70
71
    public function run()
72
    {
73
        // For debugger
74
        $this->addTimer('<i>Application</i>');
75
76
        // Create fresh response
77
        $timer = $this->addTimer('Create response');
78
        $response = new Ajde_Http_Response();
79
        $this->setResponse($response);
0 ignored issues
show
Documentation Bug introduced by
The method setResponse does not exist on object<Ajde_Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
        $this->endTimer($timer);
81
82
        Ajde_Event::trigger($this, 'onAfterResponseCreated');
83
84
        // Bootstrap init
85
        $timer = $this->addTimer('Run bootstrap queue');
86
        $bootstrap = new Ajde_Core_Bootstrap();
87
        $bootstrap->run();
88
        $this->endTimer($timer);
89
90
        Ajde_Event::trigger($this, 'onAfterBootstrap');
91
92
        // Get request
93
        $timer = $this->addTimer('Read in global request');
94
        $request = $this->loadRequest();
95
        $this->endTimer($timer);
96
97
        Ajde_Event::trigger($this, 'onAfterRequestCreated');
98
99
        // Get route
100
        $timer = $this->addTimer('Initialize route');
101
        $route = $request->initRoute();
102
        $this->setRoute($route);
0 ignored issues
show
Documentation Bug introduced by
The method setRoute does not exist on object<Ajde_Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
103
        $this->endTimer($timer);
104
105
        Ajde_Event::trigger($this, 'onAfterRouteInitialized');
106
107
        // Load document
108
        $timer = $this->addTimer('Create document');
109
        $document = Ajde_Document::fromRoute($route);
110
        $this->setDocument($document);
0 ignored issues
show
Documentation Bug introduced by
The method setDocument does not exist on object<Ajde_Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
111
        $this->endTimer($timer);
112
113
        Ajde_Event::trigger($this, 'onAfterDocumentCreated');
114
115
        // Load controller
116
        $timer = $this->addTimer('Load controller');
117
        $controller = Ajde_Controller::fromRoute($route);
118
        $this->setController($controller);
0 ignored issues
show
Documentation Bug introduced by
The method setController does not exist on object<Ajde_Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
119
        $this->endTimer($timer);
120
121
        Ajde_Event::trigger($this, 'onAfterControllerCreated');
122
123
        // Invoke controller action
124
        $timer = $this->addTimer('Invoke controller');
125
        $actionResult = $controller->invoke();
126
        $document->setBody($actionResult);
127
        $this->endTimer($timer);
128
129
        Ajde_Event::trigger($this, 'onAfterControllerInvoked');
130
131
        // Get document contents
132
        $timer = $this->addTimer('Render document');
133
        $contents = $document->render();
134
        $this->endTimer($timer);
135
136
        Ajde_Event::trigger($this, 'onAfterDocumentRendered');
137
138
        // Let the cache handle the contents and have it saved to the response
139
        $timer = $this->addTimer('Save to response');
140
        $cache = Ajde_Cache::getInstance();
141
        $cache->setContents($contents);
142
        $cache->saveResponse();
143
        $this->endTimer($timer);
144
145
        Ajde_Event::trigger($this, 'onAfterResponseSaved');
146
147
        // Output the buffer
148
        $response->send();
149
150
        Ajde_Event::trigger($this, 'onAfterResponseSent');
151
    }
152
153
    public static function routingError(Exception $exception)
154
    {
155
        if (config('app.debug') === true) {
156
            throw $exception;
157
        } else {
158
            if (class_exists('Ajde_Exception_Log')) {
159
                Ajde_Exception_Log::logException($exception);
0 ignored issues
show
Documentation introduced by
$exception is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
            }
161
            Ajde_Http_Response::redirectNotFound();
162
        }
163
    }
164
165
    public function loadRequest()
166
    {
167
        $request = Ajde_Http_Request::fromGlobal();
168
        $this->setRequest($request);
0 ignored issues
show
Documentation Bug introduced by
The method setRequest does not exist on object<Ajde_Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
169
170
        return $request;
171
    }
172
173
    /**
174
     * @return Ajde_Http_Request
175
     */
176
    public function getRequest()
177
    {
178
        if (!$this->has('request')) {
179
            $this->loadRequest();
180
        }
181
182
        return $this->get('request');
183
    }
184
185
    /**
186
     * @return Ajde_Http_Response
187
     */
188
    public function getResponse()
189
    {
190
        return $this->get('response');
191
    }
192
193
    /**
194
     * @return Ajde_Core_Route
195
     */
196
    public function getRoute()
197
    {
198
        return $this->get('route');
199
    }
200
201
    /**
202
     * @return Ajde_Document
203
     */
204
    public function getDocument()
205
    {
206
        return $this->get('document');
207
    }
208
209
    /**
210
     * @return Ajde_Controller
211
     */
212
    public function getController()
213
    {
214
        return $this->get('controller');
215
    }
216
217
    public static function includeFile($filename)
218
    {
219
        Ajde_Cache::getInstance()->addFile($filename);
220
        include $filename;
221
    }
222
223
    public static function includeFileOnce($filename)
224
    {
225
        Ajde_Cache::getInstance()->addFile($filename);
226
        include_once $filename;
227
    }
228
}
229