base.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 'logger.php';
  10. /**
  11. * Base class for instantiable classes in the WebBasics package.
  12. *
  13. * The base class defines a static 'create' method that acts as a chainable
  14. * shortcut for the class constructor:
  15. * <code>
  16. * class Foo extends Base {
  17. * function __contruct($bar, $baz) {
  18. * $this->bar = bar;
  19. * $this->baz = baz;
  20. * }
  21. * }
  22. *
  23. * $foo = Foo::create('bar', 'baz');
  24. * // is equivalent to:
  25. * $foo = new Foo('bar', 'baz');
  26. * </code>
  27. *
  28. * The advantage of the 'create' constructor is that is allows chaining:
  29. * <code>
  30. * Foo::create('bar', 'baz')->method();
  31. * // as opposed to:
  32. * $foo = new Foo('bar', 'baz');
  33. * $foo->method();
  34. * </code>
  35. *
  36. * @package WebBasics
  37. */
  38. abstract class Base {
  39. /**
  40. * Create a new object of the called class.
  41. *
  42. * This function provides a chainable constructor, which is not possible
  43. * using plain PHP code.
  44. *
  45. * @returns mixed
  46. */
  47. final static function create(/* [ arg0 [ , ... ] ] */) {
  48. $args = func_get_args();
  49. $class = get_called_class();
  50. $rc = new \ReflectionClass($class);
  51. return $rc->newInstanceArgs($args);
  52. }
  53. /**
  54. * Append a slash ('/') to the given directory name, if it is not already there.
  55. *
  56. * @param string $directory The directory to append a slash to.
  57. * @return string
  58. */
  59. static function pathWithSlash($directory) {
  60. return $directory[strlen($directory) - 1] == '/' ? $directory : $directory.'/';
  61. }
  62. }
  63. /**
  64. * Exception with sprintf()-like constructor for error message formatting.
  65. *
  66. * @package WebBasics
  67. * @link http://php.net/sprintf
  68. */
  69. class FormattedException extends \Exception {
  70. /**
  71. * Constructor, sets a formatted error message.
  72. * @link http://php.net/sprintf
  73. */
  74. function __construct() {
  75. $args = func_get_args();
  76. $this->message = call_user_func_array('sprintf', $args);
  77. }
  78. }
  79. /**
  80. * Exception, thrown when a required file does not exist.
  81. *
  82. * @package WebBasics
  83. */
  84. class FileNotFoundError extends \Exception {
  85. /**
  86. * Create a new FileNotFoundError instance.
  87. *
  88. * Sets an error message of the form 'File "path/to/file.php" does not exist.'.
  89. *
  90. * @param string $path Path to the file that does not exist.
  91. * @param bool $is_dir Whether the path points to a directory (defaults to false).
  92. */
  93. function __construct($path, $is_dir=false) {
  94. $this->message = sprintf('%s "%s" does not exist.', $is_dir ? 'Directory' : 'File', $path);
  95. }
  96. }
  97. /**
  98. * Format a string using parameters in an associative array.
  99. *
  100. * <code>
  101. * echo asprintf('foo %(bar)', array('bar' => 'baz')); // prints 'foo baz'
  102. * </code>
  103. *
  104. * @param string $format The string to format.
  105. * @param array $params An associative array with parameters that are used in $format.
  106. * @package WebBasics
  107. */
  108. function asprintf($format, array $params) {
  109. return preg_replace_callback(
  110. '/%\(([a-z0-9-_ ]*)\)/i',
  111. function($matches) use ($params) {
  112. return (string)$params[$matches[1]];
  113. },
  114. $format
  115. );
  116. }
  117. ?>