1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Phraseanet |
4
|
|
|
* |
5
|
|
|
* (c) 2005-2015 Alchemy |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Alchemy\RestProvider; |
11
|
|
|
|
12
|
|
|
use Alchemy\Rest\Request\ContentTypeMatcher; |
13
|
|
|
use Alchemy\Rest\Request\DateParser\FormatDateParser; |
14
|
|
|
use Alchemy\Rest\Response\ExceptionTransformer\DefaultExceptionTransformer; |
15
|
|
|
use Alchemy\RestBundle\EventListener; |
16
|
|
|
use Alchemy\RestBundle\Rest\Request\PaginationOptionsFactory; |
17
|
|
|
use Alchemy\RestBundle\Rest\Request\SortOptionsFactory; |
18
|
|
|
use Alchemy\RestProvider\Middleware; |
19
|
|
|
use Negotiation\Negotiator; |
20
|
|
|
use Silex\Application; |
21
|
|
|
use Silex\ServiceProviderInterface; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
|
25
|
|
|
class RestProvider implements ServiceProviderInterface |
26
|
|
|
{ |
27
|
|
|
public function register(Application $app) |
28
|
|
|
{ |
29
|
|
|
$app['alchemy_rest.debug'] = false; |
30
|
|
|
|
31
|
|
|
$this->registerContentTypeMatcher($app); |
32
|
|
|
$this->registerExceptionListener($app); |
33
|
|
|
$this->registerDateListener($app); |
34
|
|
|
$this->registerPaginationListener($app); |
35
|
|
|
$this->registerSortListener($app); |
36
|
|
|
|
37
|
|
|
$app->register(new TransformerServiceProvider()); |
38
|
|
|
$app->register(new MiddlewareServiceProvider()); |
39
|
|
|
|
40
|
|
|
$app['dispatcher'] = $app->share( |
41
|
|
|
$app->extend('dispatcher', function (EventDispatcherInterface $dispatcher) use ($app) { |
42
|
|
|
$this->bindRequestListeners($app, $dispatcher); |
43
|
|
|
|
44
|
|
|
// Bind exception |
45
|
|
|
$dispatcher->addSubscriber($app['alchemy_rest.exception_listener']); |
46
|
|
|
// This block must be called after all other result listeners |
47
|
|
|
$dispatcher->addSubscriber($app['alchemy_rest.transform_response_listener']); |
48
|
|
|
$dispatcher->addSubscriber($app['alchemy_rest.encode_response_listener']); |
49
|
|
|
|
50
|
|
|
return $dispatcher; |
51
|
|
|
}) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function bindRequestListeners(Application $app, EventDispatcherInterface $dispatcher) |
56
|
|
|
{ |
57
|
|
|
$dispatcher->addSubscriber($app['alchemy_rest.decode_request_listener']); |
58
|
|
|
$dispatcher->addSubscriber($app['alchemy_rest.paginate_request_listener']); |
59
|
|
|
$dispatcher->addSubscriber($app['alchemy_rest.sort_request_listener']); |
60
|
|
|
$dispatcher->addSubscriber($app['alchemy_rest.date_request_listener']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function boot(Application $app) |
64
|
|
|
{ |
65
|
|
|
// Nothing to do. |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function registerPaginationListener(Application $app) |
69
|
|
|
{ |
70
|
|
|
$app['alchemy_rest.paginate_options.offset_parameter'] = 'offset'; |
71
|
|
|
$app['alchemy_rest.paginate_options.limit_parameter'] = 'limit'; |
72
|
|
|
$app['alchemy_rest.paginate_options_factory'] = $app->share(function () use ($app) { |
73
|
|
|
return new PaginationOptionsFactory( |
74
|
|
|
$app['alchemy_rest.paginate_options.offset_parameter'], |
75
|
|
|
$app['alchemy_rest.paginate_options.limit_parameter'] |
76
|
|
|
); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
$app['alchemy_rest.paginate_request_listener'] = $app->share(function () use ($app) { |
80
|
|
|
return new EventListener\PaginationParamRequestListener($app['alchemy_rest.paginate_options_factory']); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function registerSortListener(Application $app) |
85
|
|
|
{ |
86
|
|
|
$app['alchemy_rest.sort_options.sort_parameter'] = 'sort'; |
87
|
|
|
$app['alchemy_rest.sort_options.direction_parameter'] = 'dir'; |
88
|
|
|
$app['alchemy_rest.sort_options.multi_sort_parameter'] = 'sorts'; |
89
|
|
|
$app['alchemy_rest.sort_options_factory'] = $app->share(function () use ($app) { |
90
|
|
|
return new SortOptionsFactory( |
91
|
|
|
$app['alchemy_rest.sort_options.sort_parameter'], |
92
|
|
|
$app['alchemy_rest.sort_options.direction_parameter'], |
93
|
|
|
$app['alchemy_rest.sort_options.multi_sort_parameter'] |
94
|
|
|
); |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
$app['alchemy_rest.sort_request_listener'] = $app->share(function () use ($app) { |
98
|
|
|
return new EventListener\SortParamRequestListener($app['alchemy_rest.sort_options_factory']); |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function registerDateListener(Application $app) |
103
|
|
|
{ |
104
|
|
|
$app['alchemy_rest.date_parser.timezone'] = 'UTC'; |
105
|
|
|
$app['alchemy_rest.date_parser.format'] = 'Y-m-d H:i:s'; |
106
|
|
|
$app['alchemy_rest.date_parser'] = $app->share(function () use ($app) { |
107
|
|
|
return new FormatDateParser( |
108
|
|
|
$app['alchemy_rest.date_parser.timezone'], |
109
|
|
|
$app['alchemy_rest.date_parser.format'] |
110
|
|
|
); |
111
|
|
|
}); |
112
|
|
|
|
113
|
|
|
$app['alchemy_rest.date_request_listener'] = $app->share(function () use ($app) { |
114
|
|
|
return new EventListener\DateParamRequestListener($app['alchemy_rest.date_parser']); |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function registerExceptionListener(Application $app) |
119
|
|
|
{ |
120
|
|
|
$app['alchemy_rest.exception_transformer'] = $app->share(function () use ($app) { |
121
|
|
|
return new DefaultExceptionTransformer($app['alchemy_rest.debug']); |
122
|
|
|
}); |
123
|
|
|
|
124
|
|
|
$app['alchemy_rest.exception_handling_content_types'] = array('application/json'); |
125
|
|
|
$app['alchemy_rest.exception_listener'] = $app->share(function () use ($app) { |
126
|
|
|
return new EventListener\ExceptionListener( |
127
|
|
|
$app['alchemy_rest.content_type_matcher'], |
128
|
|
|
$app['alchemy_rest.exception_transformer'], |
129
|
|
|
$app['alchemy_rest.exception_handling_content_types'] |
130
|
|
|
); |
131
|
|
|
}); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function registerContentTypeMatcher(Application $app) |
135
|
|
|
{ |
136
|
|
|
$app['alchemy_rest.negotiator'] = $app->share(function () use ($app) { |
137
|
|
|
return new Negotiator(); |
138
|
|
|
}); |
139
|
|
|
$app['alchemy_rest.content_type_matcher'] = $app->share(function () use ($app) { |
140
|
|
|
return new ContentTypeMatcher($app['alchemy_rest.negotiator']); |
141
|
|
|
}); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|