-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChainConfigProvider.php
82 lines (68 loc) · 1.94 KB
/
ChainConfigProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/*
* UrlRewrite Bundle for Contao Open Source CMS.
*
* @copyright Copyright (c) 2017, terminal42 gmbh
* @author terminal42 <https://terminal42.ch>
* @license MIT
*/
namespace Terminal42\UrlRewriteBundle\ConfigProvider;
use Terminal42\UrlRewriteBundle\RewriteConfigInterface;
class ChainConfigProvider implements ConfigProviderInterface
{
/**
* @var array
*/
private $providers = [];
/**
* Add the config provider.
*
* @param ConfigProviderInterface $provider
*/
public function addProvider(ConfigProviderInterface $provider): void
{
$this->providers[] = $provider;
}
/**
* {@inheritdoc}
*/
public function find(string $id): ?RewriteConfigInterface
{
list($class, $id) = explode(':', $id);
/** @var ConfigProviderInterface $provider */
foreach ($this->providers as $provider) {
if ($class === $this->getProviderIdentifier($provider) && ($config = $provider->find($id)) !== null) {
return $config;
}
}
return null;
}
/**
* {@inheritdoc}
*/
public function findAll(): array
{
$configs = [];
/** @var ConfigProviderInterface $provider */
foreach ($this->providers as $provider) {
$providerConfigs = $provider->findAll();
/** @var RewriteConfigInterface $config */
foreach ($providerConfigs as $config) {
$config->setIdentifier($this->getProviderIdentifier($provider) . ':' . $config->getIdentifier());
}
$configs = array_merge($configs, $providerConfigs);
}
return $configs;
}
/**
* Get the provider identifier
*
* @param ConfigProviderInterface $provider
*
* @return string
*/
private function getProviderIdentifier(ConfigProviderInterface $provider): string
{
return get_class($provider);
}
}