|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
| 6 | + * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/) |
| 7 | + * |
| 8 | + * Licensed under The MIT License |
| 9 | + * For full copyright and license information, please see the LICENSE.txt |
| 10 | + * Redistributions of files must retain the above copyright notice. |
| 11 | + * |
| 12 | + * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/) |
| 13 | + * @link https://cakephp.org CakePHP(tm) Project |
| 14 | + * @since 2.2.1 |
| 15 | + * @license https://opensource.org/licenses/MIT MIT License |
| 16 | + */ |
| 17 | +namespace Cake\Queue; |
| 18 | + |
| 19 | +use Bake\Command\SimpleBakeCommand; |
| 20 | +use Cake\Console\CommandCollection; |
| 21 | +use Cake\Core\BasePlugin; |
| 22 | +use Cake\Core\Configure; |
| 23 | +use Cake\Core\ContainerInterface; |
| 24 | +use Cake\Core\PluginApplicationInterface; |
| 25 | +use Cake\Queue\Command\JobCommand; |
| 26 | +use Cake\Queue\Command\PurgeFailedCommand; |
| 27 | +use Cake\Queue\Command\RequeueCommand; |
| 28 | +use Cake\Queue\Command\WorkerCommand; |
| 29 | +use InvalidArgumentException; |
| 30 | + |
| 31 | +/** |
| 32 | + * Plugin for Queue |
| 33 | + */ |
| 34 | +class QueuePlugin extends BasePlugin |
| 35 | +{ |
| 36 | + /** |
| 37 | + * Plugin name. |
| 38 | + */ |
| 39 | + protected ?string $name = 'Cake/Queue'; |
| 40 | + |
| 41 | + /** |
| 42 | + * Load routes or not |
| 43 | + */ |
| 44 | + protected bool $routesEnabled = false; |
| 45 | + |
| 46 | + /** |
| 47 | + * Load the Queue configuration |
| 48 | + * |
| 49 | + * @param \Cake\Core\PluginApplicationInterface $app The host application |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function bootstrap(PluginApplicationInterface $app): void |
| 53 | + { |
| 54 | + if (!Configure::read('Queue')) { |
| 55 | + throw new InvalidArgumentException( |
| 56 | + 'Missing `Queue` configuration key, please check the CakePHP Queue documentation' . |
| 57 | + ' to complete the plugin setup.', |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + foreach (Configure::read('Queue') as $key => $data) { |
| 62 | + if (QueueManager::getConfig($key) === null) { |
| 63 | + QueueManager::setConfig($key, $data); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Add console commands for the plugin. |
| 70 | + * |
| 71 | + * @param \Cake\Console\CommandCollection $commands The command collection to update |
| 72 | + * @return \Cake\Console\CommandCollection |
| 73 | + */ |
| 74 | + public function console(CommandCollection $commands): CommandCollection |
| 75 | + { |
| 76 | + if (class_exists(SimpleBakeCommand::class)) { |
| 77 | + $commands->add('bake job', JobCommand::class); |
| 78 | + } |
| 79 | + |
| 80 | + return $commands |
| 81 | + ->add('queue worker', WorkerCommand::class) |
| 82 | + ->add('worker', WorkerCommand::class) |
| 83 | + ->add('queue requeue', RequeueCommand::class) |
| 84 | + ->add('queue purge_failed', PurgeFailedCommand::class); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Add DI container to Worker command |
| 89 | + * |
| 90 | + * @param \Cake\Core\ContainerInterface $container The DI container |
| 91 | + * @return void |
| 92 | + */ |
| 93 | + public function services(ContainerInterface $container): void |
| 94 | + { |
| 95 | + $container->add(ContainerInterface::class, $container); |
| 96 | + $container |
| 97 | + ->add(WorkerCommand::class) |
| 98 | + ->addArgument(ContainerInterface::class); |
| 99 | + } |
| 100 | +} |
0 commit comments