|
1 | 1 | <?php |
2 | 2 | namespace PHPJava\Console\Commands\JVM; |
3 | 3 |
|
| 4 | +use PHPJava\Core\JavaArchive; |
| 5 | +use PHPJava\Core\JavaClass; |
| 6 | +use PHPJava\Core\JVM\Parameters\GlobalOptions; |
| 7 | +use PHPJava\Core\Stream\Reader\FileReader; |
4 | 8 | use Symfony\Component\Console\Command\Command; |
5 | 9 | use Symfony\Component\Console\Input\InputArgument; |
6 | 10 | use Symfony\Component\Console\Input\InputDefinition; |
@@ -36,24 +40,50 @@ protected function configure() |
36 | 40 | 'm', |
37 | 41 | InputOption::VALUE_OPTIONAL, |
38 | 42 | 'Set run mode [jar|class]. Default is class.' |
39 | | - ) |
40 | | - ->addOption( |
41 | | - 'entrypoint', |
42 | | - 'e', |
43 | | - InputOption::VALUE_OPTIONAL, |
44 | | - 'Overwrite entrypoint' |
45 | 43 | ); |
46 | 44 | } |
47 | 45 |
|
48 | 46 | protected function execute(InputInterface $input, OutputInterface $output) |
49 | 47 | { |
50 | 48 | $settings = $input->getOption('settings') ?? []; |
51 | | - $mode = $input->getOption('mode') ?? 'class'; |
52 | | - $entrypoint = $input->getOption('entrypoint') ?? null; |
| 49 | + $mode = strtolower($input->getOption('mode') ?? 'class'); |
53 | 50 | $file = $input->getArgument('file'); |
54 | 51 | $parameters = $input->getArgument('parameters'); |
55 | 52 |
|
| 53 | + // Set global options |
| 54 | + GlobalOptions::set($settings); |
| 55 | + |
| 56 | + if ($mode === 'jar') { |
| 57 | + return $this->runJar($file, $parameters); |
| 58 | + } elseif ($mode === 'class') { |
| 59 | + return $this->runClass($file, $parameters); |
| 60 | + } |
| 61 | + |
| 62 | + $output->writeln( |
| 63 | + '<error>Unable to run `' . $mode . '` mode.</error>' |
| 64 | + ); |
| 65 | + } |
56 | 66 |
|
57 | | - var_dump($file); |
| 67 | + private function runJar(string $file, array $parameters) |
| 68 | + { |
| 69 | + $jar = new JavaArchive($file); |
| 70 | + $jar->execute( |
| 71 | + $parameters |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + private function runClass(string $file, array $parameters) |
| 76 | + { |
| 77 | + $class = new JavaClass( |
| 78 | + new FileReader($file) |
| 79 | + ); |
| 80 | + $class |
| 81 | + ->getInvoker() |
| 82 | + ->getStatic() |
| 83 | + ->getMethods() |
| 84 | + ->call( |
| 85 | + 'main', |
| 86 | + $parameters |
| 87 | + ); |
58 | 88 | } |
59 | 89 | } |
0 commit comments