PageRenderTime 41ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/sample/core/classes/Vortice.php

https://github.com/caferrari/vortice_book
PHP | 455 lines | 228 code | 50 blank | 177 comment | 45 complexity | 0ad626008894994038d64f31056efd08 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright (c) 2009, Carlos André Ferrari <[carlos@]ferrari.eti.br>; Luan Almeida <[luan@]luan.eti.br>
  4. * All rights reserved.
  5. */
  6. /**
  7. * Vortice core Class, template engine and framework super core
  8. *
  9. * @version 1
  10. * @package Framework
  11. * @author Carlos André Ferrari <carlos@ferrari.eti.br>
  12. * @author Luan Almeida <luanlmd@gmail.com>
  13. */
  14. class Vortice{
  15. /**
  16. * Variables to replace on the template render
  17. * @staticvar array
  18. * @access private
  19. */
  20. private static $vars = array();
  21. /**
  22. * Templates loaded
  23. * @staticvar array
  24. * @access private
  25. */
  26. private static $templates = array();
  27. /**
  28. * Flag to check if template controller was alread executed
  29. * @staticvar bool
  30. * @access private
  31. */
  32. private static $masterload = false;
  33. /**
  34. * Template setted to render the page
  35. * @staticvar string
  36. * @access private
  37. */
  38. private static $tpl = '';
  39. /**
  40. * Setted view to render the content generated on controller
  41. * @staticvar string
  42. * @access private
  43. */
  44. private static $view = '';
  45. /**
  46. * Is the framework engine initialized?
  47. * @staticvar boolean
  48. * @access private
  49. */
  50. private static $started = false;
  51. /**
  52. * Clean html white spaces before send it to the browser?
  53. * @staticvar bool
  54. * @access private
  55. */
  56. private static $clean = false;
  57. /**
  58. * Aplication base uri
  59. * @staticvar string
  60. * @access private
  61. */
  62. private static $rootsite = '/';
  63. /**
  64. * Aplication title
  65. * @vstaticar array
  66. * @access private
  67. */
  68. private static $title = '';
  69. /**
  70. * Forget the template renderization?
  71. * @staticvar bool
  72. * @access private
  73. */
  74. private static $notemplate = false;
  75. /**
  76. * Loaded html content
  77. * @staticvar string
  78. * @access protected
  79. */
  80. protected static $contents = '';
  81. /**
  82. * Return data format
  83. * @staticvar string
  84. * @static
  85. */
  86. public static $rendermode = 'html';
  87. /**
  88. * Constructor
  89. * @return void
  90. */
  91. private function __construct(){
  92. throw (new Exception('Don\'t do that!!'));
  93. }
  94. /**
  95. * Start the template engine
  96. * @return void
  97. */
  98. public static function start(){
  99. if (!self::$started){
  100. self::$started = true;
  101. self::$title = apphash;
  102. self::$rootsite = virtualroot;
  103. self::setVar('title', self::$title);
  104. self::setView(controller . ':' . action);
  105. self::loadTemplates();
  106. ob_start();
  107. }
  108. }
  109. /**
  110. * Auto load the templates templates
  111. * @return void
  112. * @access private
  113. */
  114. private static function loadTemplates(){
  115. $dir = root . 'app/webroot/templates';
  116. if (is_dir($dir) && $dh = opendir($dir))
  117. while (($file = readdir($dh)) !== false)
  118. if (preg_match('@^[0-9a-z\_]+$@', $file) && is_dir($dir . '/' . $file) && file_exists($dir . '/' . $file . '/template.php')) self::addTemplate($file);
  119. }
  120. /**
  121. * Disable the template rendering
  122. * @return void
  123. */
  124. public static function disableTemplate(){
  125. self::$notemplate = true;
  126. }
  127. /**
  128. * Set the render mode
  129. * @param string $mode new render mode
  130. * @return void
  131. */
  132. public static function setRenderMode($mode){
  133. self::$rendermode = $mode;
  134. }
  135. /**
  136. * Manual add a template
  137. * @param string $nome template name
  138. * @return void
  139. */
  140. public static function addTemplate($nome){
  141. if (!file_exists(root . 'app/webroot/templates/' . $nome . '/template.php')) throw(new TemplateNotFoundException($nome));
  142. self::$templates[$nome] = $nome;
  143. if (self::$tpl == '') self::$tpl = $nome;
  144. }
  145. /**
  146. * Set the response to be cleaned or not
  147. * @param bool $op Clear the whitespaces before send to the browser?
  148. * @return void
  149. */
  150. public static function setClean($op = true){
  151. self::$clean = $op;
  152. }
  153. /**
  154. * Set internal var to be replaced on the template
  155. * @param string $nome name
  156. * @param string $valor value
  157. * @return void
  158. */
  159. public static function setVar($nome, $valor){
  160. self::$vars[$nome] = e($valor);
  161. }
  162. /**
  163. * Set the active template
  164. * @param string $nome template name
  165. * @return void
  166. */
  167. public static function setTemplate($nome){
  168. if (!isset(self::$templates[$nome])) throw (new TemplateNotLoadedException($nome));
  169. self::$tpl = $nome;
  170. }
  171. /**
  172. * Set the active controller name
  173. * @param string $nome controller name
  174. * @return void
  175. */
  176. public static function setController($nome){
  177. self::$controller = $nome;
  178. }
  179. /**
  180. * Set the active action
  181. * @param string $nome action name
  182. * @return void
  183. */
  184. public static function setAction($action){
  185. self::$action = $action;
  186. }
  187. /**
  188. * get the requested view
  189. * @return string
  190. */
  191. public static function getView($prefix=''){
  192. if (strstr(self::$view, ':') !== false){
  193. list($c, $v) = explode(':', self::$view);
  194. return $c . '/' . $prefix . $v;
  195. }
  196. return controller . '/' . $prefix . self::$view;
  197. }
  198. /**
  199. * Set the view
  200. * @param string $nome view name
  201. * @return void
  202. */
  203. public static function setView($nome){
  204. self::$view = $nome;
  205. }
  206. /**
  207. * Load the CSS files inside a dir
  208. * @param string $dir path
  209. * @return string
  210. */
  211. protected static function loadCssDir($dir){
  212. $tmp = array('mobile' => array(), 'screen' => array(), 'print' => array());
  213. if (is_dir(root . 'app/webroot/' . $dir)){
  214. if ($handle = opendir(root . 'app/webroot/' . $dir)) {
  215. while (false !== ($file = readdir($handle))) {
  216. $nome = explode('.', $file);
  217. if (count($nome) > 1 && $nome[count($nome)-1] == 'css'){
  218. $mtime = filemtime(root . 'app/webroot/' . $dir . '/' . $file);
  219. if (strstr($nome[0], 'mobile'))
  220. $tmp['mobile'][] = '<link href="' . virtualroot . $dir . '/' . $file . '?' . $mtime . '" rel="stylesheet" media="all" />';
  221. else
  222. if (strstr($nome[0], 'print'))
  223. $tmp['print'][] = '<link href="' . virtualroot . $dir . '/' . $file . '?' . $mtime . '" rel="stylesheet" media="print" />';
  224. else
  225. $tmp['screen'][] = '<link href="' . virtualroot . $dir . '/' . $file . '?' . $mtime . '" rel="stylesheet" media="screen" />';
  226. }
  227. }
  228. closedir($handle);
  229. }
  230. }
  231. if (mobile && count($tmp['mobile']) > 0){
  232. sort($tmp['mobile']);
  233. return implode("\r\n\t", $tmp['mobile']);
  234. }
  235. sort($tmp['screen']);
  236. return implode("\r\n\t", array_merge($tmp['screen'], $tmp['print']));
  237. }
  238. /**
  239. * Alias for geraCss method
  240. * @return void
  241. */
  242. protected static function makeCss(){
  243. return self::geraCss();
  244. }
  245. /**
  246. * Read and make all Link for the template css's
  247. * @return void
  248. */
  249. protected static function geraCss(){
  250. $pasta = self::$tpl;
  251. $tmp = self::loadCssDir('css');
  252. $tmp .= self::loadCssDir('templates/' . $pasta . '/css');
  253. return $tmp;
  254. }
  255. /**
  256. * Alias for geraJs method
  257. * @return void
  258. */
  259. protected static function makeJs(){
  260. return self::geraJs();
  261. }
  262. /**
  263. * Load the JS files inside a dir
  264. * @param string $dir path
  265. * @return string
  266. */
  267. protected static function loadJsDir($dir){
  268. $tmp = array();
  269. if (is_dir(root . 'app/webroot/' . $dir))
  270. if ($handle = opendir(root . 'app/webroot/' . $dir)){
  271. while (false !== ($file = readdir($handle))) {
  272. $mtime = filemtime(root . 'app/webroot/' . $dir . '/' . $file);
  273. if (preg_match('@\.js$@', $file)) $tmp[] = '<script type="text/javascript" src="' . self::$rootsite . $dir . '/' . $file . '?' . $mtime . '"></script>';
  274. }
  275. closedir($handle);
  276. }
  277. sort($tmp);
  278. return implode("\r\n\t", $tmp);
  279. }
  280. /**
  281. * Read and make all Link for the template scripts
  282. * @return void
  283. */
  284. protected static function geraJs(){
  285. $pasta = self::$tpl;
  286. $tmp = self::loadJsDir('js');
  287. $tmp .= self::loadJsDir('templates/' . $pasta . '/js');
  288. return $tmp;
  289. }
  290. /**
  291. * Merge the vars to the contents
  292. * @return void
  293. */
  294. protected static function mergeVars(){
  295. foreach (self::$vars as $k => $v){
  296. self::$contents = str_replace('<!--' . $k . '-->', $v, self::$contents);
  297. unset(self::$vars[$k]);
  298. }
  299. }
  300. /**
  301. * Execute everything and render the response
  302. * @return string
  303. */
  304. public static function render(){
  305. $middle = self::execute(false, action, controller);
  306. if (!self::$tpl) self::$notemplate = true;
  307. else $pasta = self::$tpl;
  308. ob_start();
  309. if (ajax){
  310. if (post){
  311. header('Content-type: application/json; charset=UTF-8');
  312. $json = Json::getInstance();
  313. return ($json->render());
  314. }
  315. self::$rendermode = 'content';
  316. }elseif (!self::$notemplate){
  317. if (mobile && file_exists(root . 'app/webroot/templates/' . $pasta . '/mobile.php'))
  318. include root . 'app/webroot/templates/' . $pasta . '/mobile.php';
  319. else
  320. include root . 'app/webroot/templates/' . $pasta . '/template.php';
  321. self::setVar('csstags', self::geraCss());
  322. self::setVar('jstags', self::geraJs());
  323. }
  324. I18n::translate($middle);
  325. self::$contents = ob_get_clean();
  326. self::$contents = self::$notemplate ? $middle : preg_replace('@<!--conte(udo|nt)-->@', $middle, self::$contents);
  327. self::setVar('rootsite', self::$rootsite);
  328. self::mergeVars();
  329. I18n::translate(self::$contents);
  330. if (self::$clean){
  331. self::$contents = preg_replace('@[[:space:]]{1,}@', ' ', self::$contents);
  332. self::$contents = preg_replace('@[ ]+<\/@', '</', self::$contents);
  333. }
  334. switch (self::$rendermode){
  335. case 'html':
  336. return self::$contents;
  337. case 'json':
  338. //header('Content-type: application/json');
  339. header('Content-type: text/plain');
  340. return json_encode(DAO::getAll());
  341. case 'serial':
  342. header('Content-type: text/plain');
  343. return serialize(DAO::getAll());
  344. case 'content':
  345. return $middle;
  346. default:
  347. $tmp_render = 'render_' . self::$rendermode;
  348. if (function_exists($tmp_render)) return $tmp_render();
  349. throw (new ResponseTypeNotFoundException('Formato de retorno inválido: ' . self::$rendermode));
  350. }
  351. }
  352. /**
  353. * Execute a request
  354. * @param string $view
  355. * @param string $action
  356. * @param string $controller
  357. * @return string
  358. */
  359. public static function execute($view, $action, $controller){
  360. ob_start();
  361. if (!self::$masterload && class_exists('MasterController')){
  362. $obj = new MasterController();
  363. self::$masterload = true;
  364. }
  365. $c = $controller;
  366. $controller = camelize($controller).'Controller';
  367. if (class_exists($controller)){
  368. $obj = new $controller();
  369. $action = lcfirst(camelize($action));
  370. if (method_exists($obj, $action)) $obj->$action();
  371. else throw (new ActionNotFoundException($controller . '->' . $action . '()'));
  372. }else{
  373. // if its a static view
  374. $vpath = array(
  375. root . 'app/view/_static/' . (uri ? uri : 'index') . '.' . request_lang . '.php',
  376. root . 'app/view/_static/' . uri . '/index.' . request_lang . '.php',
  377. root . 'app/view/_static/' . (uri ? uri : 'index') . '.php',
  378. root . 'app/view/_static/' . uri . '/index.php'
  379. );
  380. foreach ($vpath as $vp)
  381. if (file_exists($vp)){
  382. include ($vp);
  383. return ob_get_clean();
  384. }
  385. throw (new ControllerNotFoundException($controller));
  386. }
  387. $_ref = DAO::getAll();
  388. foreach ($_ref as $k => &$v)
  389. $$k = $v;
  390. unset($_ref);
  391. if (mobile){
  392. $v = $view ? $c . '/mobile.' . $view : self::getView('mobile.');
  393. if (file_exists(root . 'app/view/' . $v . '.php')){
  394. include root . 'app/view/' . $v . '.php';
  395. return ob_get_clean();
  396. }
  397. }
  398. $v = $view ? $c . '/' . $view : self::getView();
  399. $vpath = root . 'app/view/' . $v . '.php';
  400. if (file_exists($vpath)){
  401. include $vpath;
  402. return ob_get_clean();
  403. }
  404. }
  405. }