base.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, thrown when a required file does not exist.
  65. *
  66. * @package WebBasics
  67. */
  68. class FileNotFoundError extends \RuntimeException {
  69. /**
  70. * Create a new FileNotFoundError instance.
  71. *
  72. * Sets an error message of the form 'File "path/to/file.php" does not exist.'.
  73. *
  74. * @param string $path Path to the file that does not exist.
  75. * @param bool $is_dir Whether the path points to a directory (defaults to false).
  76. */
  77. function __construct($path, $is_dir=false) {
  78. $this->message = sprintf('%s "%s" does not exist.', $is_dir ? 'Directory' : 'File', $path);
  79. }
  80. }
  81. /**
  82. * Format a string using parameters in an associative array.
  83. *
  84. * <code>
  85. * echo asprintf('foo %(bar)', array('bar' => 'baz')); // prints 'foo baz'
  86. * </code>
  87. *
  88. * @param string $format The string to format.
  89. * @param array $params An associative array with parameters that are used in $format.
  90. * @package WebBasics
  91. */
  92. function asprintf($format, array $params) {
  93. return preg_replace_callback(
  94. '/%\(([a-z-_ ]*)\)/i',
  95. function($matches) use ($params) {
  96. return (string)$params[$matches[1]];
  97. },
  98. $format
  99. );
  100. }
  101. ?>