PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lynn/packages/controller.php

https://github.com/JesseDunlap/metacloud
PHP | 169 lines | 87 code | 23 blank | 59 comment | 28 complexity | 015b3fe7ec680f5414c564e5a709bbc7 MD5 | raw file
  1. <?php
  2. /**
  3. * Lynn PHP
  4. *
  5. * @category Lynn PHP
  6. * @package Lynn_Controller
  7. * @version 1.0
  8. * @author Matt Lynn
  9. * @copyright 2009 - 2010 Matthew James Lynn
  10. * @license See included LICENSE.txt file
  11. */
  12. lynn_load('lynn_view');
  13. lynn_load('lynn_http');
  14. /**
  15. * Gets a url to a controller action.
  16. *
  17. * @param $base The base url in the format controller/action.format
  18. * @param $params The array of parameters to pass to the controller/action
  19. * @return The completed url.
  20. */
  21. function lynn_mvc_href($base='', $params=null) {
  22. if($params != null) {
  23. $str = '';
  24. foreach($params as $key => $value) {
  25. $str .= '/' . $key . ':' . urlencode($value);
  26. }
  27. $params = $str;
  28. }
  29. return LYNN_URL_ROOT . $base . $params;
  30. }
  31. /**
  32. * Redirects the user to a mvc action.
  33. *
  34. * @access public
  35. * @param string $href The location to direct the user to in the format 'controller/action'
  36. * @param string $params The parameters to pass to that action in the url
  37. */
  38. function lynn_mvc_redirect($href, $params=null) {
  39. lynn_http_redirect(lynn_mvc_href($href, $params));
  40. }
  41. /**
  42. * Provides the base controller object.
  43. */
  44. class Lynn_Controller {
  45. /**
  46. * The theme that the mvc should use
  47. *
  48. * @var string
  49. */
  50. public $theme = 'default';
  51. /**
  52. * The configuration passed to the Lynn_Controller_Front.
  53. *
  54. * @var array
  55. */
  56. public $config;
  57. /**
  58. * This method is called to run an action inside of the controller object.
  59. * It is recommended you do not, but you can override this to make certain
  60. * controllers behave dramatically differently.
  61. *
  62. * @param $action The name of the action to run.
  63. */
  64. public function dispatch($action) {
  65. if(method_exists($this, 'onReady')) {
  66. $this->onReady();
  67. }
  68. if(method_exists($this, 'before_filter')) {
  69. $this->before_filter();
  70. }
  71. $action = $action . '_action';
  72. if(method_exists($this, $action)) {
  73. $this->$action();
  74. } else if(method_exists($this, 'catch_all')) {
  75. $this->catch_all();
  76. } else {
  77. require LYNN_APP_ROOT . 'application/errors/404.html';
  78. }
  79. if(method_exists($this, 'after_filter')) {
  80. $this->after_filter();
  81. }
  82. }
  83. /**
  84. * Loads a resource such as a lynn_form or lynn_model.
  85. *
  86. * @param string $name The name of the class to load.
  87. */
  88. public function getResource($name) {
  89. // Load A Form
  90. if(str_replace('_form', '', $name) != $name) {
  91. if(!class_exists($name)) {
  92. $file = $this->config['paths.forms'] . str_replace('_form', '.form.php', $name);
  93. if(!file_exists($file)) {
  94. lynn_display_error('Form not found!', 'Requested form not found in file ' . $file);
  95. } else {
  96. require $file;
  97. }
  98. }
  99. return new $name();
  100. }
  101. // Load a Model
  102. else if(str_replace('_model', '', $name) != $name) {
  103. if(!class_exists($name)) {
  104. $file = LYNN_APP_ROOT . 'application/models/' . str_replace('_model', '.model.php', $name);
  105. if(!file_exists($file)) {
  106. lynn_display_error('Model not found!', 'Requested model not found in file ' . $file);
  107. } else {
  108. require $file;
  109. }
  110. }
  111. $model = new $name();
  112. $model->db &= $this->db;
  113. if(method_exists($model, 'onReady')) {
  114. $model->onReady();
  115. }
  116. return $model;
  117. }
  118. // Load a Helper
  119. else if(str_replace('_helper', '', $name) != $name) {
  120. if(!class_exists($name)) {
  121. $file = LYNN_APP_ROOT . 'application/helpers/' . str_replace('_helper', '.helper.php', $name);
  122. if(!file_exists($file)) {
  123. lynn_display_error('Helper not found!', 'Requested helper not found in file ' . $file);
  124. } else {
  125. require $file;
  126. }
  127. }
  128. return new $name();
  129. }
  130. else {
  131. return $this->bootstrap->getResource($name);
  132. }
  133. }
  134. /**
  135. * Loads a controller plugin. Resolves $name to Lynn_Controller_Plugin_$name.
  136. *
  137. * WARNING: This is a beta api, that means it's prone to change.
  138. *
  139. * @param string $name The name of the controller plugin to load.
  140. */
  141. public function loadPlugin($name) {
  142. $klass = 'Lynn_Controller_Plugin_' . $name;
  143. lynn_load($klass);
  144. $this->$name = new $klass(&$this);
  145. }
  146. }