10000 [Feature] Use factory that allows array serialization · volldigital/laravel-json-api@65b531d · GitHub
[go: up one dir, main page]

Skip to content

Commit 65b531d

Browse files
committed
[Feature] Use factory that allows array serialization
...of JSON API documents.
1 parent 28e27fc commit 65b531d

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"require": {
2525
"php": "^5.6|^7.0",
2626
"laravel/framework": "5.1.*|5.2.*|5.3.*|5.4.*",
27-
"cloudcreativity/json-api": "^0.7",
27+
"cloudcreativity/json-api": "0.7.x-dev",
2828
"symfony/psr-http-message-bridge": "^1.0",
2929
"zendframework/zend-diactoros": "^1.3"
3030
},
@@ -45,7 +45,8 @@
4545
},
4646
"extra": {
4747
"branch-alias": {
48-
"dev-develop": "0.6.x-dev"
48+
"dev-develop": "0.7.x-dev",
49+
"dev-hotfix/0.6.1": "0.6.1-dev"
4950
}
5051
}
5152
}

src/ServiceProvider.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use CloudCreativity\JsonApi\Contracts\Utils\ConfigurableInterface;
3333
use CloudCreativity\JsonApi\Contracts\Utils\ReplacerInterface;
3434
use CloudCreativity\JsonApi\Contracts\Validators\ValidatorFactoryInterface as BaseValidatorFactoryInterface;
35+
use CloudCreativity\JsonApi\Factories\Factory;
3536
use CloudCreativity\JsonApi\Http\ApiFactory;
3637
use CloudCreativity\JsonApi\Http\Requests\RequestFactory;
3738
use CloudCreativity\JsonApi\Http\Responses\ResponseFactory;
@@ -61,14 +62,19 @@
6162
use CloudCreativity\LaravelJsonApi\Services\JsonApiService;
6263
use CloudCreativity\LaravelJsonApi\Validators\ValidatorErrorFactory;
6364
use CloudCreativity\LaravelJsonApi\Validators\ValidatorFactory;
65+
use Illuminate\Contracts\Foundation\Application;
6466
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
6567
use Illuminate\Routing\Router;
6668
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
69+
use Neomerx\JsonApi\Contracts\Document\DocumentFactoryInterface;
70+
use Neomerx\JsonApi\Contracts\Encoder\Handlers\HandlerFactoryInterface;
71+
use Neomerx\JsonApi\Contracts\Encoder\Parser\ParserFactoryInterface;
72+
use Neomerx\JsonApi\Contracts\Encoder\Stack\StackFactoryInterface;
6773
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
6874
use Neomerx\JsonApi\Contracts\Http\HttpFactoryInterface;
6975
use Neomerx\JsonApi\Contracts\Http\ResponsesInterface;
7076
use Neomerx\JsonApi\Contracts\Schema\SchemaFactoryInterface;
71-
use Neomerx\JsonApi\Factories\Factory;
77+
use Psr\Log\LoggerInterface;
7278

7379
/**
7480
* Class ServiceProvider
@@ -185,12 +191,34 @@ protected function bootResponseMacro(ResponseFactoryContract $responses)
185191

186192
/**
187193
* Bind parts of the neomerx/json-api dependency into the service container.
194+
*
195+
* For this Laravel JSON API package, we use our extended JSON API factory.
196+
* This ensures that we can override any parts of the Neomerx JSON API pacakge
197+
* that we want.
198+
*
199+
* As the Neomerx package splits the factories into multiple interfaces, we
200+
* also register aliases for each of the factory interfaces.
201+
*
202+
* The Neomerx package allows a logger to be injected into the factory. This
203+
* enables the Neomerx package to log messages. When creating the factory, we
204+
* therefore set the logger as our application's logger.
188205
*/
189206
protected function bindNeomerx()
190207
{
191-
$this->app->singleton(FactoryInterface::class, Factory::class);
192-
$this->app->singleton(SchemaFactoryInterface::class, FactoryInterface::class);
193-
$this->app->singleton(HttpFactoryInterface::class, FactoryInterface::class);
208+
$this->app->singleton(FactoryInterface::class, function (Application $app) {
209+
$logger = $app->make(LoggerInterface::class);
210+
$factory = new Factory();
211+
$factory->setLogger($logger);
212+
213+
return $factory;
214+
});
215+
216+
$this->app->alias(FactoryInterface::class, DocumentFactoryInterface::class);
217+
$this->app->alias(FactoryInterface::class, HandlerFactoryInterface::class);
218+
$this->app->alias(FactoryInterface::class, HttpFactoryInterface::class);
219+
$this->app->alias(FactoryInterface::class, ParserFactoryInterface::class);
220+
$this->app->alias(FactoryInterface::class, SchemaFactoryInterface::class);
221+
$this->app->alias(FactoryInterface::class, StackFactoryInterface::class);
194222
}
195223

196224
/**

0 commit comments

Comments
 (0)
0