|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the CleverAge/ProcessBundle package. |
| 7 | + * |
| 8 | + * Copyright (c) Clever-Age |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CleverAge\ProcessBundle\Task\File; |
| 15 | + |
| 16 | +use CleverAge\ProcessBundle\Filesystem\SplFile; |
| 17 | +use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; |
| 18 | +use CleverAge\ProcessBundle\Model\IterableTaskInterface; |
| 19 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 20 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 21 | + |
| 22 | +/** |
| 23 | + * Split long file into smaller ones. |
| 24 | + */ |
| 25 | +class FileSplitterTask extends AbstractConfigurableTask implements IterableTaskInterface |
| 26 | +{ |
| 27 | + protected ?SplFile $file = null; |
| 28 | + |
| 29 | + private ?array $splFileObjectFlags = null; |
| 30 | + |
| 31 | + private int $lineCount; |
| 32 | + |
| 33 | + public function execute(ProcessState $state): void |
| 34 | + { |
| 35 | + $options = $this->getMergedOptions($state); |
| 36 | + $this->splFileObjectFlags = [\SplFileObject::READ_AHEAD, \SplFileObject::SKIP_EMPTY]; |
| 37 | + if (!$this->file instanceof SplFile) { |
| 38 | + $this->file = new SplFile($options['file_path'], 'rb', $this->splFileObjectFlags); |
| 39 | + $this->lineCount = $this->file->getLineCount(); |
| 40 | + } |
| 41 | + |
| 42 | + // Return a temporary file containing a limited number of lines |
| 43 | + $splittedFilename = $this->splitFile($this->file, $options['max_lines']); |
| 44 | + $state->setOutput($splittedFilename); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Moves the internal pointer to the next element, |
| 49 | + * return true if the task has a next element |
| 50 | + * return false if the task has terminated it's iteration. |
| 51 | + */ |
| 52 | + public function next(ProcessState $state): bool |
| 53 | + { |
| 54 | + if (!$this->file instanceof SplFile) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + // Fix issue on PHP 8 with empty line at the end, even if SKIP_EMPTY is set |
| 59 | + $endOfFile = $this->file->isEndOfFile() || $this->file->getLineNumber() > $this->lineCount; |
| 60 | + if ($endOfFile) { |
| 61 | + $this->file = null; |
| 62 | + } |
| 63 | + |
| 64 | + return !$endOfFile; |
| 65 | + } |
| 66 | + |
| 67 | + protected function splitFile(SplFile $file, int $maxLines): string |
| 68 | + { |
| 69 | + $tmpFilePath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'php_'.uniqid('process', false).'.tmp'; |
| 70 | + $splitFile = new SplFile($tmpFilePath, 'wb', $this->splFileObjectFlags); |
| 71 | + |
| 72 | + while ($splitFile->getLineNumber() <= $maxLines && !$file->isEndOfFile()) { |
| 73 | + $line = $file->readLine(); |
| 74 | + if ('' === $line || null === $line) { |
| 75 | + continue; // This is probably an empty line, no harm to skip it |
| 76 | + } |
| 77 | + $splitFile->writeLine($line); |
| 78 | + } |
| 79 | + |
| 80 | + return $tmpFilePath; |
| 81 | + } |
| 82 | + |
| 83 | + protected function configureOptions(OptionsResolver $resolver): void |
| 84 | + { |
| 85 | + $resolver->setRequired(['file_path']); |
| 86 | + $resolver->setAllowedTypes('file_path', ['string']); |
| 87 | + $resolver->setDefaults([ |
| 88 | + 'max_lines' => 1000, |
| 89 | + ]); |
| 90 | + $resolver->setAllowedTypes('max_lines', ['int']); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return array<mixed> |
| 95 | + */ |
| 96 | + protected function getMergedOptions(ProcessState $state): array |
| 97 | + { |
| 98 | + /** @var array<mixed> $options */ |
| 99 | + $options = $this->getOptions($state); |
| 100 | + |
| 101 | + /** @var array<mixed>|mixed $input */ |
| 102 | + $input = $state->getInput() ?: []; |
| 103 | + if (!\is_array($input)) { |
| 104 | + $input = []; |
| 105 | + } |
| 106 | + // @var array<mixed> $input |
| 107 | + |
| 108 | + return array_merge($options, $input); |
| 109 | + } |
| 110 | +} |
0 commit comments