pquery.php 7.5 KB

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