10
10
use CloudCreativity \JsonApi \Contracts \Config \EncoderOptionsRepositoryInterface ;
11
11
use CloudCreativity \JsonApi \Contracts \Config \EncodersRepositoryInterface ;
12
12
use CloudCreativity \JsonApi \Contracts \Config \SchemasRepositoryInterface ;
13
- use CloudCreativity \JsonApi \Exceptions \RenderContainer ;
14
- use CloudCreativity \JsonApi \Integration \LaravelIntegration ;
13
+ use CloudCreativity \JsonApi \Contracts \Stdlib \ConfigurableInterface ;
15
14
use CloudCreativity \JsonApi \Error \ExceptionThrower ;
16
- use CloudCreativity \JsonApi \Config \Config as C ;
17
- use CloudCreativity \JsonApi \Http \Middleware \Middleware as M ;
15
+ use CloudCreativity \JsonApi \Exceptions \RenderContainer ;
18
16
use CloudCreativity \JsonApi \Http \Middleware \InitCodecMatcher ;
17
+ use CloudCreativity \JsonApi \Integration \LaravelIntegration ;
18
+ use CloudCreativity \JsonApi \Keys as C ;
19
19
use Illuminate \Contracts \Config \Repository ;
20
- use Illuminate \Contracts \Container \Container ;
21
20
use Illuminate \Contracts \Http \Kernel ;
22
21
use Illuminate \Routing \Router ;
23
22
use Illuminate \Support \ServiceProvider as BaseServiceProvider ;
23
+ use Neomerx \JsonApi \Contracts \Exceptions \RenderContainerInterface ;
24
24
use Neomerx \JsonApi \Contracts \Factories \FactoryInterface ;
25
- use Neomerx \JsonApi \Factories \Factory ;
26
25
use Neomerx \JsonApi \Contracts \Integration \CurrentRequestInterface ;
26
+ use Neomerx \JsonApi \Contracts \Integration \ExceptionThrowerInterface ;
27
27
use Neomerx \JsonApi \Contracts \Integration \NativeResponsesInterface ;
28
28
use Neomerx \JsonApi \Contracts \Responses \ResponsesInterface ;
29
- use Neomerx \JsonApi \Contracts \Integration \ExceptionThrowerInterface ;
30
- use Neomerx \JsonApi \Contracts \Exceptions \RenderContainerInterface ;
29
+ use Neomerx \JsonApi \Factories \Factory ;
31
30
use Neomerx \JsonApi \Responses \Responses ;
32
31
33
32
/**
@@ -50,10 +49,10 @@ class ServiceProvider extends BaseServiceProvider
50
49
public function boot (Router $ router , Repository $ repository , Kernel $ kernel )
51
50
{
52
51
// Add Json Api middleware to the router.
53
- $ router ->middleware (M:: JSON_API , InitCodecMatcher::class);
52
+ $ router ->middleware (C:: NAME , InitCodecMatcher::class);
54
53
55
54
// If the whole application is set to be a Json Api, push the init middleware into the kernel.
56
- $ key = sprintf ('%s.%s ' , C::KEY , C::IS_GLOBAL );
55
+ $ key = sprintf ('%s.%s ' , C::NAME , C::IS_GLOBAL );
57
56
$ global = $ repository ->get ($ key , false );
58
57
59
58
if (true === $ global && method_exists ($ kernel , 'pushMiddleware ' )) {
@@ -68,47 +67,44 @@ public function boot(Router $router, Repository $repository, Kernel $kernel)
68
67
*/
69
68
public function register ()
70
69
{
70
+ $ container = $ this ->app ;
71
+
71
72
// Factory
72
- $ this -> app ->singleton (FactoryInterface::class, Factory::class);
73
+ $ container ->singleton (FactoryInterface::class, Factory::class);
73
74
74
75
// Schemas Repository
75
- $ this ->app ->singleton (SchemasRepositoryInterface::class, function (Container $ container ) {
76
- /** @var Repository $config */
77
- $ config = $ container ->make ('config ' );
78
- $ key = sprintf ('%s.%s ' , C::KEY , C::SCHEMAS );
79
- return new SchemasRepository ((array ) $ config ->get ($ key ));
76
+ $ container ->singleton (SchemasRepositoryInterface::class, SchemasRepository::class);
77
+ $ container ->resolving (SchemasRepositoryInterface::class, function (ConfigurableInterface $ repository ) {
78
+ $ repository ->configure ($ this ->getConfig (C::SCHEMAS ));
80
79
});
81
80
82
81
// Encoder Options Repository
83
- $ this ->app ->singleton (EncoderOptionsRepositoryInterface::class, function (Container $ container ) {
84
- /** @var Repository $config */
85
- $ config = $ container ->make ('config ' );
86
- $ key = sprintf ('%s.%s ' , C::KEY , C::ENCODER_OPTIONS );
87
- return new EncoderOptionsRepository ((array ) $ config ->get ($ key ));
82
+ $ container ->singleton (EncoderOptionsRepositoryInterface::class, EncoderOptionsRepository::class);
83
+ $ container ->resolving (EncoderOptionsRepositoryInterface::class, function (ConfigurableInterface $ repository ) {
84
+ $ repository ->configure ($ this ->getConfig (C::ENCODER_OPTIONS ));
88
85
});
89
86
90
87
// Encoders Repository
91
- $ this -> app ->singleton (EncodersRepositoryInterface::class, EncodersRepository::class);
88
+ $ container ->singleton (EncodersRepositoryInterface::class, EncodersRepository::class);
92
89
93
90
// Codec Matcher Repository
94
- $ this ->app ->singleton (CodecMatcherRepositoryInterface::class, CodecMatcherRepository::class);
91
+ $ container ->singleton (CodecMatcherRepositoryInterface::class, CodecMatcherRepository::class);
92
+ $ container ->resolving (CodecMatcherRepositoryInterface::class, function (ConfigurableInterface $ repository ) {
93
+ $ repository ->configure ($ this ->getConfig (C::CODEC_MATCHER ));
94
+ });
95
95
96
96
// Laravel Integration
97
- $ this -> app ->alias (CurrentRequestInterface::class, LaravelIntegration::class);
98
- $ this -> app ->alias (NativeResponsesInterface::class, LaravelIntegration::class);
99
- $ this -> app ->singleton (ResponsesInterface::class, Responses::class);
97
+ $ container ->alias (CurrentRequestInterface::class, LaravelIntegration::class);
98
+ $ container ->alias (NativeResponsesInterface::class, LaravelIntegration::class);
99
+ $ container ->singleton (ResponsesInterface::class, Responses::class);
100
100
101
101
// Exception Thrower
102
- $ this -> app ->singleton (ExceptionThrowerInterface::class, ExceptionThrower::class);
102
+ $ container ->singleton (ExceptionThrowerInterface::class, ExceptionThrower::class);
103
103
104
104
// Exception Render Container
105
- $ this ->app ->singleton (RenderContainerInterface::class, function (Container $ container ) {
106
- /** @var Repository $config */
107
- $ config = $ container ->make ('config ' );
108
- $ key = sprintf ('%s.%s ' , C::KEY , C::EXCEPTION_RENDER_CONTAINER );
109
- $ renderContainer = new RenderContainer ();
110
- $ renderContainer ->configure ((array ) $ config ->get ($ key ));
111
- return $ renderContainer ;
105
+ $ container ->singleton (RenderContainerInterface::class, RenderContainer::class);
106
+ $ container ->resolving (RenderContainerInterface::class, function (ConfigurableInterface $ renderContainer ) {
107
+ $ renderContainer ->configure ($ this ->getConfig (C::EXCEPTIONS ));
112
108
});
113
109
}
114
110
@@ -129,4 +125,18 @@ public function provides()
129
125
RenderContainerInterface::class,
130
126
];
131
127
}
128
+
129
+ /**
130
+ * @param $key
131
+ * @return array
132
+ */
133
+ protected function getConfig ($ key )
134
+ {
135
+ /** @var Repository $config */
136
+ $ config = $ this ->app ->make ('config ' );
137
+ $ key = sprintf ('%s.%s ' , C::NAME , $ key );
138
+
139
+ return (array ) $ config ->get ($ key );
140
+ }
141
+
132
142
}
0 commit comments