Completed
Push — master ( 9bea77...1a9491 )
by Pascal
08:35
created

RouterServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 15
dl 0
loc 99
ccs 49
cts 49
cp 1
rs 9.1666
c 0
b 0
f 0

1 Method

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