|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * This file is part of the CleverAge/FlysystemProcessBundle package. |
| 4 | + * |
| 5 | + * Copyright (C) 2017-2019 Clever-Age |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace CleverAge\FlysystemProcessBundle\Task; |
| 12 | + |
| 13 | +use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; |
| 14 | +use CleverAge\ProcessBundle\Model\IterableTaskInterface; |
| 15 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 16 | +use League\Flysystem\FilesystemInterface; |
| 17 | +use League\Flysystem\MountManager; |
| 18 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 19 | + |
| 20 | +/** |
| 21 | + * Iterate over the content of a filesystem |
| 22 | + */ |
| 23 | +class ListContentTask extends AbstractConfigurableTask implements IterableTaskInterface |
| 24 | +{ |
| 25 | + use FilesystemOptionTrait; |
| 26 | + |
| 27 | + /** @var MountManager */ |
| 28 | + protected $mountManager; |
| 29 | + |
| 30 | + /** @var array|null */ |
| 31 | + protected $fsContent = null; |
| 32 | + |
| 33 | + /** |
| 34 | + * ListContentTask constructor. |
| 35 | + * |
| 36 | + * @param MountManager $mountManager |
| 37 | + */ |
| 38 | + public function __construct(MountManager $mountManager) |
| 39 | + { |
| 40 | + $this->mountManager = $mountManager; |
| 41 | + } |
| 42 | + |
| 43 | + protected function configureOptions(OptionsResolver $resolver) |
| 44 | + { |
| 45 | + $this->configureFilesystemOption($resolver, 'filesystem'); |
| 46 | + |
| 47 | + $resolver->setDefault('file_pattern', null); |
| 48 | + $resolver->setAllowedTypes('file_pattern', ['null', 'string']); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param ProcessState $state |
| 53 | + * |
| 54 | + * @throws \Symfony\Component\OptionsResolver\Exception\ExceptionInterface |
| 55 | + */ |
| 56 | + public function execute(ProcessState $state) |
| 57 | + { |
| 58 | + if ($this->fsContent === null || key($this->fsContent) === null) { |
| 59 | + $filesystem = $this->getFilesystem($state, 'filesystem'); |
| 60 | + $pattern = $this->getOption($state, 'file_pattern'); |
| 61 | + |
| 62 | + $this->fsContent = $this->getFilteredFilesystemContents($filesystem, $pattern); |
| 63 | + } |
| 64 | + |
| 65 | + if (key($this->fsContent) === null) { |
| 66 | + $state->setSkipped(true); |
| 67 | + $this->fsContent = null; |
| 68 | + } else { |
| 69 | + $state->setOutput(current($this->fsContent)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public function next(ProcessState $state) |
| 74 | + { |
| 75 | + if (!is_array($this->fsContent)) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + next($this->fsContent); |
| 80 | + |
| 81 | + return key($this->fsContent) !== null; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + /** |
| 86 | + * @param FilesystemInterface $filesystem |
| 87 | + * @param string|null $pattern |
| 88 | + * |
| 89 | + * @return array |
| 90 | + */ |
| 91 | + protected function getFilteredFilesystemContents(FilesystemInterface $filesystem, $pattern = null): array |
| 92 | + { |
| 93 | + $results = []; |
| 94 | + foreach ($filesystem->listContents() as $item) { |
| 95 | + if ($pattern === null || \preg_match($pattern, $item['path'])) { |
| 96 | + $results[] = $item; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return $results; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return MountManager |
| 105 | + */ |
| 106 | + protected function getMountManager(): MountManager |
| 107 | + { |
| 108 | + return $this->mountManager; |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments