8000 Allow client job class to be changed · sablesoft/laravel-json-api@61fb815 · GitHub
[go: up one dir, main page]

Skip to content

Commit 61fb815

Browse files
committed
Allow client job class to be changed
1 parent 93f51a0 commit 61fb815

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
@@ -27,6 +27,7 @@
2727
use CloudCreativity\LaravelJsonApi\Contracts\Validators\ValidatorFactoryInterface;
2828
use CloudCreativity\LaravelJsonApi\Factories\Factory;
2929
use CloudCreativity\LaravelJsonApi\Http\Responses\Responses;
30+
use CloudCreativity\LaravelJsonApi\Queue\ClientJob;
3031
use CloudCreativity\LaravelJsonApi\Resolver\AggregateResolver;
3132
use CloudCreativity\LaravelJsonApi\Resolver\NamespaceResolver;
3233
use GuzzleHttp\Client;
@@ -80,6 +81,11 @@ class Api
8081
*/
8182
private $url;
8283

84+
/**
85+
* @var string|null
86+
*/
87+
private $jobFqn;
88+
8389
/**
8490
* @var string|null
8591
*/
@@ -107,13 +113,14 @@ class Api
107113
private $errorRepository;
108114

109115
/**
110-
* Definition constructor.
116+
* Api constructor.
111117
*
112118
* @param Factory $factory
113119
* @param AggregateResolver $resolver
114120
* @param $apiName
115121
* @param array $codecs
116122
* @param Url $url
123+
* @param string|null $jobFqn
117124
* @param bool $useEloquent
118125
* @param string|null $supportedExt
119126
* @param array $errors
@@ -124,6 +131,7 @@ public function __construct(
124131
$apiName,
125132
array $codecs,
126133
Url $url,
134+
$jobFqn = null,
127135
$useEloquent = true,
128136
$supportedExt = null,
129137
array $errors = []
@@ -133,6 +141,7 @@ public function __construct(
133141
$this->name = $apiName;
134142
$this->codecs = $codecs;
135143
$this->url = $url;
144+
$this->jobFqn = $jobFqn;
136145
$this->useEloquent = $useEloquent;
137146
$this->supportedExt = $supportedExt;
138147
$this->errors = $errors;
@@ -203,6 +212,16 @@ public function getUrl()
203212
return $this->url;
204213
}
205214

215+
/**
216+
* Get the fully qualified name of the class to use for storing client jobs.
217+
*
218+
* @return string
219+
*/
220+
public function getJobFqn()
221+
{
222+
return $this->jobFqn ?: ClientJob::class;
223+
}
224+
206225
/**
207226
* @return CodecMatcherInterface
208227
*/

src/Api/Repository.php

< 8000 span class="sr-only">Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function createApi($apiName, $host = null)
7979
$apiName,
8080
$config['codecs'],
8181
Url::fromArray($config['url']),
82+
$config['jobs'],
8283
$config['use-eloquent'],
8384
$config['supported-ext'],
8485
$config['errors']
@@ -133,6 +134,7 @@ private function normalize(array $config, $host = null)
133134
'supported-ext' => null,
134135
'url' => null,
135136
'errors' => null,
137+
'jobs' => null,
136138
], $config);
137139

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

src/Queue/ClientDispatch.php

Lines changed: 4 additions & 3 deletions
< 6D40 col width="100%"/>
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
@@ -103,6 +103,23 @@
103103
'name' => 'api:v1:',
104104
],
105105

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

0 commit comments

Comments
 (0)
0