3894d49fa320bf5a725de6d3a39b797224d05646
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Output / NodeEventListeningFormatter.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Testwork\Output;
12
13 use Behat\Testwork\EventDispatcher\TestworkEventDispatcher;
14 use Behat\Testwork\Output\Node\EventListener\EventListener;
15 use Behat\Testwork\Output\Printer\OutputPrinter;
16 use Symfony\Component\EventDispatcher\Event;
17
18 /**
19  * Formatter built around the idea of event delegation and composition.
20  *
21  * @author Konstantin Kudryashov <ever.zet@gmail.com>
22  */
23 final class NodeEventListeningFormatter implements Formatter
24 {
25     /**
26      * @var OutputPrinter
27      */
28     private $printer;
29     /**
30      * @var array
31      */
32     private $parameters;
33     /**
34      * @var EventListener
35      */
36     private $listener;
37     /**
38      * @var string
39      */
40     private $name;
41     /**
42      * @var string
43      */
44     private $description;
45
46     /**
47      * Initializes formatter.
48      *
49      * @param string        $name
50      * @param string        $description
51      * @param array         $parameters
52      * @param OutputPrinter $printer
53      * @param EventListener $listener
54      */
55     public function __construct($name, $description, array $parameters, OutputPrinter $printer, EventListener $listener)
56     {
57         $this->name = $name;
58         $this->description = $description;
59         $this->parameters = $parameters;
60         $this->printer = $printer;
61         $this->listener = $listener;
62     }
63
64     /**
65      * Returns an array of event names this subscriber wants to listen to.
66      *
67      * @return array The event names to listen to
68      */
69     public static function getSubscribedEvents()
70     {
71         return array(TestworkEventDispatcher::BEFORE_ALL_EVENTS => 'listenEvent');
72     }
73
74     /**
75      * Proxies event to the listener.
76      *
77      * @param Event       $event
78      * @param null|string $eventName
79      */
80     public function listenEvent(Event $event, $eventName = null)
81     {
82         $eventName = $eventName ?: $event->getName();
83
84         $this->listener->listenEvent($this, $event, $eventName);
85     }
86
87     /**
88      * {@inheritdoc}
89      */
90     public function getName()
91     {
92         return $this->name;
93     }
94
95     /**
96      * {@inheritdoc}
97      */
98     public function getDescription()
99     {
100         return $this->description;
101     }
102
103     /**
104      * {@inheritdoc}
105      */
106     public function getOutputPrinter()
107     {
108         return $this->printer;
109     }
110
111     /**
112      * {@inheritdoc}
113      */
114     public function setParameter($name, $value)
115     {
116         $this->parameters[$name] = $value;
117     }
118
119     /**
120      * {@inheritdoc}
121      */
122     public function getParameter($name)
123     {
124         return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
125     }
126 }