pquery.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Base for pQuery package.
  4. *
  5. * @package pQuery
  6. */
  7. namespace pQuery;
  8. /**
  9. * Shortcut constructor for {@link pQuery}.
  10. *
  11. * @returns pQuery A new pQuery instance.
  12. */
  13. function _() {
  14. $args = func_get_args();
  15. return call_user_func_array('pQuery::__construct', $args);
  16. }
  17. /**
  18. * Indicates whether the framework is in debug mode.
  19. *
  20. * @var bool
  21. */
  22. defined('DEBUG') || define('DEBUG', true);
  23. /**
  24. * Common utility class.
  25. */
  26. class pQuery {
  27. /**
  28. * @see pQueryExtension::REQUIRED_PHP_VERSION
  29. */
  30. const REQUIRED_PHP_VERSION = '5.3';
  31. /**
  32. * A list of all plugins currently included.
  33. *
  34. * @var array
  35. */
  36. static $plugins = array();
  37. /**
  38. * The current variable.
  39. *
  40. * @var mixed
  41. */
  42. var $variable;
  43. /**
  44. * Extend pQuery with a plugin.
  45. *
  46. * @param mixed $variable The variable to parse.
  47. * @see $plugins
  48. */
  49. static function extend($class_name, $alias=null) {
  50. if( !class_exists($class_name) )
  51. return self::error('Class "%s" does not exist.', $class_name);
  52. if( !($class_name instanceof pQueryExtension) )
  53. return self::error('Class "%s" does not implement pQueryExtension.', $class_name);
  54. if( $class_name )
  55. return self::error('Class "%s" does not implement pQueryExtension.', $class_name);
  56. self::$plugins[$alias === null ? $class_name : $alias ] = $class_name;
  57. }
  58. /**
  59. * Display an error message if in {@link DEBUG} mode.
  60. *
  61. * The optional arguments are passed to {@link printf}, along with $error.
  62. *
  63. * @param string $error The error message to display.
  64. */
  65. static function error($error/*, $arg1, $arg2...*/) {
  66. $args = func_get_args();
  67. if( DEBUG ) {
  68. call_user_func_array('printf', $args);
  69. echo debug_backtrace();
  70. }
  71. }
  72. /**
  73. * Constructor.
  74. *
  75. * @param mixed $variable The variable to use an utility on.
  76. * @param string $plugin The name of an utility plugin to use (optional).
  77. */
  78. function __construct($variable, $plugin=null) {
  79. if( $plugin !== null ) {
  80. if( isset($plugins[$plugin]) ) {
  81. $class_name = $plugins[$plugin];
  82. return new $class_name($variable);
  83. } else if( DEBUG ) {
  84. self::error('Plugin "%s" does not exist.', $plugin);
  85. }
  86. }
  87. $this->parse_variable($variable);
  88. }
  89. /**
  90. * Parse the type of the given variable, and convert it if needed.
  91. *
  92. * @param mixed $variable The variable to parse.
  93. * @todo Type and conversion
  94. */
  95. function parse_variable($variable) {
  96. $this->variable = $variable;
  97. }
  98. }
  99. /**
  100. * Interface used for extending the jQuery class.
  101. */
  102. interface pQueryExtension {
  103. /**
  104. * The minimum php version required to use the package.
  105. *
  106. * @var string
  107. */
  108. const REQUIRED_PHP_VERSION;
  109. /**
  110. * Constructor.
  111. *
  112. * @param mixed $variable The variable to use an utility on.
  113. */
  114. function __construct($variable);
  115. }
  116. ?>