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