Skip to content

Commit ead0471

Browse files
author
Vincent Chalnot
committed
Initial commit
0 parents  commit ead0471

File tree

8 files changed

+329
-0
lines changed

8 files changed

+329
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/composer.lock
2+
/vendor

CleverAgeFlysystemProcessBundle.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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;
12+
13+
use Symfony\Component\HttpKernel\Bundle\Bundle;
14+
15+
/**
16+
* Class CleverAgeFlysystemProcessBundle
17+
*
18+
* @author Valentin Clavreul <[email protected]>
19+
* @author Vincent Chalnot <[email protected]>
20+
* @author Madeline Veyrenc <[email protected]>
21+
*/
22+
class CleverAgeFlysystemProcessBundle extends Bundle
23+
{
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\DependencyInjection;
12+
13+
use Sidus\BaseBundle\DependencyInjection\SidusBaseExtension;
14+
15+
/**
16+
* This is the class that loads and manages your bundle configuration.
17+
*
18+
* @see http://symfony.com/doc/current/cookbook/bundles/extension.html
19+
*
20+
* @author Valentin Clavreul <[email protected]>
21+
* @author Vincent Chalnot <[email protected]>
22+
* @author Madeline Veyrenc <[email protected]>
23+
*/
24+
class CleverAgeFlysystemProcessExtension extends SidusBaseExtension
25+
{
26+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2015-2019 Clever-Age
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CleverAge/FlysystemProcessBundle
2+
=======================
3+
4+
See process bundle documentation

Resources/config/services/task.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
CleverAge\FlysystemProcessBundle\Task\:
3+
resource: '../../../Task/*'
4+
autowire: true
5+
public: true
6+
shared: false
7+
tags:
8+
- { name: monolog.logger, channel: cleverage_process_task }

Task/FileFetchTask.php

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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\FileNotFoundException;
17+
use League\Flysystem\FilesystemInterface;
18+
use League\Flysystem\FilesystemNotFoundException;
19+
use League\Flysystem\MountManager;
20+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
21+
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
22+
use Symfony\Component\OptionsResolver\OptionsResolver;
23+
24+
/**
25+
* Copy (or move) file from one filesystem to another, using Flysystem
26+
* Either get files using a file regexp, or take files from input
27+
*
28+
* @author Madeline Veyrenc <[email protected]>
29+
*/
30+
class FileFetchTask extends AbstractConfigurableTask implements IterableTaskInterface
31+
{
32+
/** @var MountManager */
33+
protected $mountManager;
34+
35+
/** @var FilesystemInterface */
36+
protected $sourceFS;
37+
38+
/** @var FilesystemInterface */
39+
protected $destinationFS;
40+
41+
/** @var array */
42+
protected $matchingFiles = [];
43+
44+
/**
45+
* @param MountManager|null $mountManager
46+
*/
47+
public function __construct(MountManager $mountManager = null)
48+
{
49+
$this->mountManager = $mountManager;
50+
}
51+
52+
/**
53+
* @param ProcessState $state
54+
*
55+
* @throws \InvalidArgumentException
56+
* @throws ExceptionInterface
57+
* @throws FilesystemNotFoundException
58+
*/
59+
public function initialize(ProcessState $state)
60+
{
61+
if (!$this->mountManager) {
62+
throw new ServiceNotFoundException('MountManager service not found, you need to install FlySystemBundle');
63+
}
64+
// Configure options
65+
parent::initialize($state);
66+
67+
$this->sourceFS = $this->mountManager->getFilesystem($this->getOption($state, 'source_filesystem'));
68+
$this->destinationFS = $this->mountManager->getFilesystem($this->getOption($state, 'destination_filesystem'));
69+
}
70+
71+
/**
72+
* @param ProcessState $state
73+
*
74+
* @throws \InvalidArgumentException
75+
* @throws ExceptionInterface
76+
* @throws \UnexpectedValueException
77+
* @throws FilesystemNotFoundException
78+
* @throws FileNotFoundException
79+
*/
80+
public function execute(ProcessState $state): void
81+
{
82+
$this->findMatchingFiles($state);
83+
84+
$file = current($this->matchingFiles);
85+
if (!$file) {
86+
$state->setSkipped(true);
87+
88+
return;
89+
}
90+
91+
$this->doFileCopy($state, $file, $this->getOption($state, 'remove_source'));
92+
$state->setOutput($file);
93+
}
94+
95+
/**
96+
* @param ProcessState $state
97+
*
98+
* @throws \UnexpectedValueException
99+
* @throws ExceptionInterface
100+
* @throws \InvalidArgumentException
101+
*
102+
* @return bool|mixed
103+
*/
104+
public function next(ProcessState $state)
105+
{
106+
$this->findMatchingFiles($state);
107+
108+
return next($this->matchingFiles);
109+
}
110+
111+
/**
112+
* @param ProcessState $state
113+
*
114+
* @throws \UnexpectedValueException
115+
* @throws \InvalidArgumentException
116+
* @throws ExceptionInterface
117+
*/
118+
protected function findMatchingFiles(ProcessState $state)
119+
{
120+
$filePattern = $this->getOption($state, 'file_pattern');
121+
if ($filePattern) {
122+
foreach ($this->sourceFS->listContents('/') as $file) {
123+
if ('file' === $file['type']
124+
&& preg_match($filePattern, $file['path'])
125+
&& !\in_array($file['path'], $this->matchingFiles, true)) {
126+
$this->matchingFiles[] = $file['path'];
127+
}
128+
}
129+
} else {
130+
$input = $state->getInput();
131+
if (!$input) {
132+
throw new \UnexpectedValueException('No pattern neither input provided for the Task');
133+
}
134+
if (\is_array($input)) {
135+
foreach ($input as $file) {
136+
if (!\in_array($file, $this->matchingFiles, true)) {
137+
$this->matchingFiles[] = $file;
138+
}
139+
}
140+
} elseif (!\in_array($input, $this->matchingFiles, true)) {
141+
$this->matchingFiles[] = $input;
142+
}
143+
}
144+
}
145+
146+
/**
147+
* @param ProcessState $state
148+
* @param string $filename
149+
* @param bool $removeSource
150+
*
151+
* @throws FileNotFoundException
152+
* @throws ExceptionInterface
153+
* @throws FilesystemNotFoundException
154+
* @throws \InvalidArgumentException
155+
*
156+
* @return mixed
157+
*/
158+
protected function doFileCopy(ProcessState $state, $filename, $removeSource)
159+
{
160+
$prefixFrom = $this->getOption($state, 'source_filesystem');
161+
$prefixTo = $this->getOption($state, 'destination_filesystem');
162+
163+
$buffer = $this->mountManager->getFilesystem($prefixFrom)->readStream($filename);
164+
165+
if (false === $buffer) {
166+
return false;
167+
}
168+
169+
$result = $this->mountManager->getFilesystem($prefixTo)->putStream($filename, $buffer);
170+
171+
if (\is_resource($buffer)) {
172+
fclose($buffer);
173+
}
174+
175+
if ($removeSource) {
176+
$this->mountManager->delete(sprintf('%s://%s', $prefixFrom, $filename));
177+
}
178+
179+
return $result ? $filename : null;
180+
}
181+
182+
/**
183+
* {@inheritdoc}
184+
*/
185+
protected function configureOptions(OptionsResolver $resolver)
186+
{
187+
$resolver->setRequired(['source_filesystem', 'destination_filesystem']);
188+
$resolver->setAllowedTypes('source_filesystem', 'string');
189+
$resolver->setAllowedTypes('destination_filesystem', 'string');
190+
191+
$resolver->setDefault('file_pattern', null);
192+
$resolver->setAllowedTypes('file_pattern', ['string', 'null']);
193+
194+
$resolver->setDefault('remove_source', false);
195+
$resolver->setAllowedTypes('remove_source', 'boolean');
196+
}
197+
}

composer.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "cleverage/flysystem-process-bundle",
3+
"description": "Dedicated bundle for Flysystem dependencies for the Process Bundle",
4+
"keywords": [
5+
"process",
6+
"task",
7+
"etl",
8+
"transformation",
9+
"import",
10+
"export",
11+
"flysystem"
12+
],
13+
"homepage": "https://github.com/cleverage/flysystem-process-bundle",
14+
"type": "symfony-bundle",
15+
"license": "MIT",
16+
"authors": [
17+
{
18+
"name": "Vincent Chalnot",
19+
"email": "[email protected]",
20+
"homepage": "http://chalnot.fr",
21+
"role": "Lead Developer"
22+
},
23+
{
24+
"name": "Valentin Clavreul",
25+
"email": "[email protected]",
26+
"role": "Developer"
27+
},
28+
{
29+
"name": "Madeline Veyrenc",
30+
"email": "[email protected]",
31+
"homepage": "https://github.com/mveyrenc",
32+
"role": "Developer"
33+
}
34+
],
35+
"autoload": {
36+
"psr-4": {
37+
"CleverAge\\FlysystemProcessBundle\\": ""
38+
}
39+
},
40+
"require": {
41+
"cleverage/process-bundle": "3.*|dev-v3.0-dev",
42+
"oneup/flysystem-bundle": ">1.0,<3.0"
43+
},
44+
"require-dev": {
45+
"phpunit/phpunit": "~6.4"
46+
}
47+
}

0 commit comments

Comments
 (0)