LogEngineRegistry.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Log;
  17. use Cake\Core\App;
  18. use Cake\Core\ObjectRegistry;
  19. use Psr\Log\LoggerInterface;
  20. use RuntimeException;
  21. /**
  22. * Registry of loaded log engines
  23. *
  24. * @extends \Cake\Core\ObjectRegistry<\Psr\Log\LoggerInterface>
  25. */
  26. class LogEngineRegistry extends ObjectRegistry
  27. {
  28. /**
  29. * Resolve a logger classname.
  30. *
  31. * Part of the template method for Cake\Core\ObjectRegistry::load()
  32. *
  33. * @param string $class Partial classname to resolve.
  34. * @return string|null Either the correct class name or null.
  35. * @psalm-return class-string|null
  36. */
  37. protected function _resolveClassName(string $class): ?string
  38. {
  39. return App::className($class, 'Log/Engine', 'Log');
  40. }
  41. /**
  42. * Throws an exception when a logger is missing.
  43. *
  44. * Part of the template method for Cake\Core\ObjectRegistry::load()
  45. *
  46. * @param string $class The classname that is missing.
  47. * @param string|null $plugin The plugin the logger is missing in.
  48. * @return void
  49. * @throws \RuntimeException
  50. */
  51. protected function _throwMissingClassError(string $class, ?string $plugin): void
  52. {
  53. throw new RuntimeException(sprintf('Could not load class %s', $class));
  54. }
  55. /**
  56. * Create the logger instance.
  57. *
  58. * Part of the template method for Cake\Core\ObjectRegistry::load()
  59. *
  60. * @param string|\Psr\Log\LoggerInterface $class The classname or object to make.
  61. * @param string $alias The alias of the object.
  62. * @param array $settings An array of settings to use for the logger.
  63. * @return \Psr\Log\LoggerInterface The constructed logger class.
  64. * @throws \RuntimeException when an object doesn't implement the correct interface.
  65. */
  66. protected function _create($class, string $alias, array $settings): LoggerInterface
  67. {
  68. if (is_callable($class)) {
  69. $class = $class($alias);
  70. }
  71. if (is_object($class)) {
  72. $instance = $class;
  73. }
  74. if (!isset($instance)) {
  75. /** @psalm-suppress UndefinedClass */
  76. $instance = new $class($settings);
  77. }
  78. if ($instance instanceof LoggerInterface) {
  79. return $instance;
  80. }
  81. throw new RuntimeException(sprintf(
  82. 'Loggers must implement %s. Found `%s` instance instead.',
  83. LoggerInterface::class,
  84. getTypeName($instance)
  85. ));
  86. }
  87. /**
  88. * Remove a single logger from the registry.
  89. *
  90. * @param string $name The logger name.
  91. * @return $this
  92. */
  93. public function unload(string $name)
  94. {
  95. unset($this->_loaded[$name]);
  96. return $this;
  97. }
  98. }