-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from WebFiori/dev
Dev
- Loading branch information
Showing
7 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ app/storage | |
php-cs-fixer-v2.phar | ||
app/sto | ||
.idea/* | ||
test/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
require "app.php"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|