/
RunCommand.php
57 lines (50 loc) · 2.1 KB
/
RunCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace N98\Magento\Command\Script\Repository;
use N98\Magento\Command\AbstractMagentoCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Shell;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class RunCommand extends AbstractRepositoryCommand
{
protected function configure()
{
$this
->setName('script:repo:run')
->addArgument('script', InputArgument::OPTIONAL, 'Name of script in repository')
->setDescription('Run script from repository')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$files = $this->getScripts();
if ($input->getArgument('script') == null) {
$question = array();
$i = 0;
foreach ($files as $file) {
$files[$i] = $file;
$question[] = '<comment>[' . ($i + 1) . ']</comment> ' . $file['fileinfo']->getFilename() . PHP_EOL;
$i++;
}
$question[] = '<question>Please select a script file: </question>';
$selectedFile = $this->getHelper('dialog')->askAndValidate($output, $question, function($typeInput) use ($files) {
if (!isset($files[$typeInput - 1])) {
throw new \InvalidArgumentException('Invalid file');
}
return $files[$typeInput - 1]['fileinfo']->getPathname();
});
} else {
$script = $input->getArgument('script');
if (substr($script, -strlen(self::MAGERUN_EXTENSION)) !== self::MAGERUN_EXTENSION) {
$script .= self::MAGERUN_EXTENSION;
}
if (!isset($files[$script])) {
throw new \InvalidArgumentException('Invalid script');
}
$selectedFile = $files[$script]['fileinfo']->getPathname();
}
$input = new StringInput('script ' . escapeshellarg($selectedFile));
$this->getApplication()->run($input, $output);
}
}