From 71da8babdaf21293a9d496a238a0fb45f231c730 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Sat, 18 Jan 2025 01:06:15 +0000 Subject: [PATCH 1/2] code formatting --- CHANGELOG.md | 6 +-- src/Application/Application.php | 4 +- src/Auth/Guards/JwtGuard.php | 1 - src/Auth/Guards/SessionGuard.php | 18 +++++--- src/Cache/CacheConfiguration.php | 1 - src/Configuration/LoggerConfiguration.php | 5 +-- src/Console/Command.php | 10 +++-- src/Console/Command/ClearCommand.php | 1 - src/Console/Command/GenerateKeyCommand.php | 1 + .../GenerateResourceControllerCommand.php | 2 +- src/Console/Command/MiddlewareCommand.php | 2 + src/Console/Command/MigrationCommand.php | 19 +++++---- src/Console/Command/SeederCommand.php | 1 - src/Console/Command/ServiceCommand.php | 3 +- src/Console/Command/ValidationCommand.php | 3 +- src/Console/Command/WorkerCommand.php | 2 +- src/Console/Console.php | 42 +++++++++---------- src/Container/Capsule.php | 19 ++++++--- src/Event/EventProducer.php | 2 + 19 files changed, 79 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 743cac52..6229627b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,10 +39,6 @@ This method aims to execute an SQL transaction around a passed arrow function. ```php Database::transaction(fn() => $user->update(['name' => ''])); - - - - ``` Ref: #255 @@ -135,7 +131,7 @@ Release for 5.0.2 ## 5.0.0 - 2023-05-10 - [Add] Convert the project from PHP7 to PHP8 -- [Add] Multiconnection for storage system +- [Add] Multi connection for storage system - [Fix] Fixes migrations errors [#176](https://github.com/bowphp/framework/pull/176) - [Fix] Fixes the column size [#165](https://github.com/bowphp/framework/pull/165) - [Fix] Add the fallback on old request method [#170](https://github.com/bowphp/framework/pull/170) diff --git a/src/Application/Application.php b/src/Application/Application.php index cbcd2bc0..555f4860 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -374,7 +374,9 @@ public function container(?string $name = null, ?callable $callable = null): mix ); } - return $this->capsule->bind($name, $callable); + $this->capsule->bind($name, $callable); + + return $this; } /** diff --git a/src/Auth/Guards/JwtGuard.php b/src/Auth/Guards/JwtGuard.php index 8b02d453..b8e8fc79 100644 --- a/src/Auth/Guards/JwtGuard.php +++ b/src/Auth/Guards/JwtGuard.php @@ -5,7 +5,6 @@ namespace Bow\Auth\Guards; use Bow\Security\Hash; -use Bow\Support\Log; use Exception; use Policier\Policier; use Bow\Auth\Authentication; diff --git a/src/Auth/Guards/SessionGuard.php b/src/Auth/Guards/SessionGuard.php index 5114ba1d..658e0f28 100644 --- a/src/Auth/Guards/SessionGuard.php +++ b/src/Auth/Guards/SessionGuard.php @@ -5,10 +5,10 @@ namespace Bow\Auth\Guards; use Bow\Security\Hash; +use Bow\Session\Exception\SessionException; use Bow\Session\Session; use Bow\Auth\Authentication; use Bow\Auth\Exception\AuthenticationException; -use Bow\Auth\Guards\GuardContract; use Bow\Auth\Traits\LoginUserTrait; class SessionGuard extends GuardContract @@ -25,7 +25,7 @@ class SessionGuard extends GuardContract /** * Defines the session_key * - * @var array + * @var string */ private string $session_key; @@ -44,10 +44,11 @@ public function __construct(array $provider, string $guard) } /** - * Check if user is authenticate + * Check if user is authenticated * * @param array $credentials * @return bool + * @throws AuthenticationException|SessionException */ public function attempts(array $credentials): bool { @@ -72,6 +73,7 @@ public function attempts(array $credentials): bool * Get the session instance * * @return Session + * @throws AuthenticationException */ private function getSession(): Session { @@ -87,9 +89,10 @@ private function getSession(): Session } /** - * Check if user is authenticate + * Check if user is authenticated * * @return bool + * @throws AuthenticationException|SessionException */ public function check(): bool { @@ -100,6 +103,7 @@ public function check(): bool * Check if user is guest * * @return bool + * @throws AuthenticationException|SessionException */ public function guest(): bool { @@ -107,9 +111,10 @@ public function guest(): bool } /** - * Check if user is authenticate + * Check if user is authenticated * * @return ?Authentication + * @throws AuthenticationException|SessionException */ public function user(): ?Authentication { @@ -121,6 +126,7 @@ public function user(): ?Authentication * * @param mixed $user * @return bool + * @throws AuthenticationException|SessionException */ public function login(Authentication $user): bool { @@ -133,6 +139,7 @@ public function login(Authentication $user): bool * Make direct logout * * @return bool + * @throws SessionException|AuthenticationException */ public function logout(): bool { @@ -145,6 +152,7 @@ public function logout(): bool * Get the user id * * @return mixed + * @throws AuthenticationException|SessionException */ public function id(): mixed { diff --git a/src/Cache/CacheConfiguration.php b/src/Cache/CacheConfiguration.php index e85650a7..0349bf86 100644 --- a/src/Cache/CacheConfiguration.php +++ b/src/Cache/CacheConfiguration.php @@ -6,7 +6,6 @@ use Bow\Configuration\Configuration; use Bow\Configuration\Loader; -use Bow\Cache\Cache; class CacheConfiguration extends Configuration { diff --git a/src/Configuration/LoggerConfiguration.php b/src/Configuration/LoggerConfiguration.php index a2049f48..125362c3 100644 --- a/src/Configuration/LoggerConfiguration.php +++ b/src/Configuration/LoggerConfiguration.php @@ -5,15 +5,14 @@ namespace Bow\Configuration; use Bow\View\View; +use Exception; use Monolog\Logger; use Bow\Support\Collection; use Whoops\Handler\Handler; -use Bow\Configuration\Loader; use Bow\Database\Barry\Model; use Monolog\Handler\StreamHandler; use Monolog\Handler\FirePHPHandler; use Whoops\Handler\CallbackHandler; -use Bow\Configuration\Configuration; use Bow\Contracts\ResponseInterface; use Iterator; use Whoops\Handler\PrettyPageHandler; @@ -99,7 +98,7 @@ function ($exception, $inspector, $run) use ($monolog, $error_handler) { * @param string $log_dir * @param string $name * @return Logger - * @throws \Exception + * @throws Exception */ private function loadFileLogger(string $log_dir, string $name): Logger { diff --git a/src/Console/Command.php b/src/Console/Command.php index 145a2fb0..f206bc73 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -60,9 +60,9 @@ class Command extends AbstractCommand */ public function call(string $command, string $action, ...$rest): mixed { - $class = $this->command[$command] ?? null; + $classes = $this->command[$command] ?? null; - if (is_null($class)) { + if (is_null($classes)) { $this->throwFailsCommand("The command $command not found !"); } @@ -74,8 +74,10 @@ public function call(string $command, string $action, ...$rest): mixed $method = Str::camel($action); } - if (is_array($class)) { - $class = $class[$action]; + if (is_array($classes)) { + $class = $classes[$action]; + } else { + $class = $classes; } $instance = new $class($this->setting, $this->arg); diff --git a/src/Console/Command/ClearCommand.php b/src/Console/Command/ClearCommand.php index 2d26cd45..8804f24b 100644 --- a/src/Console/Command/ClearCommand.php +++ b/src/Console/Command/ClearCommand.php @@ -14,7 +14,6 @@ class ClearCommand extends AbstractCommand * * @param string $action * @return void - * @throws \ErrorException */ public function make(string $action): void { diff --git a/src/Console/Command/GenerateKeyCommand.php b/src/Console/Command/GenerateKeyCommand.php index ba3a332e..36b6a2fb 100644 --- a/src/Console/Command/GenerateKeyCommand.php +++ b/src/Console/Command/GenerateKeyCommand.php @@ -14,6 +14,7 @@ class GenerateKeyCommand extends AbstractCommand * Generate Key * * @return void + * @throws ConsoleException */ public function generate(): void { diff --git a/src/Console/Command/GenerateResourceControllerCommand.php b/src/Console/Command/GenerateResourceControllerCommand.php index 480d5790..b6bce42c 100644 --- a/src/Console/Command/GenerateResourceControllerCommand.php +++ b/src/Console/Command/GenerateResourceControllerCommand.php @@ -79,7 +79,7 @@ private function createResourceController( string $prefix, string $controller, string $model_namespace = '' - ) { + ): void { $generator->write('controller/rest', [ 'modelNamespace' => $model_namespace, 'prefix' => $prefix, diff --git a/src/Console/Command/MiddlewareCommand.php b/src/Console/Command/MiddlewareCommand.php index d5cde87c..fc116a05 100644 --- a/src/Console/Command/MiddlewareCommand.php +++ b/src/Console/Command/MiddlewareCommand.php @@ -26,6 +26,7 @@ class MiddlewareCommand extends AbstractCommand if ($generator->fileExists()) { echo Color::red("The middleware already exists"); + exit(1); } @@ -34,6 +35,7 @@ class MiddlewareCommand extends AbstractCommand ]); echo Color::green("The middleware has been well created."); + exit(0); } } diff --git a/src/Console/Command/MigrationCommand.php b/src/Console/Command/MigrationCommand.php index 4cee7f09..a2a98f8d 100644 --- a/src/Console/Command/MigrationCommand.php +++ b/src/Console/Command/MigrationCommand.php @@ -13,6 +13,7 @@ use Bow\Database\Migration\SQLGenerator; use Bow\Database\QueryBuilder; use Bow\Support\Str; +use Exception; use JetBrains\PhpStorm\NoReturn; class MigrationCommand extends AbstractCommand @@ -21,7 +22,7 @@ class MigrationCommand extends AbstractCommand * Make a migration command * * @return void - * @throws \Exception + * @throws Exception */ public function migrate(): void { @@ -32,7 +33,7 @@ public function migrate(): void * Rollback migration command * * @return void - * @throws \Exception + * @throws Exception */ public function rollback(): void { @@ -43,7 +44,7 @@ public function rollback(): void * Reset migration command * * @return void - * @throws \Exception + * @throws Exception */ public function reset(): void { @@ -55,7 +56,7 @@ public function reset(): void * * @param string $type * @return void - * @throws \Exception + * @throws Exception */ private function factory(string $type): void { @@ -105,7 +106,7 @@ protected function makeUp(array $migrations): void try { // Up migration (new $migration())->up(); - } catch (\Exception $exception) { + } catch (Exception $exception) { $this->throwMigrationException($exception, $migration); } @@ -159,7 +160,7 @@ protected function makeRollback(array $migrations): void // Rollback migration try { (new $migration())->rollback(); - } catch (\Exception $exception) { + } catch (Exception $exception) { $this->throwMigrationException($exception, $migration); } @@ -218,7 +219,7 @@ protected function makeReset(array $migrations): void // Rollback migration try { (new $migration())->rollback(); - } catch (\Exception $exception) { + } catch (Exception $exception) { $this->throwMigrationException($exception, $migration); } @@ -248,10 +249,10 @@ protected function makeReset(array $migrations): void /** * Throw migration exception * - * @param \Exception $exception + * @param Exception $exception * @param string $migration */ - #[NoReturn] private function throwMigrationException(\Exception $exception, string $migration): void + #[NoReturn] private function throwMigrationException(Exception $exception, string $migration): void { $this->printExceptionMessage( $exception->getMessage(), diff --git a/src/Console/Command/SeederCommand.php b/src/Console/Command/SeederCommand.php index 49bbb2d9..5e8acf18 100644 --- a/src/Console/Command/SeederCommand.php +++ b/src/Console/Command/SeederCommand.php @@ -61,7 +61,6 @@ public function all(): void * * @param string|null $seeder_name * @return void - * @throws ErrorException */ public function table(?string $seeder_name = null): void { diff --git a/src/Console/Command/ServiceCommand.php b/src/Console/Command/ServiceCommand.php index 379978de..f1c64da6 100644 --- a/src/Console/Command/ServiceCommand.php +++ b/src/Console/Command/ServiceCommand.php @@ -6,6 +6,7 @@ use Bow\Console\AbstractCommand; use Bow\Console\Generator; +use JetBrains\PhpStorm\NoReturn; class ServiceCommand extends AbstractCommand { @@ -15,7 +16,7 @@ class ServiceCommand extends AbstractCommand * @param string $service * @return void */ - public function generate(string $service): void + #[NoReturn] public function generate(string $service): void { $generator = new Generator( $this->setting->getServiceDirectory(), diff --git a/src/Console/Command/ValidationCommand.php b/src/Console/Command/ValidationCommand.php index 1d7bb384..6c6d22cc 100644 --- a/src/Console/Command/ValidationCommand.php +++ b/src/Console/Command/ValidationCommand.php @@ -7,6 +7,7 @@ use Bow\Console\AbstractCommand; use Bow\Console\Color; use Bow\Console\Generator; +use JetBrains\PhpStorm\NoReturn; class ValidationCommand extends AbstractCommand { @@ -16,7 +17,7 @@ class ValidationCommand extends AbstractCommand * @param string $validation * @return void */ - public function generate(string $validation): void + #[NoReturn] public function generate(string $validation): void { $generator = new Generator( $this->setting->getValidationDirectory(), diff --git a/src/Console/Command/WorkerCommand.php b/src/Console/Command/WorkerCommand.php index 639efbb3..638c777e 100644 --- a/src/Console/Command/WorkerCommand.php +++ b/src/Console/Command/WorkerCommand.php @@ -12,7 +12,7 @@ class WorkerCommand extends AbstractCommand /** * The run server command * - * @param string $connection + * @param string|null $connection * @return void */ public function run(?string $connection = null): void diff --git a/src/Console/Console.php b/src/Console/Console.php index 6b585472..0807ae84 100644 --- a/src/Console/Console.php +++ b/src/Console/Console.php @@ -7,6 +7,8 @@ use Bow\Configuration\Loader; use Bow\Console\Exception\ConsoleException; use Bow\Console\Traits\ConsoleTrait; +use ErrorException; +use Exception; /** * @method static Console addCommand(string $command, callable $cb) @@ -95,9 +97,6 @@ class Console * Bow constructor. * * @param Setting $setting - * - * @return void - * @throws \ErrorException */ public function __construct(Setting $setting) { @@ -157,7 +156,7 @@ public function run(): mixed try { $this->kernel->boot(); - } catch (\Exception $exception) { + } catch (Exception $exception) { echo Color::red($exception->getMessage()); echo Color::green($exception->getTraceAsString()); @@ -185,7 +184,7 @@ public function run(): mixed try { return $this->call($command); - } catch (\Exception $exception) { + } catch (Exception $exception) { echo Color::red($exception->getMessage()); echo Color::green($exception->getTraceAsString()); @@ -198,8 +197,8 @@ public function run(): mixed * * @param string|null $command * @return mixed - * @throws \ErrorException - * @throws \Exception + * @throws ErrorException + * @throws Exception */ public function call(?string $command): mixed { @@ -229,7 +228,7 @@ public function call(?string $command): mixed try { return call_user_func_array([$this, $command], [$target]); - } catch (\Exception $e) { + } catch (Exception $e) { echo $e->getMessage(); exit(1); } @@ -268,7 +267,7 @@ public static function register(string $command, callable|string $cb): void * * @param string $command * @return mixed - * @throws \Exception + * @throws Exception */ private function executeCustomCommand(string $command): mixed { @@ -283,7 +282,7 @@ private function executeCustomCommand(string $command): mixed $instance = new $classname($this->setting, $this->arg); return call_user_func_array([$instance, "process"], []); - } catch (\Exception $exception) { + } catch (Exception $exception) { if (php_sapi_name() !== "cli") { throw $exception; } @@ -299,7 +298,7 @@ private function executeCustomCommand(string $command): mixed * Launch a migration * * @return void - * @throws \ErrorException + * @throws ErrorException */ private function migration(): void { @@ -318,7 +317,7 @@ private function migration(): void * Launch a migration * * @return void - * @throws \ErrorException + * @throws ErrorException */ private function migrate(): void { @@ -335,7 +334,7 @@ private function migrate(): void * Create files * * @return void - * @throws \ErrorException + * @throws ErrorException */ private function add(): void { @@ -385,7 +384,7 @@ private function seed(): void /** * Launch process * - * @throws \ErrorException + * @throws ErrorException */ private function launch(): void { @@ -402,7 +401,7 @@ private function launch(): void * Allows to generate a resource on a controller * * @return void - * @throws \ErrorException + * @throws ErrorException */ private function generate(): void { @@ -419,7 +418,7 @@ private function generate(): void * Alias of generate * * @return void - * @throws \ErrorException + * @throws ErrorException */ private function gen(): void { @@ -430,7 +429,7 @@ private function gen(): void * Remove the caches * * @return void - * @throws \ErrorException + * @throws ErrorException */ private function clear(): void { @@ -443,7 +442,7 @@ private function clear(): void * Flush the connections * * @return void - * @throws \ErrorException + * @throws ErrorException */ private function flush(): void { @@ -461,7 +460,6 @@ private function flush(): void * * @param string|null $command * @return int - * @throws \ErrorException */ private function help(?string $command = null): int { @@ -640,12 +638,12 @@ private function help(?string $command = null): int exit(0); } - /* + /** * Show bow framework version and current php version in console * - * @return string + * @return void */ - private function getVersion() + private function getVersion(): void { $version = <<key[$key] = true; $this[$key] = $value; + + return $this; } /** @@ -146,11 +149,13 @@ public function bind(string $key, callable $value): void * * @param string $key * @param Closure|callable $value - * @return void + * @return Capsule */ - public function factory(string $key, Closure|callable $value): void + public function factory(string $key, Closure|callable $value): Capsule { $this->factories[$key] = $value; + + return $this; } /** @@ -158,9 +163,9 @@ public function factory(string $key, Closure|callable $value): void * * @param string $key * @param mixed $instance - * @return void + * @return Capsule */ - public function instance(string $key, mixed $instance): void + public function instance(string $key, mixed $instance): Capsule { if (!is_object($instance)) { throw new InvalidArgumentException( @@ -169,6 +174,8 @@ public function instance(string $key, mixed $instance): void } $this->instances[$key] = $instance; + + return $this; } /** diff --git a/src/Event/EventProducer.php b/src/Event/EventProducer.php index c9830f8a..44c534c8 100644 --- a/src/Event/EventProducer.php +++ b/src/Event/EventProducer.php @@ -12,11 +12,13 @@ class EventProducer extends ProducerService * EventProducer constructor * * @param EventListener|EventShouldQueue $event + * @param mixed $payload */ public function __construct( private readonly mixed $event, private readonly mixed $payload = null, ) { + parent::__construct(); } /** From 11b7aae8897a28edb11a9f4d8e81b5e6019450eb Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Sat, 18 Jan 2025 01:17:14 +0000 Subject: [PATCH 2/2] feat: integrate the notification channel --- composer.json | 3 ++- src/Notification/Channel/ChannelInterface.php | 3 +-- src/Notification/Channel/DatabaseChannel.php | 11 ++++++--- src/Notification/Channel/MailChannel.php | 18 +++++++++++---- src/Notification/Notification.php | 4 ++-- src/Queue/Adapters/BeanstalkdAdapter.php | 23 ++++++++----------- src/Queue/Adapters/DatabaseAdapter.php | 21 +++++++++++------ src/Queue/Adapters/QueueAdapter.php | 17 +++++++------- src/Queue/Adapters/SQSAdapter.php | 1 + src/Queue/ProducerService.php | 7 +++--- src/Queue/WorkerService.php | 3 ++- src/Router/Router.php | 6 ++--- src/Security/Crypto.php | 2 +- 13 files changed, 70 insertions(+), 49 deletions(-) diff --git a/composer.json b/composer.json index f1a73098..44922a26 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "neitanod/forceutf8": "^2.0", "ramsey/uuid": "^4.7", "ext-ftp": "*", - "ext-openssl": "*" + "ext-openssl": "*", + "ext-pcntl": "*" }, "require-dev": { "pda/pheanstalk": "^5.0", diff --git a/src/Notification/Channel/ChannelInterface.php b/src/Notification/Channel/ChannelInterface.php index c6b528d5..390ae240 100644 --- a/src/Notification/Channel/ChannelInterface.php +++ b/src/Notification/Channel/ChannelInterface.php @@ -7,8 +7,7 @@ interface ChannelInterface /** * Send the notification * - * @param mixed $message * @return void */ - public function send(mixed $message): void; + public function send(): void; } diff --git a/src/Notification/Channel/DatabaseChannel.php b/src/Notification/Channel/DatabaseChannel.php index a37efc86..578ea2d5 100644 --- a/src/Notification/Channel/DatabaseChannel.php +++ b/src/Notification/Channel/DatabaseChannel.php @@ -2,17 +2,22 @@ namespace Bow\Notification\Channel; -use Bow\Notification\Channel\ChannelInterface; +use Bow\Database\Database; class DatabaseChannel implements ChannelInterface { + public function __construct( + private readonly array $database + ) { + } + /** * Send the notification to database * - * @param mixed $message * @return void */ - public function send(mixed $message): void + public function send(): void { + Database::table('notifications')->insert($this->database); } } diff --git a/src/Notification/Channel/MailChannel.php b/src/Notification/Channel/MailChannel.php index b5424426..16fca7e8 100644 --- a/src/Notification/Channel/MailChannel.php +++ b/src/Notification/Channel/MailChannel.php @@ -7,16 +7,24 @@ class MailChannel implements ChannelInterface { + /** + * Set the configured message + * + * @param Message $message + * @return void + */ + public function __construct( + private readonly Message $message + ) { + } + /** * Send the notification to mail * - * @param mixed $message * @return void */ - public function send(mixed $message): void + public function send(): void { - if ($message instanceof Message) { - Mail::getInstance()->send($message); - } + Mail::getInstance()->send($this->message); } } diff --git a/src/Notification/Notification.php b/src/Notification/Notification.php index 25a5dc27..76fd088e 100644 --- a/src/Notification/Notification.php +++ b/src/Notification/Notification.php @@ -61,8 +61,8 @@ final function process(Model $notifiable): void foreach ($channels as $channel) { if (array_key_exists($channel, $this->channels)) { $result = $this->{"to" . ucfirst($channel)}($notifiable); - $target_channel = new $this->channels[$channel](); - $target_channel->send($result); + $target_channel = new $this->channels[$channel]($result); + $target_channel->send(); } } } diff --git a/src/Queue/Adapters/BeanstalkdAdapter.php b/src/Queue/Adapters/BeanstalkdAdapter.php index 1b8d811a..1183a7f6 100644 --- a/src/Queue/Adapters/BeanstalkdAdapter.php +++ b/src/Queue/Adapters/BeanstalkdAdapter.php @@ -4,6 +4,8 @@ namespace Bow\Queue\Adapters; +use ErrorException; +use Pheanstalk\Values\TubeName; use RuntimeException; use Pheanstalk\Pheanstalk; use Bow\Queue\ProducerService; @@ -46,12 +48,12 @@ public function configure(array $config): BeanstalkdAdapter /** * Get the size of the queue. * - * @param string $queue + * @param string|null $queue * @return int */ public function size(?string $queue = null): int { - $queue = new \Pheanstalk\Values\TubeName($this->getQueue($queue)); + $queue = new TubeName($this->getQueue($queue)); return (int) $this->pheanstalk->statsTube($queue)->currentJobsReady; } @@ -61,7 +63,7 @@ public function size(?string $queue = null): int * * @param ProducerService $producer * @return void - * @throws \ErrorException + * @throws ErrorException */ public function push(ProducerService $producer): void { @@ -73,7 +75,7 @@ public function push(ProducerService $producer): void } $this->pheanstalk - ->useTube(new \Pheanstalk\Values\TubeName($producer->getQueue())); + ->useTube(new TubeName($producer->getQueue())); $this->pheanstalk->put( $this->serializeProducer($producer), @@ -87,23 +89,18 @@ public function push(ProducerService $producer): void * Run the worker * * @param string|null $queue - * @return mixed - * @throws \ErrorException + * @return void + * @throws ErrorException */ public function run(string $queue = null): void { // we want jobs from define queue only. $queue = $this->getQueue($queue); - $this->pheanstalk->watch(new \Pheanstalk\Values\TubeName($queue)); + $this->pheanstalk->watch(new TubeName($queue)); // This hangs until a Job is produced. $job = $this->pheanstalk->reserve(); - if (is_null($job)) { - sleep($this->sleep ?? 5); - return; - } - try { $payload = $job->getData(); $producer = $this->unserializeProducer($payload); @@ -144,7 +141,7 @@ public function run(string $queue = null): void * * @param string|null $queue * @return void - * @throws \ErrorException + * @throws ErrorException */ public function flush(?string $queue = null): void { diff --git a/src/Queue/Adapters/DatabaseAdapter.php b/src/Queue/Adapters/DatabaseAdapter.php index a4073d71..42e00f3c 100644 --- a/src/Queue/Adapters/DatabaseAdapter.php +++ b/src/Queue/Adapters/DatabaseAdapter.php @@ -3,8 +3,10 @@ namespace Bow\Queue\Adapters; use Bow\Database\Database; +use Bow\Database\Exception\QueryBuilderException; use Bow\Database\QueryBuilder; use Bow\Queue\ProducerService; +use ErrorException; class DatabaseAdapter extends QueueAdapter { @@ -31,8 +33,9 @@ public function configure(array $config): DatabaseAdapter /** * Get the size of the queue. * - * @param string $queue + * @param string|null $queue * @return int + * @throws QueryBuilderException */ public function size(?string $queue = null): int { @@ -45,7 +48,7 @@ public function size(?string $queue = null): int * Queue a job * * @param ProducerService $producer - * @return QueueAdapter + * @return void */ public function push(ProducerService $producer): void { @@ -65,7 +68,9 @@ public function push(ProducerService $producer): void * Run the worker * * @param string|null $queue - * @return mixed + * @return void + * @throws QueryBuilderException + * @throws ErrorException */ public function run(string $queue = null): void { @@ -107,10 +112,10 @@ public function run(string $queue = null): void } // Execute the onException method for notify the producer - // and let developper to decide if the job should be delete + // and let developer decide if the job should be deleted $producer->onException($e); - // Check if the job should be delete + // Check if the job should be deleted if ($producer->jobShouldBeDelete() || $job->attempts <= 0) { $this->table->where("id", $job->id)->update([ "status" => "failed", @@ -119,7 +124,7 @@ public function run(string $queue = null): void continue; } - // Check if the job should be retry + // Check if the job should be retried $this->table->where("id", $job->id)->update([ "status" => "reserved", "attempts" => $job->attempts - 1, @@ -137,8 +142,9 @@ public function run(string $queue = null): void * * @param ProducerService $producer * @param mixed $job + * @throws QueryBuilderException */ - private function execute(ProducerService $producer, mixed $job) + private function execute(ProducerService $producer, mixed $job): void { call_user_func([$producer, "process"]); $this->table->where("id", $job->id)->update([ @@ -152,6 +158,7 @@ private function execute(ProducerService $producer, mixed $job) * * @param ?string $queue * @return void + * @throws QueryBuilderException */ public function flush(?string $queue = null): void { diff --git a/src/Queue/Adapters/QueueAdapter.php b/src/Queue/Adapters/QueueAdapter.php index 714b19d1..d7b2ddc3 100644 --- a/src/Queue/Adapters/QueueAdapter.php +++ b/src/Queue/Adapters/QueueAdapter.php @@ -5,6 +5,7 @@ namespace Bow\Queue\Adapters; use Bow\Queue\ProducerService; +use JetBrains\PhpStorm\NoReturn; abstract class QueueAdapter { @@ -15,7 +16,7 @@ abstract class QueueAdapter /** * Define the start time * - * @var int + * @var float */ protected float $start_time; @@ -95,13 +96,13 @@ public function sleep(int $seconds): void } /** - * Laund the worker + * Launch the worker * * @param integer $timeout * @param integer $memory * @return void */ - final public function work(int $timeout, int $memory): void + #[NoReturn] final public function work(int $timeout, int $memory): void { [$this->start_time, $jobs_processed] = [hrtime(true) / 1e9, 0]; @@ -124,10 +125,10 @@ final public function work(int $timeout, int $memory): void /** * Kill the process. * - * @param int $status - * @return never + * @param int $status + * @return void */ - public function kill($status = 0) + #[NoReturn] public function kill(int $status = 0): void { if (extension_loaded('posix')) { posix_kill(getmypid(), SIGKILL); @@ -163,7 +164,7 @@ private function memoryExceeded(int $memory_timit): bool * * @return void */ - protected function listenForSignals() + protected function listenForSignals(): void { pcntl_async_signals(true); @@ -178,7 +179,7 @@ protected function listenForSignals() * * @return bool */ - protected function supportsAsyncSignals() + protected function supportsAsyncSignals(): bool { return extension_loaded('pcntl'); } diff --git a/src/Queue/Adapters/SQSAdapter.php b/src/Queue/Adapters/SQSAdapter.php index ab108597..332c03fb 100644 --- a/src/Queue/Adapters/SQSAdapter.php +++ b/src/Queue/Adapters/SQSAdapter.php @@ -105,6 +105,7 @@ public function run(?string $queue = null): void { $this->sleep($this->sleep ?? 5); $message = null; + $delay = 5; try { $result = $this->sqs->receiveMessage([ diff --git a/src/Queue/ProducerService.php b/src/Queue/ProducerService.php index e7c9a05b..8eee4455 100644 --- a/src/Queue/ProducerService.php +++ b/src/Queue/ProducerService.php @@ -5,6 +5,7 @@ namespace Bow\Queue; use Bow\Support\Serializes; +use Throwable; abstract class ProducerService { @@ -66,7 +67,7 @@ abstract class ProducerService */ public function __construct() { - $this->id = sha1(uniqid(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), true)); + $this->id = str_uuid(); } /** @@ -197,10 +198,10 @@ public function jobShouldBeDelete(): bool /** * Get the job error * - * @param \Throwable $e + * @param Throwable $e * @return void */ - public function onException(\Throwable $e) + public function onException(Throwable $e) { // } diff --git a/src/Queue/WorkerService.php b/src/Queue/WorkerService.php index d02454dc..b9177c7b 100644 --- a/src/Queue/WorkerService.php +++ b/src/Queue/WorkerService.php @@ -5,6 +5,7 @@ namespace Bow\Queue; use Bow\Queue\Adapters\QueueAdapter; +use JetBrains\PhpStorm\NoReturn; class WorkerService { @@ -36,7 +37,7 @@ public function setConnection(QueueAdapter $connection): void * @param int $memory * @return void */ - public function run( + #[NoReturn] public function run( string $queue = "default", int $tries = 3, int $sleep = 5, diff --git a/src/Router/Router.php b/src/Router/Router.php index 50678b0c..e0f1fb29 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -9,7 +9,7 @@ class Router { /** - * Define the functions related to an http + * Define the functions related to a http * code executed if this code is up * * @var array @@ -132,7 +132,7 @@ public function prefix(string $prefix, callable $cb): Router { $prefix = rtrim($prefix, '/'); - if (!preg_match('@^/@', $prefix)) { + if (!str_starts_with($prefix, '/')) { $prefix = '/' . $prefix; } @@ -146,7 +146,7 @@ public function prefix(string $prefix, callable $cb): Router } /** - * Allows to associate a global middleware on an route + * Allows to associate a global middleware on a route * * @param array|string $middlewares * @return Router diff --git a/src/Security/Crypto.php b/src/Security/Crypto.php index fc883589..9271433b 100644 --- a/src/Security/Crypto.php +++ b/src/Security/Crypto.php @@ -57,7 +57,7 @@ public static function encrypt(string $data): string|bool * * @param string $data * - * @return string + * @return string|bool */ public static function decrypt(string $data): string|bool {