pquery.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. /**
  3. * Base for pQuery PHP utility framework.
  4. *
  5. * @package pQuery
  6. */
  7. /**
  8. * Common utility class.
  9. */
  10. class pQuery {
  11. /**
  12. * Name of the utilities folder
  13. *
  14. * @var string
  15. */
  16. const UTILS_FOLDER = 'utils/';
  17. /**
  18. * Pattern of the alias created for an extending plugin that has defined an alias.
  19. *
  20. * @var string
  21. */
  22. const CLASS_ALIAS_PATTERN = '__%s';
  23. /**
  24. * Pattern of a plugin's filename.
  25. *
  26. * @var string
  27. */
  28. const PLUGIN_FILENAME_PATTERN = 'pquery.%s.php';
  29. /**
  30. * The minimum php version required to use the framework.
  31. *
  32. * @var string
  33. */
  34. static $REQUIRED_PHP_VERSION = '5.3';
  35. /**
  36. * A list of all plugins currently included.
  37. *
  38. * @var array
  39. */
  40. static $plugins = array();
  41. /**
  42. * The variable types accepted by the parser.
  43. *
  44. * @var array
  45. * @see set_variable()
  46. */
  47. static $accepts = array('boolean', 'integer', 'double', 'string', 'array', 'object', 'NULL');
  48. /**
  49. * A list of names of plugins that are required to run a plugin.
  50. *
  51. * @var array
  52. */
  53. static $require_plugins = array();
  54. /**
  55. * Aliases for the variable setter and getter.
  56. *
  57. * @var string|array
  58. */
  59. static $variable_alias = array();
  60. /**
  61. * The current variable.
  62. *
  63. * @var mixed
  64. */
  65. var $variable;
  66. /**
  67. * Additional arguments that were passed to the constructor.
  68. *
  69. * @var array
  70. */
  71. var $arguments = array();
  72. /**
  73. * Parse the type of the given variable, and convert it if needed.
  74. *
  75. * @param mixed $variable The variable to parse.
  76. * @param bool $force Whether not to check the variables type against the accepted types.
  77. */
  78. function set_variable($variable, $force=false) {
  79. $this->variable = $variable;
  80. if( $force )
  81. return;
  82. $type = gettype($variable);
  83. $class_name = get_class($this);
  84. $accepts = $class_name::$accepts;
  85. if( isset($accepts[$type]) ) {
  86. $convert_method = $accepts[$type];
  87. if( !method_exists($this, $convert_method) ) {
  88. return self::error('Plugin "%s" has no conversion method "%s".',
  89. $class_name, $convert_method);
  90. }
  91. $result = $this->$convert_method($variable);
  92. $result === null || $this->variable = $result;
  93. } else if( !in_array($type, $accepts) ) {
  94. return self::error('Variable type "%s" is not accepted by class "%s".',
  95. $type, $class_name);
  96. }
  97. }
  98. /**
  99. * Getter for {@link variable}.
  100. *
  101. * @see variable_alias
  102. */
  103. function __get($name) {
  104. $class_name = get_class($this);
  105. if( in_array($name, (array)$class_name::$variable_alias) )
  106. return $this->variable;
  107. }
  108. /**
  109. * Setter for {@link variable}.
  110. *
  111. * @see variable_alias
  112. */
  113. function __set($name, $value) {
  114. $class_name = get_class($this);
  115. if( in_array($name, (array)$class_name::$variable_alias) )
  116. $this->variable = $value;
  117. }
  118. /**
  119. * Handler for pQuery exceptions.
  120. *
  121. * If the execption is a (@link pQueryException}, exit the script with
  122. * its message. Otherwise, throw the exception further.
  123. *
  124. * @param Exception $e The exception to handle.
  125. */
  126. function exception_handler($e) {
  127. if( $e instanceof pQueryException )
  128. die(nl2br($e->getMessage()));
  129. throw $e;
  130. }
  131. /**
  132. * Extend pQuery with a plugin.
  133. *
  134. * @param string $class_name The name of the plugin's base class.
  135. * @param string $alias The alias to save for the plugin (defaults to $class_name).
  136. * @see $plugins
  137. */
  138. static function extend($class_name, $alias=null) {
  139. // Assert plugin existance
  140. if( !class_exists($class_name) )
  141. return self::error('Plugin "%s" does not exist.', $class_name);
  142. // Assert that the plugin extend the base clas properly
  143. if( !in_array('pQueryExtension', class_implements($class_name)) )
  144. return self::error('Plugin "%s" does not implement pQueryExtension.', $class_name);
  145. // Assert that the required PHP version is installed
  146. if( isset($class_name::$REQUIRED_PHP_VERSION)
  147. && version_compare(PHP_VERSION, $class_name::$REQUIRED_PHP_VERSION, '<') ) {
  148. return self::error('Plugin "%s" requires PHP version %s.',
  149. $class_name, $class_name::$REQUIRED_PHP_VERSION);
  150. }
  151. if( $alias === null ) {
  152. self::$plugins[$class_name] = $class_name;
  153. } else {
  154. self::$plugins[$alias] = $class_name;
  155. class_alias($class_name, sprintf(self::CLASS_ALIAS_PATTERN, $alias));
  156. }
  157. }
  158. /**
  159. * Display an error message if in debug mode.
  160. *
  161. * The optional arguments are passed to {@link printf}, along with $error.
  162. *
  163. * @param string $error The error message to display.
  164. */
  165. static function error($error /* , $arg1, $arg2... */) {
  166. $args = func_get_args();
  167. $error = call_user_func_array('sprintf', $args);
  168. throw new pQueryException($error);
  169. }
  170. /**
  171. * Constructor.
  172. *
  173. * @param string $class_name The class to constuct an object off.
  174. * @param mixed $variable The variable to use an utility on.
  175. */
  176. static function create() {
  177. $args = func_get_args();
  178. $plugin = array_shift($args);
  179. if( $plugin === null )
  180. $class_name = 'self';
  181. elseif( isset(self::$plugins[$plugin]) )
  182. $class_name = self::$plugins[$plugin];
  183. elseif( in_array($plugin, self::$plugins) )
  184. $class_name = $plugin;
  185. else
  186. return self::error('Plugin "%s" does not exist.', $plugin);
  187. $obj = new $class_name();
  188. $obj->arguments = array_slice($args, 1);
  189. $obj->set_variable(array_shift($args));
  190. return $obj;
  191. }
  192. /**
  193. * Try to load one or more utility files.
  194. */
  195. static function load_utils(/* $basename1 $basename2, ... */) {
  196. $files = func_get_args();
  197. foreach( $files as $basename ) {
  198. $path = PQUERY_ROOT.self::UTILS_FOLDER.$basename.'.php';
  199. if( !file_exists($path) ) {
  200. return self::error('Utility "%s" could not be loaded (looked in "%s").',
  201. $basename, $path);
  202. }
  203. include_once $path;
  204. }
  205. }
  206. /**
  207. * Try to load the file containing the utility class for a specific variable type.
  208. *
  209. * @param string $type the variable type of the class to load.
  210. */
  211. static function load_plugin($type) {
  212. $path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $type);
  213. if( !file_exists($path) )
  214. return false;
  215. include_once $path;
  216. return true;
  217. }
  218. /**
  219. * Include the nescessary files for the given plugins.
  220. */
  221. static function require_plugins(/* $plugin1 [ , $plugin2, ... ] */) {
  222. $plugins = func_get_args();
  223. foreach( $plugins as $plugin ) {
  224. $path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $plugin);
  225. if( !file_exists($path) ) {
  226. return self::error('Required plugin "%s" could not be located (looked in "%s").',
  227. $plugin, $path);
  228. }
  229. include_once $path;
  230. }
  231. }
  232. /**
  233. * Assert that the given constants have been defined.
  234. * Add the prefix 'PQUERY_' to each constant name.
  235. */
  236. static function assert_defined(/* $constant1 [ , $constant2, ... ] */) {
  237. $constants = func_get_args();
  238. $undefined = array();
  239. foreach( $constants as $constant ) {
  240. $constant = 'PQUERY_'.$constant;
  241. if( !defined($constant) )
  242. $undefined[] = $constant;
  243. }
  244. count($undefined) && self::error('The following constants have'
  245. .' not been defined: %s', implode(', ', $undefined));
  246. }
  247. }
  248. /**
  249. * Exception class for error throwing.
  250. */
  251. class pQueryException extends Exception {
  252. }
  253. /**
  254. * Shortcut constructor for {@link pQuery}.
  255. *
  256. * @param mixed $variable The variable to use an utility on.
  257. * @param string $plugin The name of an utility plugin to use (optional).
  258. * @returns pQuery A new pQuery (or descendant) instance.
  259. */
  260. function _p($variable, $plugin=null) {
  261. $class_name = 'pQuery';
  262. if( $plugin === null ) {
  263. // Use custom class for this variable type
  264. $type = gettype($variable);
  265. if( pQuery::load_plugin($type) )
  266. $class_name .= ucfirst($type);
  267. } else {
  268. // Use custom plugin class
  269. if( isset(pQuery::$plugins[$plugin]) )
  270. $class_name = pQuery::$plugins[$plugin];
  271. else if( PQUERY_DEBUG )
  272. pQuery::error('Plugin "%s" does not exist.', $plugin);
  273. }
  274. return new $class_name($variable);
  275. }
  276. /*
  277. * Set an alias for the bas class consistent with plugin aliases.
  278. */
  279. class_alias('pQuery', '__p');
  280. /*
  281. * Set the exception handler
  282. */
  283. set_exception_handler('__p::exception_handler');
  284. __p::assert_defined('DEBUG', 'ROOT');
  285. ?>