1 | <?php |
||
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 | 5 | public function __construct( |
|
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 void |
||
45 | */ |
||
46 | 5 | protected function execute(InputInterface $input, OutputInterface $output) |
|
47 | { |
||
48 | 5 | $files = $input->getArgument('file'); |
|
49 | 5 | $finder = $this->finder->files(); |
|
50 | |||
51 | /** |
||
52 | * @param SplFileInfo $file |
||
53 | * @return bool |
||
54 | */ |
||
55 | 5 | $filter = function (SplFileInfo $file) { |
|
56 | 5 | if (!in_array($file->getExtension(), ['yml','json']) || $file->isDir()) { |
|
57 | return false; |
||
58 | } |
||
59 | 5 | return true; |
|
60 | 5 | }; |
|
61 | |||
62 | 5 | foreach ($files as $file) { |
|
63 | 5 | if (is_file($file)) { |
|
64 | 4 | $finder->in(dirname($file))->name(basename($file)); |
|
65 | 4 | } else { |
|
66 | 1 | $finder->in($file); |
|
67 | } |
||
68 | 5 | } |
|
69 | |||
70 | 5 | $finder->ignoreDotFiles(true)->filter($filter); |
|
71 | |||
72 | try { |
||
73 | 5 | $this->doImport($finder, $input, $output); |
|
74 | 5 | } catch (MissingTargetException $e) { |
|
75 | 1 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
76 | } |
||
77 | 4 | } |
|
78 | |||
79 | /** |
||
80 | * the actual command can do his import here.. |
||
81 | * |
||
82 | * @param Finder $finder finder |
||
83 | * @param InputInterface $input input |
||
84 | * @param OutputInterface $output output |
||
85 | * |
||
86 | * @return mixed |
||
87 | */ |
||
88 | abstract protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output); |
||
89 | } |
||
90 |