Base for a static organization website

LogEngineCollection.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Registry of loaded log engines
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Log
  15. * @since CakePHP(tm) v 2.2
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ObjectCollection', 'Utility');
  19. /**
  20. * Registry of loaded log engines
  21. *
  22. * @package Cake.Log
  23. */
  24. class LogEngineCollection extends ObjectCollection {
  25. /**
  26. * Loads/constructs a Log engine.
  27. *
  28. * @param string $name instance identifier
  29. * @param array $options Setting for the Log Engine
  30. * @return BaseLog BaseLog engine instance
  31. * @throws CakeLogException when logger class does not implement a write method
  32. */
  33. public function load($name, $options = array()) {
  34. $enable = isset($options['enabled']) ? $options['enabled'] : true;
  35. $loggerName = $options['engine'];
  36. unset($options['engine']);
  37. $className = $this->_getLogger($loggerName);
  38. $logger = new $className($options);
  39. if (!$logger instanceof CakeLogInterface) {
  40. throw new CakeLogException(
  41. __d('cake_dev', 'logger class %s does not implement a %s method.', $loggerName, 'write()')
  42. );
  43. }
  44. $this->_loaded[$name] = $logger;
  45. if ($enable) {
  46. $this->enable($name);
  47. }
  48. return $logger;
  49. }
  50. /**
  51. * Attempts to import a logger class from the various paths it could be on.
  52. * Checks that the logger class implements a write method as well.
  53. *
  54. * @param string $loggerName the plugin.className of the logger class you want to build.
  55. * @return mixed boolean false on any failures, string of classname to use if search was successful.
  56. * @throws CakeLogException
  57. */
  58. protected static function _getLogger($loggerName) {
  59. list($plugin, $loggerName) = pluginSplit($loggerName, true);
  60. if (substr($loggerName, -3) !== 'Log') {
  61. $loggerName .= 'Log';
  62. }
  63. App::uses($loggerName, $plugin . 'Log/Engine');
  64. if (!class_exists($loggerName)) {
  65. throw new CakeLogException(__d('cake_dev', 'Could not load class %s', $loggerName));
  66. }
  67. return $loggerName;
  68. }
  69. }