Skip to content

Commit aba49a3

Browse files
authored
Merge pull request #97 from php-java/add-bin
Add bin
2 parents 59079f7 + ce9e2cb commit aba49a3

5 files changed

Lines changed: 131 additions & 4 deletions

File tree

PHPJava

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/php
2+
<?php
3+
4+
foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) {
5+
if (is_file($file)) {
6+
require $file;
7+
break;
8+
}
9+
}
10+
11+
\PHPJava\Console\Command::main();

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
"description": "JVM emulator by PHP",
44
"type": "library",
55
"license": "MIT",
6-
"version": "0.0.6.5-dev",
6+
"version": "0.0.6.6-dev",
77
"authors": [
88
{
99
"name": "memory"
1010
}
1111
],
12+
"bin": [
13+
"PHPJava"
14+
],
1215
"require": {
1316
"php": ">=7.2",
1417
"ext-zip": "*",
@@ -18,11 +21,13 @@
1821
"php-java/java-io-package": "*",
1922
"php-java/java-net-package": "*",
2023
"php-java/java-nio-package": "*",
21-
"gabrielelana/byte-units": "dev-master"
24+
"gabrielelana/byte-units": "dev-master",
25+
"symfony/console": "4.2"
2226
},
2327
"autoload": {
2428
"psr-4": {
25-
"PHPJava\\": "src/"
29+
"PHPJava\\": "src/",
30+
"PHPJava\\Console\\": "console/"
2631
}
2732
},
2833
"autoload-dev": {

console/Command.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
namespace PHPJava\Console;
3+
4+
use PHPJava\Console\Commands\JVM\RunCommand;
5+
use PHPJava\Core\JavaArchive;
6+
use PHPJava\Core\PHPJava;
7+
use Symfony\Component\Console\Application;
8+
9+
class Command
10+
{
11+
public static function main()
12+
{
13+
$application = new Application();
14+
$runCommand = new RunCommand();
15+
$application->add($runCommand);
16+
$application->setDefaultCommand(
17+
$runCommand->getName(),
18+
true
19+
);
20+
$application->run();
21+
}
22+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
namespace PHPJava\Console\Commands\JVM;
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;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputDefinition;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Input\InputOption;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
15+
class RunCommand extends Command
16+
{
17+
protected static $defaultName = 'jvm:run';
18+
19+
protected function configure()
20+
{
21+
$this
22+
->addArgument(
23+
'file',
24+
InputArgument::REQUIRED,
25+
'Specify to run [jar|class] file.'
26+
)
27+
->addArgument(
28+
'parameters',
29+
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
30+
'Specify parameters to pass for entrypoint.'
31+
)
32+
->addOption(
33+
'settings',
34+
's',
35+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
36+
'Set option settings.'
37+
)
38+
->addOption(
39+
'mode',
40+
'm',
41+
InputOption::VALUE_OPTIONAL,
42+
'Set run mode [jar|class]. Default is class.'
43+
);
44+
}
45+
46+
protected function execute(InputInterface $input, OutputInterface $output)
47+
{
48+
$settings = $input->getOption('settings') ?? [];
49+
$mode = strtolower($input->getOption('mode') ?? 'class');
50+
$file = $input->getArgument('file');
51+
$parameters = $input->getArgument('parameters');
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+
}
66+
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+
);
88+
}
89+
}

src/Core/PHPJava.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ final class PHPJava
1212
/**
1313
* As same as composer version.
1414
*/
15-
const VERSION = 0x000065;
15+
const VERSION = 0x000066;
1616
}

0 commit comments

Comments
 (0)