1<?php
2
3/**
4 * @see       https://github.com/laminas/laminas-log for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-log/blob/master/COPYRIGHT.md
6 * @license   https://github.com/laminas/laminas-log/blob/master/LICENSE.md New BSD License
7 */
8
9namespace Laminas\Log\Writer;
10
11use FirePHP as FirePHPService;
12use Laminas\Log\Exception;
13use Laminas\Log\Formatter\FirePhp as FirePhpFormatter;
14use Laminas\Log\Logger;
15use Traversable;
16
17class FirePhp extends AbstractWriter
18{
19    /**
20     * A FirePhpInterface instance that is used to log messages to.
21     *
22     * @var FirePhp\FirePhpInterface
23     */
24    protected $firephp;
25
26    /**
27     * Initializes a new instance of this class.
28     *
29     * @param null|FirePhp\FirePhpInterface|array|Traversable $instance An instance of FirePhpInterface
30     *        that should be used for logging
31     */
32    public function __construct($instance = null)
33    {
34        if ($instance instanceof Traversable) {
35            $instance = iterator_to_array($instance);
36        }
37
38        if (is_array($instance)) {
39            parent::__construct($instance);
40            $instance = isset($instance['instance']) ? $instance['instance'] : null;
41        }
42
43        if ($instance !== null && ! ($instance instanceof FirePhp\FirePhpInterface)) {
44            throw new Exception\InvalidArgumentException('You must pass a valid FirePhp\FirePhpInterface');
45        }
46
47        $this->firephp   = $instance;
48        $this->formatter = new FirePhpFormatter();
49    }
50
51    /**
52     * Write a message to the log.
53     *
54     * @param  array $event event data
55     * @return void
56     */
57    protected function doWrite(array $event)
58    {
59        $firephp = $this->getFirePhp();
60
61        if (! $firephp->getEnabled()) {
62            return;
63        }
64
65        list($line, $label) = $this->formatter->format($event);
66
67        switch ($event['priority']) {
68            case Logger::EMERG:
69            case Logger::ALERT:
70            case Logger::CRIT:
71            case Logger::ERR:
72                $firephp->error($line, $label);
73                break;
74            case Logger::WARN:
75                $firephp->warn($line, $label);
76                break;
77            case Logger::NOTICE:
78            case Logger::INFO:
79                $firephp->info($line, $label);
80                break;
81            case Logger::DEBUG:
82                $firephp->trace($line);
83                break;
84            default:
85                $firephp->log($line, $label);
86                break;
87        }
88    }
89
90    /**
91     * Gets the FirePhpInterface instance that is used for logging.
92     *
93     * @return FirePhp\FirePhpInterface
94     * @throws Exception\RuntimeException
95     */
96    public function getFirePhp()
97    {
98        if (! $this->firephp instanceof FirePhp\FirePhpInterface
99            && ! class_exists('FirePHP')
100        ) {
101            // No FirePHP instance, and no way to create one
102            throw new Exception\RuntimeException('FirePHP Class not found');
103        }
104
105        // Remember: class names in strings are absolute; thus the class_exists
106        // here references the canonical name for the FirePHP class
107        if (! $this->firephp instanceof FirePhp\FirePhpInterface
108            && class_exists('FirePHP')
109        ) {
110            // FirePHPService is an alias for FirePHP; otherwise the class
111            // names would clash in this file on this line.
112            $this->setFirePhp(new FirePhp\FirePhpBridge(new FirePHPService()));
113        }
114
115        return $this->firephp;
116    }
117
118    /**
119     * Sets the FirePhpInterface instance that is used for logging.
120     *
121     * @param  FirePhp\FirePhpInterface $instance A FirePhpInterface instance to set.
122     * @return FirePhp
123     */
124    public function setFirePhp(FirePhp\FirePhpInterface $instance)
125    {
126        $this->firephp = $instance;
127
128        return $this;
129    }
130}
131