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 | 1 | if (!in_array($file->getExtension(), ['yml','json']) || $file->isDir()) { |
|
57 | return false; |
||
58 | } |
||
59 | 1 | return true; |
|
60 | 5 | }; |
|
61 | |||
62 | 5 | $finder->in($files)->ignoreDotFiles(true)->filter($filter); |
|
63 | |||
64 | try { |
||
65 | 1 | $this->doImport($finder, $input, $output); |
|
66 | 1 | } catch (MissingTargetException $e) { |
|
67 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * the actual command can do his import here.. |
||
73 | * |
||
74 | * @param Finder $finder finder |
||
75 | * @param InputInterface $input input |
||
76 | * @param OutputInterface $output output |
||
77 | * |
||
78 | * @return mixed |
||
79 | */ |
||
80 | abstract protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output); |
||
81 | } |
||
82 |