Skip to content

Commit

Permalink
Run CS Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Feb 15, 2024
1 parent 42753c7 commit ae7c9ed
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 43 deletions.
42 changes: 24 additions & 18 deletions bin/InitAppCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use webfiori\cli\CLICommand;
use webfiori\cli\Argument;
use webfiori\cli\CLICommand;
use webfiori\file\File;
/**
* A class which is used to initialize a new CLI application.
Expand All @@ -14,49 +14,39 @@ public function __construct() {
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->create(true);
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 @@ -74,8 +64,24 @@ private function createAppClass(string $appPath, string $dirName) {
$file->append("exit(\$runner->start());\n\n");
$file->create(true);
$file->write(false);

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());
2 changes: 1 addition & 1 deletion webfiori/cli/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function addAllowedValue(string $val) : Argument {
if (!in_array($trim, $this->getAllowedValues())) {
$this->allowedValues[] = $trim;
}

return $this;
}
/**
Expand Down
4 changes: 2 additions & 2 deletions webfiori/cli/CLICommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function clear(int $numberOfCols = 1, bool $beforeCursor = true) : CLICom
$this->moveCursorLeft($numberOfCols + 1);
}
}

return $this;
}
/**
Expand All @@ -224,7 +224,7 @@ public function clear(int $numberOfCols = 1, bool $beforeCursor = true) : CLICom
*/
public function clearConsole() : CLICommand {
$this->prints("\ec");

return $this;
}
/**
Expand Down
11 changes: 5 additions & 6 deletions webfiori/cli/Option.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace webfiori\cli;

/**
Expand All @@ -8,11 +7,6 @@
* @author Ibrahim
*/
class Option {
/**
* An option which is used to tell if the argument is optional or not.
* Accepts 'true' or 'false' as its value.
*/
const OPTIONAL = 'optional';
/**
* An option which is used to set a default value for the argument if not
* provided and it was optional.
Expand All @@ -22,6 +16,11 @@ class Option {
* Help text of the argument. Used when the command 'help' is executed.
*/
const DESCRIPTION = 'description';
/**
* An option which is used to tell if the argument is optional or not.
* Accepts 'true' or 'false' as its value.
*/
const OPTIONAL = 'optional';
/**
* An array of values at which the argument can accept. Used to restrict
* the values that can be supplied to the argument.
Expand Down
20 changes: 10 additions & 10 deletions webfiori/cli/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function isInteractive() : bool {
*/
public function register(CLICommand $cliCommand) : Runner {
$this->commands[$cliCommand->getName()] = $cliCommand;

return $this;
}
/**
Expand Down Expand Up @@ -348,7 +348,7 @@ public function reset() : Runner {
$this->inputStream = new StdIn();
$this->outputStream = new StdOut();
$this->commands = [];

return $this;
}
/**
Expand Down Expand Up @@ -444,7 +444,7 @@ public function setActiveCommand(CLICommand $c = null) : Runner {
$this->getActiveCommand()->setInputStream($this->getInputStream());
$this->getActiveCommand()->setOwner($this);
}

return $this;
}
/**
Expand All @@ -467,7 +467,7 @@ public function setAfterExecution(callable $func, array $params = []) : Runner {
'func' => $func,
'params' => $params
];

return $this;
}
/**
Expand All @@ -488,7 +488,7 @@ public function setAfterExecution(callable $func, array $params = []) : Runner {
*/
public function setArgsVector(array $argsVector) : Runner {
$this->argsV = $argsVector;

return $this;
}
/**
Expand All @@ -505,7 +505,7 @@ public function setArgsVector(array $argsVector) : Runner {
*/
public function setBeforeStart(callable $func) : Runner {
$this->beforeStartPool[] = $func;

return $this;
}
/**
Expand All @@ -524,7 +524,7 @@ public function setDefaultCommand(string $commandName) : Runner {
if ($c !== null) {
$this->defaultCommand = $c;
}

return $this;
}
/**
Expand All @@ -547,7 +547,7 @@ public function setDefaultCommand(string $commandName) : Runner {
public function setInputs(array $inputs = []) : Runner {
$this->setInputStream(new ArrayInputStream($inputs));
$this->setOutputStream(new ArrayOutputStream());

return $this;
}

Expand All @@ -561,7 +561,7 @@ public function setInputs(array $inputs = []) : Runner {
*/
public function setInputStream(InputStream $stream) : Runner {
$this->inputStream = $stream;

return $this;
}
/**
Expand All @@ -574,7 +574,7 @@ public function setInputStream(InputStream $stream) : Runner {
*/
public function setOutputStream(OutputStream $stream) : Runner {
$this->outputStream = $stream;

return $this;
}
/**
Expand Down
2 changes: 1 addition & 1 deletion webfiori/cli/commands/HelpCommand.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace webfiori\cli\commands;

use webfiori\cli\CLICommand;
use webfiori\cli\Argument;
use webfiori\cli\CLICommand;

/**
* A class that implements a basic help command.
Expand Down

0 comments on commit ae7c9ed

Please sign in to comment.