base.php 4.5 KB

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