diff --git a/PHPJava b/PHPJava new file mode 100755 index 00000000..36e35079 --- /dev/null +++ b/PHPJava @@ -0,0 +1,11 @@ +#!/usr/bin/php +=7.2", "ext-zip": "*", @@ -18,11 +21,13 @@ "php-java/java-io-package": "*", "php-java/java-net-package": "*", "php-java/java-nio-package": "*", - "gabrielelana/byte-units": "dev-master" + "gabrielelana/byte-units": "dev-master", + "symfony/console": "4.2" }, "autoload": { "psr-4": { - "PHPJava\\": "src/" + "PHPJava\\": "src/", + "PHPJava\\Console\\": "console/" } }, "autoload-dev": { diff --git a/console/Command.php b/console/Command.php new file mode 100755 index 00000000..cfc01285 --- /dev/null +++ b/console/Command.php @@ -0,0 +1,22 @@ +add($runCommand); + $application->setDefaultCommand( + $runCommand->getName(), + true + ); + $application->run(); + } +} diff --git a/console/Commands/JVM/RunCommand.php b/console/Commands/JVM/RunCommand.php new file mode 100644 index 00000000..290944c9 --- /dev/null +++ b/console/Commands/JVM/RunCommand.php @@ -0,0 +1,89 @@ +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( + 'Unable to run `' . $mode . '` mode.' + ); + } + + 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 FileReader($file) + ); + $class + ->getInvoker() + ->getStatic() + ->getMethods() + ->call( + 'main', + $parameters + ); + } +} diff --git a/src/Core/PHPJava.php b/src/Core/PHPJava.php index aad57825..cc140d54 100644 --- a/src/Core/PHPJava.php +++ b/src/Core/PHPJava.php @@ -12,5 +12,5 @@ final class PHPJava /** * As same as composer version. */ - const VERSION = 0x000065; + const VERSION = 0x000066; }