PageRenderTime 94ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/fLoader.php

https://github.com/netcarver/flourish
PHP | 314 lines | 177 code | 36 blank | 101 comment | 16 complexity | 9f01be940726c3224d8b39cca419ee4e MD5 | raw file
  1. <?php
  2. /**
  3. * A class that loads Flourish
  4. *
  5. * @copyright Copyright (c) 2011 Will Bond, others
  6. * @author Will Bond [wb] <will@flourishlib.com>
  7. * @author netcarver [n] <fContrib@netcarving.com>
  8. * @license http://flourishlib.com/license
  9. *
  10. * @package Flourish
  11. * @link http://flourishlib.com/fLoader
  12. *
  13. * @version 1.0.0b3
  14. * @changes 1.0.0b3 Added fEmail() constructor function [n, 2011-09-12]
  15. * @changes 1.0.0b2 Added fPagination [wb, 2011-09-06]
  16. * @changes 1.0.0b The initial implementation [wb, 2011-08-26]
  17. */
  18. class fLoader
  19. {
  20. // The following constants allow for nice looking callbacks to static methods
  21. const autoload = 'fLoader::autoload';
  22. const best = 'fLoader::best';
  23. const eager = 'fLoader::eager';
  24. const hasOpcodeCache = 'fLoader::hasOpcodeCache';
  25. const lazy = 'fLoader::lazy';
  26. /**
  27. * The Flourish classes in dependency order
  28. *
  29. * @var array
  30. */
  31. static private $classes = array(
  32. 'fException',
  33. 'fExpectedException',
  34. 'fEmptySetException',
  35. 'fNoRemainingException',
  36. 'fNoRowsException',
  37. 'fNotFoundException',
  38. 'fValidationException',
  39. 'fUnexpectedException',
  40. 'fConnectivityException',
  41. 'fEnvironmentException',
  42. 'fProgrammerException',
  43. 'fSQLException',
  44. 'fActiveRecord',
  45. 'fAuthorization',
  46. 'fAuthorizationException',
  47. 'fBuffer',
  48. 'fCRUD',
  49. 'fCache',
  50. 'fCookie',
  51. 'fCore',
  52. 'fCryptography',
  53. 'fDatabase',
  54. 'fDate',
  55. 'fDirectory',
  56. 'fEmail',
  57. 'fFile',
  58. 'fFilesystem',
  59. 'fGrammar',
  60. 'fHTML',
  61. 'fImage',
  62. 'fJSON',
  63. 'fMailbox',
  64. 'fMessaging',
  65. 'fMoney',
  66. 'fNumber',
  67. 'fORM',
  68. 'fORMColumn',
  69. 'fORMDatabase',
  70. 'fORMDate',
  71. 'fORMFile',
  72. 'fORMJSON',
  73. 'fORMMoney',
  74. 'fORMOrdering',
  75. 'fORMRelated',
  76. 'fORMSchema',
  77. 'fORMValidation',
  78. 'fPagination',
  79. 'fRecordSet',
  80. 'fRequest',
  81. 'fResult',
  82. 'fSMTP',
  83. 'fSQLSchemaTranslation',
  84. 'fSQLTranslation',
  85. 'fSchema',
  86. 'fSession',
  87. 'fStatement',
  88. 'fTemplating',
  89. 'fText',
  90. 'fTime',
  91. 'fTimestamp',
  92. 'fURL',
  93. 'fUTF8',
  94. 'fUnbufferedResult',
  95. 'fUpload',
  96. 'fValidation',
  97. 'fXML'
  98. );
  99. /**
  100. * The path Flourish is installed into
  101. *
  102. * @var string
  103. */
  104. static private $path = NULL;
  105. /**
  106. * Tries to load a Flourish class
  107. *
  108. * @internal
  109. *
  110. * @param string $class The class to load
  111. * @return void
  112. */
  113. static public function autoload($class)
  114. {
  115. if ($class[0] != 'f' || ord($class[1]) < 65 || ord($class[1]) > 90) {
  116. return;
  117. }
  118. if (!in_array($class, self::$classes)) {
  119. return;
  120. }
  121. include self::$path . $class . '.php';
  122. }
  123. /**
  124. * Performs eager loading if an op-code cache is present, otherwise lazy
  125. *
  126. * @return void
  127. */
  128. static public function best()
  129. {
  130. if (self::hasOpcodeCache()) {
  131. return self::eager();
  132. }
  133. self::lazy();
  134. }
  135. /**
  136. * Creates functions that act as chainable constructors
  137. *
  138. * @return void
  139. */
  140. static private function createConstructorFunctions()
  141. {
  142. if (function_exists('fDate')) {
  143. return;
  144. }
  145. function fDate($date=NULL)
  146. {
  147. return new fDate($date);
  148. }
  149. function fDirectory($directory)
  150. {
  151. return new fDirectory($directory);
  152. }
  153. function fEmail()
  154. {
  155. return new fEmail();
  156. }
  157. function fFile($file)
  158. {
  159. return new fFile($file);
  160. }
  161. function fImage($file_path)
  162. {
  163. return new fImage($file_path);
  164. }
  165. function fMoney($amount, $currency=NULL)
  166. {
  167. return new fMoney($amount, $currency);
  168. }
  169. function fNumber($value, $scale=NULL)
  170. {
  171. return new fNumber($value, $scale);
  172. }
  173. function fTime($time=NULL)
  174. {
  175. return new fTime($time);
  176. }
  177. function fTimestamp($datetime=NULL, $timezone=NULL)
  178. {
  179. return new fTimestamp($datetime, $timezone);
  180. }
  181. }
  182. /**
  183. * Loads all Flourish classes when called
  184. *
  185. * @return void
  186. */
  187. static public function eager()
  188. {
  189. self::setPath();
  190. self::createConstructorFunctions();
  191. foreach (self::$classes as $class) {
  192. include self::$path . $class . '.php';
  193. }
  194. }
  195. /**
  196. * Check if a PHP opcode cache is installed
  197. *
  198. * The following opcode caches are currently detected:
  199. *
  200. * - [http://pecl.php.net/package/APC APC]
  201. * - [http://eaccelerator.net eAccelerator]
  202. * - [http://www.nusphere.com/products/phpexpress.htm Nusphere PhpExpress]
  203. * - [http://turck-mmcache.sourceforge.net/index_old.html Turck MMCache]
  204. * - [http://xcache.lighttpd.net XCache]
  205. * - [http://www.zend.com/en/products/server/ Zend Server (Optimizer+)]
  206. * - [http://www.zend.com/en/products/platform/ Zend Platform (Code Acceleration)]
  207. *
  208. * @return boolean If a PHP opcode cache is loaded
  209. */
  210. static public function hasOpcodeCache()
  211. {
  212. $apc = ini_get('apc.enabled');
  213. $eaccelerator = ini_get('eaccelerator.enable');
  214. $mmcache = ini_get('mmcache.enable');
  215. $phpexpress = function_exists('phpexpress');
  216. $xcache = ini_get('xcache.size') > 0 && ini_get('xcache.cacher');
  217. $zend_accelerator = ini_get('zend_accelerator.enabled');
  218. $zend_plus = ini_get('zend_optimizerplus.enable');
  219. return $apc || $eaccelerator || $mmcache || $phpexpress || $xcache || $zend_accelerator || $zend_plus;
  220. }
  221. /**
  222. * Registers an autoloader for Flourish via [http://php.net/spl_autoload_register `spl_autoload_register()`]
  223. *
  224. * @return void
  225. */
  226. static public function lazy()
  227. {
  228. self::setPath();
  229. self::createConstructorFunctions();
  230. if (function_exists('__autoload') && !spl_autoload_functions()) {
  231. throw new Exception(
  232. 'fLoader::lazy() was called, which adds an autoload function ' .
  233. 'via spl_autoload_register(). It appears an __autoload ' .
  234. 'function has already been defined, but not registered via ' .
  235. 'spl_autoload_register(). Please call ' .
  236. 'spl_autoload_register("__autoload") after fLoader::lazy() ' .
  237. 'to ensure your autoloader continues to function.'
  238. );
  239. }
  240. spl_autoload_register(array('fLoader', 'autoload'));
  241. }
  242. /**
  243. * Determines where Flourish is installed
  244. *
  245. * @return void
  246. */
  247. static private function setPath()
  248. {
  249. self::$path = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
  250. }
  251. /**
  252. * Forces use as a static class
  253. *
  254. * @return fLoader
  255. */
  256. private function __construct() { }
  257. }
  258. /**
  259. * Copyright (c) 2011 Will Bond <will@flourishlib.com>, others
  260. *
  261. * Permission is hereby granted, free of charge, to any person obtaining a copy
  262. * of this software and associated documentation files (the "Software"), to deal
  263. * in the Software without restriction, including without limitation the rights
  264. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  265. * copies of the Software, and to permit persons to whom the Software is
  266. * furnished to do so, subject to the following conditions:
  267. *
  268. * The above copyright notice and this permission notice shall be included in
  269. * all copies or substantial portions of the Software.
  270. *
  271. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  272. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  273. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  274. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  275. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  276. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  277. * THE SOFTWARE.
  278. */