From c5b7155dc5d2624d1cec36e6b1bbf4b46145e796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammet=20=C5=9EAFAK?= Date: Sat, 31 Aug 2024 13:45:11 +0300 Subject: [PATCH] 0.0.2 --- App/Commands/{ => Mail}/MailConsumer.php | 21 ++++----- App/Commands/Mail/MailProducer.php | 34 ++++++++++++++ App/Commands/MailProducer.php | 36 -------------- App/Commands/Payments/PaymentConsumer.php | 40 ++++++++++++++++ App/Commands/SMS/SMSConsumer.php | 31 ++++++++++++ App/Commands/SMS/SMSProducer.php | 37 +++++++++++++++ App/Helpers/general_helper.php | 6 --- App/Helpers/payment_helper.php | 5 ++ App/Messages/MailMessage.php | 40 ++++++++++++++++ App/Messages/SMSMessage.php | 31 ++++++++++++ App/Queues/NotificationQueue.php | 32 +++++++++++++ .../{MailQueue.php => PaymentQueue.php} | 6 +-- README.md | 21 +++++++++ Sys/Application.php | 40 ++++++++++++---- Sys/Commands/AppTruncateCommand.php | 47 +++++++++++++++++++ {App => Sys}/Commands/MakeCommand.php | 2 +- Sys/Helpers/system_helper.php | 7 +++ application => qmanager | 4 ++ 18 files changed, 373 insertions(+), 67 deletions(-) rename App/Commands/{ => Mail}/MailConsumer.php (51%) create mode 100644 App/Commands/Mail/MailProducer.php delete mode 100644 App/Commands/MailProducer.php create mode 100644 App/Commands/Payments/PaymentConsumer.php create mode 100644 App/Commands/SMS/SMSConsumer.php create mode 100644 App/Commands/SMS/SMSProducer.php create mode 100644 App/Helpers/payment_helper.php create mode 100644 App/Messages/MailMessage.php create mode 100644 App/Messages/SMSMessage.php create mode 100644 App/Queues/NotificationQueue.php rename App/Queues/{MailQueue.php => PaymentQueue.php} (85%) create mode 100644 Sys/Commands/AppTruncateCommand.php rename {App => Sys}/Commands/MakeCommand.php (98%) create mode 100644 Sys/Helpers/system_helper.php rename application => qmanager (84%) diff --git a/App/Commands/MailConsumer.php b/App/Commands/Mail/MailConsumer.php similarity index 51% rename from App/Commands/MailConsumer.php rename to App/Commands/Mail/MailConsumer.php index 7e91915..1bec050 100644 --- a/App/Commands/MailConsumer.php +++ b/App/Commands/Mail/MailConsumer.php @@ -1,11 +1,11 @@ consume(function (MessageInterface $message) { - $payload = $message->getPayload(); - if (filter_var($payload['to_mail'], FILTER_VALIDATE_EMAIL)) { - sleep(2); + // The workers that will work for NotificationQueue are defined in Message. - return true; - } - - return false; + return true; }); return Command::SUCCESS; diff --git a/App/Commands/Mail/MailProducer.php b/App/Commands/Mail/MailProducer.php new file mode 100644 index 0000000..aa4b589 --- /dev/null +++ b/App/Commands/Mail/MailProducer.php @@ -0,0 +1,34 @@ +mail('example' . $i . '@example.com', 'Welcome ' . $i, ''); + + $queue->publish($message); + } + + return Command::SUCCESS; + } + + public function configure(): void + { + $this->setDescription(""); + } + +} diff --git a/App/Commands/MailProducer.php b/App/Commands/MailProducer.php deleted file mode 100644 index 39df52e..0000000 --- a/App/Commands/MailProducer.php +++ /dev/null @@ -1,36 +0,0 @@ -setPayload([ - 'to_mail' => 'example' . $i . '@example.com', - ]); - - $queue->publish($message); - } - - return Command::SUCCESS; - } - - public function configure(): void - { - $this->setDescription(""); - } - -} diff --git a/App/Commands/Payments/PaymentConsumer.php b/App/Commands/Payments/PaymentConsumer.php new file mode 100644 index 0000000..7f5d7b9 --- /dev/null +++ b/App/Commands/Payments/PaymentConsumer.php @@ -0,0 +1,40 @@ +consume(function (MessageInterface $message) { + try { + $payload = $message->getPayload(); + + return credit_card_payment($payload['credit_card_number'], + $payload['credit_card_holder_name'], + $payload['credit_card_expiry'], + $payload['credit_card_cvv'], $payload['amount']); + + } catch (\Throwable $e) { + return false; + } + }); + + return Command::SUCCESS; + } + + public function configure(): void + { + $this->setDescription(""); + } + +} diff --git a/App/Commands/SMS/SMSConsumer.php b/App/Commands/SMS/SMSConsumer.php new file mode 100644 index 0000000..31bcbdc --- /dev/null +++ b/App/Commands/SMS/SMSConsumer.php @@ -0,0 +1,31 @@ +consume(function (MessageInterface $message) { + // The workers that will work for NotificationQueue are defined in Message. + + return true; + }); + return Command::SUCCESS; + } + + public function configure(): void + { + $this->setDescription(""); + } + +} diff --git a/App/Commands/SMS/SMSProducer.php b/App/Commands/SMS/SMSProducer.php new file mode 100644 index 0000000..58faa4b --- /dev/null +++ b/App/Commands/SMS/SMSProducer.php @@ -0,0 +1,37 @@ +setPayload([ + 'phone_number' => '+9011111111' . $i + ]); + + $queue->publish($message); + } + + return Command::SUCCESS; + } + + public function configure(): void + { + $this->setDescription(""); + } + +} diff --git a/App/Helpers/general_helper.php b/App/Helpers/general_helper.php index 75e2591..b3d9bbc 100644 --- a/App/Helpers/general_helper.php +++ b/App/Helpers/general_helper.php @@ -1,7 +1 @@ getPayload(); + if (filter_var($payload['to_mail'], FILTER_VALIDATE_EMAIL)) { + sleep(2); + + return true; + } + + return false; + } catch (\Throwable $e) { + return false; + } + } + + public function mail(string $toMail, string $subject, string $body) + { + $this->setPayload([ + 'to_mail' => $toMail, + 'subject' => $subject, + 'body' => $body, + ]); + } + +} diff --git a/App/Messages/SMSMessage.php b/App/Messages/SMSMessage.php new file mode 100644 index 0000000..3eb6a75 --- /dev/null +++ b/App/Messages/SMSMessage.php @@ -0,0 +1,31 @@ +getPayload(); + if (!empty($payload['phone_number'])) { + sleep(2); + + return true; + } + + return false; + } catch (\Throwable $e) { + return false; + } + } + +} diff --git a/App/Queues/NotificationQueue.php b/App/Queues/NotificationQueue.php new file mode 100644 index 0000000..d45cc2f --- /dev/null +++ b/App/Queues/NotificationQueue.php @@ -0,0 +1,32 @@ + env("RABBITMQ_HOST", "localhost"), + 'port' => env("RABBITMQ_PORT", 5672), + 'username' => env("RABBITMQ_USER", "guest"), + 'password' => env("RABBITMQ_PASS", "guest"), + ]); + + parent::__construct($adapter); + } + + public function getName(): string + { + return 'notification_queue'; + } + + public function getDLQName(): string + { + return 'notification_queue_dead_letter_queue'; + } + +} diff --git a/App/Queues/MailQueue.php b/App/Queues/PaymentQueue.php similarity index 85% rename from App/Queues/MailQueue.php rename to App/Queues/PaymentQueue.php index 5663c13..ab1362b 100644 --- a/App/Queues/MailQueue.php +++ b/App/Queues/PaymentQueue.php @@ -5,7 +5,7 @@ use PHPQueueManager\PHPQueueManager\Adapters\AdapterFactory; use PHPQueueManager\PHPQueueManager\Queue\Queue; -class MailQueue extends Queue +class PaymentQueue extends Queue { public function __construct() @@ -22,12 +22,12 @@ public function __construct() public function getName(): string { - return 'mail_queue'; + return 'payment_queue'; } public function getDLQName(): string { - return 'mail_queue_dead_letter_queue'; + return 'payment_queue_dead_letter_queue'; } } diff --git a/README.md b/README.md index 5c2cc8f..031a3d4 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,27 @@ This application was created to easily create and manage your own job queues. ``` composer create-project phpqueuemanager/application ``` +Check out the sample template under the "App" directory before you start developing! And then when you are ready to develop your own app you can execute the following command to **destroy everything in the "App" directory**. + +``` +php qmanager app:truncate +``` + +You will also need plugins appropriate for the AMQP system you use. + +#### RabbitMQ + +- [https://pecl.php.net/package/amqp](https://pecl.php.net/package/amqp) +- Include the `php-amqplib/php-amqplib` package in your project. + +``` +composer require php-amqplib/php-amqplib +``` + +#### Kafka + +- [https://pecl.php.net/package/rdkafka](https://pecl.php.net/package/rdkafka) +- [php.net Documentation](https://arnaud.le-blanc.net/php-rdkafka-doc/phpdoc/rdkafka.setup.html) ## Getting Help diff --git a/Sys/Application.php b/Sys/Application.php index f897396..6927e85 100644 --- a/Sys/Application.php +++ b/Sys/Application.php @@ -14,11 +14,9 @@ public function boot(): self { is_file(ROOT_DIR . '.env') && (new Dotenv())->load(ROOT_DIR . '.env'); - $helpers = glob(APP_DIR . "Helpers/*.php"); + $helpers = array_merge($this->scanFiles(APP_DIR . 'Helpers'), $this->scanFiles(SYS_DIR . 'Helpers')); foreach ($helpers as $helper) { - if (file_exists($helper)) { - require_once $helper; - } + require_once $helper; } return $this; @@ -29,17 +27,43 @@ public function boot(): self */ public function run(): void { - $console = new \Symfony\Component\Console\Application("Bulutklinik CLI", "1.0"); - $commands = glob(APP_DIR . "Commands/*.php"); - foreach ($commands as $command) { + $console = new \Symfony\Component\Console\Application(APP_NAME, APP_VERSION); - $commandClass = '\\App\\Commands\\' . basename($command, '.php'); + $sysCommands = $this->scanFiles(SYS_DIR . 'Commands'); + foreach ($sysCommands as $sysCommand) { + $commandClass = '\\Sys\\Commands\\' . str_replace([SYS_DIR . 'Commands' . DIRECTORY_SEPARATOR, '.php', '/'], ['', '', '\\'], $sysCommand); if (!class_exists($commandClass)) { continue; } $console->add(new $commandClass()); } + + $appCommands = $this->scanFiles(APP_DIR . 'Commands'); + foreach ($appCommands as $appCommand) { + $commandClass = '\\App\\Commands\\' . str_replace([APP_DIR . 'Commands' . DIRECTORY_SEPARATOR, '.php', '/'], ['', '', '\\'], $appCommand); + if (!class_exists($commandClass)) { + continue; + } + $console->add(new $commandClass()); + } + $console->run(); } + + private function scanFiles(string $directory, string $extension = 'php'): array + { + $files = []; + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory)); + + foreach ($iterator as $file) { + if ($file->isFile() && $file->getExtension() === $extension) { + $files[] = $file->getPathname(); + } + } + + return $files; + } + + } diff --git a/Sys/Commands/AppTruncateCommand.php b/Sys/Commands/AppTruncateCommand.php new file mode 100644 index 0000000..58df929 --- /dev/null +++ b/Sys/Commands/AppTruncateCommand.php @@ -0,0 +1,47 @@ +deleteExcept(APP_DIR, [ + APP_DIR . "Queues", + APP_DIR . "Messages", + APP_DIR . "Helpers", + APP_DIR . "Commands", + ]); + return Command::SUCCESS; + } + + public function configure(): void + { + $this->setDescription('Be Careful! This command completely empties the App directory.') + ->setHelp(''); + } + + private function deleteExcept($dir, $except = []) { + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST); + + foreach ($iterator as $file) { + $filePath = $file->getPathname(); + + if (in_array($filePath, $except)) { + continue; + } + + $file->isDir() ? rmdir($filePath) : unlink($filePath); + } + } + +} diff --git a/App/Commands/MakeCommand.php b/Sys/Commands/MakeCommand.php similarity index 98% rename from App/Commands/MakeCommand.php rename to Sys/Commands/MakeCommand.php index f19e56c..f2d9bba 100644 --- a/App/Commands/MakeCommand.php +++ b/Sys/Commands/MakeCommand.php @@ -1,5 +1,5 @@ boot()