|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Webmozarts Console Parallelization package. |
| 5 | + * |
| 6 | + * (c) Webmozarts GmbH <office@webmozarts.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Webmozarts\Console\Parallelization\Fixtures\Command; |
| 15 | + |
| 16 | +use LogicException; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | +use Symfony\Contracts\Service\Attribute\SubscribedService; |
| 20 | +use Symfony\Contracts\Service\ServiceSubscriberInterface; |
| 21 | +use Symfony\Contracts\Service\ServiceSubscriberTrait; |
| 22 | +use UnexpectedValueException; |
| 23 | +use Webmozarts\Console\Parallelization\ErrorHandler\ErrorHandler; |
| 24 | +use Webmozarts\Console\Parallelization\ErrorHandler\ResetServiceErrorHandler; |
| 25 | +use Webmozarts\Console\Parallelization\Fixtures\Counter; |
| 26 | +use Webmozarts\Console\Parallelization\Input\ParallelizationInput; |
| 27 | +use Webmozarts\Console\Parallelization\ParallelCommand; |
| 28 | +use Webmozarts\Console\Parallelization\ParallelExecutorFactory; |
| 29 | +use function array_map; |
| 30 | +use function range; |
| 31 | +use function strval; |
| 32 | + |
| 33 | +final class SubscribedServiceCommand extends ParallelCommand implements ServiceSubscriberInterface |
| 34 | +{ |
| 35 | + use ServiceSubscriberTrait; |
| 36 | + |
| 37 | + private bool $threwOnce = false; |
| 38 | + |
| 39 | + public function __construct() |
| 40 | + { |
| 41 | + parent::__construct('subscribed-service'); |
| 42 | + } |
| 43 | + |
| 44 | + protected function initialize(InputInterface $input, OutputInterface $output): void |
| 45 | + { |
| 46 | + // Ensure this command cannot be run with child processes. The purpose of this command |
| 47 | + // is to test that the service counter is properly reset, doing this with child processes |
| 48 | + // would require to persist its state across processes which would be needlessly complicated. |
| 49 | + $input->setOption(ParallelizationInput::MAIN_PROCESS_OPTION, true); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @return list<string> |
| 54 | + */ |
| 55 | + protected function fetchItems(InputInterface $input, OutputInterface $output): array |
| 56 | + { |
| 57 | + return array_map( |
| 58 | + strval(...), |
| 59 | + range(0, 3), |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + protected function getItemName(?int $count): string |
| 64 | + { |
| 65 | + return 1 === $count ? 'item' : 'items'; |
| 66 | + } |
| 67 | + |
| 68 | + protected function configureParallelExecutableFactory( |
| 69 | + ParallelExecutorFactory $parallelExecutorFactory, |
| 70 | + InputInterface $input, |
| 71 | + OutputInterface $output, |
| 72 | + ): ParallelExecutorFactory { |
| 73 | + return $parallelExecutorFactory |
| 74 | + ->withBatchSize(2) |
| 75 | + ->withRunAfterLastCommand($this->checkCounter(...)); |
| 76 | + } |
| 77 | + |
| 78 | + protected function runSingleCommand(string $item, InputInterface $input, OutputInterface $output): void |
| 79 | + { |
| 80 | + $counter = $this->counter(); |
| 81 | + |
| 82 | + if ($counter->getCount() >= 2 && !$this->threwOnce) { |
| 83 | + $this->threwOnce = true; |
| 84 | + |
| 85 | + throw new UnexpectedValueException('3rd item reached.'); |
| 86 | + } |
| 87 | + |
| 88 | + $counter->increment(); |
| 89 | + } |
| 90 | + |
| 91 | + private function checkCounter(): void |
| 92 | + { |
| 93 | + $counter = $this->counter(); |
| 94 | + $count = $counter->getCount(); |
| 95 | + |
| 96 | + if ($count >= 2) { |
| 97 | + throw new LogicException('The Counter service was not correctly reset.'); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + #[SubscribedService] |
| 102 | + private function counter(): Counter |
| 103 | + { |
| 104 | + return $this->container->get(__METHOD__); |
| 105 | + } |
| 106 | + |
| 107 | + protected function createErrorHandler(InputInterface $input, OutputInterface $output): ErrorHandler |
| 108 | + { |
| 109 | + return ResetServiceErrorHandler::forContainer($this->getContainer()); |
| 110 | + } |
| 111 | +} |
0 commit comments