1 | <?php |
||
15 | class WidgetsContainer |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private $widgets = []; |
||
21 | |||
22 | /** |
||
23 | * @var EventDispatcherInterface |
||
24 | */ |
||
25 | private $dispatcher; |
||
26 | |||
27 | /** |
||
28 | * @param EventDispatcherInterface $dispatcher |
||
29 | */ |
||
30 | public function __construct(EventDispatcherInterface $dispatcher) |
||
31 | { |
||
32 | $this->dispatcher = $dispatcher; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param string $place |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | public function getWidgetsForPlace($place) |
||
41 | { |
||
42 | // send the event to those who did not add widgets could do it |
||
43 | $this->dispatcher->dispatch(StoreEvents::GET, new Get($this, $place)); |
||
44 | |||
45 | return isset($this->widgets[$place]) ? $this->widgets[$place] : []; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Regist widget. |
||
50 | * |
||
51 | * Controller example: |
||
52 | * AcmeDemoBundle:Welcome:index |
||
53 | * AcmeArticleBundle:Article:show |
||
54 | * |
||
55 | * @param string $place |
||
56 | * @param string $controller |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function registr($place, $controller) |
||
61 | { |
||
62 | if (preg_match('/^[a-z0-9]+:[a-z0-9]+:[_a-z0-9]+$/i', $controller)) { |
||
63 | if (!isset($this->widgets[$place])) { |
||
64 | $this->widgets[$place][] = $controller; |
||
65 | } elseif (!in_array($controller, $this->widgets[$place])) { |
||
66 | $this->widgets[$place][] = $controller; |
||
67 | } |
||
68 | |||
69 | return true; |
||
70 | } |
||
71 | |||
72 | return false; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param string $place |
||
77 | * @param string $controller |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function unregistr($place, $controller) |
||
93 | } |
||
94 |