1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* base abstract for import based commands where a bunch of file must be collected and |
4
|
|
|
* done something with them.. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Graviton\ImportExport\Command; |
8
|
|
|
|
9
|
|
|
use Graviton\ImportExport\Exception\MissingTargetException; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Finder\Finder; |
14
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author List of contributors <https://github.com/libgraviton/import-export/graphs/contributors> |
18
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
19
|
|
|
* @link http://swisscom.ch |
20
|
|
|
*/ |
21
|
|
|
abstract class ImportCommandAbstract extends Command |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Finder |
25
|
|
|
*/ |
26
|
|
|
protected $finder; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Finder $finder finder instance |
30
|
|
|
*/ |
31
|
6 |
|
public function __construct( |
32
|
|
|
Finder $finder |
33
|
|
|
) { |
34
|
6 |
|
$this->finder = $finder; |
35
|
6 |
|
parent::__construct(); |
36
|
6 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Executes the current command. |
40
|
|
|
* |
41
|
|
|
* @param InputInterface $input User input on console |
42
|
|
|
* @param OutputInterface $output Output of the command |
43
|
|
|
* |
44
|
|
|
* @return integer |
45
|
|
|
*/ |
46
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
47
|
|
|
{ |
48
|
6 |
|
$exitCode = 1; |
49
|
|
|
try { |
50
|
6 |
|
$exitCode = $this->doImport($this->getFinder($input), $input, $output); |
|
|
|
|
51
|
6 |
|
} catch (MissingTargetException $e) { |
52
|
1 |
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
53
|
|
|
} |
54
|
6 |
|
return $exitCode; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns a Finder according to the input params |
59
|
|
|
* |
60
|
|
|
* @param InputInterface $input User input on console |
61
|
|
|
* |
62
|
|
|
* @return Finder|SplFileInfo[] finder |
63
|
|
|
*/ |
64
|
6 |
|
protected function getFinder($input) |
65
|
|
|
{ |
66
|
6 |
|
$files = $input->getArgument('file'); |
67
|
6 |
|
$inputFile = $input->getOption('input-file'); |
68
|
|
|
|
69
|
6 |
|
if (empty($files) && is_null($inputFile)) { |
70
|
|
|
throw new \LogicException('You either need to provide <file> arguments or the --input-file option.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
$finder = $this->finder->files(); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param SplFileInfo $file |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
$filter = function (SplFileInfo $file) { |
|
|
|
|
80
|
6 |
|
if (!in_array($file->getExtension(), ['yml','json']) || $file->isDir()) { |
81
|
1 |
|
return false; |
82
|
|
|
} |
83
|
6 |
|
return true; |
84
|
6 |
|
}; |
85
|
|
|
|
86
|
6 |
|
if (is_null($inputFile)) { |
87
|
|
|
// normal way - file arguments.. |
88
|
6 |
View Code Duplication |
foreach ($files as $file) { |
|
|
|
|
89
|
6 |
|
if (is_file($file)) { |
90
|
4 |
|
$finder->in(dirname($file))->name(basename($file)); |
91
|
4 |
|
} else { |
92
|
2 |
|
$finder->in($file); |
93
|
|
|
} |
94
|
6 |
|
} |
95
|
6 |
|
} else { |
96
|
|
|
// file list via input file |
97
|
|
|
if (!file_exists($inputFile)) { |
98
|
|
|
throw new \LogicException('File '.$inputFile.' does not seem to exist.'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$fileList = explode(PHP_EOL, file_get_contents($inputFile)); |
102
|
|
|
|
103
|
|
|
$fileList = array_filter( |
104
|
|
|
$fileList, |
105
|
|
|
function ($val) { |
106
|
|
|
if (!empty($val)) { |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$finder->append($fileList); |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
$finder->ignoreDotFiles(true)->filter($filter); |
117
|
|
|
|
118
|
6 |
|
return $finder; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* the actual command can do his import here.. |
123
|
|
|
* |
124
|
|
|
* @param Finder $finder finder |
125
|
|
|
* @param InputInterface $input input |
126
|
|
|
* @param OutputInterface $output output |
127
|
|
|
* |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
abstract protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output); |
131
|
|
|
} |
132
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.