|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Eole\RestApi; |
|
4
|
|
|
|
|
5
|
|
|
use Eole\Silex\Application as BaseApplication; |
|
6
|
|
|
|
|
7
|
|
|
class Application extends BaseApplication |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* {@InheritDoc} |
|
11
|
|
|
*/ |
|
12
|
|
|
public function __construct(array $values = array()) |
|
13
|
|
|
{ |
|
14
|
|
|
parent::__construct($values); |
|
15
|
|
|
|
|
16
|
|
|
$this->registerServices(); |
|
|
|
|
|
|
17
|
|
|
$this->registerEventListeners(); |
|
18
|
|
|
$this->mountOAuth2Controller(); |
|
19
|
|
|
$this->loadRestApis(); |
|
20
|
|
|
$this->handleErrors(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Register RestApi services |
|
25
|
|
|
*/ |
|
26
|
|
|
private function registerServices() |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
$this['eole.api_response_filter'] = function () { |
|
29
|
|
|
return new \Alcalyn\SerializableApiResponse\ApiResponseFilter( |
|
30
|
|
|
$this['serializer'] |
|
31
|
|
|
); |
|
32
|
|
|
}; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function registerEventListeners() |
|
36
|
|
|
{ |
|
37
|
|
|
$corsOrigin = $this['environment']['cors']['access_control_allow_origin']; |
|
38
|
|
|
|
|
39
|
|
|
if ($corsOrigin) { |
|
40
|
|
|
$this->register(new \JDesrosiers\Silex\Provider\CorsServiceProvider(), array( |
|
41
|
|
|
'cors.allowOrigin' => $corsOrigin, |
|
42
|
|
|
)); |
|
43
|
|
|
|
|
44
|
|
|
$this->after($this['cors']); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->on(\Symfony\Component\HttpKernel\KernelEvents::VIEW, function ($event) { |
|
48
|
|
|
$this['eole.api_response_filter']->onKernelView($event); |
|
49
|
|
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Mount /oauth |
|
54
|
|
|
*/ |
|
55
|
|
|
private function mountOAuth2Controller() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->mount('oauth', new ControllerProvider\OAuth2ControllerProvider()); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Mount Eole and games RestApi endpoints. |
|
62
|
|
|
*/ |
|
63
|
|
|
private function loadRestApis() |
|
64
|
|
|
{ |
|
65
|
|
|
foreach ($this['environment']['mods'] as $modName => $modConfig) { |
|
66
|
|
|
$modClass = $modConfig['provider']; |
|
67
|
|
|
$mod = new $modClass(); |
|
68
|
|
|
$provider = $mod->createControllerProvider(); |
|
69
|
|
|
$prefix = 'api'; |
|
70
|
|
|
|
|
71
|
|
|
if ($mod instanceof \Eole\Silex\GameProvider) { |
|
72
|
|
|
$prefix = 'api/games/'.$modName; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($provider instanceof \Pimple\ServiceProviderInterface) { |
|
76
|
|
|
$this->register($provider); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($provider instanceof \Silex\Api\ControllerProviderInterface) { |
|
80
|
|
|
$this->mount($prefix, $provider); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Handle errors to display json exception. |
|
87
|
|
|
*/ |
|
88
|
|
|
private function handleErrors() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->error(function (\Exception $e) { |
|
91
|
|
|
// Returns Api response on HttpException. |
|
92
|
|
|
if ($e instanceof \Symfony\Component\HttpKernel\Exception\HttpException) { |
|
93
|
|
|
$errorData = [ |
|
94
|
|
|
'status_code' => $e->getStatusCode(), |
|
95
|
|
|
'message' => $e->getMessage(), |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
return new \Alcalyn\SerializableApiResponse\ApiResponse($errorData, $errorData['status_code']); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Hide internal exception if no debug. |
|
102
|
|
|
if (!$this['debug']) { |
|
103
|
|
|
$errorData = [ |
|
104
|
|
|
'status_code' => 500, |
|
105
|
|
|
'message' => 'Internal Server Error.', |
|
106
|
|
|
]; |
|
107
|
|
|
|
|
108
|
|
|
return new \Alcalyn\SerializableApiResponse\ApiResponse($errorData, $errorData['status_code']); |
|
109
|
|
|
} |
|
110
|
|
|
}); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: