|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Devhelp\Silex\Piwik; |
|
5
|
|
|
|
|
6
|
|
|
use Devhelp\Piwik\Api\Api; |
|
7
|
|
|
use Silex\Application; |
|
8
|
|
|
use Silex\ServiceProviderInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Adds Api services to the container. |
|
12
|
|
|
*/ |
|
13
|
|
|
class PiwikApiServiceProvider implements ServiceProviderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $config; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Application |
|
22
|
|
|
*/ |
|
23
|
|
|
private $app; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* example config: |
|
27
|
|
|
* array ( |
|
28
|
|
|
* "client" => "my_piwik.client" (service id) |
|
29
|
|
|
* "api" => array( |
|
30
|
|
|
* "reader" => "http://my_piwik_instance.piwik.pro", |
|
31
|
|
|
* "default_params" => array( |
|
32
|
|
|
* (can be either a service id or raw value) |
|
33
|
|
|
* "token_auth" => "my_token_auth_param_service_id", |
|
34
|
|
|
* "idSite" => 123 |
|
35
|
|
|
* ) |
|
36
|
|
|
* ) |
|
37
|
|
|
* ) |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $config |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(array $config) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->config = $config; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function register(Application $app) |
|
47
|
|
|
{ |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function boot(Application $app) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->app = $app; |
|
53
|
|
|
|
|
54
|
|
|
$this->addApis(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function addApis() |
|
58
|
|
|
{ |
|
59
|
|
|
$clientServiceId = $this->config['client']; |
|
60
|
|
|
$apiConfig = $this->config['api']; |
|
61
|
|
|
$apiDefaultConfig = array( |
|
62
|
|
|
'url' => null, |
|
63
|
|
|
'default_params' => array() |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$firstApiName = key($apiConfig); |
|
67
|
|
|
|
|
68
|
|
|
foreach ($apiConfig as $name => $config) { |
|
69
|
|
|
$config = array_merge($apiDefaultConfig, $config); |
|
70
|
|
|
$this->addApi($clientServiceId, $name, $config); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->addAliases($firstApiName); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function addApi($clientServiceId, $name, $config) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!$config['url']) { |
|
79
|
|
|
throw new \InvalidArgumentException("'url' must be defined"); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$app = $this->app; |
|
83
|
|
|
|
|
84
|
|
|
$apiCallable = function () use ($app, $clientServiceId, $config) { |
|
85
|
|
|
|
|
86
|
|
|
if (!isset($app[$clientServiceId])) { |
|
87
|
|
|
throw new \InvalidArgumentException("'$clientServiceId' service does not exists"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$url = $config['url']; |
|
91
|
|
|
$defaultParams = array(); |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* resolve params to container services if applicable |
|
95
|
|
|
*/ |
|
96
|
|
|
foreach ($config['default_params'] as $param => $value) { |
|
97
|
|
|
$value = isset($app[$value]) ? $app[$value] : $value; |
|
98
|
|
|
$defaultParams[$param] = $value; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* create new Api |
|
103
|
|
|
*/ |
|
104
|
|
|
$api = new Api($app[$clientServiceId], $url); |
|
105
|
|
|
$api->setDefaultParams($defaultParams); |
|
106
|
|
|
|
|
107
|
|
|
return $api; |
|
108
|
|
|
}; |
|
109
|
|
|
|
|
110
|
|
|
$this->app['devhelp_piwik.api.'.$name] = $this->app->share($apiCallable); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private function addAliases($aliasedApiName) |
|
114
|
|
|
{ |
|
115
|
|
|
$app = $this->app; |
|
116
|
|
|
$aliasCallable = function () use ($app, $aliasedApiName) { |
|
117
|
|
|
return $app['devhelp_piwik.api.'.$aliasedApiName]; |
|
118
|
|
|
}; |
|
119
|
|
|
|
|
120
|
|
|
$this->app['devhelp_piwik.api'] = $this->app->share($aliasCallable); |
|
121
|
|
|
$this->app['devhelp_piwik.api.default'] = $this->app->share($aliasCallable); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|