Skip to content

Commit 0dcd64e

Browse files
authored
Merge pull request #355 from bowphp/refactor/code-base
Refactoring queue and add should queue support
2 parents 33a90c4 + fff5732 commit 0dcd64e

37 files changed

Lines changed: 256 additions & 213 deletions

src/Console/Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use Bow\Console\Command\Generator\GenerateNotificationCommand;
3030
use Bow\Console\Command\Generator\GenerateConfigurationCommand;
3131
use Bow\Console\Command\Generator\GenerateEventListenerCommand;
32-
use Bow\Console\Command\Generator\GenerateJobCommand;
32+
use Bow\Console\Command\Generator\GenerateTaskCommand;
3333
use Bow\Console\Command\Generator\GenerateRouterResourceCommand;
3434

3535
class Command extends AbstractCommand
@@ -57,7 +57,7 @@ class Command extends AbstractCommand
5757
"add:validation" => GenerateValidationCommand::class,
5858
"add:event" => GenerateAppEventCommand::class,
5959
"add:listener" => GenerateEventListenerCommand::class,
60-
"add:job" => GenerateJobCommand::class,
60+
"add:task" => GenerateTaskCommand::class,
6161
"add:command" => GenerateConsoleCommand::class,
6262
"add:notifier" => GenerateNotifierCommand::class,
6363
"run:console" => ReplCommand::class,

src/Console/Command/Generator/GenerateJobCommand.php

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bow\Console\Command\Generator;
6+
7+
use Bow\Console\AbstractCommand;
8+
use Bow\Console\Color;
9+
use Bow\Console\Generator;
10+
11+
class GenerateTaskCommand extends AbstractCommand
12+
{
13+
/**
14+
* Add task
15+
*
16+
* @param string $task
17+
* @return void
18+
*/
19+
public function run(string $task): void
20+
{
21+
$generator = new Generator(
22+
$this->setting->getTaskDirectory(),
23+
$task
24+
);
25+
26+
if ($generator->fileExists()) {
27+
echo Color::red("The task already exists");
28+
exit(1);
29+
}
30+
31+
$generator->write('task', [
32+
'baseNamespace' => $this->namespaces['task'] ?? 'App\\Tasks'
33+
]);
34+
35+
echo Color::green("The task has been well created.");
36+
exit(0);
37+
}
38+
}

src/Console/Console.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Console
6060
'service',
6161
'exception',
6262
'event',
63-
'job',
63+
'task',
6464
'command',
6565
'listener',
6666
'notifier'
@@ -540,7 +540,7 @@ private function help(?string $command = null): int
540540
\033[0;33madd:migration\033[00m Create a new migration
541541
\033[0;33madd:event\033[00m Create a new event
542542
\033[0;33madd:listener\033[00m Create a new event listener
543-
\033[0;33madd:job\033[00m Create a new job
543+
\033[0;33madd:task\033[00m Create a new task
544544
\033[0;33madd:command\033[00m Create a new console command
545545
\033[0;33madd:notifier\033[00m Create a new messaging handler
546546
@@ -564,7 +564,7 @@ private function help(?string $command = null): int
564564
\033[0;32mRUN\033[00m Launch development tools
565565
\033[0;33mrun:console\033[00m Show PsySH PHP REPL for debugging code
566566
\033[0;33mrun:server\033[00m Start local development server
567-
\033[0;33mrun:worker\033[00m Start consumer/worker to handle queue jobs
567+
\033[0;33mrun:worker\033[00m Start consumer/worker to handle queue tasks
568568
569569
USAGE;
570570
echo $usage;
@@ -593,7 +593,7 @@ private function help(?string $command = null): int
593593
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:seeder name [--seed=n] Create a new seeder
594594
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:migration name Create a new migration
595595
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:event name Create a new event listener
596-
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:job name Create a new queue job
596+
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:task name Create a new queue task
597597
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:command name Create a new console command
598598
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add:notifier name Create a new messaging handler
599599
\033[0;33m$\033[00m php \033[0;34mbow\033[00m add help Display this help
@@ -642,7 +642,7 @@ private function help(?string $command = null): int
642642
643643
\033[0;33m$\033[00m php \033[0;34mbow\033[00m run:console Show PsySH PHP REPL for debugging code
644644
\033[0;33m$\033[00m php \033[0;34mbow\033[00m run:server [option] Start local development server
645-
\033[0;33m$\033[00m php \033[0;34mbow\033[00m run:worker [option] Start worker to handle queue jobs
645+
\033[0;33m$\033[00m php \033[0;34mbow\033[00m run:worker [option] Start worker to handle queue tasks
646646
647647
U; // phpcs:enable
648648
break;

src/Console/Setting.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ class Setting
137137
private string $service_directory;
138138

139139
/**
140-
* The job directory
140+
* The task directory
141141
*
142142
* @var string
143143
*/
144-
private string $job_directory;
144+
private string $task_directory;
145145
/**
146146
* The command directory
147147
*
@@ -402,24 +402,24 @@ public function setServiceDirectory(string $service_directory): void
402402
}
403403

404404
/**
405-
* Get the job directory
405+
* Get the task directory
406406
*
407407
* @return string
408408
*/
409-
public function getJobDirectory(): string
409+
public function getTaskDirectory(): string
410410
{
411-
return $this->job_directory;
411+
return $this->task_directory;
412412
}
413413

414414
/**
415-
* Set the job directory
415+
* Set the task directory
416416
*
417-
* @param string $job_directory
417+
* @param string $task_directory
418418
* @return void
419419
*/
420-
public function setJobDirectory(string $job_directory): void
420+
public function setTaskDirectory(string $task_directory): void
421421
{
422-
$this->job_directory = $job_directory;
422+
$this->task_directory = $task_directory;
423423
}
424424

425425
/**
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace {baseNamespace}{namespace};
44

5-
use Bow\Queue\QueueJob;
5+
use Bow\Queue\QueueTask;
66

7-
class {className} extends QueueJob
7+
class {className} extends QueueTask
88
{
99
/**
1010
* {className} constructor

src/Database/Barry/Model.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public static function retrieveAndDelete(
337337
}
338338

339339
if ($model instanceof Collection) {
340-
$model->delete();
340+
$model->map(fn ($m) => $m->delete());
341341
return $model;
342342
}
343343

@@ -861,9 +861,7 @@ public function toArray(): array
861861
{
862862
return array_filter(
863863
$this->attributes,
864-
function ($key) {
865-
return !in_array($key, $this->hidden);
866-
},
864+
fn ($key) => !in_array($key, $this->hidden),
867865
ARRAY_FILTER_USE_KEY
868866
);
869867
}
@@ -875,9 +873,7 @@ public function jsonSerialize(): array
875873
{
876874
return array_filter(
877875
$this->attributes,
878-
function ($key) {
879-
return !in_array($key, $this->hidden);
880-
},
876+
fn ($key) => !in_array($key, $this->hidden),
881877
ARRAY_FILTER_USE_KEY
882878
);
883879
}

src/Database/Database.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,19 +426,19 @@ public static function inTransaction(): bool
426426
*/
427427
public static function commit(): void
428428
{
429-
static::ensureDatabaseConnection();
430-
431-
static::$adapter->getConnection()->commit();
429+
if (static::inTransaction()) {
430+
static::$adapter->getConnection()->commit();
431+
}
432432
}
433433

434434
/**
435435
* Cancel a transaction
436436
*/
437437
public static function rollback(): void
438438
{
439-
static::ensureDatabaseConnection();
440-
441-
static::$adapter->getConnection()->rollBack();
439+
if (static::inTransaction()) {
440+
static::$adapter->getConnection()->rollBack();
441+
}
442442
}
443443

444444
/**
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use Bow\Event\Contracts\EventListener;
66
use Bow\Event\Contracts\EventShouldQueue;
7-
use Bow\Queue\QueueJob;
7+
use Bow\Queue\QueueTask;
88

9-
class EventQueueJob extends QueueJob
9+
class EventQueueTask extends QueueTask
1010
{
1111
/**
12-
* EventQueueJob constructor
12+
* EventQueueTask constructor
1313
*
1414
* @param EventListener|EventShouldQueue $event
1515
* @param mixed $payload

src/Event/Listener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function call(array $data = []): mixed
5050
$instance = app($callable);
5151
if ($instance instanceof EventListener) {
5252
if ($instance instanceof EventShouldQueue) {
53-
queue(new EventQueueJob($instance, $data));
53+
queue(new EventQueueTask($instance, $data));
5454
return null;
5555
}
5656
$callable = [$instance, 'process'];

0 commit comments

Comments
 (0)