1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alchemy\RestProvider; |
4
|
|
|
|
5
|
|
|
use Alchemy\Rest\Response\ArrayTransformer; |
6
|
|
|
use Alchemy\RestBundle\EventListener; |
7
|
|
|
use League\Fractal\Manager; |
8
|
|
|
use Silex\Application; |
9
|
|
|
use Silex\ServiceProviderInterface; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
|
13
|
|
|
class TransformerServiceProvider implements ServiceProviderInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
private $subscriberKeys = []; |
17
|
|
|
|
18
|
|
|
public function register(Application $app) |
19
|
|
|
{ |
20
|
|
|
$this->registerResponseListener($app); |
21
|
|
|
|
22
|
|
|
$this->registerRequestDecoder($app); |
23
|
|
|
$this->registerResponseEncoder($app); |
24
|
|
|
|
25
|
|
|
$app['dispatcher'] = $app->share( |
26
|
|
|
$app->extend('dispatcher', function (EventDispatcherInterface $dispatcher) use ($app) { |
27
|
|
|
$this->bindResultListeners($app, $dispatcher); |
28
|
|
|
|
29
|
|
|
return $dispatcher; |
30
|
|
|
}) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function shareEventListener(Application $app, $key, callable $factory) |
35
|
|
|
{ |
36
|
|
|
$app[$key] = $app->share($factory); |
37
|
|
|
|
38
|
|
|
$this->subscriberKeys[] = $key; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function bindResultListeners(Application $app, EventDispatcherInterface $dispatcher) |
42
|
|
|
{ |
43
|
|
|
foreach ($this->subscriberKeys as $subscriberKey) { |
44
|
|
|
$dispatcher->addSubscriber($app[$subscriberKey]); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function boot(Application $app) |
49
|
|
|
{ |
50
|
|
|
// no-op |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Application $app |
55
|
|
|
* @return Application |
56
|
|
|
*/ |
57
|
|
|
private function registerRequestDecoder(Application $app) |
58
|
|
|
{ |
59
|
|
|
$app['alchemy_rest.decode_request_content_types'] = array('application/json'); |
60
|
|
|
|
61
|
|
|
$app['alchemy_rest.decode_request_listener'] = $app->share(function () use ($app) { |
62
|
|
|
return new EventListener\DecodeJsonBodyRequestListener($app['alchemy_rest.content_type_matcher']); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
$app['alchemy_rest.request_decoder'] = $app->protect(function (Request $request) use ($app) { |
66
|
|
|
$app['alchemy_rest.decode_request_listener']->decodeBody($request); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Application $app |
72
|
|
|
* @return Application |
73
|
|
|
*/ |
74
|
|
|
private function registerResponseEncoder(Application $app) |
75
|
|
|
{ |
76
|
|
|
$app['alchemy_rest.encode_response_listener'] = $app->share(function () use ($app) { |
77
|
|
|
return new EventListener\EncodeJsonResponseListener(); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Application $app |
83
|
|
|
* @return Application |
84
|
|
|
*/ |
85
|
|
|
private function registerResponseListener(Application $app) |
86
|
|
|
{ |
87
|
|
|
$this->shareEventListener($app, 'alchemy_rest.transform_success_result_listener', function () { |
88
|
|
|
return new EventListener\SuccessResultListener(); |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
$this->shareEventListener($app, 'alchemy_rest.transform_bad_request_listener', function () { |
92
|
|
|
return new EventListener\BadRequestListener(); |
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
$this->shareEventListener($app, 'alchemy_rest.transform_request_accepted_listener', function () { |
96
|
|
|
return new EventListener\RequestAcceptedListener(); |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
$this->shareEventListener($app, 'alchemy_rest.transform_resource_created_listener', function () use ($app) { |
100
|
|
|
return new EventListener\ResourceCreatedListener($app['alchemy_rest.array_transformer']); |
101
|
|
|
}); |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
$app['alchemy_rest.fractal_manager'] = $app->share(function () { |
105
|
|
|
return new Manager(); |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
|
$app['alchemy_rest.transformers_registry'] = $app->share(function () { |
109
|
|
|
return new \Pimple(); |
110
|
|
|
}); |
111
|
|
|
|
112
|
|
|
$app['alchemy_rest.array_transformer'] = $app->share(function () use ($app) { |
113
|
|
|
return new ArrayTransformer( |
114
|
|
|
$app['alchemy_rest.fractal_manager'], |
115
|
|
|
$app['alchemy_rest.transformers_registry'] |
116
|
|
|
); |
117
|
|
|
}); |
118
|
|
|
|
119
|
|
|
$app['alchemy_rest.transform_response_listener'] = $app->share(function () use ($app) { |
120
|
|
|
return new EventListener\TransformResponseListener( |
121
|
|
|
$app['alchemy_rest.array_transformer'], |
122
|
|
|
$app['url_generator'] |
123
|
|
|
); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|