1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Manager; |
13
|
|
|
|
14
|
|
|
use WBW\Bundle\CoreBundle\Provider\ProviderInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Abstract manager. |
18
|
|
|
* |
19
|
|
|
* @author webeweb <https://github.com/webeweb/> |
20
|
|
|
* @package WBW\Bundle\CoreBundle\Manager |
21
|
|
|
* @abstract |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractManager implements ManagerInterface { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Providers. |
27
|
|
|
* |
28
|
|
|
* @var ProviderInterface[] |
29
|
|
|
*/ |
30
|
|
|
private $providers; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
*/ |
35
|
|
|
protected function __construct() { |
36
|
|
|
$this->setProviders([]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function addProvider(ProviderInterface $provider) { |
43
|
|
|
$this->providers[] = $provider; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function contains(ProviderInterface $provider) { |
51
|
|
|
return -1 !== $this->indexOf($provider); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the providers. |
56
|
|
|
* |
57
|
|
|
* @return ProviderInterface[] Returns the provider. |
58
|
|
|
*/ |
59
|
|
|
public function &getProviders() { |
60
|
|
|
return $this->providers; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function hasProviders() { |
67
|
|
|
return 0 < $this->size(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function indexOf(ProviderInterface $provider) { |
74
|
|
|
for ($i = count($this->providers) - 1; 0 <= $i; --$i) { |
75
|
|
|
if ($provider !== $this->providers[$i]) { |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
return $i; |
79
|
|
|
} |
80
|
|
|
return -1; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function removeProvider(ProviderInterface $provider) { |
87
|
|
|
$indexOf = $this->indexOf($provider); |
88
|
|
|
if (-1 !== $indexOf) { |
89
|
|
|
unset($this->providers[$indexOf]); |
90
|
|
|
} |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set the providers. |
96
|
|
|
* |
97
|
|
|
* @param ProviderInterface[] $providers The providers. |
98
|
|
|
* @return ManagerInterface Returns this manager. |
99
|
|
|
*/ |
100
|
|
|
protected function setProviders(array $providers) { |
101
|
|
|
$this->providers = $providers; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function size() { |
109
|
|
|
return count($this->providers); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|