PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Dispatch.php

http://kohana-mptt.googlecode.com/
PHP | 128 lines | 95 code | 22 blank | 11 comment | 8 complexity | f53484eb129635547930b83be991391b MD5 | raw file
  1. <?php
  2. class Dispatch_Core{
  3. protected $controller;
  4. public static function controller($controller)
  5. {
  6. $controller_file=strtolower($controller);
  7. // Set controller class name
  8. $controller = ucfirst($controller).'_Controller';
  9. if(!class_exists($controller, FALSE))
  10. {
  11. // If the file doesn't exist, just return
  12. if (($filepath = Kohana::find_file('controllers', $controller_file)) === FALSE)
  13. return FALSE;
  14. // Include the Controller file
  15. require_once $filepath;
  16. }
  17. // Run system.pre_controller
  18. Event::run('dispatch.pre_controller');
  19. // Initialize the controller
  20. $controller = new $controller;
  21. // Run system.post_controller_constructor
  22. Event::run('dispatch.post_controller_constructor');
  23. return new Dispatch($controller);
  24. }
  25. public function __construct(Controller $controller)
  26. {
  27. $this->controller=$controller;
  28. }
  29. public function __get($key)
  30. {
  31. if($key=='controller')
  32. {
  33. return $this->$key;
  34. }
  35. else
  36. {
  37. return $this->controller->$key;
  38. }
  39. }
  40. public function __set($key,$value)
  41. {
  42. $this->controller->$key=$value;
  43. }
  44. public function __toString()
  45. {
  46. return $this->render();
  47. }
  48. public function render()
  49. {
  50. return (string) $this->controller;
  51. }
  52. public function __call($name,$arguments=null)
  53. {
  54. if(method_exists($this->controller,$name))
  55. {
  56. return $this->method($name,$arguments);
  57. }
  58. return false;
  59. }
  60. public function method($method,$arguments=null)
  61. {
  62. if(!method_exists($this->controller,$method))
  63. return false;
  64. if (method_exists($this->controller,'_remap'))
  65. {
  66. // Make the arguments routed
  67. $arguments = array($method, $arguments);
  68. // The method becomes part of the arguments
  69. array_unshift($arguments, $method);
  70. // Set the method to _remap
  71. $method = '_remap';
  72. }
  73. ob_start();
  74. if(is_string($arguments))
  75. {
  76. $arguments=array($arguments);
  77. }
  78. switch(count($arguments))
  79. {
  80. case 1:
  81. $result=$this->controller->$method($arguments[0]);
  82. break;
  83. case 2:
  84. $result=$this->controller->$method($arguments[0], $arguments[1]);
  85. break;
  86. case 3:
  87. $result=$this->controller->$method($arguments[0], $arguments[1], $arguments[2]);
  88. break;
  89. case 4:
  90. $result=$this->controller->$method($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
  91. break;
  92. default:
  93. // Resort to using call_user_func_array for many segments
  94. $result=call_user_func_array(array($this->controller, $method), $arguments);
  95. break;
  96. }
  97. // Run system.post_controller
  98. Event::run('dispatch.post_controller');
  99. if($result!=NULL)
  100. {
  101. $result=ob_get_contents();
  102. ob_end_clean();
  103. }
  104. return $result;
  105. }
  106. }