1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\GoogleAnalyticsBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Cache\Adapter\Void\VoidCachePool; |
6
|
|
|
use Happyr\GoogleAnalyticsBundle\Http\AnalyticsClientInterface; |
7
|
|
|
use Happyr\GoogleAnalyticsBundle\Http\HttpClient; |
8
|
|
|
use Happyr\GoogleAnalyticsBundle\Http\VoidHttpClient; |
9
|
|
|
use Happyr\GoogleAnalyticsBundle\Service\AnalyticsDataFetcher; |
10
|
|
|
use Happyr\GoogleAnalyticsBundle\Service\DataFetcher; |
11
|
|
|
use Happyr\GoogleAnalyticsBundle\Service\Tracker; |
12
|
|
|
use Symfony\Component\Config\FileLocator; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
16
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This is the class that loads and manages your bundle configuration. |
20
|
|
|
* |
21
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
22
|
|
|
*/ |
23
|
|
|
class HappyrGoogleAnalyticsExtension extends Extension |
24
|
|
|
{ |
25
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
26
|
|
|
{ |
27
|
1 |
|
$configuration = new Configuration(); |
28
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
29
|
|
|
|
30
|
1 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
31
|
1 |
|
$loader->load('services.yml'); |
32
|
|
|
|
33
|
1 |
|
$trackerDef = $container->getDefinition(Tracker::class); |
34
|
1 |
|
$trackerDef->replaceArgument(2, $config['tracking_id']) |
35
|
1 |
|
->replaceArgument(3, $config['version']); |
36
|
|
|
|
37
|
1 |
|
if (!$config['enabled']) { |
38
|
|
|
$container->setAlias(AnalyticsClientInterface::class, VoidHttpClient::class); |
39
|
|
|
} else { |
40
|
1 |
|
$container->register(AnalyticsClientInterface::class, HttpClient::class) |
41
|
1 |
|
->setPublic(false) |
42
|
1 |
|
->addArgument(new Reference($config['http_client'])) |
43
|
1 |
|
->addArgument(new Reference($config['http_message_factory'])) |
44
|
1 |
|
->addArgument($config['endpoint']); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
if (empty($config['fetching']['client_service'])) { |
48
|
1 |
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!empty($config['fetching']['cache_service'])) { |
52
|
|
|
$cacheService = $config['fetching']['cache_service']; |
53
|
|
|
} else { |
54
|
|
|
$cacheService = 'happyr.google_analytics.cache.void'; |
55
|
|
|
$container->register($cacheService, VoidCachePool::class); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$container->register(AnalyticsDataFetcher::class, AnalyticsDataFetcher::class) |
59
|
|
|
->addArgument(new Reference($cacheService)) |
60
|
|
|
->addArgument(new Reference($config['fetching']['client_service'])) |
61
|
|
|
->addArgument($config['fetching']['view_id']) |
62
|
|
|
->addArgument($config['fetching']['cache_lifetime']); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|