Autoload.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. /**
  3. * The autoloader class helps to load controllers and models as well as user objects
  4. * when they are requested by the application.
  5. *
  6. * @author Ironpilot
  7. * @copyright Copywrite (c) 2011, STAPLE CODE
  8. *
  9. * This file is part of the STAPLE Framework.
  10. *
  11. * The STAPLE Framework is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by the
  13. * Free Software Foundation, either version 3 of the License, or (at your option)
  14. * any later version.
  15. *
  16. * The STAPLE Framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. class Staple_Autoload
  25. {
  26. const CONTROLLER_SUFFIX = 'Controller';
  27. const FORM_SUFFIX = 'Form';
  28. const MODEL_SUFFIX = 'Model';
  29. const STAPLE_PREFIX = 'Staple_';
  30. const STAPLE_TRAIT_PREFIX = 'Staple_Trait';
  31. const PHP_FILE_EXTENSION = '.php';
  32. const CLASS_FILE_EXTENSION = '.class.php';
  33. const TRAIT_FILE_EXTENSION = '.trait.php';
  34. const VIEW_FILE_EXTENSION = '.phtml';
  35. const TRAIT_FOLDER = 'Traits';
  36. /**
  37. * Controller Class Suffix Value
  38. * @var string
  39. */
  40. protected $controllerSuffix;
  41. /**
  42. * Array of search directories for the models
  43. * @var array[string]
  44. */
  45. protected $controllerSearchDirectories = array();
  46. /**
  47. * Form Class Suffix Value
  48. * @var string
  49. */
  50. protected $formSuffix;
  51. /**
  52. * Array of search directories for the models
  53. * @var array[string]
  54. */
  55. protected $formSearchDirectories = array();
  56. /**
  57. * Model Class Suffix Value
  58. * @var string
  59. */
  60. protected $modelSuffix;
  61. /**
  62. * Array of search directories for the models
  63. * @var array[string]
  64. */
  65. protected $modelSearchDirectories = array();
  66. /**
  67. * Array of search directories for the views
  68. * @var array[string]
  69. */
  70. protected $viewSearchDirectories = array();
  71. /**
  72. * Array of search directories for the views
  73. * @var array[string]
  74. */
  75. protected $layoutSearchDirectories = array();
  76. /**
  77. * Booleon: On loader failure throw an exception
  78. * @var bool
  79. */
  80. protected $throwOnFailure = true;
  81. /**
  82. * Automatically loads class files for the application.
  83. * @param string $class_name
  84. * @throws Exception
  85. */
  86. public function __construct()
  87. {
  88. //Add the default controller location
  89. $this->addControllerSearchDirectory(CONTROLLER_ROOT,false);
  90. $this->setControllerSuffix(self::CONTROLLER_SUFFIX);
  91. //Add the default form location
  92. $this->addFormSearchDirectory(FORMS_ROOT,false);
  93. $this->setFormSuffix(static::FORM_SUFFIX);
  94. //Add the default model location
  95. $this->addModelSearchDirectory(MODEL_ROOT,false);
  96. $this->setModelSuffix(static::MODEL_SUFFIX);
  97. //Add the default view location
  98. $this->addLayoutSearchDirectory(LAYOUT_ROOT,false);
  99. //Add the default view location
  100. $this->addViewSearchDirectory(VIEW_ROOT,false);
  101. }
  102. /**
  103. * Function alias for the loadClass() method
  104. * @param string $class_name
  105. */
  106. public function load($class_name)
  107. {
  108. return $this->loadClass($class_name);
  109. }
  110. /**
  111. * Load a class into the application
  112. * @param string $class_name
  113. * @throws Exception
  114. */
  115. public function loadClass($class_name)
  116. {
  117. if(substr($class_name,0,strlen(static::STAPLE_TRAIT_PREFIX)) == static::STAPLE_TRAIT_PREFIX) //Look for STAPLE Traits
  118. {
  119. $class_name = substr($class_name,strlen(static::STAPLE_TRAIT_PREFIX),strlen($class_name));
  120. $folders = explode('_', $class_name);
  121. if(count($folders) > 1)
  122. {
  123. $subpath = '';
  124. for($i = 0; $i < count($folders)-1; $i++)
  125. {
  126. $subpath .= $folders[$i].DIRECTORY_SEPARATOR;
  127. }
  128. $include = STAPLE_ROOT.static::TRAIT_FOLDER.DIRECTORY_SEPARATOR.$subpath.$folders[count($folders)-1].static::TRAIT_FILE_EXTENSION;
  129. if(file_exists($include))
  130. {
  131. require_once $include;
  132. }
  133. else
  134. {
  135. throw new Exception('Error Loading Framework'.$include, Staple_Error::LOADER_ERROR);
  136. }
  137. }
  138. else
  139. {
  140. $include = STAPLE_ROOT.static::TRAIT_FOLDER.DIRECTORY_SEPARATOR.$class_name.static::TRAIT_FILE_EXTENSION;
  141. if(file_exists($include))
  142. {
  143. require_once $include;
  144. }
  145. else
  146. {
  147. throw new Exception('Error Loading Framework'.$include, Staple_Error::LOADER_ERROR);
  148. }
  149. }
  150. }
  151. elseif(substr($class_name,0,strlen(static::STAPLE_PREFIX)) == static::STAPLE_PREFIX) //Look for STAPLE classes
  152. {
  153. $class_name = substr($class_name,strlen(static::STAPLE_PREFIX),strlen($class_name));
  154. $folders = explode('_', $class_name);
  155. if(count($folders) > 1)
  156. {
  157. $path = STAPLE_ROOT;
  158. for($i = 0; $i < count($folders)-1; $i++)
  159. {
  160. $path .= $folders[$i].DIRECTORY_SEPARATOR;
  161. }
  162. $include = $path.$folders[count($folders)-1].static::CLASS_FILE_EXTENSION;
  163. if(file_exists($include))
  164. {
  165. require_once $include;
  166. }
  167. else
  168. {
  169. throw new Exception('Error Loading Framework', Staple_Error::LOADER_ERROR);
  170. }
  171. }
  172. else
  173. {
  174. $include = STAPLE_ROOT.$class_name.static::CLASS_FILE_EXTENSION;
  175. if(file_exists($include))
  176. {
  177. require_once $include;
  178. }
  179. else
  180. {
  181. throw new Exception('Error Loading Framework', Staple_Error::LOADER_ERROR);
  182. }
  183. }
  184. }
  185. elseif(substr($class_name,strlen($class_name)-strlen($this->getControllerSuffix()),strlen($this->getControllerSuffix())) == $this->getControllerSuffix()) //Look for Controllers
  186. {
  187. $include = CONTROLLER_ROOT.$class_name.static::PHP_FILE_EXTENSION;
  188. if(file_exists($include))
  189. {
  190. require_once $include;
  191. }
  192. else
  193. {
  194. if($this->throwOnFailure === true)
  195. {
  196. throw new Exception('Page Not Found',Staple_Error::PAGE_NOT_FOUND);
  197. }
  198. }
  199. }
  200. elseif(substr($class_name,strlen($class_name)-strlen($this->getModelSuffix()),strlen($this->getModelSuffix())) == $this->getModelSuffix()) //Look for Models
  201. {
  202. $include = MODEL_ROOT.$class_name.static::PHP_FILE_EXTENSION;
  203. if(file_exists($include))
  204. {
  205. require_once $include;
  206. }
  207. else
  208. {
  209. if($this->throwOnFailure === true)
  210. {
  211. throw new Exception('Model Not Found',Staple_Error::LOADER_ERROR);
  212. }
  213. }
  214. }
  215. elseif(substr($class_name,strlen($class_name)-4,4) == "Form") //Look for Forms
  216. {
  217. $include = FORMS_ROOT.$class_name.static::PHP_FILE_EXTENSION;
  218. if(file_exists($include))
  219. {
  220. require_once $include;
  221. }
  222. else
  223. {
  224. if($this->throwOnFailure === true)
  225. {
  226. throw new Exception('Form Not Found',Staple_Error::LOADER_ERROR);
  227. }
  228. }
  229. }
  230. /*elseif(substr($class_name,0,5) == 'Zend_' && file_exists(LIBRARY_ROOT.'Zend/Loader.php')) //Look for Zend Classes
  231. {
  232. //Add Library Root to Include Path
  233. if(strpos(get_include_path(), LIBRARY_ROOT) === false)
  234. {
  235. set_include_path(get_include_path().PATH_SEPARATOR.LIBRARY_ROOT);
  236. }
  237. try{
  238. require_once LIBRARY_ROOT . 'Zend/Loader.php';
  239. if(class_exists('Zend_Loader'))
  240. {
  241. Zend_Loader::loadClass($class_name);
  242. }
  243. }
  244. catch(Exception $e)
  245. {
  246. if($this->throwOnFailure === true)
  247. {
  248. throw new Exception('Zend Loader Not Found',Staple_Error::LOADER_ERROR);
  249. }
  250. }
  251. }*/
  252. else //Look for other elements
  253. {
  254. if(file_exists(ELEMENTS_ROOT.$class_name.static::PHP_FILE_EXTENSION))
  255. {
  256. require_once ELEMENTS_ROOT.$class_name.static::PHP_FILE_EXTENSION;
  257. }
  258. else
  259. {
  260. if($this->throwOnFailure === true)
  261. {
  262. throw new Exception("Class Not Found",Staple_Error::LOADER_ERROR);
  263. }
  264. }
  265. }
  266. }
  267. /**
  268. * Load a View into the application
  269. * @param string $controller
  270. * @param string $view
  271. * @param bool $required
  272. */
  273. public function loadView($controller,$view,$required = false)
  274. {
  275. foreach($this->viewSearchDirectories as $dir)
  276. {
  277. $theView = $dir;
  278. if(substr($theView,strlen($theView)-2) == DIRECTORY_SEPARATOR)
  279. {
  280. $theView .= DIRECTORY_SEPARATOR;
  281. }
  282. $theView .= $controller.DIRECTORY_SEPARATOR.$view.static::VIEW_FILE_EXTENSION;
  283. if(file_exists($theView))
  284. {
  285. return $theView;
  286. }
  287. }
  288. if($required === true)
  289. {
  290. throw new Exception('Failed to load the view.', Staple_Error::LOADER_ERROR);
  291. }
  292. }
  293. /**
  294. * Load a View into the application
  295. * @param string $controller
  296. * @param string $view
  297. * @param bool $required
  298. */
  299. public function loadLayout($name)
  300. {
  301. foreach($this->layoutSearchDirectories as $dir)
  302. {
  303. $theLayout = $dir;
  304. if(substr($theLayout,strlen($theLayout)-2) == DIRECTORY_SEPARATOR)
  305. {
  306. $theLayout .= DIRECTORY_SEPARATOR;
  307. }
  308. $theLayout .= $name.static::VIEW_FILE_EXTENSION;
  309. if(file_exists($theLayout))
  310. {
  311. return $theLayout;
  312. }
  313. }
  314. throw new Exception('Unable to locate layout.', Staple_Error::LOADER_ERROR);
  315. }
  316. /**
  317. * Return the value of $throwOnFailure
  318. * @return the $throwOnFailure
  319. */
  320. public function getThrowOnFailure()
  321. {
  322. return $this->throwOnFailure;
  323. }
  324. /**
  325. * Allows the programmer to disable thrown exceptions when failing to load classes. Allows another loading system to take over and load the class.
  326. * @param boolean $throwOnFailure
  327. */
  328. public function setThrowOnFailure($throwOnFailure)
  329. {
  330. $this->throwOnFailure = (bool)$throwOnFailure;
  331. return $this;
  332. }
  333. /**
  334. * @return the $controllerSuffix
  335. */
  336. public function getControllerSuffix()
  337. {
  338. return $this->controllerSuffix;
  339. }
  340. /**
  341. * @return the $formSuffix
  342. */
  343. public function getFormSuffix()
  344. {
  345. return $this->formSuffix;
  346. }
  347. /**
  348. * @return the $modelSuffix
  349. */
  350. public function getModelSuffix()
  351. {
  352. return $this->modelSuffix;
  353. }
  354. /**
  355. * @param string $controllerSuffix
  356. */
  357. private function setControllerSuffix($controllerSuffix)
  358. {
  359. $this->controllerSuffix = $controllerSuffix;
  360. return $this;
  361. }
  362. /**
  363. * @param string $formSuffix
  364. */
  365. private function setFormSuffix($formSuffix)
  366. {
  367. $this->formSuffix = $formSuffix;
  368. return $this;
  369. }
  370. /**
  371. * @param string $modelSuffix
  372. */
  373. private function setModelSuffix($modelSuffix)
  374. {
  375. $this->modelSuffix = $modelSuffix;
  376. return $this;
  377. }
  378. /**
  379. * Add a search directory for the application to look for controller class files. The second parameter will make the new directory take precedence
  380. * over any previous directories. It is the default to add new directories as the primary directory.
  381. * @param string $dir
  382. */
  383. public function addControllerSearchDirectory($dir, $primary = true)
  384. {
  385. if($primary === true)
  386. {
  387. array_unshift($this->controllerSearchDirectories, $dir);
  388. }
  389. else
  390. {
  391. array_push($this->controllerSearchDirectories, $dir);
  392. }
  393. return $this;
  394. }
  395. /**
  396. * Add a search directory for the application to look for form class files. The second parameter will make the new directory take precedence
  397. * over any previous directories. It is the default to add new directories as the primary directory.
  398. * @param string $dir
  399. */
  400. public function addFormSearchDirectory($dir, $primary = true)
  401. {
  402. if($primary === true)
  403. {
  404. array_unshift($this->formSearchDirectories, $dir);
  405. }
  406. else
  407. {
  408. array_push($this->formSearchDirectories, $dir);
  409. }
  410. return $this;
  411. }
  412. /**
  413. * Add a search directory for the application to look for model class files. The second parameter will make the new directory take precedence
  414. * over any previous directories. It is the default to add new directories as the primary directory.
  415. * @param string $dir
  416. */
  417. public function addModelSearchDirectory($dir, $primary = true)
  418. {
  419. if($primary === true)
  420. {
  421. array_unshift($this->modelSearchDirectories, $dir);
  422. }
  423. else
  424. {
  425. array_push($this->modelSearchDirectories, $dir);
  426. }
  427. return $this;
  428. }
  429. /**
  430. * Add a search directory for the application to look for view files. The second parameter will make the new directory take precedence
  431. * over any previous directories. It is the default to add new directories as the primary directory.
  432. * @param string $dir
  433. */
  434. public function addLayoutSearchDirectory($dir, $primary = true)
  435. {
  436. if($primary === true)
  437. {
  438. array_unshift($this->layoutSearchDirectories, $dir);
  439. }
  440. else
  441. {
  442. array_push($this->layoutSearchDirectories, $dir);
  443. }
  444. return $this;
  445. }
  446. /**
  447. * Add a search directory for the application to look for view files. The second parameter will make the new directory take precedence
  448. * over any previous directories. It is the default to add new directories as the primary directory.
  449. * @param string $dir
  450. */
  451. public function addViewSearchDirectory($dir, $primary = true)
  452. {
  453. if($primary === true)
  454. {
  455. array_unshift($this->viewSearchDirectories, $dir);
  456. }
  457. else
  458. {
  459. array_push($this->viewSearchDirectories, $dir);
  460. }
  461. return $this;
  462. }
  463. /**
  464. * @return the $controllerSearchDirectories
  465. */
  466. public function getControllerSearchDirectories()
  467. {
  468. return $this->controllerSearchDirectories;
  469. }
  470. /**
  471. * @return the $formSearchDirectories
  472. */
  473. public function getFormSearchDirectories()
  474. {
  475. return $this->formSearchDirectories;
  476. }
  477. /**
  478. * @return the $modelSearchDirectories
  479. */
  480. public function getModelSearchDirectories()
  481. {
  482. return $this->modelSearchDirectories;
  483. }
  484. /**
  485. * @return the $viewSearchDirectories
  486. */
  487. public function getViewSearchDirectories()
  488. {
  489. return $this->viewSearchDirectories;
  490. }
  491. /**
  492. * @return the $layoutSearchDirectories
  493. */
  494. public function getLayoutSearchDirectories()
  495. {
  496. return $this->layoutSearchDirectories;
  497. }
  498. /**
  499. * @param array[string] $layoutSearchDirectories
  500. */
  501. public function setLayoutSearchDirectories(array $layoutSearchDirectories)
  502. {
  503. $this->layoutSearchDirectories = $layoutSearchDirectories;
  504. return $this;
  505. }
  506. /**
  507. * @param array[string] $controllerSearchDirectories
  508. */
  509. public function setControllerSearchDirectories(array $controllerSearchDirectories)
  510. {
  511. $this->controllerSearchDirectories = $controllerSearchDirectories;
  512. return $this;
  513. }
  514. /**
  515. * @param array[string] $formSearchDirectories
  516. */
  517. public function setFormSearchDirectories(array $formSearchDirectories)
  518. {
  519. $this->formSearchDirectories = $formSearchDirectories;
  520. return $this;
  521. }
  522. /**
  523. * @param array[string] $modelSearchDirectories
  524. */
  525. public function setModelSearchDirectories(array $modelSearchDirectories)
  526. {
  527. $this->modelSearchDirectories = $modelSearchDirectories;
  528. return $this;
  529. }
  530. /**
  531. * @param array[string] $viewSearchDirectories
  532. */
  533. public function setViewSearchDirectories(array $viewSearchDirectories)
  534. {
  535. $this->viewSearchDirectories = $viewSearchDirectories;
  536. return $this;
  537. }
  538. }