PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/W3br.php

https://gitlab.com/leandro.chaves/w3br
PHP | 296 lines | 192 code | 26 blank | 78 comment | 34 complexity | dbdc83e935fe06b48fea36d05a54dba0 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. $dirs = Array();
  3. /**
  4. * Performs the necessary additions and scans the page to load
  5. * @author Leandro Chaves <leandro.chaves@h2asol.com>
  6. * @link http://leandrochaves.com
  7. */
  8. class W3br {
  9. protected $dirList;
  10. protected $starterList;
  11. function W3br() {
  12. // $dirList = DirList::getInstance();
  13. }
  14. public static function import() {
  15. self::loadFile(W3BR_PATH . 'SingletonList.php');
  16. self::loadFile(W3BR_PATH . 'DirList.php');
  17. self::loadFile(W3BR_PATH . 'StarterList.php');
  18. self::loadFile(W3BR_PATH . 'AutoLoadList.php');
  19. self::dirRegistry(W3BR_PATH . 'io');
  20. }
  21. /**
  22. * Assemble a list of directories with Controller's files
  23. * @author Leandro Chaves
  24. * @link http://leandrochaves.com
  25. */
  26. public static function dirRegistry($dir) {
  27. // echo $dir.'<br/>';
  28. if (is_array($dir)) {
  29. foreach ($dir as $obj) {
  30. self::dirRegistry($obj);
  31. }
  32. } elseif (is_string($dir) && is_dir($dir)) {
  33. $dirList = DirList::getInstance();
  34. // echo $dir.'<br/>';
  35. $dirList->registry($dir);
  36. }
  37. return;
  38. }
  39. /**
  40. * Assemble a list of functions to run on start the framework
  41. * @author Leandro Chaves
  42. * @link http://leandrochaves.com
  43. */
  44. public static function starterRegistry($function) {
  45. if (is_array($function)) {
  46. foreach ($function as $obj) {
  47. self::starterRegistry($obj);
  48. }
  49. } elseif (is_string($function)) {
  50. $starterList = StarterList::getInstance();
  51. $starterList->registry($function);
  52. }
  53. return;
  54. }
  55. /**
  56. * Assemble a list of functions to run on autoload of the framework
  57. * @author Leandro Chaves
  58. * @link http://leandrochaves.com
  59. */
  60. public static function autoLoadRegistry($function) {
  61. if (is_array($function)) {
  62. foreach ($function as $obj) {
  63. self::autoLoadRegistry($obj);
  64. }
  65. } elseif (is_string($function)) {
  66. $array = explode("::", $function);
  67. self::loadClass($array[0]);
  68. $autoLoadList = AutoLoadList::getInstance();
  69. $autoLoadList->registry($function);
  70. }
  71. return;
  72. }
  73. /**
  74. * Percorre a lista de diretórios registrados para o autoLoad
  75. * para tentar carregar a classe.
  76. * @param String $class - nome da classe a ser carregada.
  77. * @return boolean true - se a classe for encontrada.
  78. */
  79. public function autoLoad($class) {
  80. // $dirList = new DirList();
  81. $dirList = DirList::getInstance();
  82. // print_r($dirList->get());
  83. foreach ($dirList->get() as $dir) {
  84. $address = $dir . "/" . $class . '.php';
  85. if (is_file($address)) {
  86. include_once($address);
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. /**
  93. * Instantiate a new object of the class passed as parameter
  94. * @param String $class
  95. */
  96. static function loadClass($class) {
  97. if (!class_exists($class)) {
  98. if (self::autoLoad($class)) {
  99. return TRUE;
  100. } else {
  101. // through the list of autoload functions
  102. $autoLoadList = AutoLoadList::getInstance();
  103. foreach ($autoLoadList->get() as $function) {
  104. $array = explode("::", $function);
  105. $obj = new $array[0];
  106. if ($obj->$array[1]($class)) {
  107. return true;
  108. }
  109. }
  110. // throw exception if the file does not exist
  111. // throw new Exception('Classe ' . $class . ' Inexistente.');
  112. }
  113. }
  114. }
  115. public static function loadFile($file) {
  116. $file = DIR_RAIZ . ((preg_match('/\/$/', DIR_RAIZ)) ? $file : '/' . $file);
  117. require_once $file;
  118. }
  119. /**
  120. * Try to load the starter page.
  121. * @param Array $url
  122. */
  123. function index($url) {
  124. try {
  125. $cmd = new IndexController;
  126. $cmd->display($url);
  127. } catch (Exception $e) {
  128. echo 'The starter page not exist.';
  129. }
  130. }
  131. public static function getUri() {
  132. $uri = Array();
  133. /* Quebra a url em um array */
  134. if (!self::isConsole()) {
  135. // Busca a uri, se o acesso for web
  136. $uri = str_replace(str_replace('http://', '', SUB), '', $_SERVER["REQUEST_URI"]);
  137. $uri = explode("?", $uri);
  138. $uri = explode("/", $uri[0]);
  139. while (0 < sizeof($uri) && ($uri[0] == null || $uri[0] == null)) {
  140. array_shift($uri);
  141. }
  142. } else {
  143. // Busca o primeiro parâmetro, se o acesso for via console
  144. $uri = self::getParamUri();
  145. }
  146. return $uri;
  147. }
  148. private static function getParamUri() {
  149. $uri = Array();
  150. if (0 < $_SERVER['argc']) {
  151. $uri = explode(':', $_SERVER['argv'][1]);
  152. }
  153. return $uri;
  154. }
  155. function paths() {
  156. if (!defined("MODULES_PATH")) {
  157. define("MODULES_PATH", W3BR_PATH . "/Modules");
  158. }
  159. }
  160. /**
  161. * Run the page based on url
  162. */
  163. private function run($url) {
  164. // Inicia o controle do buffer de saída
  165. if (!self::isConsole()) {
  166. ob_start();
  167. }
  168. if (sizeof($url) > 2) {
  169. // remove o último item da url e acrescenta o posfixo.
  170. $uri = array_reverse($url);
  171. array_shift($uri);
  172. $uri[0] = ucfirst(strtolower($uri[0])) . "Controller";
  173. $uri = array_reverse($uri);
  174. $this->autoLoad(implode("/", $uri));
  175. $control = ucfirst(strtolower(W3brUtils::getValue($url, sizeof($url) - 2))) . 'Controller';
  176. $action = explode("?", strtolower(W3brUtils::getValue($url, sizeof($url) - 1)));
  177. $function = $action[0] . 'Action';
  178. } else {
  179. $control = ucfirst(strtolower(W3brUtils::getValue($url, 0))) . 'Controller';
  180. $action = explode("?", strtolower(W3brUtils::getValue($url, 1)));
  181. $function = $action[0] . 'Action';
  182. }
  183. //print_r($url);
  184. // Page on mantenance
  185. if (MAINTENANCE == true) {
  186. $cmd = new IndexController();
  187. $cmd->manutencao($url);
  188. //check if controller is empty
  189. } elseif (!isset($url[0]) || NULL == $url[0]) {
  190. $this->index($url);
  191. } else {
  192. try {
  193. if (!class_exists($control)) {
  194. throw new Exception("404: Url não encotrada!");
  195. }
  196. $cmd = new $control;
  197. if ($function) {
  198. $cmd->$function($url);
  199. }
  200. } catch (Exception $e) {
  201. LogModule::log($e->getMessage(), 'ERRO');
  202. // if ($this->isAjax()) {
  203. $response = Response::getInstance();
  204. $response->sucesso = Response::NOT_SUCCESS;
  205. $response->erro = $e->getMessage();
  206. $response->trace = $e->getTraceAsString();
  207. // } else {
  208. // header('Location: /' . SUB);
  209. // }
  210. }
  211. }
  212. // Escreve todo o buffer de saída e desliga o buffer
  213. // echo ob_get_length();
  214. if (1 >= ob_get_length() && !self::isConsole()) {
  215. Response::send();
  216. }
  217. if (!self::isConsole()) {
  218. ob_end_flush();
  219. }
  220. }
  221. /**
  222. * Performs the necessary additions and scans the page to load
  223. * @author Leandro Chaves <leandro@chaves.in>
  224. * @link http://leandrochaves.com
  225. */
  226. public function start() {
  227. $this->paths();
  228. if (!self::isConsole()) {
  229. session_start();
  230. }
  231. // ini_set("memory_limit", "16M");
  232. ini_set('default_charset', 'UTF-8');
  233. $url = self::getUri();
  234. //Check for files to include
  235. $dirs = array(
  236. 'www/Persistencia',
  237. 'www/Controller',
  238. W3BR_PATH . 'Includes',
  239. 'www/Model');
  240. foreach ($dirs as $dir) {
  241. self::dirRegistry(DIR_RAIZ . $dir);
  242. }
  243. // through the list of registered starter functions
  244. $starterList = StarterList::getInstance();
  245. foreach ($starterList->get() as $function) {
  246. $array = explode("::", $function);
  247. // self::loadClass($array[0]);
  248. $obj = new $array[0];
  249. $obj->$array[1]();
  250. }
  251. $this->run($url);
  252. }
  253. /**
  254. * Verifica se a requisição veio via ajax ou aplicação.
  255. *
  256. * Sempre que uma requsição Ajax é disparada um header é setado nesta requisição chamada HTTP_X_REQUESTED_WITH e o valor dela é setado como XMLHttpRequest.
  257. *
  258. */
  259. public function isAjax() {
  260. return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
  261. }
  262. /**
  263. * Verifica se a aplicação está rodando no console.
  264. * @return Boolean
  265. */
  266. public static function isConsole() {
  267. return (!empty($_SERVER['argc']));
  268. }
  269. }
  270. ?>