pquery.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. * Extend pQuery with a plugin.
  74. *
  75. * @param string $class_name The name of the plugin's base class.
  76. * @param string $alias The alias to save for the plugin (defaults to $class_name).
  77. * @see $plugins
  78. */
  79. static function extend($class_name, $alias=null) {
  80. // Assert plugin existance
  81. if( !class_exists($class_name) )
  82. return self::error('Plugin "%s" does not exist.', $class_name);
  83. // Assert that the plugin extend the base clas properly
  84. if( !in_array('pQueryExtension', class_implements($class_name)) )
  85. return self::error('Plugin "%s" does not implement pQueryExtension.', $class_name);
  86. // Assert that the required PHP version is installed
  87. if( isset($class_name::$REQUIRED_PHP_VERSION)
  88. && version_compare(PHP_VERSION, $class_name::$REQUIRED_PHP_VERSION, '<') ) {
  89. return self::error('Plugin "%s" requires PHP version %s.',
  90. $class_name, $class_name::$REQUIRED_PHP_VERSION);
  91. }
  92. if( $alias === null ) {
  93. self::$plugins[$class_name] = $class_name;
  94. } else {
  95. self::$plugins[$alias] = $class_name;
  96. class_alias($class_name, sprintf(self::CLASS_ALIAS_PATTERN, $alias));
  97. }
  98. }
  99. /**
  100. * Display an error message if in {@link DEBUG} mode.
  101. *
  102. * The optional arguments are passed to {@link printf}, along with $error.
  103. *
  104. * @param string $error The error message to display.
  105. */
  106. static function error($error /* , $arg1, $arg2... */) {
  107. $args = func_get_args();
  108. $error = call_user_func_array('sprintf', $args);
  109. throw new pQueryException($error);
  110. }
  111. /**
  112. * Constructor.
  113. *
  114. * @param string $class_name The class to constuct an object off.
  115. * @param mixed $variable The variable to use an utility on.
  116. */
  117. static function create() {
  118. $args = func_get_args();
  119. $plugin = array_shift($args);
  120. if( $plugin === null )
  121. $class_name = 'self';
  122. elseif( isset(self::$plugins[$plugin]) )
  123. $class_name = self::$plugins[$plugin];
  124. elseif( in_array($plugin, self::$plugins) )
  125. $class_name = $plugin;
  126. else
  127. return self::error('Plugin "%s" does not exist.', $plugin);
  128. $obj = new $class_name();
  129. $obj->arguments = $args;
  130. $obj->set_variable(array_shift($args));
  131. return $obj;
  132. }
  133. /**
  134. * Try to load one or more utility files.
  135. */
  136. static function load_utils(/* $basename1 $basename2, ... */) {
  137. $files = func_get_args();
  138. foreach( $files as $basename ) {
  139. $path = PQUERY_ROOT.self::UTILS_FOLDER.$basename.'.php';
  140. if( !file_exists($path) ) {
  141. return self::error('Utility "%s" could not be loaded (looked in "%s").',
  142. $basename, $path);
  143. }
  144. include_once $path;
  145. }
  146. }
  147. /**
  148. * Try to load the file containing the utility class for a specific variable type.
  149. *
  150. * @param string $type the variable type of the class to load.
  151. */
  152. static function load_plugin($type) {
  153. $path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $type);
  154. if( !file_exists($path) )
  155. return false;
  156. include_once $path;
  157. return true;
  158. }
  159. /**
  160. * Include the nescessary files for the given plugins.
  161. */
  162. static function require_plugins(/* $plugin1 [ , $plugin2, ... ] */) {
  163. $plugins = func_get_args();
  164. foreach( $plugins as $plugin ) {
  165. $path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $plugin);
  166. if( !file_exists($path) ) {
  167. return self::error('Required plugin "%s" could not be located (looked in "%s").',
  168. $plugin, $path);
  169. }
  170. include_once $path;
  171. }
  172. }
  173. /**
  174. * Parse the type of the given variable, and convert it if needed.
  175. *
  176. * @param mixed $variable The variable to parse.
  177. * @param bool $force Whether not to check the variables type against the accepted types.
  178. */
  179. function set_variable($variable, $force=false) {
  180. $this->variable = $variable;
  181. if( $force )
  182. return;
  183. $type = gettype($variable);
  184. $class_name = get_class($this);
  185. $accepts = $class_name::$accepts;
  186. if( isset($accepts[$type]) ) {
  187. $convert_method = $accepts[$type];
  188. if( !method_exists($this, $convert_method) ) {
  189. return self::error('Plugin "%s" has no conversion method "%s".',
  190. $class_name, $convert_method);
  191. }
  192. $result = $this->$convert_method($variable);
  193. $result === null || $this->variable = $result;
  194. } else if( !in_array($type, $accepts) ) {
  195. return self::error('Variable type "%s" is not accepted by class "%s".',
  196. $type, $class_name);
  197. }
  198. }
  199. /**
  200. * Getter for {@link variable}.
  201. *
  202. * @see variable_alias
  203. */
  204. function __get($name) {
  205. $class_name = get_class($this);
  206. if( in_array($name, (array)$class_name::$variable_alias) )
  207. return $this->variable;
  208. }
  209. /**
  210. * Setter for {@link variable}.
  211. *
  212. * @see variable_alias
  213. */
  214. function __set($name, $value) {
  215. $class_name = get_class($this);
  216. if( in_array($name, (array)$class_name::$variable_alias) )
  217. $this->variable = $value;
  218. }
  219. /**
  220. * Handler for pQuery exceptions.
  221. *
  222. * If the execption is a (@link pQueryException}, exit the script with
  223. * its message. Otherwise, throw the exception further.
  224. *
  225. * @param Exception $e The exception to handle.
  226. */
  227. function exception_handler($e) {
  228. if( $e instanceof pQueryException )
  229. die(nl2br($e->getMessage()));
  230. throw $e;
  231. }
  232. }
  233. /**
  234. * Exception class for error throwing
  235. */
  236. class pQueryException extends Exception {
  237. }
  238. /**
  239. * Interface used for extending the jQuery class.
  240. */
  241. interface pQueryExtension {
  242. /**
  243. * Constructor.
  244. *
  245. * @param mixed $variable The variable to use an utility on.
  246. */
  247. //function __construct();
  248. }
  249. /**
  250. * Shortcut constructor for {@link pQuery}.
  251. *
  252. * @param mixed $variable The variable to use an utility on.
  253. * @param string $plugin The name of an utility plugin to use (optional).
  254. * @returns pQuery A new pQuery (or descendant) instance.
  255. */
  256. function _p($variable, $plugin=null) {
  257. $class_name = 'pQuery';
  258. if( $plugin === null ) {
  259. // Use custom class for this variable type
  260. $type = gettype($variable);
  261. if( pQuery::load_plugin($type) )
  262. $class_name .= ucfirst($type);
  263. } else {
  264. // Use custom plugin class
  265. if( isset(pQuery::$plugins[$plugin]) )
  266. $class_name = pQuery::$plugins[$plugin];
  267. else if( DEBUG )
  268. pQuery::error('Plugin "%s" does not exist.', $plugin);
  269. }
  270. return new $class_name($variable);
  271. }
  272. /*
  273. * Set an alias for the bas class consistent with plugin aliases.
  274. */
  275. class_alias('pQuery', '__p');
  276. /*
  277. * Set the exception handler
  278. */
  279. set_exception_handler('__p::exception_handler');
  280. ?>