-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathRunCommand.php
More file actions
92 lines (83 loc) Β· 2.61 KB
/
RunCommand.php
File metadata and controls
92 lines (83 loc) Β· 2.61 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace PHPJava\Console\Commands\JVM;
use PHPJava\Core\JavaArchive;
use PHPJava\Core\JavaClass;
use PHPJava\Core\JavaCompiledClass;
use PHPJava\Core\JVM\Parameters\GlobalOptions;
use PHPJava\Core\Stream\Reader\FileReader;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class RunCommand extends Command
{
protected static $defaultName = 'jvm:run';
protected function configure()
{
$this
->addArgument(
'file',
InputArgument::REQUIRED,
'Specify to run [jar|class] file.'
)
->addArgument(
'parameters',
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'Specify parameters to pass for entrypoint.'
)
->addOption(
'settings',
's',
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Set option settings.'
)
->addOption(
'mode',
'm',
InputOption::VALUE_OPTIONAL,
'Set run mode [jar|class]. Default is class.'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$settings = $input->getOption('settings') ?? [];
$mode = strtolower($input->getOption('mode') ?? 'class');
$file = $input->getArgument('file');
$parameters = $input->getArgument('parameters');
// Set global options
GlobalOptions::set($settings);
if ($mode === 'jar') {
return $this->runJar($file, $parameters);
} elseif ($mode === 'class') {
return $this->runClass($file, $parameters);
}
$output->writeln(
'<error>Unable to run `' . $mode . '` mode.</error>'
);
}
private function runJar(string $file, array $parameters)
{
$jar = new JavaArchive($file);
$jar->execute(
$parameters
);
}
private function runClass(string $file, array $parameters)
{
$class = new JavaClass(
new JavaCompiledClass(
new FileReader($file)
)
);
$class
->getInvoker()
->getStatic()
->getMethods()
->call(
'main',
$parameters
);
}
}