JnsXhprofExtension::getAlias()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Jns\Bundle\XhprofBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6
use Symfony\Component\Config\Definition\Processor;
7
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\Config\FileLocator;
11
12
class JnsXhprofExtension extends Extension
13
{
14
    /**
15
     * Xml config files to load
16
     * @var array
17
     */
18
    protected $resources = array(
19
        'services' => 'services.xml',
20
    );
21
22
    /**
23
     * Loads the services based on your application configuration.
24
     *
25
     * @param array $configs
26
     * @param ContainerBuilder $container
27
     */
28
    public function load(array $configs, ContainerBuilder $container)
29
    {
30
        $processor = new Processor();
31
        $configuration = new Configuration();
32
        $config = $processor->process($configuration->getConfigTreeBuilder()->buildTree(), $configs);
33
34
        if ($config['enabled']) {
35
            if (!$config['require_extension_exists'] || function_exists('xhprof_enable')) {
36
                $this->loadDefaults($container);
37
38
                foreach ($config as $key => $value) {
39
                    $container->setParameter($this->getAlias().'.'.$key, $value);
40
                }
41
            } else {
42
                throw new InvalidConfigurationException("Xhprof Bundle is enabled but the xhprof extension is not enabled.");
43
            }
44
        }
45
    }
46
47
    public function getAlias()
48
    {
49
        return 'jns_xhprof';
50
    }
51
52
    /**
53
     * Get File Loader
54
     *
55
     * @param ContainerBuilder $container
56
     *
57
     * @return XmlFileLoader
58
     */
59
    public function getFileLoader($container)
60
    {
61
        return new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
62
    }
63
64
    /**
65
     * @param ContainerBuilder $container
66
     */
67
    protected function loadDefaults($container)
68
    {
69
        $loader = $this->getFileLoader($container);
70
71
        foreach ($this->resources as $resource) {
72
            $loader->load($resource);
73
        }
74
    }
75
}
76