Completed
Push — master ( c4f7f5...ee49f4 )
by Alejandro
04:40
created

Tight::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Tight;
4
5
/*
6
 * The MIT License
7
 *
8
 * Copyright 2016 Alejandro Peña Florentín ([email protected])
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to deal
12
 * in the Software without restriction, including without limitation the rights
13
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in
18
 * all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 */
28
29
/**
30
 * Tight framework main class
31
 *
32
 * @author Alejandro Peña Florentín ([email protected])
33
 */
34
class Tight
35
{
36
37
    /**
38
     * Current Version
39
     */
40
    const VERSION = "v1.0.1";
41
42
    private static $INSTANCE = null;
43
44
    /**
45
     *
46
     * @var Tight\TightConfig Configuration
47
     */
48
    private $config = null;
49
50
    /**
51
     *
52
     * @var Tight\Router 
53
     */
54
    private $router;
55
    private $smarty;
56
57
    /**
58
     * Creates an instance of Tight Framework.
59
     * @param array $config Settings to override the config file
60
     */
61
    public function __construct($config = []) {
62
        set_exception_handler([$this, "exceptionHandler"]);
63
        $this->setConfig($config);
64
        $this->router = new Router($this->config->basePath);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Tight\Router($this->config->basePath) of type object<Tight\Router> is incompatible with the declared type object<Tight\Tight\Router> of property $router.

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...
65
        $this->smarty = new \Smarty;
66
        $this->smarty->template_dir = $this->config->smarty["template_dir"];
67
        $this->smarty->compile_dir = $this->config->smarty["compile_dir"];
68
        $this->smarty->config_dir = $this->config->smarty["config_dir"];
69
        $this->smarty->cache_dir = $this->config->smarty["cache_dir"];
70
    }
71
72
    public static function getInstance() {
73
        if (null !== self::$INSTANCE) {
74
            return self::$INSTANCE;
75
        } else {
76
            return new \Tight\Tight;
77
        }
78
    }
79
80
    public function setConfig($config) {
81
        if (is_array($config)) {
82
            $config = new \Tight\TightConfig($config);
83
        } elseif (!$config instanceof \Tight\TightConfig) {
84
            throw new \InvalidArgumentException("Argument passed to Tight::__constructor must be array or <b>\Tight\TightConfig</b> object");
85
        }
86
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type object<Tight\TightConfig> is incompatible with the declared type object<Tight\Tight\TightConfig> of property $config.

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...
87
    }
88
89
    public function getConfig() {
90
        return $this->config;
91
    }
92
93
    /**
94
     * 
95
     * @return \Smarty
96
     */
97
    public function getSmarty() {
98
        return $this->smarty;
99
    }
100
101
    /**
102
     * Custom exception handler
103
     * @param \Exception $ex Exception
104
     * @codeCoverageIgnore
105
     */
106
    public function exceptionHandler($ex) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ex. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
107
        $lastTrace = $ex->getTrace()[count($ex->getTrace()) - 1];
108
        $output = <<<EXC
109
                <!DOCTYPE>
110
                <html>
111
                    <head>
112
                        <title>Tight Framework Exception</title>
113
                        <style>
114
                        *{
115
                            margin: 0;
116
                            padding: 0;
117
                        }
118
                        html {
119
                            font-family: Helvetica;
120
                        }
121
                        body {
122
                            padding: 1em;
123
                        }
124
                        h1 {
125
                            margin-bottom: 0.5em;
126
                        }
127
                        p {
128
                            margin-top: 0.25em;
129
                            margin-bottom: 0.5em;
130
                        }
131
                        .padding-left {
132
                            padding-left: 2em;
133
                        }
134
                        </style>
135
                    </head>
136
                    <body>
137
                        <h1>Tight Framework Exception</h1>
138
EXC;
139
        $output .= "<p><strong>" . get_class($ex) . ": </strong>" . $ex->getMessage() . "</p>";
140
        $output .= "<p class='padding-left'>in <strong>" . $lastTrace['file'] . "</strong> at line <strong>" . $lastTrace['line'] . "</strong></p>";
141
        $output .= "<br/>";
142
        $output .= "<p>Stack Trace:</p>";
143
        $trace = $ex->getTrace();
144
        for ($index = 0; $index < count($trace); $index++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
145
            $el = $trace[$index];
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $el. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
146
            $class = isset($el["class"]) ? $el["class"] : "";
147
            $type = isset($el["type"]) ? $el["type"] : "";
148
            $func = isset($el["function"]) ? $el["function"] : "";
149
            $file = isset($el["file"]) ? "at <strong>" . $el["file"] . "</strong>" : "";
150
            $line = isset($el["line"]) ? "at line <strong>" . $el["line"] . "</strong>" : "";
151
            $output .= "<p>#" . ($index + 1) . ": " . $class . $type . $func . "() ". $file .  $line . "</strong></p>";
152
        }
153
        echo $output;
154
    }
155
156
    /**
157
     * 
158
     * @return \Tight\Router
0 ignored issues
show
Documentation introduced by
Should the return type not be Tight\Router?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
159
     */
160
    public function getRouter() {
161
        return $this->router;
162
    }
163
164
    /**
165
     * Runs the router to send response to the request URI
166
     */
167
    public function run() {
168
        if ($this->config->mvc["enableRouter"]) {
169
            $this->router->runMvc();
170
        } else {
171
            $this->router->run();
172
        }
173
    }
174
175
}
176