/zan/classes/class.load.php

https://github.com/Eyenrique/ZanPHP · PHP · 702 lines · 580 code · 35 blank · 87 comment · 59 complexity · e70aa44824bcfb7e08b4c96c9ee8201d MD5 · raw file

  1. <?php
  2. /* ex: set tabstop=2 noexpandtab: */
  3. /**
  4. * ZanPHP
  5. *
  6. * An open source agile and rapid development framework for PHP 5
  7. *
  8. * @package ZanPHP
  9. * @author MilkZoft Developer Team
  10. * @copyright Copyright (c) 2011, MilkZoft, Inc.
  11. * @license http://www.zanphp.com/documentation/en/license/
  12. * @link http://www.zanphp.com
  13. * @version 1.0
  14. */
  15. /**
  16. * Access from index.php
  17. */
  18. if(!defined("_access")) {
  19. die("Error: You don't have permission to access here...");
  20. }
  21. /**
  22. * Includes Singleton
  23. */
  24. include "class.singleton.php";
  25. /**
  26. * ZanPHP Load Class
  27. *
  28. * This class is used to load models, views, controllers, classes, libraries, helpers as well as interact directly with templates
  29. *
  30. * @package ZanPHP
  31. * @subpackage core
  32. * @category classes
  33. * @author MilkZoft Developer Team
  34. * @link http://www.zanphp.com/documentation/en/classes/load_class
  35. */
  36. class ZP_Load {
  37. /**
  38. *
  39. *
  40. *
  41. */
  42. public $application = FALSE;
  43. /**
  44. * Contains an Instance (object) of the Templates Class
  45. *
  46. * @object public $Templates
  47. */
  48. public $Templates;
  49. /**
  50. * Contains the array of views
  51. *
  52. * @var private $views = array()
  53. */
  54. private $views = array();
  55. /**
  56. * Loads helper autoload, database config and class templates
  57. *
  58. * @return void
  59. */
  60. public function __construct() {
  61. $helpers = array("autoload", "router", "validations");
  62. $this->helper($helpers);
  63. $this->config("cache");
  64. $this->config("exceptions");
  65. $this->config("languages");
  66. }
  67. /**
  68. *
  69. *
  70. * @param string
  71. * @return
  72. */
  73. public function app($application) {
  74. $this->application = $application;
  75. return $application;
  76. }
  77. /**
  78. * Loads an application class
  79. *
  80. * @param string $class = NULL
  81. * @param string $application = NULL
  82. * @return object value
  83. */
  84. public function classes($class = NULL, $application = NULL) {
  85. if(file_exists(_www . _sh . _applications . _sh . $this->application . _sh . _classes . _sh . _class . _dot . strtolower($class) . _PHP)) {
  86. $file = _www . _sh . _applications . _sh . $this->application . _sh . _classes . _sh . _class . _dot . strtolower($class) . _PHP;
  87. } elseif(file_exists(_www . _sh . _classes . _sh . _class . _dot . strtolower($class) . _PHP)) {
  88. $file = _www . _sh . _classes . _sh . _class . _dot . strtolower($class) . _PHP;
  89. } elseif(file_exists(_www . _sh . _applications . _sh . $application . _sh . _classes . _sh . _class . _dot . strtolower($class) . _PHP)) {
  90. $file = _www . _sh . _applications . _sh . $application . _sh . _classes . _sh . _class . _dot . strtolower($class) . _PHP;
  91. } else {
  92. $file = FALSE;
  93. }
  94. $this->Cache = $this->core("Cache");
  95. if(file_exists($file)) {
  96. if(class_exists($class)) {
  97. if($this->Cache->get($class, "classes")) {
  98. return $this->Cache->get($class, "classes");
  99. } else {
  100. return ZP_Singleton::instance($class);
  101. }
  102. }
  103. if($this->Cache->get($class, "classes")) {
  104. return $this->Cache->get($class, "classes");
  105. } else {
  106. include $file;
  107. }
  108. if(_cacheStatus) {
  109. $$class = ZP_Singleton::instance($class);
  110. $this->Cache->save($$class, $class, "classes");
  111. }
  112. return ZP_Singleton::instance($class);
  113. } else {
  114. die("$class class does not exists");
  115. }
  116. }
  117. /**
  118. * Loads a config file
  119. *
  120. * @param string $name
  121. * @param bool $application = FALSE
  122. * @return void
  123. */
  124. public function config($name) {
  125. if(file_exists(_www . _sh . _config . _sh . _config . _dot . $name . _PHP)) {
  126. include_once _www . _sh . _config . _sh . _config . _dot . $name . _PHP;
  127. } else {
  128. if(file_exists(_www . _sh . _applications . _sh . $name . _sh . _config . _sh . _config . _dot . $name . _PHP)) {
  129. include_once _www . _sh . _applications . _sh . $name . _sh . _config . _sh . _config . _dot . $name . _PHP;
  130. } else {
  131. die("$name config doesn't exists");
  132. }
  133. }
  134. }
  135. /**
  136. * Loads a controller
  137. *
  138. * @param string $name
  139. * @return object value
  140. */
  141. public function controller($controller, $application = NULL) {
  142. $parts = explode("_", $controller);
  143. if(!$this->application) {
  144. if(file_exists(_www . _sh . _applications . _sh . $application . _sh . _controllers . _sh . _controller . _dot . strtolower($parts[0]) . _PHP)) {
  145. $file = _www . _sh . _applications . _sh . $application . _sh . _controllers . _sh . _controller . _dot . strtolower($parts[0]) . _PHP;
  146. } elseif(count($parts) === 2) {
  147. $file = _www . _sh . _applications . _sh . strtolower($parts[0]) . _sh . _controllers . _sh . _controller . _dot . strtolower($parts[0]) . _PHP;
  148. }
  149. } else {
  150. if(file_exists(_www . _sh . _applications . _sh . $application . _sh . _controllers . _sh . _controller . _dot . strtolower($parts[0]) . _PHP)) {
  151. $file = _www . _sh . _applications . _sh . $application . _sh . _controllers . _sh . _controller . _dot . strtolower($parts[0]) . _PHP;
  152. } elseif(file_exists(_www . _sh . _applications . _sh . $this->application . _sh . _controllers . _sh . _controller . _dot . strtolower($parts[0]) . _PHP)) {
  153. $file = _www . _sh . _applications . _sh . $this->application . _sh . _controllers . _sh . _controller . _dot . strtolower($parts[0]) . _PHP;
  154. } else {
  155. $file = _www . _sh . _applications . _sh . strtolower($parts[0]) . _sh . _controllers . _sh . _controller . _dot . strtolower($parts[0]) . _PHP;
  156. }
  157. }
  158. if(file_exists($file)) {
  159. if(class_exists($controller)) {
  160. return ZP_Singleton::instance($controller);
  161. }
  162. include $file;
  163. return ZP_Singleton::instance($controller);
  164. }
  165. return FALSE;
  166. }
  167. /**
  168. * Loads a core class
  169. *
  170. * @param string $class
  171. * @return object value
  172. */
  173. public function core($core) {
  174. return ZP_Singleton::instance("ZP_$core");
  175. }
  176. /**
  177. * Sets a CSS file from an specific application
  178. *
  179. * @param string $CSS = NULL
  180. * @param string $application = NULL
  181. * @param bool $print = FALSE
  182. * @return void
  183. */
  184. public function CSS($CSS = NULL, $application = NULL, $print = FALSE) {
  185. $this->Templates = $this->core("Templates");
  186. $this->Templates->CSS($CSS, $application, $print);
  187. }
  188. public function driver($driver = NULL) {
  189. if(file_exists(_corePath . _sh . _drivers . _sh . _driver . _dot . strtolower($driver) . _PHP)) {
  190. $file = _corePath . _sh . _drivers . _sh . _driver . _dot . strtolower($driver) . _PHP;
  191. } else {
  192. $file = FALSE;
  193. }
  194. $this->Cache = $this->core("Cache");
  195. if(file_exists($file)) {
  196. if(class_exists($driver)) {
  197. if($this->Cache->get($driver, "drivers")) {
  198. return $this->Cache->get($driver, "drivers");
  199. } else {
  200. return ZP_Singleton::instance("ZP_". $driver);
  201. }
  202. }
  203. if($this->Cache->get($driver, "drivers")) {
  204. return $this->Cache->get($driver, "drivers");
  205. } else {
  206. include $file;
  207. }
  208. if(_cacheStatus) {
  209. $$driver= ZP_Singleton::instance("ZP_". $driver);
  210. $this->Cache->save($$driver, $driver, "drivers");
  211. }
  212. return ZP_Singleton::instance("ZP_". $driver);
  213. } else {
  214. die("$driver driver does not exists");
  215. }
  216. }
  217. public function exception($exception) {
  218. if(file_exists(_www . _sh . _lib . _sh . _exceptions . _sh . _exception . _dot . strtolower($exception) . _PHP)) {
  219. include_once _www . _sh . _lib . _sh . _exceptions . _sh . _exception . _dot . strtolower($exception) . _PHP;
  220. } else {
  221. return FALSE;
  222. }
  223. }
  224. public function execute($Class, $method, $params = array(), $type = "controller") {
  225. if($type === "controller") {
  226. $this->$Class = $this->controller($Class);
  227. } elseif($type === "model") {
  228. $this->$Class = $this->model($Class);
  229. }
  230. call_user_func_array(array($this->$Class, $method), $params);
  231. }
  232. /**
  233. * Loads a footer template
  234. *
  235. * @return void
  236. */
  237. private function footer() {
  238. if($this->Templates->exists("footer")) {
  239. if(count($this->views) > 0) {
  240. for($i = 0; $i <= count($this->views) - 1; $i++) {
  241. if($this->views[$i]["vars"] !== FALSE) {
  242. $this->Templates->vars($this->views[$i]["vars"]);
  243. }
  244. }
  245. }
  246. $this->Templates->load("footer");
  247. }
  248. }
  249. /**
  250. * Load header template
  251. *
  252. * @return void
  253. */
  254. private function header() {
  255. if($this->Templates->exists("header")) {
  256. if(count($this->views) > 0) {
  257. for($i = 0; $i <= count($this->views) - 1; $i++) {
  258. if($this->views[$i]["vars"] !== FALSE) {
  259. $this->Templates->vars($this->views[$i]["vars"]);
  260. }
  261. }
  262. }
  263. $this->Templates->load("header");
  264. }
  265. }
  266. /**
  267. * Loads a helper or multiple helper files
  268. *
  269. * @param mixed $helper
  270. * @param string $application
  271. * @return void
  272. */
  273. public function helper($helper, $application = NULL) {
  274. if(is_array($helper)) {
  275. for($i = 0; $i <= count($helper) - 1; $i++) {
  276. if($application === NULL) {
  277. if(file_exists(_corePath . _sh . _helpers . _sh . _helper . _dot . $helper[$i] . _PHP)) {
  278. include_once _corePath . _sh . _helpers . _sh . _helper . _dot . $helper[$i] . _PHP;
  279. } elseif(file_exists(_www . _sh . _helpers . _sh . _helper . _dot . $helper[$i] . _PHP)) {
  280. include_once _www . _sh . _helpers . _sh . _helper . _dot . $helper[$i] . _PHP;
  281. } else {
  282. die("$helper[$i] helper doesn't exists");
  283. }
  284. } else {
  285. if(file_exists(_www . _sh . _applications . _sh . $application . _sh . _helpers . _sh . _helper . _dot . $helper[$i] . _PHP)) {
  286. include_once _www . _sh . _applications . _sh . $application . _sh . _helpers . _sh . _helper . _dot . $helper[$i] . _PHP;
  287. } else {
  288. die("$helper[$i] helper doesn't exists");
  289. }
  290. }
  291. }
  292. } else {
  293. if(is_null($application)) {
  294. if(file_exists(_corePath . _sh . _helpers . _sh . _helper . _dot . $helper . _PHP)) {
  295. include_once _corePath . _sh . _helpers . _sh . _helper . _dot . $helper . _PHP;
  296. } elseif(file_exists(_www . _sh . _helpers . _sh . _helper . _dot . $helper . _PHP)) {
  297. include_once _www . _sh . _helpers . _sh . _helper . _dot . $helper . _PHP;
  298. } else {
  299. die("$name helper doesn't exists");
  300. }
  301. } else {
  302. if(file_exists(_www . _sh . _applications . _sh . $application . _sh . _helpers . _sh . _helper . _dot . $helper . _PHP)) {
  303. include_once _www . _sh . _applications . _sh . $application . _sh . _helpers . _sh . _helper . _dot . $helper . _PHP;
  304. } else {
  305. die("$name helper doesn't exists");
  306. }
  307. }
  308. }
  309. }
  310. /**
  311. * Loads a hook or multiple hook files
  312. *
  313. * @param string $hook
  314. * @param string $application = NULL
  315. * @return void
  316. */
  317. public function hook($hook, $application = NULL) {
  318. if(is_array($hook)) {
  319. for($i = 0; $i <= count($hook) - 1; $i++) {
  320. if(is_null($application)) {
  321. if(file_exists(_corePath . _sh . _hooks . _sh . _hook . _dot . $hook[$i] . _PHP)) {
  322. include_once _corePath . _sh . _hooks . _sh . _hook . _dot . $hook[$i] . _PHP;
  323. } else {
  324. die("$name hook doesn't exists");
  325. }
  326. } else {
  327. if(file_exists(_www . _sh . _applications . _sh . $application . _sh . _hooks . _sh . _hook . _dot . $hook[$i] . _PHP)) {
  328. include_once _www . _sh . _applications . _sh . $application . _sh . _hooks . _sh . _hook . _dot . $hook[$i] . _PHP;
  329. } else {
  330. die("$name hook doesn't exists");
  331. }
  332. }
  333. }
  334. } else {
  335. if(is_null($application)) {
  336. if(file_exists(_corePath . _sh . _hooks . _sh . _hook . _dot . $hook . _PHP)) {
  337. include_once _corePath . _sh . _hooks . _sh . _hook . _dot . $hook . _PHP;
  338. } else {
  339. die("$name hook doesn't exists");
  340. }
  341. } else {
  342. if(file_exists(_www . _sh . _applications . _sh . $application . _sh . _hooks . _sh . _hook . _dot . $hook . _PHP)) {
  343. include_once _www . _sh . _applications . _sh . $application . _sh . _hooks . _sh . _hook . _dot . $hook . _PHP;
  344. } else {
  345. die("$name hook doesn't exists");
  346. }
  347. }
  348. }
  349. }
  350. /**
  351. * Sets a JS file from an specific application
  352. *
  353. * @param string $script
  354. * @param string $application = NULL
  355. * @param bool $extra = NULL
  356. * @param bool $getJs = FALSE
  357. * @return void
  358. */
  359. public function js($script, $application = NULL, $extra = NULL, $getJs = FALSE) {
  360. $this->Templates = $this->core("Templates");
  361. $this->Templates->js($script, $application, $extra, $getJs);
  362. }
  363. /**
  364. * Load languages files
  365. *
  366. * @param string $language
  367. * @return void
  368. */
  369. public function language($language) {
  370. if(file_exists(_www . _sh . _lib . _sh . _languages . _sh . _language . _dot . strtolower($language) . _PHP)) {
  371. include_once _www . _sh . _lib . _sh . _languages . _sh . _language . _dot . strtolower($language) . _PHP;
  372. } else {
  373. return FALSE;
  374. }
  375. }
  376. /**
  377. * Loads a left template
  378. *
  379. * @return void
  380. */
  381. private function left() {
  382. if($this->Templates->exists("left")) {
  383. if(count($this->views) > 0) {
  384. for($i = 0; $i <= count($this->views) - 1; $i++) {
  385. if($this->views[$i]["vars"] !== FALSE) {
  386. $this->Templates->vars($this->views[$i]["vars"]);
  387. }
  388. }
  389. }
  390. $this->Templates->load("left");
  391. }
  392. }
  393. /**
  394. * Loads a library file
  395. *
  396. * @param string $name
  397. * @param string $library
  398. * @return void
  399. */
  400. public function library($name, $library = NULL, $application = NULL) {
  401. $lib = str_replace("library.", "", $name);
  402. if(isset($name) and $library !== NULL) {
  403. if(file_exists(_corePath . _sh . _libraries . _sh . $library . _sh . $name . _PHP)) {
  404. include_once _corePath . _sh . _libraries . _sh . $library . _sh . $name . _PHP;
  405. } else {
  406. die("$name library doesn't exists");
  407. }
  408. } elseif(isset($name) and !is_null($application)) {
  409. if(file_exists(_www . _sh . _applications . _sh . $application . _sh . _libraries . _sh . _library . "." . $name . _PHP)) {
  410. include_once _www . _sh . _applications . _sh . $application . _sh . _libraries . _sh . _library . "." . $name . _PHP;
  411. } else {
  412. die("$name library doesn't exists");
  413. }
  414. } else {
  415. if(file_exists(_corePath . _sh . _libraries . _sh . $lib . _sh . strtolower($name) . _PHP)) {
  416. include_once _corePath . _sh . _libraries . _sh . $lib . _sh . strtolower($name) . _PHP;
  417. } else {
  418. die("$name library doesn't exists");
  419. }
  420. }
  421. }
  422. /**
  423. * Loads a model file
  424. *
  425. * @param string $name
  426. * @return object value
  427. */
  428. public function model($model) {
  429. $parts = explode("_", $model);
  430. if(!$this->application) {
  431. if(count($parts) === 2) {
  432. $file = _www . _sh . _applications . _sh . strtolower($parts[0]) . _sh . _models . _sh . _model . _dot . strtolower($parts[0]) . _PHP;
  433. }
  434. } else {
  435. if(count($parts) === 2) {
  436. if(file_exists(_www . _sh . _applications . _sh . $this->application . _sh . _models . _sh . _model . _dot . strtolower($parts[0]) . _PHP)) {
  437. $file = _www . _sh . _applications . _sh . $this->application . _sh . _models . _sh . _model . _dot . strtolower($parts[0]) . _PHP;
  438. } else {
  439. $file = _www . _sh . _applications . _sh . strtolower($parts[0]) . _sh . _models . _sh . _model . _dot . strtolower($parts[0]) . _PHP;
  440. }
  441. }
  442. }
  443. if(file_exists($file)) {
  444. if(class_exists($model)) {
  445. return ZP_Singleton::instance($model);
  446. }
  447. include $file;
  448. return ZP_Singleton::instance($model);
  449. }
  450. return FALSE;
  451. }
  452. /**
  453. * Render and output templates
  454. *
  455. * @return void
  456. */
  457. public function render() {
  458. $numArgs = func_num_args();
  459. $args = func_get_args();
  460. if($numArgs > 0) {
  461. for($i = 0; $i <= $numArgs - 1; $i++) {
  462. if($this->views[$i]["vars"] !== FALSE) {
  463. $this->Templates->vars($this->views[$i]["vars"]);
  464. }
  465. if(isset($args[$i]) and $args[$i] === "header") {
  466. $this->header();
  467. }
  468. if($args[$i] !== "header" and $args[$i] !== "left" and $args[$i] !== "right" and $args[$i] !== "footer") {
  469. if($this->Templates->exists($args[$i])) {
  470. $this->Templates->load($args[$i]);
  471. continue;
  472. }
  473. }
  474. if(isset($args[$i]) and $args[$i] === "left") {
  475. $this->left();
  476. }
  477. if(count($this->views) > 0) {
  478. for($i = 0; $i <= count($this->views) - 1; $i++) {
  479. $this->Templates->load($this->views[$i]["name"]);
  480. }
  481. }
  482. if(isset($args[$i]) and $args[$i] === "right") {
  483. $this->right();
  484. }
  485. if(isset($args[$i]) and $args[$i] === "footer") {
  486. $this->footer();
  487. }
  488. }
  489. } else {
  490. for($i = 0; $i <= count($this->views) - 1; $i++) {
  491. if($this->views[$i]["vars"] !== FALSE) {
  492. $this->Templates->vars($this->views[$i]["vars"]);
  493. }
  494. }
  495. $this->header();
  496. $this->left();
  497. if(count($this->views) > 0) {
  498. for($i = 0; $i <= count($this->views) - 1; $i++) {
  499. $this->Templates->load($this->views[$i]["name"]);
  500. }
  501. }
  502. $this->right();
  503. $this->footer();
  504. }
  505. }
  506. /**
  507. * Loads a right template
  508. *
  509. * @return void
  510. */
  511. private function right() {
  512. if($this->Templates->exists("right")) {
  513. if(count($this->views) > 0) {
  514. for($i = 0; $i <= count($this->views) - 1; $i++) {
  515. if($this->views[$i]["vars"] !== FALSE) {
  516. $this->Templates->vars($this->views[$i]["vars"]);
  517. }
  518. }
  519. }
  520. $this->Templates->load("right");
  521. }
  522. }
  523. /**
  524. * Loads templates
  525. *
  526. * @param string $name
  527. * @param string $vars
  528. * @return string value / void
  529. */
  530. public function template($name, $vars = NULL) {
  531. if(is_array($vars)) {
  532. if(count($this->views) === 0) {
  533. $this->views[0]["name"] = $name;
  534. $this->views[0]["vars"] = $vars;
  535. } else {
  536. $i = count($this->views);
  537. $this->views[$i]["name"] = $name;
  538. $this->views[$i]["vars"] = $vars;
  539. }
  540. } else {
  541. $i = count($this->views);
  542. $this->views[$i]["name"] = $name;
  543. $this->views[$i]["vars"] = FALSE;
  544. }
  545. }
  546. /**
  547. * Set the current theme
  548. *
  549. * @return void
  550. */
  551. public function theme($theme) {
  552. $this->Templates = $this->core("Templates");
  553. $this->Templates->theme($theme);
  554. }
  555. /**
  556. * Set title for header template
  557. *
  558. * @param string $title = NULL
  559. * @return void
  560. */
  561. public function title($title = NULL) {
  562. $this->Templates = $this->core("Templates");
  563. $this->Templates->title(__($title));
  564. }
  565. /**
  566. * Loads a view
  567. *
  568. * @param string $name
  569. * @param string $application = NULL
  570. * @param string $vars = NULL
  571. * @return string value / void
  572. */
  573. public function view($name, $vars = NULL, $application = NULL) {
  574. if(is_null($application)) {
  575. $application = whichApplication();
  576. }
  577. if(!is_null($application)) {
  578. $view = _www . _sh . _applications . _sh . $application . _sh . _views . _sh . _view . _dot . $name . _PHP;
  579. $cacheID = cacheSession($name);
  580. if(is_array($vars)) {
  581. $key = array_keys($vars);
  582. $size = sizeof($key);
  583. for($i = 0; $i < $size; $i++) {
  584. $$key[$i] = $vars[$key[$i]];
  585. }
  586. } elseif($vars) {
  587. return $view;
  588. }
  589. if(file_exists($view)) {
  590. $this->Cache = $this->core("Cache");
  591. if($this->Cache->get($cacheID, "views")) {
  592. print $this->Cache->get($cacheID, "views");
  593. } else {
  594. include $view;
  595. }
  596. if(_cacheStatus) {
  597. $output = ob_get_contents();
  598. ob_end_clean();
  599. $this->Cache->save($output, $cacheID, "views");
  600. print $output;
  601. }
  602. } else {
  603. die("Error 404: $view view not found");
  604. }
  605. } else {
  606. return FALSE;
  607. }
  608. }
  609. }