base.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Commonly used classes used in the WebBasics package.
  4. *
  5. * @author Taddeus Kroes
  6. * @date 13-07-2012
  7. */
  8. namespace webbasics;
  9. require_once 'utils.php';
  10. require_once 'logger.php';
  11. /**
  12. * Base class for instantiable classes in the WebBasics package.
  13. *
  14. * The base class defines a static 'create' method that acts as a chainable
  15. * shortcut for the class constructor:
  16. * <code>
  17. * class Foo extends Base {
  18. * function __contruct($bar, $baz) {
  19. * $this->bar = bar;
  20. * $this->baz = baz;
  21. * }
  22. * }
  23. *
  24. * $foo = Foo::create('bar', 'baz');
  25. * // is equivalent to:
  26. * $foo = new Foo('bar', 'baz');
  27. * </code>
  28. *
  29. * The advantage of the 'create' constructor is that is allows chaining:
  30. * <code>
  31. * Foo::create('bar', 'baz')->method();
  32. * // as opposed to:
  33. * $foo = new Foo('bar', 'baz');
  34. * $foo->method();
  35. * </code>
  36. *
  37. * @package WebBasics
  38. */
  39. abstract class Base {
  40. /**
  41. * Create a new object of the called class.
  42. *
  43. * This function provides a chainable constructor, which is not possible
  44. * using plain PHP code.
  45. *
  46. * @returns mixed
  47. */
  48. final static function create(/* [ arg0 [ , ... ] ] */) {
  49. $args = func_get_args();
  50. $class = get_called_class();
  51. $rc = new \ReflectionClass($class);
  52. return $rc->newInstanceArgs($args);
  53. }
  54. /**
  55. * Append a slash ('/') to the given directory name, if it is not already there.
  56. *
  57. * @param string $directory The directory to append a slash to.
  58. * @return string
  59. */
  60. static function pathWithSlash($directory) {
  61. return $directory[strlen($directory) - 1] == '/' ? $directory : $directory . '/';
  62. }
  63. }
  64. /**
  65. * Exception with sprintf()-like constructor for error message formatting.
  66. *
  67. * @package WebBasics
  68. * @link http://php.net/sprintf
  69. */
  70. class FormattedException extends \Exception {
  71. /**
  72. * Constructor, sets a formatted error message.
  73. * @link http://php.net/sprintf
  74. */
  75. function __construct() {
  76. $args = func_get_args();
  77. $this->message = call_user_func_array('sprintf', $args);
  78. }
  79. }
  80. /**
  81. * Exception, thrown when a required file does not exist.
  82. *
  83. * @package WebBasics
  84. */
  85. class FileNotFoundError extends \Exception {
  86. /**
  87. * Create a new FileNotFoundError instance.
  88. *
  89. * Sets an error message of the form 'File "path/to/file.php" does not exist.'.
  90. *
  91. * @param string $path Path to the file that does not exist.
  92. * @param bool $is_dir Whether the path points to a directory (defaults to false).
  93. */
  94. function __construct($path, $is_dir=false) {
  95. $this->message = sprintf('%s "%s" does not exist.', $is_dir ? 'Directory' : 'File', $path);
  96. }
  97. }
  98. /**
  99. * The Singleton interface mshould be implemented by classes that allow only
  100. * one instance.
  101. *
  102. * The instance must be saved statically after the constructor has been
  103. * called. When getInstance() is called another time, this instance is
  104. * returned.
  105. */
  106. interface Singleton {
  107. /**
  108. * Create a new singleton instance, and save it in the static $instance variable.
  109. *
  110. * @return object An existing instance from the $instance variable, or a new instance.
  111. */
  112. public static function getInstance();
  113. }
  114. /**
  115. * Format a string using parameters in an associative array.
  116. *
  117. * <code>
  118. * echo asprintf('foo %(bar)', array('bar' => 'baz')); // prints 'foo baz'
  119. * </code>
  120. *
  121. * @param string $format The string to format.
  122. * @param array $params An associative array with parameters that are used in $format.
  123. * @package WebBasics
  124. */
  125. function asprintf($format, array $params) {
  126. return preg_replace_callback(
  127. '/%\(([a-z0-9-_ ]*)\)/i',
  128. function($matches) use ($params) {
  129. return (string)$params[$matches[1]];
  130. },
  131. $format
  132. );
  133. }
  134. ?>