reborn /heart/reborn/src/Reborn/Cores/Application.php

Language PHP Lines 272
MD5 Hash d3d08a1017934d593abe26b4d773d7c9 Estimated Cost $3,403 (why?)
Repository https://bitbucket.org/yelinaung/reborn.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php

namespace Reborn\Cores;

use Symfony\Component\HttpFoundation\Request as BaseRequest;
use Symfony\Component\HttpFoundation\Response as BaseResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
//use Symfony\Component\HttpKernel\HttpKernelInterface as HttpKernelInterface;

/**
 * Main Application Class for Reborn CMS
 *
 * @package Cores
 * @author Reborn CMS Development Team
 **/
class Application extends \Pimple
{

    /**
     * Marking for application started or not
     *
     * @var bool
     **/
    protected $started = false;

    /**
     * Constructor Method
     * Create new object for Reborn Application
     *
     * @return void
     **/
    public function __construct()
    {
        $this['request'] = Request::createFromGlobals();

        $this['response'] = Response::create('');

        $this['router'] =  $this->share(function ($this) {
            return new Router($this);
        });

        /*$this['input'] = $this->share(function ($this) {
            return new Input($this);
        });*/

        $this['log'] = $this->share(function () {
            return new Log();
        });

        $this['view'] = $this->share( function() {
            return new View();
        });

        $controller_container = new ControllerContainer();
        $this['controller.container'] = $controller_container->creator($this);
    }

    /**
     * Start the application
     * Make decision for Request, Response, Config
     *
     * @return void
     **/
    public function start()
    {
        if($this->started)
        {
            throw new \RbException("Reborn CMS Application is already started!");
        }

        // Set Exception and Error Handler
        $this->setExceptionHandler();
        $this->setErrorHandler();

        // call the appInitialize method
        $this->appInitialize();

        $response = $this['router']->dispatch();

        if(! $response instanceof Response)
        {
            $response = new Response($response);
        }

        //dump($response);

        $this->started = true;

        // Send response to the end method
        $this->end($response);
    }

    /**
     * Start the Initialize method from require classes.
     * But this method is call from application start method only.
     * Don't call more than once.
     *
     */
    public function appInitialize()
    {
        if($this->started)
        {
            return true;
        }

        // Start the Database initialize
        DB::initialize();

        // Start the Input Class
        Input::create($this);

        // Start the Event initialize
        Event::initialize();

        // Start the Setting initialize
        Setting::initialize();

        // Start the Module initialize
        Module::initialize();
    }

    /**
     * End point of application
     *
     * @return void
     **/
    public function end(Response $response)
    {
        $response->prepare($this['request']);

        if($response->getContent() == '')
        {
            // Set content is empty
            $response->setStatusCode(204);
        }

        if($response->isNotModified($this['request']))
        {
            //dump($response->getStatusCode());
        }
        else
        {
           // dump($response->isNotModified($this['request']));
        }

        $time = number_format((microtime(true) - REBORN_START_TIME) * 1000, 4)."ms";
        $mem = memory_get_peak_usage() - REBORN_START_MEMORY;
        $mem = (round($mem / pow(1024, 2), 3)."MB");

        $profiler = "<p>$time $mem</p>";

        $response->setContent($response->getContent().$profiler);

        return $response->send();
    }

    /**
     * Set Locale for application
     * Default locale is en
     *
     * @param string $locale
     * @return void
     **/
    public function setLocale($locale = 'en')
    {
        $this['locale'] = $locale;
    }

    /**
     * undocumented function
     *
     * @return void
     **/
    public function setExceptionHandler()
    {
        //$exp = new RbException(\Exception $e);
        //set_exception_handler(array($exp, "getStaticException"));
    }

    /**
     * Set the Fatal Error Handler
     *
     * @return void
     */
    public function setErrorHandler()
    {
        set_error_handler(array($this, "rbErrorHandler"));
    }

    public function rbErrorHandler($errno, $errstr, $errfile, $errline)
    {
        if (!(error_reporting() & $errno))
        {
            // This error code is not included in error_reporting
            return;
        }

        $style = <<<STYLE
        <style>
            p.php_error {
                background:#f9f9f9;
                color:#d00;
                border:1px solid #aaa;
                padding:10px;
                margin:15px;
                box-shadow: 2px 2px 2px #ccc;
                border-radius: 4px;
            }
            span.err_var {
                font-weight: bold;
            }
        </style>
STYLE;
        echo $style;

        echo '<p class="php_error">';
        switch ($errno) {
            case E_USER_ERROR:
                echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
                echo "  Fatal error on line $errline in file $errfile";
                echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
                echo "Aborting...<br />\n";
                exit(1);
                break;

            case E_USER_WARNING:
                echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
                break;

            case E_USER_NOTICE:
                echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
                break;

            default:
                echo "Unknown error type: [$errno] { $errstr } at line";
                echo " <span class='err_var';>$errline</span>";
                echo " from <span class='err_var';>$errfile</span><br />\n";
                break;
            }

        echo '</p>';

            /* Don't execute PHP internal error handler */
            return true;

    }

    /**
     * Magic setter method
     *
     * @param string $key
     * @param mixed $value
     * @return void
     **/
    public function __set($key, $value)
    {
        $this[$key] = $value;
    }

    /**
     * Magic getter method
     *
     * @param string $key
     * @return mixed
     **/
    public function __get($key)
    {
        return isset($this[$key]) ? $this[$key] : null;
    }

} // END class Application
Back to Top