8000 Add some minor changes to the feature · sablesoft/laravel-json-api@aff3e3e · GitHub
[go: up one dir, main page]

Skip to content

Commit aff3e3e

Browse files
committed
Add some minor changes to the feature
1 parent 901f643 commit aff3e3e

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"illuminate/pagination": "5.5.*|5.6.*|5.7.*",
3434
"illuminate/support": "5.5.*|5.6.*|5.7.*",
3535
"neomerx/json-api": "^1.0.3",
36+
"ramsey/uuid": "^3.0",
3637
"symfony/psr-http-message-bridge": "^1.0",
3738
"zendframework/zend-diactoros": "^1.3"
3839
},

docs/basics/api.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,22 @@ The default API name is `default`. You can change the default name via the JSON
99
the following to the `boot()` method of your `AppServiceProvider`:
1010

1111
```php
12-
public function boot()
12+
<?php
13+
14+
namespace App\Providers;
15+
16+
use CloudCreativity\LaravelJsonApi\LaravelJsonApi;
17+
use Illuminate\Support\ServiceProvider;
18+
19+
class AppServiceProvider extends ServiceProvider
1320
{
14-
JsonApi::defaultApi('v1');
21+
public function boot()
22+
{
23+
LaravelJsonApi::defaultApi('v1');
24+
}
25+
26+
// ...
27+
1528
}
1629
```
1730

src/Adapter/AbstractResourceAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ protected function fillRelated($record, ResourceObject $resource, EncodingParame
232232
protected function fillAndPersist($record, ResourceObject $resource, EncodingParametersInterface $parameters)
233233
{
234234
$this->fill($record, $resource, $parameters);
235+
$async = $this->persist($record);
235236

236-
if ($async = $this->persist($record)) {
237+
if ($async instanceof AsynchronousProcess) {
237238
return $async;
238239
}
239240

src/LaravelJsonApi.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
class LaravelJsonApi
66
{
77

8+
/**
9+
* The default API name.
10+
*
11+
* @var null
12+
*/
13+
public static $defaultApi = 'default';
14+
815
/**
916
* Indicates if Laravel JSON API migrations will be run.
1017
*
@@ -19,12 +26,29 @@ class LaravelJsonApi
1926
*/
2027
public static $queueBindings = true;
2128

29+
/**
30+
* Set the default API name.
31+
*
32+
* @param string $name
33+
* @return LaravelJsonApi
34+
*/
35+
public static function defaultApi(string $name): self
36+
{
37+
if (empty($name)) {
38+
throw new \InvalidArgumentException('Default API name must not be empty.');
39+
}
40+
41+
self::$defaultApi = $name;
42+
43+
return new self();
44+
}
45+
2246
/**
2347
* @return LaravelJsonApi
2448
*/
2549
public static function runMigrations(): self
2650
{
27-
static::$runMigrations = true;
51+
self::$runMigrations = true;
2852

2953
return new self();
3054
}
@@ -34,7 +58,7 @@ public static function runMigrations(): self
3458
*/
3559
public static function skipQueueBindings(): self
3660
{
37-
static::$queueBindings = false;
61+
self::$queueBindings = false;
3862

3963
return new self();
4064
}

src/Services/JsonApiService.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use CloudCreativity\LaravelJsonApi\Contracts\Utils\ErrorReporterInterface;
2626
use CloudCreativity\LaravelJsonApi\Exceptions\RuntimeException;
2727
use CloudCreativity\LaravelJsonApi\Http\Requests\JsonApiRequest;
28+
use CloudCreativity\LaravelJsonApi\LaravelJsonApi;
2829
use CloudCreativity\LaravelJsonApi\Routing\ResourceRegistrar;
2930
use Exception;
3031
use Illuminate\Contracts\Container\Container;
@@ -42,11 +43,6 @@ class JsonApiService
4243
*/
4344
private $container;
4445

45-
/**
46-
* @var string
47-
*/
48-
private $default;
49-
5046
/**
5147
* JsonApiService constructor.
5248
*
@@ -55,26 +51,24 @@ class JsonApiService
5551
public function __construct(Container $container)
5652
{
5753
$this->container = $container;
58-
$this->default = 'default';
5954
}
6055

6156
/**
6257
* Set or get the default API name.
6358
*
6459
* @param string|null $apiName
6560
* @return string
61+
* @deprecated 2.0.0 setting the API name via this method will be removed (getter will remain).
6662
*/
6763
public function defaultApi($apiName = null)
6864
{
6965
if (is_null($apiName)) {
70-
return $this->default;
66+
return LaravelJsonApi::$defaultApi;
7167
}
7268

73-
if (!is_string($apiName) || empty($apiName)) {
74-
throw new \InvalidArgumentException('Expecting a non-empty string API name.');
75-
}
69+
LaravelJsonApi::defaultApi($apiName);
7670

77-
return $this->default = $apiName;
71+
return $apiName;
7872
}
7973

8074
/**
@@ -90,7 +84,7 @@ public function api($apiName = null)
9084
/** @var Repository $repo */
9185
$repo = $this->container->make(Repository::class);
9286

93-
return $repo->createApi($apiName ?: $this->default);
87+
return $repo->createApi($apiName ?: $this->defaultApi());
9488
}
9589

9690
/**

0 commit comments

Comments
 (0)
0