Passed
Branch phpunit-bridge (ebf7df)
by Pascal
08:31
created

RouterServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 15
dl 0
loc 103
ccs 52
cts 52
cp 1
rs 9.1666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C register() 0 100 7
1
<?php
2
3
namespace Skalpa\Silex\Symfony\Routing;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Doctrine\Common\Annotations\CachedReader;
8
use Doctrine\Common\Cache\ArrayCache;
9
use Pimple\Container;
10
use Pimple\ServiceProviderInterface;
11
use Sensio\Bundle\FrameworkExtraBundle\Routing\AnnotatedRouteControllerLoader;
12
use Skalpa\Silex\Symfony\Routing\Loader\ArrayLoader;
13
use Symfony\Component\Config\FileLocator;
14
use Symfony\Component\Config\Loader\DelegatingLoader;
15
use Symfony\Component\Config\Loader\LoaderResolver;
16
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
17
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
18
use Symfony\Component\Routing\Loader\PhpFileLoader;
19
use Symfony\Component\Routing\Loader\XmlFileLoader;
20
use Symfony\Component\Routing\Loader\YamlFileLoader;
21
use Symfony\Component\Routing\Router;
22
use Symfony\Component\Yaml\Yaml;
23
24
/**
25
 * Symfony Routing service provider for Silex.
26
 */
27
class RouterServiceProvider implements ServiceProviderInterface
28
{
29 9
    public function register(Container $container)
30
    {
31 9
        $container['router.debug'] = isset($container['debug']) ? $container['debug'] : false;
32 9
        $container['router.resource'] = null;
33 9
        $container['router.resource_type'] = null;
34 9
        $container['router.cache_dir'] = null;
35 9
        $container['router.file_locator.paths'] = [];
36 9
        $container['router.options'] = [];
37
        $container['router'] = function (Container $container) {
38 9
            $options = array_replace([
39 9
                'debug'         => $container['router.debug'],
40 9
                'cache_dir'     => $container['router.cache_dir'],
41 9
                'resource_type' => $container['router.resource_type'],
42 9
            ], $container['router.options']);
43
44 9
            return new Router(
45 9
                $container['router.loader'],
46 9
                $container['router.resource'],
47 9
                $options,
48 9
                $container['request_context'],
49 9
                $container['logger']
50 9
            );
51
        };
52
53
        $container['router.file_locator'] = function (Container $container) {
54 8
            return new FileLocator($container['router.file_locator.paths']);
55
        };
56
        $container['router.loader.xml'] = function (Container $container) {
57 8
            return new XmlFileLoader($container['router.file_locator']);
58
        };
59
        $container['router.loader.php'] = function (Container $container) {
60 8
            return new PhpFileLoader($container['router.file_locator']);
61
        };
62
63 9
        $loaders = ['router.loader.xml', 'router.loader.php'];
64
65 9
        if (class_exists(Yaml::class)) {
66
            $container['router.loader.yaml'] = function (Container $container) {
67 8
                return new YamlFileLoader($container['router.file_locator']);
68
            };
69 9
            $loaders[] = 'router.loader.yaml';
70 9
        }
71
72 9
        if (class_exists(AnnotatedRouteControllerLoader::class) && class_exists(AnnotationReader::class)) {
73
            $container['router.annotations_reader'] = function () {
74 7
                AnnotationRegistry::registerLoader('class_exists');
75
76 7
                return new CachedReader(new AnnotationReader(), new ArrayCache());
77
            };
78
79
            $container['router.annotations_loader'] = function (Container $container) {
80 8
                if (is_string($reader = $container['router.annotations_reader'])) {
81 1
                    $reader = $container[$reader];
82 1
                }
83
84 8
                return new AnnotatedRouteControllerLoader($reader);
85
            };
86
87
            $container['router.loader.annotation_file'] = function (Container $container) {
88 8
                return new AnnotationFileLoader($container['router.file_locator'], $container['router.annotations_loader']);
89
            };
90
91
            $container['router.loader.annotation_directory'] = function (Container $container) {
92 8
                return new AnnotationDirectoryLoader($container['router.file_locator'], $container['router.annotations_loader']);
93
            };
94
95 9
            $loaders[] = 'router.loader.annotation_file';
96 9
            $loaders[] = 'router.loader.annotation_directory';
97 9
        }
98 9
        $container['router.loaders'] = $loaders;
99
100
        $container['router.delegating_loader'] = function (Container $container) {
101 8
            return new DelegatingLoader($container['router.loader_resolver']);
102
        };
103
104
        $container['router.loader_resolver'] = function (Container $container) {
105 8
            $loaders = [];
106 8
            foreach ($container['router.loaders'] as $serviceName) {
107 8
                $loaders[] = $container[$serviceName];
108 8
            }
109
110 8
            return new LoaderResolver($loaders);
111
        };
112
113
        $container['router.loader'] = function (Container $container) {
114 9
            return new ArrayLoader($container, 'router.delegating_loader');
115
        };
116
117
        $container['url_generator'] = function (Container $container) {
118 1
            return $container['router'];
119
        };
120
121
        $container['request_matcher'] = function (Container $container) {
122 7
            return $container['router'];
123
        };
124
125 7
        $container['routes'] = function (Container $container) {
126 7
            return $container['router']->getRouteCollection();
127
        };
128 9
    }
129
}
130