1 <?php
 2 /**
 3  * @package     Joomla.Platform
 4  * @subpackage  Log
 5  *
 6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
 7  * @license     GNU General Public License version 2 or later; see LICENSE
 8  */
 9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Joomla! Callback Log class
14  *
15  * This class allows logging to be handled by a callback function.
16  * This allows unprecedented flexibility in the way logging can be handled.
17  *
18  * @since  12.2
19  */
20 class JLogLoggerCallback extends JLogLogger
21 {
22     /**
23      * The function to call when an entry is added
24      *
25      * @var    callable
26      * @since  12.2
27      */
28     protected $callback;
29 
30     /**
31      * Constructor.
32      *
33      * @param   array  &$options  Log object options.
34      *
35      * @since   12.2
36      * @throws  RuntimeException
37      */
38     public function __construct(array &$options)
39     {
40         // Call the parent constructor.
41         parent::__construct($options);
42 
43         // Throw an exception if there is not a valid callback
44         if (!isset($this->options['callback']) || !is_callable($this->options['callback']))
45         {
46             throw new RuntimeException('JLogLoggerCallback created without valid callback function.');
47         }
48 
49         $this->callback = $this->options['callback'];
50     }
51 
52     /**
53      * Method to add an entry to the log.
54      *
55      * @param   JLogEntry  $entry  The log entry object to add to the log.
56      *
57      * @return  void
58      *
59      * @since   12.2
60      * @throws  RuntimeException
61      */
62     public function addEntry(JLogEntry $entry)
63     {
64         // Pass the log entry to the callback function
65         call_user_func($this->callback, $entry);
66     }
67 }
68