PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendors/Cupcake/cupcake_controller.php

https://github.com/fernyb/Cupcake
PHP | 272 lines | 227 code | 36 blank | 9 comment | 35 complexity | 5f540ade4f6ccd5618a961a58f5a1fba MD5 | raw file
  1. <?php
  2. class CupcakeController {
  3. public $controller;
  4. public $request_uri;
  5. public $action = null;
  6. public $params = array();
  7. public $view_params = array();
  8. public $view;
  9. public $helper = null;
  10. public $before_filter = array();
  11. public $after_filter = array();
  12. protected $render_called = false;
  13. public function __construct($uri, $params=array()) {
  14. $this->request_uri = $uri;
  15. $this->params = $params;
  16. $this->controller = $this;
  17. if(!empty($params["action"])) {
  18. $this->action = $params["action"];
  19. }
  20. }
  21. public function handle_request($params) {
  22. if(array_key_exists("format", $params) === false) {
  23. $params["format"] = "html";
  24. }
  25. $this->params = $params;
  26. $controller_name = Inflector::camelize($params["controller"], "first");
  27. CupcakeLogger::process_controller($controller_name, $params["action"], env("REQUEST_METHOD"), $params);
  28. if(CupcakeImport::controller($params["controller"])) {
  29. CupcakeImport::helper($params["controller"]);
  30. $helper_name = "{$controller_name}Helper";
  31. $this->controller = new $controller_name($this->request_uri, $params);
  32. if(class_exists($helper_name)) {
  33. $this->controller->helper = new $helper_name();
  34. }
  35. $action = $params["action"];
  36. } else {
  37. $this->controller = new self($this->request_uri, $params);
  38. $action = "not_found";
  39. $this->controller->{$action}();
  40. }
  41. $this->controller->format = $this->params["format"];
  42. $this->controller->handle_action($action);
  43. }
  44. public function controller_exists($controller_name) {
  45. return file_exists(CONTROLLER_DIR . "/" . $controller_name .".php");
  46. }
  47. public function run_filter_methods($filters, $action, $methods) {
  48. foreach($filters as $key => $value) {
  49. $filter_method = $value[0];
  50. if(array_key_exists("skip", $value) && $action === $value["skip"]) {
  51. continue;
  52. }
  53. if(array_search($filter_method, $methods)) {
  54. if(isset($value["only"]) && $value["only"] === $action) {
  55. $this->{$filter_method}();
  56. } else {
  57. $this->{$filter_method}();
  58. }
  59. }
  60. }
  61. }
  62. public function handle_flash_messages() {
  63. foreach($_COOKIE as $k => $v) {
  64. if(preg_match("/^flash_/", $k)) {
  65. setcookie($k, "", time() - (3600 * 8));
  66. }
  67. }
  68. }
  69. public function load_session() {
  70. CupcakeSession::getInstance()->load();
  71. }
  72. public function save_session() {
  73. CupcakeSession::getInstance()->save();
  74. }
  75. #
  76. # handle_action will call the before filter, action and after filter
  77. # after doing so it will attempt to render the view
  78. #
  79. public function handle_action($action) {
  80. $start_timer = microseconds();
  81. $this->load_session();
  82. $this->handle_flash_messages();
  83. $methods = get_class_methods($this);
  84. # call before filter methods before calling action method
  85. $this->run_filter_methods($this->before_filter, $action, $methods);
  86. # Search for action Methods
  87. if(array_key_exists($action, array_flip($methods))) {
  88. $this->{$action}();
  89. }
  90. # call after filter methods after calling action method
  91. $this->run_filter_methods($this->after_filter, $action, $methods);
  92. $this->render();
  93. CupcakeLogger::info("CupcakeController Action: ". (microseconds() - $start_timer) ." ms");
  94. }
  95. public function render($options=array()) {
  96. $this->save_session();
  97. if($this->render_called === false) {
  98. $this->view = new CupcakeView($this->request_uri, $this->params, $this->view_params);
  99. $this->view->controller = $this->controller;
  100. // Figure out the template to use:
  101. if(!empty($options["action"]) && strpos("/", $options["action"])) {
  102. $this->view->template = $options["action"];
  103. } else if(!empty($options["action"])) {
  104. $this->view->template = $this->params["controller"] ."/". $options["action"];
  105. }
  106. if(!empty($options["template"])) {
  107. $parts = explode("/", $options["template"]);
  108. if(count($parts) === 0) {
  109. $this->view->template = $this->params["controller"] ."/". $options["template"];
  110. }
  111. }
  112. if(!empty($options["ext"])) {
  113. $this->view->ext = $options["ext"];
  114. }
  115. if(!empty($options["format"])) {
  116. $this->format = $options["format"];
  117. }
  118. // Figure out the layout to use:
  119. if(!empty($options["layout"])) {
  120. $this->view->layout = "layouts/". $options["layout"];
  121. }
  122. $this->view->content_type = MimeType::lookup_by_extension($this->format);
  123. $this->view->format = MimeType::extension_by_mime_type($this->view->content_type);
  124. $this->render_called = true;
  125. $this->view->render();
  126. }
  127. }
  128. public function render_action($action, $options=array()) {
  129. $this->save_session();
  130. $this->render(array_merge(array("action" => $action), $options));
  131. }
  132. public function render_template($template_name, $options=array()) {
  133. list($template, $format, $ext) = explode(".", $template_name, 3);
  134. $this->render(array_merge(array(
  135. "template" => $template,
  136. "format" => $format,
  137. "ext" => $ext
  138. ), $options));
  139. }
  140. public function render_text($text="") {
  141. $this->save_session();
  142. echo $text;
  143. exit;
  144. }
  145. public function render_html($file) {
  146. $this->save_session();
  147. CupcakeImport::html($file, $this->view_params);
  148. exit;
  149. }
  150. public function redirect_to($url, $status=302) {
  151. $this->save_session();
  152. $codes = $this->status_code;
  153. if (!empty($status)) {
  154. if(is_string($status)) {
  155. $codes = array_combine(array_values($codes), array_keys($codes));
  156. }
  157. if(isset($codes[$status])) {
  158. $code = $msg = $codes[$status];
  159. if(is_numeric($status)) { $code = $status; }
  160. if(is_string($status)) { $msg = $status; }
  161. Header::set_raw("HTTP/1.1 {$code} {$msg}");
  162. $status = "HTTP/1.1 {$code} {$msg}";
  163. } else {
  164. $status = null;
  165. }
  166. }
  167. if($url !== null) {
  168. Header::set("Location", "{$url}");
  169. }
  170. if(CUPCAKE_ENV !== "test") {
  171. Header::send();
  172. }
  173. exit;
  174. }
  175. public $status_code = array(
  176. 100 => 'Continue',
  177. 101 => 'Switching Protocols',
  178. 200 => 'OK',
  179. 201 => 'Created',
  180. 202 => 'Accepted',
  181. 203 => 'Non-Authoritative Information',
  182. 204 => 'No Content',
  183. 205 => 'Reset Content',
  184. 206 => 'Partial Content',
  185. 300 => 'Multiple Choices',
  186. 301 => 'Moved Permanently',
  187. 302 => 'Found',
  188. 303 => 'See Other',
  189. 304 => 'Not Modified',
  190. 305 => 'Use Proxy',
  191. 307 => 'Temporary Redirect',
  192. 400 => 'Bad Request',
  193. 401 => 'Unauthorized',
  194. 402 => 'Payment Required',
  195. 403 => 'Forbidden',
  196. 404 => 'Not Found',
  197. 405 => 'Method Not Allowed',
  198. 406 => 'Not Acceptable',
  199. 407 => 'Proxy Authentication Required',
  200. 408 => 'Request Time-out',
  201. 409 => 'Conflict',
  202. 410 => 'Gone',
  203. 411 => 'Length Required',
  204. 412 => 'Precondition Failed',
  205. 413 => 'Request Entity Too Large',
  206. 414 => 'Request-URI Too Large',
  207. 415 => 'Unsupported Media Type',
  208. 416 => 'Requested range not satisfiable',
  209. 417 => 'Expectation Failed',
  210. 500 => 'Internal Server Error',
  211. 501 => 'Not Implemented',
  212. 502 => 'Bad Gateway',
  213. 503 => 'Service Unavailable',
  214. 504 => 'Gateway Time-out'
  215. );
  216. public function status_code($code) {
  217. if(array_key_exists($code, $this->status_code)) {
  218. if(CUPCAKE_ENV !== "test") {
  219. header("HTTP/1.1 ". $code ." ". $this->status_code[$code]);
  220. }
  221. }
  222. }
  223. public function not_found() {
  224. $this->status_code(404);
  225. $this->render_html("404");
  226. }
  227. public function set($key, $value) {
  228. if($key !== "controller" && $key !== "action") {
  229. $this->view_params[$key] = $value;
  230. }
  231. }
  232. public function xhr() {
  233. return (env("X-Requested-With") === "XMLHttpRequest");
  234. }
  235. }
  236. ?>