Registry.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog;
  11. use InvalidArgumentException;
  12. /**
  13. * Monolog log registry
  14. *
  15. * Allows to get `Logger` instances in the global scope
  16. * via static method calls on this class.
  17. *
  18. * <code>
  19. * $application = new Monolog\Logger('application');
  20. * $api = new Monolog\Logger('api');
  21. *
  22. * Monolog\Registry::addLogger($application);
  23. * Monolog\Registry::addLogger($api);
  24. *
  25. * function testLogger()
  26. * {
  27. * Monolog\Registry::api()->addError('Sent to $api Logger instance');
  28. * Monolog\Registry::application()->addError('Sent to $application Logger instance');
  29. * }
  30. * </code>
  31. *
  32. * @author Tomas Tatarko <tomas@tatarko.sk>
  33. */
  34. class Registry
  35. {
  36. /**
  37. * List of all loggers in the registry (by named indexes)
  38. *
  39. * @var Logger[]
  40. */
  41. private static $loggers = array();
  42. /**
  43. * Adds new logging channel to the registry
  44. *
  45. * @param Logger $logger Instance of the logging channel
  46. * @param string|null $name Name of the logging channel ($logger->getName() by default)
  47. * @param bool $overwrite Overwrite instance in the registry if the given name already exists?
  48. * @throws \InvalidArgumentException If $overwrite set to false and named Logger instance already exists
  49. */
  50. public static function addLogger(Logger $logger, $name = null, $overwrite = false)
  51. {
  52. $name = $name ?: $logger->getName();
  53. if (isset(self::$loggers[$name]) && !$overwrite) {
  54. throw new InvalidArgumentException('Logger with the given name already exists');
  55. }
  56. self::$loggers[$name] = $logger;
  57. }
  58. /**
  59. * Checks if such logging channel exists by name or instance
  60. *
  61. * @param string|Logger $logger Name or logger instance
  62. */
  63. public static function hasLogger($logger)
  64. {
  65. if ($logger instanceof Logger) {
  66. $index = array_search($logger, self::$loggers, true);
  67. return false !== $index;
  68. } else {
  69. return isset(self::$loggers[$logger]);
  70. }
  71. }
  72. /**
  73. * Removes instance from registry by name or instance
  74. *
  75. * @param string|Logger $logger Name or logger instance
  76. */
  77. public static function removeLogger($logger)
  78. {
  79. if ($logger instanceof Logger) {
  80. if (false !== ($idx = array_search($logger, self::$loggers, true))) {
  81. unset(self::$loggers[$idx]);
  82. }
  83. } else {
  84. unset(self::$loggers[$logger]);
  85. }
  86. }
  87. /**
  88. * Clears the registry
  89. */
  90. public static function clear()
  91. {
  92. self::$loggers = array();
  93. }
  94. /**
  95. * Gets Logger instance from the registry
  96. *
  97. * @param string $name Name of the requested Logger instance
  98. * @throws \InvalidArgumentException If named Logger instance is not in the registry
  99. * @return Logger Requested instance of Logger
  100. */
  101. public static function getInstance($name)
  102. {
  103. if (!isset(self::$loggers[$name])) {
  104. throw new InvalidArgumentException(sprintf('Requested "%s" logger instance is not in the registry', $name));
  105. }
  106. return self::$loggers[$name];
  107. }
  108. /**
  109. * Gets Logger instance from the registry via static method call
  110. *
  111. * @param string $name Name of the requested Logger instance
  112. * @param array $arguments Arguments passed to static method call
  113. * @throws \InvalidArgumentException If named Logger instance is not in the registry
  114. * @return Logger Requested instance of Logger
  115. */
  116. public static function __callStatic($name, $arguments)
  117. {
  118. return self::getInstance($name);
  119. }
  120. }