LogEngineRegistry.php 2.9 KB

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