base.php 2.6 KB

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