pquery.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 = nl2br(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 an utility file.
  130. *
  131. * @param string $basename
  132. */
  133. static function load_util($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. * Try to load the file containing the utility class for a specific variable type.
  143. *
  144. * @param string $type the variable type of the class to load.
  145. */
  146. static function load_plugin($type) {
  147. $path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $type);
  148. if( !file_exists($path) )
  149. return false;
  150. include_once $path;
  151. return true;
  152. }
  153. /**
  154. * Include the nescessary files for the given plugins.
  155. */
  156. static function require_plugins(/* $plugin1 [ , $plugin2, ... ] */) {
  157. $plugins = func_get_args();
  158. foreach( $plugins as $plugin ) {
  159. $path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $plugin);
  160. if( !file_exists($path) ) {
  161. return self::error('Required plugin "%s" could not be located (looked in "%s").',
  162. $plugin, $path);
  163. }
  164. include_once $path;
  165. }
  166. }
  167. /**
  168. * Parse the type of the given variable, and convert it if needed.
  169. *
  170. * @param mixed $variable The variable to parse.
  171. * @param bool $force Whether not to check the variables type against the accepted types.
  172. */
  173. function set_variable($variable, $force=false) {
  174. $this->variable = $variable;
  175. if( $force )
  176. return;
  177. $type = gettype($variable);
  178. $class_name = get_class($this);
  179. $accepts = $class_name::$accepts;
  180. if( isset($accepts[$type]) ) {
  181. $convert_method = $accepts[$type];
  182. if( !method_exists($this, $convert_method) ) {
  183. return self::error('Plugin "%s" has no conversion method "%s".',
  184. $class_name, $convert_method);
  185. }
  186. $result = $this->$convert_method($variable);
  187. $result === null || $this->variable = $result;
  188. } else if( !in_array($type, $accepts) ) {
  189. return self::error('Variable type "%s" is not accepted by class "%s".',
  190. $type, $class_name);
  191. }
  192. }
  193. /**
  194. * Getter for {@link variable}.
  195. *
  196. * @see variable_alias
  197. */
  198. function __get($name) {
  199. $class_name = get_class($this);
  200. if( in_array($name, (array)$class_name::$variable_alias) )
  201. return $this->variable;
  202. }
  203. /**
  204. * Setter for {@link variable}.
  205. *
  206. * @see variable_alias
  207. */
  208. function __set($name, $value) {
  209. $class_name = get_class($this);
  210. if( in_array($name, (array)$class_name::$variable_alias) )
  211. $this->variable = $value;
  212. }
  213. }
  214. /**
  215. * Exception class for error throwing
  216. */
  217. class pQueryException extends Exception {
  218. }
  219. /**
  220. * Interface used for extending the jQuery class.
  221. */
  222. interface pQueryExtension {
  223. /**
  224. * Constructor.
  225. *
  226. * @param mixed $variable The variable to use an utility on.
  227. */
  228. //function __construct();
  229. }
  230. /**
  231. * Shortcut constructor for {@link pQuery}.
  232. *
  233. * @param mixed $variable The variable to use an utility on.
  234. * @param string $plugin The name of an utility plugin to use (optional).
  235. * @returns pQuery A new pQuery (or descendant) instance.
  236. */
  237. function _p($variable, $plugin=null) {
  238. $class_name = 'pQuery';
  239. if( $plugin === null ) {
  240. // Use custom class for this variable type
  241. $type = gettype($variable);
  242. if( pQuery::load_plugin($type) )
  243. $class_name .= ucfirst($type);
  244. } else {
  245. // Use custom plugin class
  246. if( isset(pQuery::$plugins[$plugin]) )
  247. $class_name = pQuery::$plugins[$plugin];
  248. else if( DEBUG )
  249. pQuery::error('Plugin "%s" does not exist.', $plugin);
  250. }
  251. return new $class_name($variable);
  252. }
  253. /**
  254. * Set an alias for the bas class consistent with plugin aliases.
  255. */
  256. class_alias('pQuery', '__p');
  257. ?>