Completed
Push — master ( 5579ff...34af96 )
by Alejandro
02:43
created

Tight::getLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
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.1.0-dev";
41
42
    private static $INSTANCE = null;
43
44
    /**
45
     *
46
     * @var Tight\TightConfig Configuration
47
     */
48
    private $config = null;
49
50
    /**
51
     * @var Tight\Router Router
52
     */
53
    private $router;
54
55
    /**
56
     * @var \Smarty Smarty templates system
57
     */
58
    private $smarty;
59
60
    /**
61
     * @var \Tight\Modules\Localize\Localize Localize module for translations
62
     */
63
    private $locale;
64
65
    /**
66
     * Creates an instance of Tight Framework.
67
     * @param array $config Settings to override the config file
68
     */
69
    public function __construct($config = []) {
70
        // Sets the instance
71
        self::$INSTANCE = $this;
72
        set_exception_handler([$this, "exceptionHandler"]);
73
        $this->setConfig($config);
74
        $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...
75
        $this->smarty = new \Smarty;
76
        $this->smarty->template_dir = $this->config->smarty["template_dir"];
77
        $this->smarty->compile_dir = $this->config->smarty["compile_dir"];
78
        $this->smarty->config_dir = $this->config->smarty["config_dir"];
79
        $this->smarty->cache_dir = $this->config->smarty["cache_dir"];
80
        $this->locale = new \Tight\Modules\Localize\Localize($this->config->locale);
81
    }
82
83
    /**
84
     * Gets the class instance
85
     * @return \Tight\Tight
86
     */
87
    public static function getInstance() {
88
        if (null !== self::$INSTANCE) {
89
            return self::$INSTANCE;
90
        } else {
91
            return new \Tight\Tight;
92
        }
93
    }
94
95
    /**
96
     * Sets the configuration
97
     * @param array|\Tight\TightConfig $config Configuration object or 
98
     * associative array with available options
99
     * @throws \InvalidArgumentException If $config is not an array or an 
100
     * instance of \Tight\TightConfig class
101
     */
102
    public function setConfig($config) {
103
        if (is_array($config)) {
104
            $config = new \Tight\TightConfig($config);
105
        } elseif (!$config instanceof \Tight\TightConfig) {
106
            throw new \InvalidArgumentException("Argument passed to Tight::__constructor must be array or <b>\Tight\TightConfig</b> object");
107
        }
108
        $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...
109
    }
110
111
    /**
112
     * Gets the configuration
113
     * @return \Tight\TightConfig
0 ignored issues
show
Documentation introduced by
Should the return type not be Tight\TightConfig?

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...
114
     */
115
    public function getConfig() {
116
        return $this->config;
117
    }
118
119
    /**
120
     * Gets the Smarty
121
     * @return \Smarty
122
     */
123
    public function getSmarty() {
124
        return $this->smarty;
125
    }
126
127
    /**
128
     * Gets locale
129
     * @return \Tight\Modules\Localize\Localize
130
     */
131
    public function getLocale() {
132
        return $this->locale;
133
    }
134
135
    /**
136
     * Custom exception handler
137
     * @param \Exception $ex Exception
138
     */
139
    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...
140
        self::printException($ex);
141
    }
142
143
    /**
144
     * Custom exception handler
145
     * @param \Exception $ex Exception
146
     * @codeCoverageIgnore
147
     */
148
    public static function printException($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...
149
        $lastTrace = $ex->getTrace()[count($ex->getTrace()) - 1];
150
        $output = <<<EXC
151
                <!DOCTYPE>
152
                <html>
153
                    <head>
154
                        <title>Tight Framework Exception</title>
155
                        <style>
156
                        *{
157
                            margin: 0;
158
                            padding: 0;
159
                        }
160
                        html {
161
                            font-family: Helvetica;
162
                        }
163
                        body {
164
                            padding: 1em;
165
                        }
166
                        h1 {
167
                            margin-bottom: 0.5em;
168
                        }
169
                        p {
170
                            margin-top: 0.25em;
171
                            margin-bottom: 0.5em;
172
                        }
173
                        .padding-left {
174
                            padding-left: 2em;
175
                        }
176
                        </style>
177
                    </head>
178
                    <body>
179
                        <h1>Tight Framework Exception</h1>
180
EXC;
181
        $output .= "<p><strong>" . get_class($ex) . ": </strong>" . $ex->getMessage() . "</p>";
182
        $output .= "<p class='padding-left'>in <strong>" . $lastTrace['file'] . "</strong> at line <strong>" . $lastTrace['line'] . "</strong></p>";
183
        $output .= "<br/>";
184
        $output .= "<p>Stack Trace:</p>";
185
        $trace = $ex->getTrace();
186
        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...
187
            $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...
188
            $class = isset($el["class"]) ? $el["class"] : "";
189
            $type = isset($el["type"]) ? $el["type"] : "";
190
            $func = isset($el["function"]) ? $el["function"] : "";
191
            $file = isset($el["file"]) ? "at <strong>" . $el["file"] . "</strong>" : "";
192
            $line = isset($el["line"]) ? " at line <strong>" . $el["line"] . "</strong>" : "";
193
            $output .= "<p>#" . ($index + 1) . ": " . $class . $type . $func . "() " . $file . $line . "</strong></p>";
194
        }
195
        echo $output;
196
    }
197
198
    /**
199
     * Gets the router
200
     * @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...
201
     */
202
    public function getRouter() {
203
        return $this->router;
204
    }
205
206
    /**
207
     * Runs the router to send response to the request URI
208
     */
209
    public function run() {
210
        if ($this->config->mvc["enableRouter"]) {
211
            $this->router->runMvc();
212
        } else {
213
            $this->router->run();
214
        }
215
    }
216
217
}
218