1: <?php
2: /**
3: * PHP version 5
4: * @package generalDriver
5: * @author Christian Schiffler <c.schiffler@cyberspectrum.de>
6: * @author Stefan Heimes <stefan_heimes@hotmail.com>
7: * @author Tristan Lins <tristan.lins@bit3.de>
8: * @copyright The MetaModels team.
9: * @license LGPL.
10: * @filesource
11: */
12:
13: namespace DcGeneral\Contao\Callback;
14:
15: /**
16: * Class AbstractCallbackListener.
17: *
18: * Abstract base class for a callback listener.
19: *
20: * @package DcGeneral\Contao\Callback
21: */
22: abstract class AbstractCallbackListener
23: {
24: /**
25: * The callback to use.
26: *
27: * @var array|callable
28: */
29: protected $callback;
30:
31: /**
32: * Create a new instance of the listener.
33: *
34: * @param array|callable $callback The callback to call when invoked.
35: */
36: public function __construct($callback = null)
37: {
38: $this->callback = $callback;
39: }
40:
41: /**
42: * Retrieve the attached callback.
43: *
44: * @return array|callable
45: */
46: public function getCallback()
47: {
48: return $this->callback;
49: }
50:
51: /**
52: * Retrieve the arguments for the callback.
53: *
54: * @param \Symfony\Component\EventDispatcher\Event $event The event being emitted.
55: *
56: * @return array
57: */
58: abstract public function getArgs($event);
59:
60: /**
61: * Invoke the callback.
62: *
63: * @param \Symfony\Component\EventDispatcher\Event $event The Event for which the callback shall be invoked.
64: *
65: * @return void
66: */
67: public function __invoke($event)
68: {
69: if ($this->getCallback())
70: {
71: Callbacks::callArgs($this->getCallback(), $this->getArgs($event));
72: }
73: }
74: }
75: