|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JDesrosiers\Silex\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Pimple\Container; |
|
6
|
|
|
use Pimple\ServiceProviderInterface; |
|
7
|
|
|
use Silex\Api\BootableProviderInterface; |
|
8
|
|
|
use Silex\Application; |
|
9
|
|
|
use Silex\Controller; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
|
13
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
14
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The CORS service provider provides a `cors` service that a can be included in your project as application middleware. |
|
18
|
|
|
*/ |
|
19
|
|
|
class CorsServiceProvider implements ServiceProviderInterface, BootableProviderInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Add OPTIONS method support for all routes |
|
23
|
|
|
* |
|
24
|
|
|
* @param Container $app |
|
25
|
|
|
*/ |
|
26
|
41 |
|
public function boot(Application $app) |
|
27
|
|
|
{ |
|
28
|
41 |
|
$app->on(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) { |
|
29
|
5 |
|
$e = $event->getException(); |
|
30
|
5 |
|
if ($e instanceof MethodNotAllowedHttpException && $e->getHeaders()["Allow"] === "OPTIONS") { |
|
31
|
1 |
|
$event->setException(new NotFoundHttpException("No route found for \"{$event->getRequest()->getMethod()} {$event->getRequest()->getPathInfo()}\"")); |
|
32
|
1 |
|
} |
|
33
|
41 |
|
}); |
|
34
|
41 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Register the cors function and set defaults |
|
38
|
|
|
* |
|
39
|
|
|
* @param Container $app |
|
40
|
|
|
*/ |
|
41
|
41 |
|
public function register(Container $app) |
|
42
|
|
|
{ |
|
43
|
41 |
|
$app["cors.allowOrigin"] = "*"; // Defaults to all |
|
44
|
41 |
|
$app["cors.allowMethods"] = null; // Defaults to all |
|
45
|
41 |
|
$app["cors.allowHeaders"] = null; // Defaults to all |
|
46
|
41 |
|
$app["cors.maxAge"] = null; |
|
47
|
41 |
|
$app["cors.allowCredentials"] = null; |
|
48
|
41 |
|
$app["cors.exposeHeaders"] = null; |
|
49
|
|
|
|
|
50
|
41 |
|
$app["allow"] = $app->protect(new Allow()); |
|
51
|
|
|
|
|
52
|
|
|
$app["options"] = $app->protect(function ($subject) use ($app) { |
|
53
|
41 |
|
$optionsController = function () { |
|
54
|
23 |
|
return Response::create("", 204); |
|
55
|
41 |
|
}; |
|
56
|
|
|
|
|
57
|
41 |
|
if ($subject instanceof Controller) { |
|
58
|
4 |
|
$optionsRoute = $app->options($subject->getRoute()->getPath(), $optionsController) |
|
|
|
|
|
|
59
|
4 |
|
->after($app["allow"]); |
|
60
|
4 |
|
} else { |
|
61
|
37 |
|
$optionsRoute = $subject->options("{path}", $optionsController) |
|
62
|
37 |
|
->after($app["allow"]) |
|
63
|
37 |
|
->assert("path", ".*"); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
41 |
|
return $optionsRoute; |
|
67
|
41 |
|
}); |
|
68
|
|
|
|
|
69
|
|
|
$app["cors-enabled"] = $app->protect(function ($subject, $config = []) use ($app) { |
|
70
|
38 |
|
$optionsController = $app["options"]($subject); |
|
71
|
38 |
|
$cors = new Cors($config); |
|
72
|
|
|
|
|
73
|
38 |
|
if ($subject instanceof Controller) { |
|
74
|
4 |
|
$optionsController->after($cors); |
|
75
|
4 |
|
} |
|
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
|
41 |
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: