8000 Update configuration and add examples. · dtr0yan/laravel-json-api@6ed2a04 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ed2a04

Browse files
committed
Update configuration and add examples.
1 parent 770f0e7 commit 6ed2a04

File tree

6 files changed

+218
-58
lines changed

6 files changed

+218
-58
lines changed

config/json-api-advanced-example.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
use CloudCreativity\JsonApi\Contracts\Config\CodecMatcherRepositoryInterface as CM;
4+
use CloudCreativity\JsonApi\Contracts\Config\EncoderOptionsRepositoryInterface as Enc;
5+
use CloudCreativity\JsonApi\Contracts\Config\SchemasRepositoryInterface as Sch;
6+
use CloudCreativity\JsonApi\Exceptions\RenderContainer as Ex;
7+
use CloudCreativity\JsonApi\Keys as C;
8+
9+
return [
10+
11+
/**
12+
* Whether every route in the application is a JSON API endpoint.
13+
*
14+
* If true, will install the 'json-api' middleware on the HTTP Kernel.
15+
*/
16+
C::IS_GLOBAL => true,
17+
18+
/**
19+
* Codec Matcher
20+
*/
21+
C::CODEC_MATCHER => [
22+
// The default codec matcher.
23+
CM::DEFAULTS => [
24+
CM::ENCODERS => [
25+
// These will use the default schemas and encoder options
26+
'application/vnd.api+json',
27+
'application/vnd.api+json;charset=utf-8',
28+
],
29+
CM::DECODERS => [
30+
// These will use the default decoder.
31+
'application/vnd.api+json',
32+
'application/vnd.api+json;charset=utf-8',
33+
],
34+
],
35+
// A codec matcher that adds support for 'application/json' to the defaults.
36+
'with-json' => [
37+
CM::ENCODERS => [
38+
'application/json' => [
39+
// Use the schemas known as 'extras'
40+
CM::ENCODER_SCHEMAS => 'extras',
41+
// Use the encoder options known as 'json' instead of the default encoder options.
42+
CM::ENCODER_OPTIONS => 'json',
43+
],
44+
],
45+
],
46+
// A codec matcher that adds text encoding to the defaults.
47+
'humanized' => [
48+
CM::ENCODERS => [
49+
'text/plain' => [
50+
CM::ENCODER_OPTIONS => 'pretty-print',
51+
],
52+
],
53+
],
54+
],
55+
56+
/**
57+
* Encoder Options
58+
*/
59+
C::ENCODER_OPTIONS => [
60+
Enc::DEFAULTS => [
61+
Enc::IS_SHOW_VERSION_INFO => true,
62+
Enc::VERSION_META => [
63+
'version' => '1.0',
64+
],
65+
],
66+
// these named options override the defaults.
67+
'with-json' => [
68+
Enc::OPTIONS => JSON_BIGINT_AS_STRING,
69+
],
70+
'pretty-print' => [
71+
Enc::OPTIONS => JSON_PRETTY_PRINT,
72+
// recursively merged on top
73+
Enc::VERSION_META => [
74+
'hello' => 'world!',
75+
],
76+
],
77+
],
78+
79+
/**
80+
* Schemas
81+
*/
82+
C::SCHEMAS => [
83+
Sch::DEFAULTS => [
84+
'Author' => 'AuthorSchema',
85+
'Post' => 'PostSchema',
86+
'Comment' => 'CommentSchema',
87+
],
88+
// These schemas are added to the defaults
89+
'extras' => [
90+
'Tag' => 'TagSchema',
91+
],
92+
],
93+
94+
/**
95+
* Exception Render Container
96+
*/
97+
C::EXCEPTIONS => [
98+
Ex::HTTP_CODE_MAPPING => [
99+
'SomeExceptionClass' => 422,
100+
],
101+
],
102+
];

config/json-api-example.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
use CloudCreativity\JsonApi\Keys as C;
4+
use CloudCreativity\JsonApi\Contracts\Config\CodecMatcherRepositoryInterface as CM;
5+
use CloudCreativity\JsonApi\Contracts\Config\EncoderOptionsRepositoryInterface as Enc;
6+
use CloudCreativity\JsonApi\Exceptions\RenderContainer as Ex;
7+
8+
return [
9+
10+
/**
11+
* Whether every route in the application is a JSON API endpoint.
12+
*
13+
* If true, will install the 'json-api' middleware on the HTTP Kernel.
14+
*/
15+
C::IS_GLOBAL => true,
16+
17+
/**
18+
* Codec Matcher
19+
*/
20+
C::CODEC_MATCHER => [
21+
CM::ENCODERS => [
22+
'application/vnd.api+json',
23+
'application/vnd.api+json;charset=utf-8',
24+
'application/json',
25+
],
26+
CM::DECODERS => [
27+
'application/vnd.api+json',
28+
'application/vnd.api+json;charset=utf-8',
29+
],
30+
],
31+
32+
/**
33+
* Encoder Options
34+
*/
35+
C::ENCODER_OPTIONS => [
36+
Enc::OPTIONS => JSON_PRETTY_PRINT,
37+
Enc::DEPTH => 250,
38+
],
39+
40+
/**
41+
* Schemas
42+
*/
43+
C::SCHEMAS => [
44+
'Author' => 'AuthorSchema',
45+
'Post' => 'PostSchema',
46+
'Comment' => 'CommentSchema',
47+
],
48+
49+
/**
50+
* Exception Render Container
51+
*/
52+
C::EXCEPTIONS => [
53+
Ex::HTTP_CODE_MAPPING => [
54+
'SomeExceptionClass' => 422,
55+
],
56+
],
57+
];

src/Http/Middleware/InitCodecMatcher.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,38 @@
77
use Illuminate\Http\Request;
88
use Neomerx\JsonApi\Contracts\Codec\CodecMatcherInterface;
99

10+
/**
11+
* Class InitCodecMatcher
12+
* @package CloudCreativity\JsonApi
13+
*/
1014
class InitCodecMatcher
1115
{
1216

1317
/**
1418
* @var Application
1519
*/
16-
private $_application;
20+
private $application;
1721

1822
/**
1923
* @param Application $application
2024
*/
2125
public function __construct(Application $application)
2226
{
23-
$this->_application = $application;
27+
$this->application = $application;
2428
}
2529

2630
/**
2731
* @param Request $request
2832
* @param \Closure $next
33+
* @param string|null $codecMatcherName
2934
* @return mixed
3035
*/
3136
public function handle(Request $request, \Closure $next, $codecMatcherName = null)
3237
{
3338
/** @var CodecMatcherRepositoryInterface $repository */
34-
$repository = $this->_application->make(CodecMatcherRepositoryInterface::class);
35-
$codecMatcher = $repository->get($codecMatcherName ?: CodecMatcherRepositoryInterface::DEFAULTS);
36-
$this->_application->instance(CodecMatcherInterface::class, $codecMatcher);
39+
$repository = $this->application->make(CodecMatcherRepositoryInterface::class);
40+
$codecMatcher = $repository->getCodecMatcher($codecMatcherName);
41+
$this->application->instance(CodecMatcherInterface::class, $codecMatcher);
3742

3843
return $next($request);
3944
}

src/Http/Middleware/Middleware.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Config/Config.php renamed to src/Keys.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22

3-
namespace CloudCreativity\JsonApi\Config;
3+
namespace CloudCreativity\JsonApi;
44

55
/**
66
* Class Config
77
* @package CloudCreativity\JsonApi
88
*/
9-
class Config
9+
class Keys
1010
{
1111

12-
/** The config file name. */
13-
const KEY = 'json-api';
12+
/** The config file name, and the name of the route middleware. */
13+
const NAME = 'json-api';
1414

1515
/** If a true boolean, Json Api support will be initialised for the entire application. */
1616
const IS_GLOBAL = 'is-global';
@@ -25,5 +25,5 @@ class Config
2525
const CODEC_MATCHER = 'codec-matcher';
2626

2727
/** The key for Exception Render Container config */
28-
const EXCEPTION_RENDER_CONTAINER = 'exception-render-container';
28+
const EXCEPTIONS = 'exceptions';
2929
}

src/JsonApiProvider.php renamed to src/ServiceProvider.php

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,23 @@
1010
use CloudCreativity\JsonApi\Contracts\Config\EncoderOptionsRepositoryInterface;
1111
use CloudCreativity\JsonApi\Contracts\Config\EncodersRepositoryInterface;
1212
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;
1514
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;
1816
use CloudCreativity\JsonApi\Http\Middleware\InitCodecMatcher;
17+
use CloudCreativity\JsonApi\Integration\LaravelIntegration;
18+
use CloudCreativity\JsonApi\Keys as C;
1919
use Illuminate\Contracts\Config\Repository;
20-
use Illuminate\Contracts\Container\Container;
2120
use Illuminate\Contracts\Http\Kernel;
2221
use Illuminate\Routing\Router;
2322
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
23+
use Neomerx\JsonApi\Contracts\Exceptions\RenderContainerInterface;
2424
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
25-
use Neomerx\JsonApi\Factories\Factory;
2625
use Neomerx\JsonApi\Contracts\Integration\CurrentRequestInterface;
26+
use Neomerx\JsonApi\Contracts\Integration\ExceptionThrowerInterface;
2727
use Neomerx\JsonApi\Contracts\Integration\NativeResponsesInterface;
2828
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;
3130
use Neomerx\JsonApi\Responses\Responses;
3231

3332
/**
@@ -50,10 +49,10 @@ class ServiceProvider extends BaseServiceProvider
5049
public function boot(Router $router, Repository $repository, Kernel $kernel)
5150
{
5251
// Add Json Api middleware to the router.
53-
$router->middleware(M::JSON_API, InitCodecMatcher::class);
52+
$router->middleware(C::NAME, InitCodecMatcher::class);
5453

5554
// 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);
5756
$global = $repository->get($key, false);
5857

5958
if (true === $global && method_exists($kernel, 'pushMiddleware')) {
@@ -68,47 +67,44 @@ public function boot(Router $router, Repository $repository, Kernel $kernel)
6867
*/
6968
public function register()
7069
{
70+
$container = $this->app;
71+
7172
// Factory
72-
$this->app->singleton(FactoryInterface::class, Factory::class);
73+
$container->singleton(FactoryInterface::class, Factory::class);
7374

7475
// 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));
8079
});
8180

8281
// 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));
8885
});
8986

9087
// Encoders Repository
91-
$this->app->singleton(EncodersRepositoryInterface::class, EncodersRepository::class);
88+
$container->singleton(EncodersRepositoryInterface::class, EncodersRepository::class);
9289

9390
// 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+
});
9595

9696
// 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);
100100

101101
// Exception Thrower
102-
$this->app->singleton(ExceptionThrowerInterface::class, ExceptionThrower::class);
102+
$container->singleton(ExceptionThrowerInterface::class, ExceptionThrower::class);
103103

104104
// 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));
112108
});
113109
}
114110

@@ -129,4 +125,18 @@ public function provides()
129125
RenderContainerInterface::class,
130126
];
131127
}
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+
132142
}

0 commit comments

Comments
 (0)
0