/Library/Spawn/Spawn.php

https://github.com/Spawnm/Spawn-Framework · PHP · 201 lines · 129 code · 28 blank · 44 comment · 15 complexity · 31c85554695da4815cf4ad46576a80d7 MD5 · raw file

  1. <?php
  2. /**
  3. * Spawn Framework
  4. *
  5. * Front controller
  6. *
  7. * @author Paweł Makowski
  8. * @copyright (c) 2010-2014 Paweł Makowski
  9. * @license http://spawnframework.com/license New BSD License
  10. */
  11. namespace Spawn;
  12. final class Spawn
  13. {
  14. /**
  15. * @var \Spawn\Loader
  16. */
  17. public $loader;
  18. /**
  19. * @var \Spawn\Router
  20. */
  21. public $router;
  22. /**
  23. * default action
  24. * @var string
  25. */
  26. public $action;
  27. /**
  28. * default controller
  29. * @var string
  30. */
  31. public $controller;
  32. /*
  33. * @var string
  34. */
  35. public static $controllerSeparator = '-';
  36. /**
  37. * load bootstrap.php
  38. */
  39. public function bootstrap($di)
  40. {
  41. $bootstrap = new \Bootstrap($di);
  42. $bootstrap->start();
  43. }
  44. /**
  45. * load page
  46. */
  47. public function init()
  48. {
  49. ob_start();
  50. try{
  51. $di = new DI;
  52. $this -> event = new Event($di);
  53. $firewall = new \Spawn\Firewall($di);
  54. $di->set('event', $this->event);
  55. $di->set('firewall', $firewall);
  56. $this -> bootstrap($di);
  57. $this -> event -> run('Spawn.Ready') -> delete('Spawn.Ready');
  58. register_shutdown_function( array($this, 'shutdown') );
  59. $uri = new Request\Uri();
  60. $uri -> initPath() -> initArgs();
  61. $controller_core = $uri->param(0, $this -> controller);
  62. $controller = '\Controller\\' .str_replace(' ', '\\',ucwords(str_replace(self::$controllerSeparator, ' ', $controller_core)));
  63. $action = $uri->param(1, $this -> action ).'Action';
  64. if( !class_exists($controller) or (class_exists($controller) && !method_exists($controller, $action )) ){
  65. $controller_core = $controller_core . self::$controllerSeparator . $this->controller;
  66. $controller = $controller.'\\'.$this->controller;
  67. }
  68. if( !class_exists($controller) or (class_exists($controller) && !method_exists($controller, $action )) ){
  69. $this -> router -> init( $uri );
  70. $uri = $this -> router -> getUri();
  71. $controller = '\Controller\\' . str_replace(' ', '\\', ucwords(str_replace(self::$controllerSeparator, ' ', $this -> router -> getController())));
  72. $action = $this -> router -> getAction() . 'Action';
  73. }else{
  74. $uri -> setParam(0, $controller_core ) -> setParam(1, $uri->param(1, $this -> action ) );
  75. }
  76. if( !class_exists($controller) or (class_exists($controller) && !method_exists($controller, $action )) ){
  77. self::error404();
  78. }
  79. $firewall->start();
  80. $di->delete('firewall');
  81. $this -> event -> run('Spawn.Execute') -> delete('Spawn.Execute');
  82. $controller = new $controller;
  83. $request = new Request();
  84. $request -> uri = $uri;
  85. $di->set('request', $request);
  86. $controller->di = $di;
  87. $controller -> request = $request;
  88. $controller -> loader = $this -> loader;
  89. $controller -> event = $this -> event;
  90. $controller -> init();
  91. $controller -> $action( $request );
  92. $controller -> end();
  93. echo $controller -> response;
  94. $this -> event -> run('Spawn.Finish') -> delete('Spawn.Finish');
  95. }catch(\Exception $error) {
  96. $this -> event -> run('Spawn.Exception', $error) -> delete('Spawn.Exception');
  97. self::exceptionPage($error);
  98. }
  99. }
  100. /**
  101. *
  102. */
  103. public function shutdown()
  104. {
  105. $this -> event -> run('Spawn.Shutdown');
  106. }
  107. /**
  108. *
  109. */
  110. public static function baseDetect()
  111. {
  112. $script_name = $_SERVER['SCRIPT_NAME'];
  113. $sn = explode('/', $script_name);
  114. $index = current(array_reverse($sn));
  115. $base = str_replace($index,'',$script_name);
  116. if(strpos($base, '/')!==0) {
  117. throw new \Exception('Invalid script name');
  118. }
  119. if(Config::load('Uri')->get('base') != $base) {
  120. $config = include(ROOT_PATH.'Bin/Config/Uri.php');
  121. $config['base'] = $base;
  122. Config::load('Uri')->set('base',$base);
  123. $data = '<?php '.PHP_EOL.'return $config = '.var_export($config, true).';';
  124. file_put_contents(ROOT_PATH.'Bin/Config/Uri.php', $data);
  125. if(file_exists(ROOT_PATH.'.htaccess')) {
  126. $htaccess = file_get_contents(ROOT_PATH.'.htaccess');
  127. $htaccess = preg_replace('#RewriteBase (.*)#i', 'RewriteBase '.$base, $htaccess);
  128. file_put_contents(ROOT_PATH.'.htaccess', $htaccess);
  129. }
  130. }
  131. }
  132. /**
  133. *render 404 page
  134. */
  135. public static function error404()
  136. {
  137. $buff = ob_get_contents();
  138. ob_end_clean();
  139. $di = new DI;
  140. $di->event-> run('Spawn.404') -> delete('Spawn.404');
  141. $error = new \Controller\Error();
  142. $error -> indexAction();
  143. echo $error -> response;
  144. exit;
  145. }
  146. /**
  147. *
  148. */
  149. public static function exception( $severity, $message='', $filename=__FILE__, $lineno=__LINE__ )
  150. {
  151. if (0 == error_reporting()) {
  152. return;
  153. }
  154. if( class_exists('Log') ){
  155. Log::add($message.' File: '.$filename.' Line: '.$lineno);
  156. }
  157. if(is_object($severity)){
  158. self::exceptionPage($severity);
  159. }else{
  160. throw new \ErrorException($message, E_ERROR, $severity, $filename, $lineno);
  161. }
  162. }
  163. public static function exceptionPage($error)
  164. {
  165. $buff = ob_get_contents();
  166. ob_end_clean();
  167. if(DEV){
  168. include_once(ROOT_PATH . 'Application'. DIRECTORY_SEPARATOR .'View'. DIRECTORY_SEPARATOR .'Error'. DIRECTORY_SEPARATOR .'exception.phtml');
  169. }else{
  170. include_once(ROOT_PATH . 'Application'. DIRECTORY_SEPARATOR .'View'. DIRECTORY_SEPARATOR .'Error'. DIRECTORY_SEPARATOR .'warning.phtml');
  171. }
  172. }
  173. }//spawn