base.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Base for pQuery PHP utility framework.
  4. *
  5. * @package pQuery
  6. */
  7. include_once 'config.php';
  8. /**
  9. * Common utility class.
  10. */
  11. class pQuery {
  12. /**
  13. * Pattern of tha alias created for an extending plugin that has defined an alias.
  14. *
  15. * @var string
  16. */
  17. const CLASS_ALIAS_PATTERN = '__%s';
  18. /**
  19. * The minimum php version required to use the framework.
  20. *
  21. * @var string
  22. */
  23. static $REQUIRED_PHP_VERSION = '5.3';
  24. /**
  25. * A list of all plugins currently included.
  26. *
  27. * @var array
  28. */
  29. static $plugins = array();
  30. /**
  31. * The variable types accepted by the parser.
  32. *
  33. * @var array
  34. * @see set_variable()
  35. */
  36. static $accepts = array('boolean', 'integer', 'double', 'string', 'array', 'object', 'NULL');
  37. /**
  38. * The current variable.
  39. *
  40. * @var mixed
  41. */
  42. var $variable;
  43. /**
  44. * Additional arguments that were passed to the constructor.
  45. *
  46. * @var array
  47. */
  48. var $arguments = array();
  49. /**
  50. * Extend pQuery with a plugin.
  51. *
  52. * @param string $class_name The name of the plugin's base class.
  53. * @param string $alias The alias to save for the plugin (defaults to $class_name).
  54. * @see $plugins
  55. */
  56. static function extend($class_name, $alias=null) {
  57. // Assert plugin existance
  58. if( !class_exists($class_name) )
  59. return self::error('Plugin "%s" does not exist.', $class_name);
  60. // Assert that the plugin extend the base clas properly
  61. if( !in_array('pQueryExtension', class_implements($class_name)) )
  62. return self::error('Plugin "%s" does not implement pQueryExtension.', $class_name);
  63. // Assert that the required PHP version is installed
  64. if( isset($class_name::$REQUIRED_PHP_VERSION)
  65. && version_compare(PHP_VERSION, $class_name::$REQUIRED_PHP_VERSION, '<') ) {
  66. return self::error('Plugin "%s" requires PHP version %s.',
  67. $class_name, $class_name::$REQUIRED_PHP_VERSION);
  68. }
  69. if( $alias === null ) {
  70. self::$plugins[$class_name] = $class_name;
  71. } else {
  72. self::$plugins[$alias] = $class_name;
  73. class_alias($class_name, sprintf(self::CLASS_ALIAS_PATTERN, $alias));
  74. }
  75. }
  76. /**
  77. * Display an error message if in {@link DEBUG} mode.
  78. *
  79. * The optional arguments are passed to {@link printf}, along with $error.
  80. *
  81. * @param string $error The error message to display.
  82. */
  83. static function error($error/*, $arg1, $arg2...*/) {
  84. $args = func_get_args();
  85. if( DEBUG ) {
  86. call_user_func_array('printf', $args);
  87. //echo debug_backtrace();
  88. }
  89. ERROR_IS_FATAL && exit;
  90. }
  91. /**
  92. * Constructor.
  93. *
  94. * @param string $class_name The class to constuct an object off.
  95. * @param mixed $variable The variable to use an utility on.
  96. */
  97. static function create() {
  98. $args = func_get_args();
  99. $class_name = array_shift($args);
  100. $obj = $class_name === null ? new self() : new $class_name();
  101. $variable = array_shift($args);
  102. $obj->arguments = $args;
  103. $obj->set_variable($variable);
  104. return $obj;
  105. }
  106. /**
  107. * Parse the type of the given variable, and convert it if needed.
  108. *
  109. * @param mixed $variable The variable to parse.
  110. * @param bool $force Whether not to check the variables type against the accepted types.
  111. */
  112. function set_variable($variable, $force=false) {
  113. $this->variable = $variable;
  114. if( $force )
  115. return;
  116. $type = gettype($variable);
  117. $class_name = get_class($this);
  118. $accepts = $class_name::$accepts;
  119. if( isset($accepts[$type]) ) {
  120. $convert_method = $accepts[$type];
  121. if( !method_exists($this, $convert_method) )
  122. return self::error('Plugin "%s" has no conversion method "%s".', $class_name, $convert_method);
  123. $result = $this->$convert_method($variable);
  124. $result === null || $this->variable = $result;
  125. } else if( !in_array($type, $accepts) ) {
  126. return self::error('Variable type "%s" is not accepted by class "%s".', $type, $class_name);
  127. }
  128. }
  129. /**
  130. * Try to load the file containing the utility class for a specific variable type.
  131. *
  132. * @param mixed $type the variable type of the class to load.
  133. */
  134. static function load_plugin($type) {
  135. $file = PQUERY_ROOT.$type.'.php';
  136. if( !file_exists($file) )
  137. return false;
  138. include_once $file;
  139. return true;
  140. }
  141. /**
  142. * Include the nescessary files for the given plugins.
  143. */
  144. static function require_plugins(/* $plugin1 [ , $plugin2, ... ] */) {
  145. $plugins = func_get_args();
  146. foreach( $plugins as $plugin ) {
  147. $path = PQUERY_ROOT.$plugin.'.php';
  148. if( !file_exists($path) ) {
  149. return self::error('Required plugin "%s" could not be located (looked in "%s").',
  150. $plugin, $path);
  151. }
  152. include_once $path;
  153. }
  154. }
  155. }
  156. /**
  157. * Interface used for extending the jQuery class.
  158. */
  159. interface pQueryExtension {
  160. /**
  161. * Constructor.
  162. *
  163. * @param mixed $variable The variable to use an utility on.
  164. */
  165. //function __construct();
  166. }
  167. /**
  168. * Shortcut constructor for {@link pQuery}.
  169. *
  170. * @param mixed $variable The variable to use an utility on.
  171. * @param string $plugin The name of an utility plugin to use (optional).
  172. * @returns pQuery A new pQuery (or descendant) instance.
  173. */
  174. function _p($variable, $plugin=null) {
  175. $class_name = 'pQuery';
  176. if( $plugin === null ) {
  177. // Use custom class for this variable type
  178. $type = gettype($variable);
  179. if( pQuery::load_plugin($type) )
  180. $class_name .= ucfirst($type);
  181. } else {
  182. // Use custom plugin class
  183. if( isset(pQuery::$plugins[$plugin]) )
  184. $class_name = pQuery::$plugins[$plugin];
  185. else if( DEBUG )
  186. pQuery::error('Plugin "%s" does not exist.', $plugin);
  187. }
  188. return new $class_name($variable);
  189. }
  190. ?>