|
17 | 17 | use Cake\Core\App;
|
18 | 18 | use Cake\Error;
|
19 | 19 | use Cake\Log\LogInterface;
|
20 |
| -use Cake\Utility\ObjectCollection; |
| 20 | +use Cake\Utility\ObjectRegistry; |
21 | 21 |
|
22 | 22 | /**
|
23 | 23 | * Registry of loaded log engines
|
24 |
| - * |
25 |
| - * @package Cake.Log |
26 | 24 | */
|
27 |
| -class LogEngineRegistry extends ObjectCollection { |
| 25 | +class LogEngineRegistry extends ObjectRegistry { |
| 26 | + |
28 | 27 |
|
29 | 28 | /**
|
30 |
| - * Loads/constructs a Log engine. |
| 29 | + * Resolve a logger classname. |
31 | 30 | *
|
32 |
| - * @param string $name instance identifier |
33 |
| - * @param LogInterface|array $options Setting for the Log Engine, or the log engine |
34 |
| - * If a log engine is used, the adapter will be enabled. |
35 |
| - * @return BaseLog BaseLog engine instance |
36 |
| - * @throws Cake\Error\Exception when logger class does not implement a write method |
| 31 | + * Part of the template method for Cake\Utility\ObjectRegistry::load() |
| 32 | + * |
| 33 | + * @param string $class Partial classname to resolve. |
| 34 | + * @return string|false Either the correct classname or false. |
37 | 35 | */
|
38 |
| - public function load($name, $options = array()) { |
39 |
| - $logger = false; |
40 |
| - if ($options instanceof LogInterface) { |
41 |
| - $enable = true; |
42 |
| - $logger = $options; |
43 |
| - $options = null; |
44 |
| - } |
45 |
| - if (is_array($options)) { |
46 |
| - $enable = isset($options['enabled']) ? $options['enabled'] : true; |
47 |
| - $logger = $this->_getLogger($options); |
48 |
| - } |
49 |
| - if (!$logger instanceof LogInterface) { |
50 |
| - throw new Error\Exception(sprintf( |
51 |
| - __d('cake_dev', 'logger class %s is not an instance of Cake\Log\LogInterface.'), $name |
52 |
| - )); |
53 |
| - } |
54 |
| - $this->_loaded[$name] = $logger; |
55 |
| - if ($enable) { |
56 |
| - $this->enable($name); |
57 |
| - } |
58 |
| - return $logger; |
| 36 | + protected function _resolveClassName($class) { |
| 37 | + return App::classname($class, 'Log/Engine', 'Log'); |
59 | 38 | }
|
60 | 39 |
|
61 | 40 | /**
|
62 |
| - * Attempts to import a logger class from the various paths it could be on. |
63 |
| - * Checks that the logger class implements a write method as well. |
| 41 | + * Throws an exception when a logger is missing. |
64 | 42 | *
|
65 |
| - * @param array $options The configuration options to load a logger with. |
66 |
| - * @return false|LogInterface boolean false on any failures, log adapter interface on success |
| 43 | + * Part of the template method for Cake\Utility\ObjectRegistry::load() |
| 44 | + * |
| 45 | + * @param string $class The classname that is missing. |
| 46 | + * @param string $plugin The plugin the logger is missing in. |
67 | 47 | * @throws Cake\Error\Exception
|
68 | 48 | */
|
69 |
| - protected static function _getLogger($options) { |
70 |
| - $name = isset($options['engine']) ? $options['engine'] : null; |
71 |
| - unset($options['engine']); |
72 |
| - $class = App::classname($name, 'Log/Engine', 'Log'); |
73 |
| - if (!$class) { |
74 |
| - throw new Error\Exception(__d('cake_dev', 'Could not load class %s', $name)); |
| 49 | + protected function _throwMissingClassError($class, $plugin) { |
| 50 | + throw new Error\Exception(__d('cake_dev', 'Could not load class %s', $class)); |
| 51 | + } |
| 52 | + |
| 53 | +/** |
| 54 | + * Create the logger instance. |
| 55 | + * |
| 56 | + * Part of the template method for Cake\Utility\ObjectRegistry::load() |
| 57 | + * @param string $class The classname that is missing. |
| 58 | + * @param array $settings An array of settings to use for the logger. |
| 59 | + * @return LogEngine The constructed logger class. |
| 60 | + */ |
| 61 | + protected function _create($class, $settings) { |
| 62 | + $instance = new $class($settings); |
| 63 | + $this->_checkInstance($instance); |
| 64 | + return $instance; |
| 65 | + } |
| 66 | + |
| 67 | +/** |
| 68 | + * Check an instance to see if it implements the LogInterface. |
| 69 | + * |
| 70 | + * @param mixed $instance The instance to check. |
| 71 | + * @return void |
| 72 | + * @throws Cake\Error\Exception when an object doesn't implement |
| 73 | + * the correct interface. |
| 74 | + */ |
| 75 | + protected function _checkInstance($instance) { |
| 76 | + if ($instance instanceof LogInterface) { |
| 77 | + return; |
75 | 78 | }
|
76 |
| - $logger = new $class($options); |
77 |
| - return $logger; |
| 79 | + throw new Error\Exception(__d( |
| 80 | + 'cake_dev', |
| 81 | + 'Loggers must implement Cake\Log\LogInterface.' |
| 82 | + )); |
| 83 | + } |
| 84 | + |
| 85 | +/** |
| 86 | + * Add a logger into a given name. |
| 87 | + * |
| 88 | + * @param string $name The name of the logger. |
| 89 | + * @param Cake\Log\LogInterface $instance A logger implementing LogInterface. |
| 90 | + */ |
| 91 | + public function add($name, $instance) { |
| 92 | + $this->_checkInstance($instance); |
| 93 | + $this->_loaded[$name] = $instance; |
| 94 | + } |
| 95 | + |
| 96 | +/** |
| 97 | + * Remove a single logger from the registry. |
| 98 | + * |
| 99 | + * @param string $name The logger name. |
| 100 | + * @return void |
| 101 | + */ |
| 102 | + public function unload($name) { |
| 103 | + unset($this->_loaded[$name]); |
78 | 104 | }
|
79 | 105 |
|
80 | 106 | }
|
0 commit comments