Skip to content

Commit

Permalink
Merge pull request #22 from WebFiori/dev
Browse files Browse the repository at this point in the history
Multiple Refactoring
  • Loading branch information
usernane authored Feb 15, 2024
2 parents 2a5adce + ae7c9ed commit d8e16e1
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 93 deletions.
47 changes: 27 additions & 20 deletions bin/InitAppCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use webfiori\cli\Argument;
use webfiori\cli\CLICommand;
use webfiori\cli\CommandArgument;
use webfiori\file\File;
/**
* A class which is used to initialize a new CLI application.
Expand All @@ -11,51 +11,42 @@
class InitAppCommand extends CLICommand {
public function __construct() {
parent::__construct('init', [
new CommandArgument('--dir', 'The name of application root directory.'),
new CommandArgument('--entry', 'The name of entry point that is used to execute the application. Default value is application directory name.', true)
new Argument('--dir', 'The name of application root directory.'),
new Argument('--entry', 'The name of entry point that is used to execute the application. Default value is application directory name.', true)
], 'Initialize new CLI application.');

}
public function exec(): int {
$dirName = $this->getArgValue('--dir');
$entry = $this->getArgValue('--entry');

if ($entry === null) {
$entry = $dirName;
}


if (defined('ROOT_DIR')) {
$appPath = ROOT_DIR.DIRECTORY_SEPARATOR.$dirName;
} else {
$appPath = substr(__DIR__, 0, strlen(__DIR__) - strlen('vendor\webfiori\cli\bin')).$dirName;
}

try {
$this->createAppClass($appPath, $dirName);
$this->createEntryPoint($appPath, $dirName, $entry);
$this->success('App created successfully.');

return 0;
} catch (Exception $ex) {
$this->error('Unable to initialize due to an exception:');
$this->println($ex->getCode().' - '.$ex->getMessage());

return -1;
}
}
private function createEntryPoint(string $appPath, string $dir, string $eName) {
$this->println('Creating "'.$dir.'/'.$eName.'"...');
$file = new File($eName, $appPath);
if (!$file->isExist()) {
$data = "#!/usr/bin/env php\n"
."<?php\n"
."require \"app.php\";\n\n";
file_put_contents($file->getDir().DIRECTORY_SEPARATOR.$eName, $data);
return true;
}
$this->warning('File '.$eName.' already exist!');
}
private function createAppClass(string $appPath, string $dirName) {
$this->println('Creating "'.$dirName.'/app.php"...');
$file = new File($appPath.DIRECTORY_SEPARATOR.'app.php');

if (!$file->isExist()) {
$file->append("<?php\n\n");
$file->append("namespace $dirName;\n\n");
Expand All @@ -71,10 +62,26 @@ private function createAppClass(string $appPath, string $dirName) {
$file->append("\$runner->setDefaultCommand('help');\n\n");
$file->append("//Start your application.\n");
$file->append("exit(\$runner->start());\n\n");
$file->create(true);
$file->write(false);

$file->write(false, true);
return true;
}
$this->warning('File app.php already exist!');
}
private function createEntryPoint(string $appPath, string $dir, string $eName) {
$this->println('Creating "'.$dir.'/'.$eName.'"...');
$file = new File($eName, $appPath);

if (!$file->isExist()) {
$data = "#!/usr/bin/env php\n"
."<?php\n"
."require \"app.php\";\n\n";
$file->create(true);
file_put_contents($file->getDir().DIRECTORY_SEPARATOR.$eName, $data);

return true;
}
$this->warning('File '.$eName.' already exist!');
}
}
10 changes: 5 additions & 5 deletions bin/app.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';
include $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php';

use webfiori\cli\commands\HelpCommand;
use webfiori\cli\Runner;

require 'InitAppCommand.php';
$runner = new Runner();
$runner->register(new HelpCommand());
$runner->register(new InitAppCommand());
$runner->setDefaultCommand('help');
exit($runner->start());
exit($runner->register(new HelpCommand())
->register(new InitAppCommand())
->setDefaultCommand('help')
->start());
5 changes: 3 additions & 2 deletions example/app/HelloWorldCommand.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

use webfiori\cli\CLICommand;
use webfiori\cli\Option;

class HelloWorldCommand extends CLICommand {
public function __construct() {
parent::__construct('hello', [
'--person-name' => [
'description' => 'Name of someone to greet.',
'optional' => true
Option::DESCRIPTION => 'Name of someone to greet.',
Option::OPTIONAL => true
]
], 'A command to show greetings.');
}
Expand Down
3 changes: 2 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
require_once $baseDir.DS.'streams'.DS.'InputStream.php';
require_once $baseDir.DS.'streams'.DS.'OutputStream.php';
require_once $baseDir.DS.'CLICommand.php';
require_once $baseDir.DS.'CommandArgument.php';
require_once $baseDir.DS.'Argument.php';
require_once $baseDir.DS.'Formatter.php';
require_once $baseDir.DS.'KeysMap.php';
require_once $baseDir.DS.'Runner.php';
require_once $baseDir.DS.'Option.php';
require_once $baseDir.DS.'InputValidator.php';
require_once $baseDir.DS.'streams'.DS.'ArrayInputStream.php';
require_once $baseDir.DS.'streams'.DS.'ArrayOutputStream.php';
Expand Down
30 changes: 15 additions & 15 deletions tests/webfiori/tests/cli/CLICommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace webfiori\tests\cli;

use PHPUnit\Framework\TestCase;
use webfiori\cli\CommandArgument;
use webfiori\cli\Argument;
use webfiori\cli\exceptions\IOException;
use webfiori\cli\InputValidator;
use webfiori\cli\Runner;
Expand Down Expand Up @@ -41,7 +41,7 @@ public function testSussess00() {
*/
public function testSussess01() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand All @@ -66,7 +66,7 @@ public function testInfo00() {
*/
public function testInfo01() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand All @@ -91,7 +91,7 @@ public function testWarning00() {
*/
public function testWarning01() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand All @@ -116,7 +116,7 @@ public function testError00() {
*/
public function testError01() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand Down Expand Up @@ -264,7 +264,7 @@ public function testSelect04() {
*/
public function testSelect05() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand Down Expand Up @@ -395,7 +395,7 @@ public function testConfirm02() {
*/
public function testConfirm03() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand All @@ -415,7 +415,7 @@ public function testConfirm03() {
*/
public function testConfirm04() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand All @@ -432,7 +432,7 @@ public function testConfirm04() {
*/
public function testConfirm05() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand Down Expand Up @@ -463,7 +463,7 @@ public function testGetInput00() {
*/
public function testGetInput01() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand All @@ -483,7 +483,7 @@ public function testGetInput01() {
*/
public function testGetInput02() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand All @@ -501,7 +501,7 @@ public function testGetInput02() {
*/
public function testGetInput03() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand All @@ -519,7 +519,7 @@ public function testGetInput03() {
*/
public function testGetInput04() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand All @@ -546,7 +546,7 @@ public function testGetInput04() {
*/
public function testGetInput05() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand All @@ -573,7 +573,7 @@ public function testGetInput05() {
*/
public function testGetInput06() {
$command = new TestCommand('cool');
$ansiArg = new CommandArgument('--ansi');
$ansiArg = new Argument('--ansi');
$ansiArg->setValue('');
$command->addArgument($ansiArg);
$command->setOutputStream(new ArrayOutputStream());
Expand Down
22 changes: 11 additions & 11 deletions tests/webfiori/tests/cli/CommandArgumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace webfiori\tests\cli;
use PHPUnit\Framework\TestCase;
use webfiori\cli\CommandArgument;
use webfiori\cli\Argument;
use webfiori\cli\Runner;
/**
* Description of CommandArgumentTest
Expand All @@ -17,7 +17,7 @@ public function extractValueTest00() {
$_SERVER['argv'] = [
'name=ibrahim'
];
$this->assertEquals('ibrahim', CommandArgument::extractValue('name'));
$this->assertEquals('ibrahim', Argument::extractValue('name'));
}
/**
* @test
Expand All @@ -30,7 +30,7 @@ public function extractValueTest01() {
$r->setArgsVector([
'name=ali'
]);
$this->assertEquals('ali', CommandArgument::extractValue('name', $r));
$this->assertEquals('ali', Argument::extractValue('name', $r));
}
/**
* @test
Expand All @@ -40,14 +40,14 @@ public function extractValueTest03() {
'name="ibrahim Ali"',
"last-name='bin'"
];
$this->assertEquals('ibrahim Ali', CommandArgument::extractValue('name'));
$this->assertEquals('bin', CommandArgument::extractValue('last-name'));
$this->assertEquals('ibrahim Ali', Argument::extractValue('name'));
$this->assertEquals('bin', Argument::extractValue('last-name'));
}
/**
* @test
*/
public function test00() {
$arg = new CommandArgument();
$arg = new Argument();
$this->assertNull($arg->getValue());
$this->assertEquals('', $arg->getDefault());
$this->assertEquals('', $arg->getDescription());
Expand All @@ -68,14 +68,14 @@ public function test00() {
* @test
*/
public function test01() {
$arg = new CommandArgument('');
$arg = new Argument('');
$this->assertEquals('arg', $arg->getName());
}
/**
* @test
*/
public function test02() {
$arg = new CommandArgument('--config');
$arg = new Argument('--config');
$this->assertNull($arg->getValue());
$this->assertEquals('', $arg->getDefault());
$this->assertEquals('', $arg->getDescription());
Expand All @@ -88,7 +88,7 @@ public function test02() {
* @test
*/
public function testSetName() {
$arg = new CommandArgument(' ');
$arg = new Argument(' ');
$this->assertEquals('arg', $arg->getName());
$this->assertTrue($arg->setName('my-val'));
$this->assertEquals('my-val', $arg->getName());
Expand All @@ -101,7 +101,7 @@ public function testSetName() {
* @test
*/
public function testSetValue00() {
$arg = new CommandArgument();
$arg = new Argument();
$this->assertNull($arg->getValue());
$arg->setValue('');
$this->assertEquals('', $arg->getValue());
Expand All @@ -112,7 +112,7 @@ public function testSetValue00() {
* @test
*/
public function testSetValue01() {
$arg = new CommandArgument();
$arg = new Argument();
$this->assertNull($arg->getValue());
$arg->addAllowedValue('Super');
$this->assertFalse($arg->setValue(''));
Expand Down
4 changes: 2 additions & 2 deletions tests/webfiori/tests/cli/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use webfiori\cli\commands\HelpCommand;
use webfiori\tests\cli\testCommands\WithExceptionCommand;
use webfiori\tests\cli\testCommands\Command01;
use webfiori\cli\CommandArgument;
use webfiori\cli\Argument;
/**
* Description of RunnerTest
*
Expand Down Expand Up @@ -48,7 +48,7 @@ public function testRunner00() {
$this->assertNull($runner->getDefaultCommand());
$this->assertNull($runner->getActiveCommand());

$argObj = new CommandArgument('--ansi');
$argObj = new Argument('--ansi');
$this->assertFalse($runner->addArgument($argObj));

$this->assertTrue($runner->addArg('global-arg', [
Expand Down
Loading

0 comments on commit d8e16e1

Please sign in to comment.