/System/Packages/Solidocs/Application.php

https://github.com/carlmcdade/Solidocs · PHP · 254 lines · 130 code · 33 blank · 91 comment · 13 complexity · 12f38368699505515d5b39cd023bbc45 MD5 · raw file

  1. <?php
  2. /**
  3. * Application
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @package Solidocs
  9. * @author Karl Roos <karlroos93@gmail.com>
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php
  11. */
  12. class Solidocs_Application extends Solidocs_Base
  13. {
  14. /**
  15. * Controller
  16. */
  17. public $controller;
  18. /**
  19. * Init
  20. */
  21. public function init(){
  22. // Error handler
  23. set_error_handler(array($this, 'error_handler'));
  24. try{
  25. // Setup core, database, plugins and router
  26. $this->setup_core();
  27. $this->setup_database();
  28. $this->setup_plugins();
  29. $this->setup_router();
  30. // Setup libraries
  31. Solidocs::do_action('pre_libraries');
  32. $this->setup_libraries();
  33. Solidocs::do_action('post_libraries');
  34. // Setup models
  35. Solidocs::do_action('pre_models');
  36. $this->setup_models();
  37. Solidocs::do_action('post_models');
  38. // Check in cache
  39. if(isset($this->cache) AND $this->cache->exists($this->router->request_uri)){
  40. $this->render($this->cache->get($this->router->request_uri));
  41. }
  42. else{
  43. // Configure
  44. Solidocs::do_action('pre_configure');
  45. $this->setup_configure();
  46. Solidocs::do_action('post_configure,post_setup');
  47. // Execute
  48. Solidocs::do_action('pre_execute');
  49. $this->execute();
  50. Solidocs::do_action('post_execute');
  51. }
  52. }
  53. catch(Exception $e){
  54. $this->dispatch_exception($e);
  55. }
  56. try{
  57. // Render
  58. Solidocs::do_action('pre_render');
  59. $this->render();
  60. Solidocs::do_action('post_render');
  61. }
  62. catch(Exception $e){
  63. ob_end_clean();
  64. $this->dispatch_exception($e);
  65. }
  66. }
  67. /**
  68. * Setup core
  69. */
  70. public function setup_core(){
  71. // Include core
  72. include(PACKAGE . '/Solidocs/Config.php');
  73. include(PACKAGE . '/Solidocs/Load.php');
  74. // Default config
  75. $config = array(APP . '/Config/Application');
  76. // Development config
  77. if(APPLICATION_ENV == 'development' AND file_exists(APP . '/Config/Application.development.ini')){
  78. $config[] = APP . '/Config/Application.development';
  79. }
  80. else{
  81. ini_set('display_errors', 0);
  82. }
  83. // Set up core libraries
  84. Solidocs::$registry->config = new Solidocs_Config($config);
  85. Solidocs::$registry->load = new Solidocs_Load;
  86. Solidocs::apply_config($this->load, $this->config->get('Load'));
  87. }
  88. /**
  89. * Setup db
  90. */
  91. public function setup_database(){
  92. $this->load->library('Db');
  93. $this->db->connect();
  94. $this->db->select_db();
  95. $this->db->sql('SET NAMES utf8')->run();
  96. }
  97. /**
  98. * Setup plugins
  99. */
  100. public function setup_plugins(){
  101. if($plugins = $this->config->get('Autoload.plugins')){
  102. foreach($plugins as $class){
  103. $this->load->plugin($class);
  104. }
  105. }
  106. }
  107. /**
  108. * Setup router
  109. */
  110. public function setup_router(){
  111. $this->load->library('Router');
  112. // Load production routes
  113. $routes = $this->config->load_file(APP . '/Config/Routes', true);
  114. // Development routes
  115. if(APPLICATION_ENV == 'development' AND $this->config->file_exists(APP . '/Config/Routes.development')){
  116. $routes = array_merge($routes, $this->config->load_file(APP . '/Config/Routes.development', true));
  117. }
  118. // Set routes
  119. $this->router->set_routes($routes);
  120. }
  121. /**
  122. * Setup libraries
  123. */
  124. public function setup_libraries(){
  125. $this->load->libraries($this->config->get('Autoload.libraries'));
  126. }
  127. /**
  128. * Setup models
  129. */
  130. public function setup_models(){
  131. $this->load->models($this->config->get('Autoload.models'));
  132. }
  133. /**
  134. * Setup configure
  135. */
  136. public function setup_configure(){
  137. // Set view handler
  138. $this->load->set_view_handler($this->output);
  139. }
  140. /**
  141. * Execute
  142. */
  143. public function execute(){
  144. // Route, set output type and dispatch
  145. $this->router->route();
  146. $this->output->set_type($this->router->output_type);
  147. $this->dispatch($this->router->package, $this->router->controller, $this->router->action);
  148. }
  149. /**
  150. * Render
  151. *
  152. * @param string Optional.
  153. */
  154. public function render($output = ''){
  155. // Retrieve output if it wasn't given
  156. if(empty($output)){
  157. $output = $this->output->render();
  158. // Cache output
  159. if(isset($this->cache) AND $this->cache->cache_page()){
  160. $this->cache->store($this->router->request_uri, $output);
  161. }
  162. }
  163. // Display and apply filter
  164. echo Solidocs::apply_filter('render', $output);
  165. }
  166. /**
  167. * Dispatch
  168. *
  169. * @param string
  170. * @param string
  171. * @param string Optional.
  172. */
  173. public function dispatch($package, $controller, $action = 'index'){
  174. // Load package if the package isn't "application"
  175. if(strtolower($package) !== 'application'){
  176. $this->load->package($package);
  177. }
  178. // Load controller and dispatch
  179. $this->controller = $this->load->controller($controller, $package);
  180. $this->controller->dispatch_action($action);
  181. }
  182. /**
  183. * Dispatch exception
  184. *
  185. * @param object
  186. */
  187. public function dispatch_exception($e){
  188. // Error
  189. if($e->getCode() == '404'){
  190. $this->dispatch('Application', 'Error', '404');
  191. }
  192. else{
  193. if(APPLICATION_ENV == 'development'){
  194. die('<pre>' . $e . '</pre>');
  195. }
  196. $this->dispatch('Application', 'Error', '500');
  197. }
  198. }
  199. /**
  200. * Error handler
  201. *
  202. * @param integer
  203. * @param string
  204. * @param string
  205. * @param integer
  206. */
  207. public function error_handler($errno, $string, $file, $line){
  208. // Fix variables
  209. $string = str_replace(ROOT, '', strip_tags($string));
  210. $file = str_replace(ROOT, '', $file);
  211. // Log
  212. Solidocs::$registry->errors[] = array(
  213. 'errno' => $errno,
  214. 'string' => $string,
  215. 'file' => $file,
  216. 'line' => $line
  217. );
  218. // Throw exception
  219. throw new Exception($string . ' in <strong>' . $file . '</strong> on line <strong>' . $line . '</strong>');
  220. }
  221. }