RouterServiceProvider   C
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 19

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 19
dl 0
loc 112
ccs 63
cts 63
cp 1
rs 6.875
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C register() 0 109 8
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 Silex\Provider\Routing\RedirectableUrlMatcher;
12
use Skalpa\Silex\Symfony\Routing\Loader\AnnotationClassLoader;
13
use Skalpa\Silex\Symfony\Routing\Loader\ArrayLoader;
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\Config\Loader\DelegatingLoader;
16
use Symfony\Component\Config\Loader\LoaderResolver;
17
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
18
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
19
use Symfony\Component\Routing\Loader\PhpFileLoader;
20
use Symfony\Component\Routing\Loader\XmlFileLoader;
21
use Symfony\Component\Routing\Loader\YamlFileLoader;
22
use Symfony\Component\Routing\Route;
23
use Symfony\Component\Routing\Router;
24
use Symfony\Component\Yaml\Yaml;
25
26
/**
27
 * Symfony Routing service provider for Silex.
28
 */
29
class RouterServiceProvider implements ServiceProviderInterface
30
{
31 19
    public function register(Container $container)
32
    {
33 19
        $container['router.debug'] = isset($container['debug']) ? $container['debug'] : false;
34 19
        $container['router.cache_dir'] = null;
35 19
        $container['router.resource'] = null;
36 19
        $container['router.resource_type'] = null;
37 19
        $container['router.file_locator.paths'] = [];
38 19
        $container['router.options'] = [];
39
        $container['router'] = function (Container $container) {
40 19
            $options = array_replace([
41 19
                'debug'              => $container['router.debug'],
42 19
                'cache_dir'          => $container['router.cache_dir'],
43 19
                'resource_type'      => $container['router.resource_type'],
44 19
                'matcher_class'      => RedirectableUrlMatcher::class,
45 19
                'matcher_base_class' => RedirectableUrlMatcher::class,
46 19
            ], $container['router.options']);
47
48 19
            return new Router(
49 19
                $container['router.loader'],
50 19
                $container['router.resource'],
51 19
                $options,
52 19
                $container['request_context'],
53 19
                $container['logger']
54 19
            );
55
        };
56
57
        $container['router.file_locator'] = function (Container $container) {
58 18
            return new FileLocator($container['router.file_locator.paths']);
59
        };
60
        $container['router.loader.xml'] = function (Container $container) {
61 18
            return new XmlFileLoader($container['router.file_locator']);
62
        };
63
        $container['router.loader.php'] = function (Container $container) {
64 18
            return new PhpFileLoader($container['router.file_locator']);
65
        };
66
67 19
        $loaders = ['router.loader.xml', 'router.loader.php'];
68
69 19
        if (class_exists(Yaml::class)) {
70
            $container['router.loader.yaml'] = function (Container $container) {
71 18
                return new YamlFileLoader($container['router.file_locator']);
72
            };
73 19
            $loaders[] = 'router.loader.yaml';
74 19
        }
75
76 19
        if (class_exists(AnnotationReader::class)) {
77
            $container['router.annotation.reader'] = function () {
78 17
                AnnotationRegistry::registerLoader('class_exists');
79
80 17
                return new CachedReader(new AnnotationReader(), new ArrayCache());
81
            };
82
83
            $container['router.annotation.configuration_strategy'] = $container->protect(function (Route $route, \ReflectionClass $class, \ReflectionMethod $method) use ($container) {
84 7
                if (isset($container[$class->name])) {
85 1
                    $route->setDefault('_controller', $class->name.':'.$method->name);
86 7
                } elseif ('__invoke' === $method->name) {
87 6
                    $route->setDefault('_controller', $class->name);
88 6
                } else {
89 6
                    $route->setDefault('_controller', $class->name.'::'.$method->name);
90
                }
91 19
            });
92
93
            $container['router.loader.annotation.class'] = function (Container $container) {
94 18
                return new AnnotationClassLoader(
95 18
                    is_string($reader = $container['router.annotation.reader']) ? $container[$reader] : $reader,
96 18
                    $container['router.annotation.configuration_strategy']
97 18
                );
98
            };
99
100
            $container['router.loader.annotation.file'] = function (Container $container) {
101 18
                return new AnnotationFileLoader($container['router.file_locator'], $container['router.loader.annotation.class']);
102
            };
103
104
            $container['router.loader.annotation.directory'] = function (Container $container) {
105 18
                return new AnnotationDirectoryLoader($container['router.file_locator'], $container['router.loader.annotation.class']);
106
            };
107
108 19
            $loaders[] = 'router.loader.annotation.class';
109 19
            $loaders[] = 'router.loader.annotation.file';
110 19
            $loaders[] = 'router.loader.annotation.directory';
111 19
        }
112
113 19
        $container['router.loaders'] = $loaders;
114
115
        $container['router.delegating_loader'] = function (Container $container) {
116 18
            return new DelegatingLoader($container['router.loader_resolver']);
117
        };
118
119
        $container['router.loader_resolver'] = function (Container $container) {
120 18
            $loaders = [];
121 18
            foreach ($container['router.loaders'] as $serviceName) {
122 18
                $loaders[] = $container[$serviceName];
123 18
            }
124
125 18
            return new LoaderResolver($loaders);
126
        };
127
128
        $container['router.loader'] = function (Container $container) {
129 19
            return new ArrayLoader($container, 'router.delegating_loader');
130
        };
131
132
        $container->extend('url_generator', function ($silexGenerator, $container) {
133 3
            return new ChainUrlGenerator([$container['router'], $silexGenerator], $container['request_context']);
134 19
        });
135
136 19
        $container->extend('request_matcher', function ($silexMatcher, $container) {
137 3
            return new ChainRequestMatcher([$container['router'], $silexMatcher], $container['request_context']);
138 19
        });
139 19
    }
140
}
141