|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AnimeDb package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace AnimeDb\Bundle\AppBundle\Service; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
12
|
|
|
use AnimeDb\Bundle\AppBundle\Event\Widget\StoreEvents; |
|
13
|
|
|
use AnimeDb\Bundle\AppBundle\Event\Widget\Get; |
|
14
|
|
|
|
|
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) |
|
82
|
|
|
{ |
|
83
|
|
|
if (isset($this->widgets[$place]) && |
|
84
|
|
|
($key = array_search($controller, $this->widgets[$place])) !== false |
|
85
|
|
|
) { |
|
86
|
|
|
unset($this->widgets[$place][$key]); |
|
87
|
|
|
|
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|