1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JDesrosiers\Silex\Provider; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
use Silex\Controller; |
7
|
|
|
use Silex\ServiceProviderInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
12
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The CORS service provider provides a `cors` service that a can be included in your project as application middleware. |
16
|
|
|
*/ |
17
|
|
|
class CorsServiceProvider implements ServiceProviderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Add OPTIONS method support for all routes |
21
|
|
|
* |
22
|
|
|
* @param Application $app |
23
|
|
|
*/ |
24
|
|
|
public function boot(Application $app) |
25
|
|
|
{ |
26
|
41 |
|
$app->on(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) { |
27
|
5 |
|
$e = $event->getException(); |
28
|
5 |
|
if ($e instanceof MethodNotAllowedHttpException && $e->getHeaders()["Allow"] === "OPTIONS") { |
29
|
1 |
|
$event->setException(new NotFoundHttpException("No route found for \"{$event->getRequest()->getMethod()} {$event->getRequest()->getPathInfo()}\"")); |
30
|
|
|
} |
31
|
41 |
|
}); |
32
|
41 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Register the cors function and set defaults |
36
|
|
|
* |
37
|
|
|
* @param Container $app |
38
|
|
|
*/ |
39
|
41 |
|
public function register(Application $app) |
40
|
|
|
{ |
41
|
41 |
|
$app["cors.allowOrigin"] = "*"; // Defaults to all |
42
|
41 |
|
$app["cors.allowMethods"] = null; // Defaults to all |
43
|
41 |
|
$app["cors.allowHeaders"] = null; // Defaults to all |
44
|
41 |
|
$app["cors.maxAge"] = null; |
45
|
41 |
|
$app["cors.allowCredentials"] = null; |
46
|
41 |
|
$app["cors.exposeHeaders"] = null; |
47
|
|
|
|
48
|
41 |
|
$app["allow"] = $app->protect(new Allow()); |
49
|
|
|
|
50
|
|
|
$app["options"] = $app->protect(function ($subject) use ($app) { |
51
|
41 |
|
$optionsController = function () { |
52
|
23 |
|
return Response::create("", 204); |
53
|
41 |
|
}; |
54
|
|
|
|
55
|
41 |
|
if ($subject instanceof Controller) { |
56
|
4 |
|
$optionsRoute = $app->match($subject->getRoute()->getPath(), $optionsController) |
57
|
4 |
|
->method("OPTIONS") |
58
|
4 |
|
->after($app["allow"]); |
59
|
|
|
} else { |
60
|
37 |
|
$optionsRoute = $subject->match("{path}", $optionsController) |
61
|
37 |
|
->method("OPTIONS") |
62
|
37 |
|
->after($app["allow"]) |
63
|
37 |
|
->assert("path", ".*"); |
64
|
|
|
} |
65
|
|
|
|
66
|
41 |
|
return $optionsRoute; |
67
|
41 |
|
}); |
68
|
|
|
|
69
|
41 |
|
$app["cors-enabled"] = $app->protect(function ($subject, $config = []) use ($app) { |
70
|
38 |
|
$optionsController = $app["options"]($subject); |
71
|
38 |
|
$cors = new Cors($app, $config); |
72
|
|
|
|
73
|
38 |
|
if ($subject instanceof Controller) { |
74
|
4 |
|
$optionsController->after($cors); |
75
|
|
|
} |
76
|
|
|
|
77
|
38 |
|
$subject->after($cors); |
78
|
|
|
|
79
|
38 |
|
return $subject; |
80
|
41 |
|
}); |
81
|
|
|
|
82
|
|
|
$app["cors"] = function () use ($app) { |
83
|
|
|
$app["options"]($app); |
84
|
|
|
return new Cors(); |
|
|
|
|
85
|
|
|
}; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check looks for function calls that miss required arguments.