1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Devhelp\PiwikBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
6
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
7
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
|
10
|
|
|
class DevhelpPiwikExtension extends Extension |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $config; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* params to be injected as default (per client) |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $apiParams = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ContainerBuilder |
27
|
|
|
*/ |
28
|
|
|
protected $container; |
29
|
|
|
|
30
|
|
|
public function load(array $configs, ContainerBuilder $container) |
31
|
|
|
{ |
32
|
|
|
$this->container = $container; |
33
|
|
|
|
34
|
|
|
$configuration = new Configuration(); |
35
|
|
|
|
36
|
|
|
if ($this->allConfigsAreEmpty($configs)) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->config = $this->processConfiguration($configuration, $configs); |
41
|
|
|
|
42
|
|
|
$container->setParameter('devhelp_piwik.client.service_id', $this->config['client']); |
43
|
|
|
|
44
|
|
|
$this->addApis(); |
45
|
|
|
|
46
|
|
|
$container->setParameter('devhelp_piwik.api.parameters', $this->apiParams); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function addApis() |
50
|
|
|
{ |
51
|
|
|
$firstApiName = key($this->config['api']); |
52
|
|
|
|
53
|
|
|
foreach ($this->config['api'] as $name => $definition) { |
54
|
|
|
$this->addApi($name, $definition); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->container->setAlias('devhelp_piwik.api', 'devhelp_piwik.api.'.$firstApiName); |
58
|
|
|
$this->container->setAlias('devhelp_piwik.api.default', 'devhelp_piwik.api.'.$firstApiName); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function addApi($name, $data) |
62
|
|
|
{ |
63
|
|
|
$apiDefinition = new Definition('Devhelp\Piwik\Api\Api', array( |
64
|
|
|
new Reference('devhelp_piwik.client'), |
65
|
|
|
$data['url'] |
66
|
|
|
)); |
67
|
|
|
|
68
|
|
|
$this->apiParams[$name] = $data['default_params']; |
69
|
|
|
|
70
|
|
|
$this->container->setDefinition('devhelp_piwik.api.'.$name, $apiDefinition); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function allConfigsAreEmpty(array $configs) |
74
|
|
|
{ |
75
|
|
|
foreach ($configs as $config) { |
76
|
|
|
if ($config) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|