Skip to content

Commit

Permalink
Merge pull request #18 from WebFiori/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
usernane authored Aug 21, 2023
2 parents 99f2242 + f85813a commit 568c19d
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ app/storage
php-cs-fixer-v2.phar
app/sto
.idea/*
test/*
80 changes: 80 additions & 0 deletions bin/InitAppCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

use webfiori\cli\CLICommand;
use webfiori\cli\CommandArgument;
use webfiori\file\File;
/**
* A class which is used to initialize a new CLI application.
*
* @author Ibrahim
*/
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)
], '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");
$file->append("//Entry point of your application.\n\n");
$file->append("require '../vendor/autoload.php';\n\n");
$file->append("use webfiori\cli\Runner;\n");
$file->append("use webfiori\cli\commands\HelpCommand;\n\n");


$file->append("\$runner = new Runner();\n");
$file->append("//TODO: Register Commands.\n");
$file->append("\$runner->register(new HelpCommand());\n");
$file->append("\$runner->setDefaultCommand('help');\n\n");
$file->append("//Start your application.\n");
$file->append("exit(\$runner->start());\n\n");

$file->write(false, true);
return true;
}
$this->warning('File app.php already exist!');
}
}
13 changes: 13 additions & 0 deletions bin/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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());
3 changes: 3 additions & 0 deletions bin/wfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env php
<?php
require "app.php";
13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@
],
"require": {
"webfiori/file":"1.3.*"
},
"require-dev": {

},
"autoload" :{
"psr-4":{
"webfiori\\cli\\":"webfiori\\cli"
}
}
},
"scripts" : {
"check-cs": "bin/ecs check --ansi",
"fix-cs": "bin/ecs check --fix --ansi",
"phpstan": "vendor/bin/phpstan analyse --ansi --error-format symplify"
},
"bin": [
"bin/wfc"
]
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
require_once $baseDir.DS.'exceptions'.DS.'IOException.php';
require_once $baseDir.DS.'commands'.DS.'HelpCommand.php';

require_once $rootDir.DS.'bin'.DS.'InitAppCommand.php';
require_once $rootDir.'/tests/webfiori/tests/cli/TestCommand.php';
require_once $rootDir.'/tests/webfiori/tests/cli/testCommands/Command00.php';
require_once $rootDir.'/tests/webfiori/tests/cli/testCommands/Command01.php';
Expand Down
122 changes: 122 additions & 0 deletions tests/webfiori/tests/cli/InitAppCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
namespace webfiori\tests\cli;

use InitAppCommand;
use PHPUnit\Framework\TestCase;
use webfiori\cli\Runner;

/**
* Description of InitAppCommandTest
*
* @author Ibrahim
*/
class InitAppCommandTest extends TestCase {
/**
* @test
*/
public function test00() {
$r = new Runner();
$r->register(new InitAppCommand());
$r->setDefaultCommand('init');
$r->setInputs([]);
$r->setArgsVector([
'app.php',
'init'
]);
$this->assertEquals(-1, $r->start());
$this->assertEquals([
"Error: The following required argument(s) are missing: '--dir'\n"
], $r->getOutput());
}
/**
* @test
*/
public function test01() {
$r = new Runner();
$r->register(new InitAppCommand());
$r->setDefaultCommand('init');
$r->setInputs([]);
$r->setArgsVector([
'app.php',
'init',
'--dir' => "test\0a"
]);
$this->assertEquals(-1, $r->start());
}
/**
* @test
* @depends test01
*/
public function test02() {
$r = new Runner();
$r->register(new InitAppCommand())
->setDefaultCommand('init')
->setInputs([])
->setArgsVector([
'app.php',
'init',
'--dir' => 'test'
]);
$this->assertEquals(0, $r->start());
$this->assertEquals([
"Creating \"test/app.php\"...\n",
"Creating \"test/test\"...\n",
"Success: App created successfully.\n"
], $r->getOutput());
}
/**
* @test
* @depends test02
*/
public function test03() {
$r = new Runner();
$r->register(new InitAppCommand());
$r->setDefaultCommand('init');
$r->setInputs([]);
$r->setArgsVector([
'app.php',
'init',
'--dir' => 'test'
]);
$this->assertEquals(0, $r->start());
$this->assertEquals([
"Creating \"test/app.php\"...\n",
"Warning: File app.php already exist!\n",
"Creating \"test/test\"...\n",
"Warning: File test already exist!\n",
"Success: App created successfully.\n"
], $r->getOutput());
unlink(ROOT_DIR.DS.'test'.DS.'app.php');
unlink(ROOT_DIR.DS.'test'.DS.'test');
rmdir(ROOT_DIR.DS.'test');
}
/**
* @test
*/
public function test04() {
$r = new Runner();
$r->register(new InitAppCommand());
$r->setDefaultCommand('init');
$r->setInputs([]);
$r->setArgsVector([
'app.php',
'init',
'--dir' => 'test2',
'--entry' => 'bang'
]);
$this->assertEquals(0, $r->start());
$this->assertEquals([
"Creating \"test2/app.php\"...\n",
"Creating \"test2/bang\"...\n",
"Success: App created successfully.\n"
], $r->getOutput());
unlink(ROOT_DIR.DS.'test2'.DS.'app.php');
unlink(ROOT_DIR.DS.'test2'.DS.'bang');
rmdir(ROOT_DIR.DS.'test2');
}
}





0 comments on commit 568c19d

Please sign in to comment.