PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 8ms RepoModel.GetById 33ms app.codeStats 0ms

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

https://bitbucket.org/yelinaung/reborn
PHP | 271 lines | 140 code | 47 blank | 84 comment | 8 complexity | d3d08a1017934d593abe26b4d773d7c9 MD5 | raw file
  1<?php
  2
  3namespace Reborn\Cores;
  4
  5use Symfony\Component\HttpFoundation\Request as BaseRequest;
  6use Symfony\Component\HttpFoundation\Response as BaseResponse;
  7use Symfony\Component\HttpFoundation\RedirectResponse;
  8//use Symfony\Component\HttpKernel\HttpKernelInterface as HttpKernelInterface;
  9
 10/**
 11 * Main Application Class for Reborn CMS
 12 *
 13 * @package Cores
 14 * @author Reborn CMS Development Team
 15 **/
 16class Application extends \Pimple
 17{
 18
 19    /**
 20     * Marking for application started or not
 21     *
 22     * @var bool
 23     **/
 24    protected $started = false;
 25
 26    /**
 27     * Constructor Method
 28     * Create new object for Reborn Application
 29     *
 30     * @return void
 31     **/
 32    public function __construct()
 33    {
 34        $this['request'] = Request::createFromGlobals();
 35
 36        $this['response'] = Response::create('');
 37
 38        $this['router'] =  $this->share(function ($this) {
 39            return new Router($this);
 40        });
 41
 42        /*$this['input'] = $this->share(function ($this) {
 43            return new Input($this);
 44        });*/
 45
 46        $this['log'] = $this->share(function () {
 47            return new Log();
 48        });
 49
 50        $this['view'] = $this->share( function() {
 51            return new View();
 52        });
 53
 54        $controller_container = new ControllerContainer();
 55        $this['controller.container'] = $controller_container->creator($this);
 56    }
 57
 58    /**
 59     * Start the application
 60     * Make decision for Request, Response, Config
 61     *
 62     * @return void
 63     **/
 64    public function start()
 65    {
 66        if($this->started)
 67        {
 68            throw new \RbException("Reborn CMS Application is already started!");
 69        }
 70
 71        // Set Exception and Error Handler
 72        $this->setExceptionHandler();
 73        $this->setErrorHandler();
 74
 75        // call the appInitialize method
 76        $this->appInitialize();
 77
 78        $response = $this['router']->dispatch();
 79
 80        if(! $response instanceof Response)
 81        {
 82            $response = new Response($response);
 83        }
 84
 85        //dump($response);
 86
 87        $this->started = true;
 88
 89        // Send response to the end method
 90        $this->end($response);
 91    }
 92
 93    /**
 94     * Start the Initialize method from require classes.
 95     * But this method is call from application start method only.
 96     * Don't call more than once.
 97     *
 98     */
 99    public function appInitialize()
100    {
101        if($this->started)
102        {
103            return true;
104        }
105
106        // Start the Database initialize
107        DB::initialize();
108
109        // Start the Input Class
110        Input::create($this);
111
112        // Start the Event initialize
113        Event::initialize();
114
115        // Start the Setting initialize
116        Setting::initialize();
117
118        // Start the Module initialize
119        Module::initialize();
120    }
121
122    /**
123     * End point of application
124     *
125     * @return void
126     **/
127    public function end(Response $response)
128    {
129        $response->prepare($this['request']);
130
131        if($response->getContent() == '')
132        {
133            // Set content is empty
134            $response->setStatusCode(204);
135        }
136
137        if($response->isNotModified($this['request']))
138        {
139            //dump($response->getStatusCode());
140        }
141        else
142        {
143           // dump($response->isNotModified($this['request']));
144        }
145
146        $time = number_format((microtime(true) - REBORN_START_TIME) * 1000, 4)."ms";
147        $mem = memory_get_peak_usage() - REBORN_START_MEMORY;
148        $mem = (round($mem / pow(1024, 2), 3)."MB");
149
150        $profiler = "<p>$time $mem</p>";
151
152        $response->setContent($response->getContent().$profiler);
153
154        return $response->send();
155    }
156
157    /**
158     * Set Locale for application
159     * Default locale is en
160     *
161     * @param string $locale
162     * @return void
163     **/
164    public function setLocale($locale = 'en')
165    {
166        $this['locale'] = $locale;
167    }
168
169    /**
170     * undocumented function
171     *
172     * @return void
173     **/
174    public function setExceptionHandler()
175    {
176        //$exp = new RbException(\Exception $e);
177        //set_exception_handler(array($exp, "getStaticException"));
178    }
179
180    /**
181     * Set the Fatal Error Handler
182     *
183     * @return void
184     */
185    public function setErrorHandler()
186    {
187        set_error_handler(array($this, "rbErrorHandler"));
188    }
189
190    public function rbErrorHandler($errno, $errstr, $errfile, $errline)
191    {
192        if (!(error_reporting() & $errno))
193        {
194            // This error code is not included in error_reporting
195            return;
196        }
197
198        $style = <<<STYLE
199        <style>
200            p.php_error {
201                background:#f9f9f9;
202                color:#d00;
203                border:1px solid #aaa;
204                padding:10px;
205                margin:15px;
206                box-shadow: 2px 2px 2px #ccc;
207                border-radius: 4px;
208            }
209            span.err_var {
210                font-weight: bold;
211            }
212        </style>
213STYLE;
214        echo $style;
215
216        echo '<p class="php_error">';
217        switch ($errno) {
218            case E_USER_ERROR:
219                echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
220                echo "  Fatal error on line $errline in file $errfile";
221                echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
222                echo "Aborting...<br />\n";
223                exit(1);
224                break;
225
226            case E_USER_WARNING:
227                echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
228                break;
229
230            case E_USER_NOTICE:
231                echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
232                break;
233
234            default:
235                echo "Unknown error type: [$errno] { $errstr } at line";
236                echo " <span class='err_var';>$errline</span>";
237                echo " from <span class='err_var';>$errfile</span><br />\n";
238                break;
239            }
240
241        echo '</p>';
242
243            /* Don't execute PHP internal error handler */
244            return true;
245
246    }
247
248    /**
249     * Magic setter method
250     *
251     * @param string $key
252     * @param mixed $value
253     * @return void
254     **/
255    public function __set($key, $value)
256    {
257        $this[$key] = $value;
258    }
259
260    /**
261     * Magic getter method
262     *
263     * @param string $key
264     * @return mixed
265     **/
266    public function __get($key)
267    {
268        return isset($this[$key]) ? $this[$key] : null;
269    }
270
271} // END class Application