/system/engine/loader.php

https://gitlab.com/dadangnh/sb1-bon · PHP · 193 lines · 126 code · 54 blank · 13 comment · 20 complexity · 44d73f1733f5b88a9a7f19ddc2726bb3 MD5 · raw file

  1. <?php
  2. final class Loader {
  3. protected $registry;
  4. public function __construct($registry) {
  5. $this->registry = $registry;
  6. }
  7. public function controller($route, $data = array()) {
  8. // Sanitize the call
  9. $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route);
  10. $output = null;
  11. // Trigger the pre events
  12. $result = $this->registry->get('event')->trigger('controller/' . $route . '/before', array(&$route, &$data, &$output));
  13. if ($result) {
  14. return $result;
  15. }
  16. if (!$output) {
  17. $action = new Action($route);
  18. $output = $action->execute($this->registry, array(&$data));
  19. }
  20. // Trigger the post events
  21. $result = $this->registry->get('event')->trigger('controller/' . $route . '/after', array(&$route, &$data, &$output));
  22. if ($output instanceof Exception) {
  23. return false;
  24. }
  25. return $output;
  26. }
  27. public function model($route) {
  28. // Sanitize the call
  29. $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route);
  30. // Trigger the pre events
  31. $this->registry->get('event')->trigger('model/' . $route . '/before', array(&$route));
  32. if (!$this->registry->has('model_' . str_replace(array('/', '-', '.'), array('_', '', ''), $route))) {
  33. $file = DIR_APPLICATION . 'model/' . $route . '.php';
  34. $class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', $route);
  35. if (is_file($file)) {
  36. include_once($file);
  37. $proxy = new Proxy();
  38. foreach (get_class_methods($class) as $method) {
  39. $proxy->{$method} = $this->callback($this->registry, $route . '/' . $method);
  40. }
  41. $this->registry->set('model_' . str_replace(array('/', '-', '.'), array('_', '', ''), (string)$route), $proxy);
  42. } else {
  43. throw new \Exception('Error: Could not load model ' . $route . '!');
  44. }
  45. }
  46. // Trigger the post events
  47. $this->registry->get('event')->trigger('model/' . $route . '/after', array(&$route));
  48. }
  49. public function view($route, $data = array()) {
  50. $output = null;
  51. // Sanitize the call
  52. $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route);
  53. // Trigger the pre events
  54. $result = $this->registry->get('event')->trigger('view/' . $route . '/before', array(&$route, &$data, &$output));
  55. if ($result) {
  56. return $result;
  57. }
  58. if (!$output) {
  59. $template = new Template($this->registry->get('config')->get('template_type'));
  60. foreach ($data as $key => $value) {
  61. $template->set($key, $value);
  62. }
  63. $output = $template->render($route . '.tpl');
  64. }
  65. // Trigger the post events
  66. $result = $this->registry->get('event')->trigger('view/' . $route . '/after', array(&$route, &$data, &$output));
  67. if ($result) {
  68. return $result;
  69. }
  70. return $output;
  71. }
  72. public function library($route) {
  73. // Sanitize the call
  74. $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route);
  75. $file = DIR_SYSTEM . 'library/' . $route . '.php';
  76. $class = str_replace('/', '\\', $route);
  77. if (is_file($file)) {
  78. include_once($file);
  79. $this->registry->set(basename($route), new $class($this->registry));
  80. } else {
  81. throw new \Exception('Error: Could not load library ' . $route . '!');
  82. }
  83. }
  84. public function helper($route) {
  85. $file = DIR_SYSTEM . 'helper/' . preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route) . '.php';
  86. if (is_file($file)) {
  87. include_once($file);
  88. } else {
  89. throw new \Exception('Error: Could not load helper ' . $route . '!');
  90. }
  91. }
  92. public function config($route) {
  93. $this->registry->get('event')->trigger('config/' . $route . '/before', array(&$route));
  94. $this->registry->get('config')->load($route);
  95. $this->registry->get('event')->trigger('config/' . $route . '/after', array(&$route));
  96. }
  97. public function language($route) {
  98. $output = null;
  99. $this->registry->get('event')->trigger('language/' . $route . '/before', array(&$route, &$output));
  100. $output = $this->registry->get('language')->load($route);
  101. $this->registry->get('event')->trigger('language/' . $route . '/after', array(&$route, &$output));
  102. return $output;
  103. }
  104. protected function callback($registry, $route) {
  105. return function($args) use($registry, &$route) {
  106. static $model = array();
  107. $output = null;
  108. // Trigger the pre events
  109. $result = $registry->get('event')->trigger('model/' . $route . '/before', array(&$route, &$args, &$output));
  110. if ($result) {
  111. return $result;
  112. }
  113. // Store the model object
  114. if (!isset($model[$route])) {
  115. $file = DIR_APPLICATION . 'model/' . substr($route, 0, strrpos($route, '/')) . '.php';
  116. $class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', substr($route, 0, strrpos($route, '/')));
  117. if (is_file($file)) {
  118. include_once($file);
  119. $model[$route] = new $class($registry);
  120. } else {
  121. throw new \Exception('Error: Could not load model ' . substr($route, 0, strrpos($route, '/')) . '!');
  122. }
  123. }
  124. $method = substr($route, strrpos($route, '/') + 1);
  125. $callable = array($model[$route], $method);
  126. if (is_callable($callable)) {
  127. $output = call_user_func_array($callable, $args);
  128. } else {
  129. throw new \Exception('Error: Could not call model/' . $route . '!');
  130. }
  131. // Trigger the post events
  132. $result = $registry->get('event')->trigger('model/' . $route . '/after', array(&$route, &$args, &$output));
  133. if ($result) {
  134. return $result;
  135. }
  136. return $output;
  137. };
  138. }
  139. }