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