8000 Merge branch 'feature/async' into async-with-cn · CodingSeo/laravel-json-api@0845222 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0845222

Browse files
committed
Merge branch 'feature/async' into async-with-cn
2 parents 3201bc9 + 61fb815 commit 0845222

File tree

6 files changed

+61
-6
lines changed

6 files changed

+61
-6
lines changed

src/Api/Api.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use CloudCreativity\LaravelJsonApi\Exceptions\RuntimeException;
2929
use CloudCreativity\LaravelJsonApi\Factories\Factory;
3030
use CloudCreativity\LaravelJsonApi\Http\Responses\Responses 8000 ;
31+
use CloudCreativity\LaravelJsonApi\Queue\ClientJob;
3132
use CloudCreativity\LaravelJsonApi\Resolver\AggregateResolver;
3233
use CloudCreativity\LaravelJsonApi\Resolver\NamespaceResolver;
3334
use GuzzleHttp\Client;
@@ -81,6 +82,11 @@ class Api
8182
*/
8283
private $url;
8384

85+
/**
86+
* @var string|null
87+
*/
88+
private $jobFqn;
89+
8490
/**
8591
* @var string|null
8692
*/
@@ -113,13 +119,14 @@ class Api
113119
private $responses;
114120

115121
/**
116-
* Definition constructor.
122+
* Api constructor.
117123
*
118124
* @param Factory $factory
119125
* @param AggregateResolver $resolver
120126
* @param $apiName
121127
* @param Codecs $codecs
122128
* @param Url $url
129+
* @param string|null $jobFqn
123130
* @param bool $useEloquent
124131
* @param string|null $supportedExt
125132
* @param array $errors
@@ -130,6 +137,7 @@ public function __construct(
130137
$apiName,
131138
Codecs $codecs,
132139
Url $url,
140+
$jobFqn = null,
133141
$useEloquent = true,
134142
$supportedExt = null,
135143
array $errors = []
@@ -143,6 +151,7 @@ public function __construct(
143151
$this->name = $apiName;
144152
$this->codecs = $codecs;
145153
$this->url = $url;
154+
$this->jobFqn = $jobFqn;
146155
$this->useEloquent = $useEloquent;
147156
$this->supportedExt = $supportedExt;
148157
$this->errors = $errors;
@@ -213,6 +222,16 @@ public function getUrl()
213222
return $this->url;
214223
}
215224

225+
/**
226+
* Get the fully qualified name of the class to use for storing client jobs.
227+
*
228+
* @return string
229+
*/
230+
public function getJobFqn()
231+
{
232+
return $this->jobFqn ?: ClientJob::class;
233+
}
234+
216235
/**
217236
* @return ContainerInterface|null
218237
*/

src/Api/Repository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function createApi($apiName, $host = null)
8080
$apiName,
8181
Codecs::fromArray($config['codecs'], $url->toString()),
8282
$url,
83+
$config['jobs'],
8384
$config['use-eloquent'],
8485
$config['supported-ext'],
8586
$config['errors']
@@ -134,6 +135,7 @@ private function normalize(array $config, $host = null)
134135
'supported-ext' => null,
135136
'url' => null,
136137
'errors' => null,
138+
'jobs' => null,
137139
], $config);
138140

139141
if (!$config['namespace']) {

src/Queue/ClientDispatch.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ class ClientDispatch extends PendingDispatch
2828
/**
2929
* ClientDispatch constructor.
3030
*
31-
* @param AsynchronousProcess $process
3231
* @param mixed $job
3332
*/
34-
public function __construct(AsynchronousProcess $process, $job)
33+
public function __construct($job)
3534
{
3635
parent::__construct($job);
37-
$job->clientJob = $process;
3836
$this->resourceId = false;
3937
}
4038

@@ -143,6 +141,9 @@ public function getMaxTries(): ?int
143141
*/
144142
public function dispatch(): AsynchronousProcess
145143
{
144+
$fqn = json_api($this->getApi())->getJobFqn();
145+
146+
$this->job->clientJob = new $fqn;
146147
$this->job->clientJob->dispatching($this);
147148

148149
parent::__destruct();

src/Queue/ClientDispatchable.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ trait ClientDispatchable
1919
public static function client(...$args): ClientDispatch
2020
{
2121
return new ClientDispatch(
22-
new ClientJob(),
2322
new static(...$args)
2423
);
2524
}

stubs/api.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
| `'posts' => App\Post::class`
5454
*/
5555
'resources' => [
56-
'posts' => App\Post::class,
56+
'posts' => \App\Post::class,
5757
],
5858

5959
/*
@@ -94,6 +94,23 @@
9494
'name' => 'api:v1:',
9595
],
9696

97+
/*
98+
|--------------------------------------------------------------------------
99+
| Jobs
100+
|--------------------------------------------------------------------------
101+
|
102+
| Defines the class that is used to store client dispatched jobs. The
103+
| storing of these classes allows you to implement the JSON API
104+
| recommendation for asynchronous processing.
105+
|
106+
| We recommend referring to the Laravel JSON API documentation on
107+
| asynchronous processing if you are using this feature. If you use a
108+
| different class here, it must implement the asynchronous process
109+
| interface.
110+
|
111+
*/
112+
'jobs' => \CloudCreativity\LaravelJsonApi\Queue\ClientJob::class,
113+
97114
/*
98115
|--------------------------------------------------------------------------
99116
| Supported JSON API Extensions

tests/dummy/config/json-api-v1.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@
104104
'name' => 'api:v1:',
105105
],
106106

107+
/*
108+
|--------------------------------------------------------------------------
109+
| Jobs
110+
|--------------------------------------------------------------------------
111+
|
112+
| Defines the class that is used to store client dispatched jobs. The
113+
| storing of these classes allows you to implement the JSON API
114+
| recommendation for asynchronous processing.
115+
|
116+
| We recommend referring to the Laravel JSON API documentation on
117+
| asynchronous processing if you are using this feature. If you use a
118+
| different class here, it must implement the asynchronous process
119+
| interface.
120+
|
121+
*/
122+
'jobs' => \CloudCreativity\LaravelJsonApi\Queue\ClientJob::class,
123+
107124
/*
108125
|--------------------------------------------------------------------------
109126
| Supported JSON API Extensions

0 commit comments

Comments
 (0)
0