diff --git a/commands.php b/commands.php index bdb9ac7..ed3a542 100644 --- a/commands.php +++ b/commands.php @@ -1,79 +1,75 @@ - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ - -$conso->command('--version', function($inp, $out, $app){ - - $out->writeLn("\n " . $app->getName() . " version " . $app->getVersion() . "\n", "yellow"); - +$conso->command('--version', function ($inp, $out, $app) { + $out->writeLn("\n ".$app->getName().' version '.$app->getVersion()."\n", 'yellow'); })->alias(['-v']); -$conso->command('--help', function($inp, $out, $app){ - - $out->writeLn("\nUsage: \n\n", 'yellow'); - $out->writeLn(" command:subcommand [options] [-flags]\n\n"); - - $out->writeLn("Special flags: \n\n", 'yellow'); - - $flags = $inp->reservedFlags(); - $out->writeLn(" " . $flags[0] . " ", 'green'); - $out->writeLn(" " . $flags[1] . " \n", 'green'); - $out->writeLn(" " . $flags[2] . " ", 'green'); - $out->writeLn(" " . $flags[3] . " \n", 'green'); - $out->writeLn(" " . $flags[4] . " ", 'green'); - $out->writeLn(" " . $flags[5] . " \n", 'green'); - $out->writeLn(" " . $flags[6] . " ", 'green'); - $out->writeLn(" " . $flags[7] . " \n", 'green'); - $out->writeLn(" " . $flags[8] . " ", 'green'); - $out->writeLn( $flags[9] . " \n", 'green'); - $out->writeLn(" " . $flags[10] . " ", 'green'); - $out->writeLn(" " . $flags[11] . " \n", 'green'); - - $out->writeLn("\nHelp: \n\n", 'yellow'); - $out->writeLn(" The help special flag displays help for a given command: \n\n"); - $out->writeLn(" To display application version, please use the -v or --version special flags.\n"); - $out->writeLn(" To display the list of available commands, please use the -c or --commands special flags.\n"); - $out->writeLn(" To silent warning, please use the -q or --quiet special flags.\n"); - $out->writeLn(" To disable ansi, please use the --no-ansi special flag.\n"); - +$conso->command('--help', function ($inp, $out, $app) { + $out->writeLn("\nUsage: \n\n", 'yellow'); + $out->writeLn(" command:subcommand [options] [-flags]\n\n"); + + $out->writeLn("Special flags: \n\n", 'yellow'); + + $flags = $inp->reservedFlags(); + $out->writeLn(' '.$flags[0].' ', 'green'); + $out->writeLn(' '.$flags[1]." \n", 'green'); + $out->writeLn(' '.$flags[2].' ', 'green'); + $out->writeLn(' '.$flags[3]." \n", 'green'); + $out->writeLn(' '.$flags[4].' ', 'green'); + $out->writeLn(' '.$flags[5]." \n", 'green'); + $out->writeLn(' '.$flags[6].' ', 'green'); + $out->writeLn(' '.$flags[7]." \n", 'green'); + $out->writeLn(' '.$flags[8].' ', 'green'); + $out->writeLn($flags[9]." \n", 'green'); + $out->writeLn(' '.$flags[10].' ', 'green'); + $out->writeLn(' '.$flags[11]." \n", 'green'); + + $out->writeLn("\nHelp: \n\n", 'yellow'); + $out->writeLn(" The help special flag displays help for a given command: \n\n"); + $out->writeLn(" To display application version, please use the -v or --version special flags.\n"); + $out->writeLn(" To display the list of available commands, please use the -c or --commands special flags.\n"); + $out->writeLn(" To silent warning, please use the -q or --quiet special flags.\n"); + $out->writeLn(" To disable ansi, please use the --no-ansi special flag.\n"); })->alias(['-h']); -$conso->command('--commands', function($inp, $out, $app){ +$conso->command('--commands', function ($inp, $out, $app) { // remove special commands - $commands = array_filter($app->getCommands(), function($elem){ - if(strpos($elem['name'], '--') === FALSE) return $elem; + $commands = array_filter($app->getCommands(), function ($elem) { + if (strpos($elem['name'], '--') === false) { + return $elem; + } }); - if(count($commands) > 0) - { + if (count($commands) > 0) { $out->writeLn("\nAvailable Commands: \n\n", 'yellow'); - $max = max(array_map(function($elem){ return count($elem);}, $commands)); + $max = max(array_map(function ($elem) { return count($elem); }, $commands)); - foreach($commands as $command) - { - $out->writeLn(" ". $command['name'] . str_repeat(' ', ($max - strlen($command['name'])) + 4 ), 'green'); - $out->writeLn($command['description'] . "\n"); + foreach ($commands as $command) { + $out->writeLn(' '.$command['name'].str_repeat(' ', ($max - strlen($command['name'])) + 4), 'green'); + $out->writeLn($command['description']."\n"); } $out->writeLn("\n"); } - })->alias(['-c']); -$conso->command('--no-ansi', function($inp, $out, $app){ +$conso->command('--no-ansi', function ($inp, $out, $app) { $out->disableAnsi(); // disable ansi - $cm = explode(DIRECTORY_SEPARATOR, getcwd()); - $cmd = ' php ' . $cm[count($cm) - 1]; + $cm = explode(DIRECTORY_SEPARATOR, getcwd()); + $cmd = ' php '.$cm[count($cm) - 1]; passthru($cmd); }); -$conso->command('command', 'Conso\\Commands\\Command'); \ No newline at end of file +$conso->command('command', 'Conso\\Commands\\Command'); diff --git a/src/Conso/Command.php b/src/Conso/Command.php index 4e6a844..43d3ec4 100644 --- a/src/Conso/Command.php +++ b/src/Conso/Command.php @@ -1,12 +1,16 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ @@ -14,34 +18,37 @@ use Conso\Contracts\OutputInterface; /** - * This class is base command class + * This class is base command class. */ class Command { /** - * base constructor + * base constructor. * - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output - * @param Conso $app + * @param Conso $app */ public function __construct(InputInterface $input, OutputInterface $output, Conso $app) { - if($input->flag(0) == '--no-ansi') + if ($input->flag(0) == '--no-ansi') { $output->disableAnsi(); + } //if($input->flag(0) == '-q' || $input->flag(0) == '--quiet'); // if quiet flag - if($input->flag(0) == '-h' || $input->flag(0) == '--help') + if ($input->flag(0) == '-h' || $input->flag(0) == '--help') { $this->displayCommandHelp($input, $output, $app); + } } /** - * display help for a given command + * display help for a given command. * - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output - * @param Conso $app + * @param Conso $app + * * @return void */ protected function displayCommandHelp($input, $output, $app) @@ -49,20 +56,19 @@ protected function displayCommandHelp($input, $output, $app) $name = $app->activeCommand['name']; $help = $app->activeCommand['help']; - $output->writeLn("\n help for [" . $name . "] command:\n\n", "yellow"); + $output->writeLn("\n help for [".$name."] command:\n\n", 'yellow'); $output->writeLn(" php conso $name:{sub command} {options}\n\n"); - if(is_array($help) && count($help) > 0) - { - foreach($help as $key => $value) - { - $output->writeLn(" [". $key ."]\n\n", 'yellow'); + if (is_array($help) && count($help) > 0) { + foreach ($help as $key => $value) { + $output->writeLn(' ['.$key."]\n\n", 'yellow'); - foreach($value as $a => $b) - $output->writeLn(" " . $a . " : " . $b . "\n\n"); + foreach ($value as $a => $b) { + $output->writeLn(' '.$a.' : '.$b."\n\n"); + } } } exit; } -} \ No newline at end of file +} diff --git a/src/Conso/CommandInvoker.php b/src/Conso/CommandInvoker.php index 4142e17..f98e864 100644 --- a/src/Conso/CommandInvoker.php +++ b/src/Conso/CommandInvoker.php @@ -1,12 +1,16 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ @@ -16,57 +20,58 @@ /** * This class is responsible of invoking commands - * invoke callback or controller method + * invoke callback or controller method. */ class CommandInvoker { /** - * conso app instance + * conso app instance. * * @var Cons */ protected $app; /** - * input object + * input object. * * @var InputInterface */ protected $input; /** - * output object + * output object. * * @var OutputInterface */ protected $output; /** - * constructor + * constructor. * - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output */ public function __construct(InputInterface $input, OutputInterface $output, Conso $app) { - $this->input = $input; + $this->input = $input; $this->output = $output; - $this->app = $app; + $this->app = $app; } /** - * invoke application information method + * invoke application information method. * * @param array $commands + * * @return void */ public function invokeInformation(array $commands) { - $this->output->writeLn($this->app->getSignature() . "\n"); - $this->output->writeLn($this->app->getName() . " ", "yellow"); - $this->output->writeLn("version "); + $this->output->writeLn($this->app->getSignature()."\n"); + $this->output->writeLn($this->app->getName().' ', 'yellow'); + $this->output->writeLn('version '); $this->output->writeLn($this->app->getVersion(), 'green'); - $this->output->writeLn(" by " . $this->app->getAuthor()); + $this->output->writeLn(' by '.$this->app->getAuthor()); $this->output->writeLn("\n\nUsage: \n\n", 'yellow'); $this->output->writeLn(" command:subcommand [options] [-flags]\n\n"); @@ -74,87 +79,92 @@ public function invokeInformation(array $commands) $this->output->writeLn("Special flags: \n\n", 'yellow'); $flags = $this->input->reservedFlags(); - $this->output->writeLn(" " . $flags[0] . " ", 'green'); - $this->output->writeLn(" " . $flags[1] . " \n", 'green'); - $this->output->writeLn(" " . $flags[2] . " ", 'green'); - $this->output->writeLn(" " . $flags[3] . " \n", 'green'); - $this->output->writeLn(" " . $flags[4] . " ", 'green'); - $this->output->writeLn(" " . $flags[5] . " \n", 'green'); - $this->output->writeLn(" " . $flags[6] . " ", 'green'); - $this->output->writeLn(" " . $flags[7] . " \n", 'green'); - $this->output->writeLn(" " . $flags[8] . " ", 'green'); - $this->output->writeLn( $flags[9] . " \n", 'green'); - $this->output->writeLn(" " . $flags[10] . " ", 'green'); - $this->output->writeLn(" " . $flags[11] . " \n", 'green'); + $this->output->writeLn(' '.$flags[0].' ', 'green'); + $this->output->writeLn(' '.$flags[1]." \n", 'green'); + $this->output->writeLn(' '.$flags[2].' ', 'green'); + $this->output->writeLn(' '.$flags[3]." \n", 'green'); + $this->output->writeLn(' '.$flags[4].' ', 'green'); + $this->output->writeLn(' '.$flags[5]." \n", 'green'); + $this->output->writeLn(' '.$flags[6].' ', 'green'); + $this->output->writeLn(' '.$flags[7]." \n", 'green'); + $this->output->writeLn(' '.$flags[8].' ', 'green'); + $this->output->writeLn($flags[9]." \n", 'green'); + $this->output->writeLn(' '.$flags[10].' ', 'green'); + $this->output->writeLn(' '.$flags[11]." \n", 'green'); // remove special commands - $commands = array_filter($commands, function($elem){ - if(strpos($elem['name'], '--') === FALSE) return $elem; + $commands = array_filter($commands, function ($elem) { + if (strpos($elem['name'], '--') === false) { + return $elem; + } }); - if(count($commands) > 0) - { + if (count($commands) > 0) { $this->output->writeLn("\nAvailable Commands: \n\n", 'yellow'); - $max = max(array_map(function($elem){ return count($elem);}, $commands)); + $max = max(array_map(function ($elem) { return count($elem); }, $commands)); - foreach($commands as $command) - { - $this->output->writeLn(" ". $command['name'] . str_repeat(' ', ($max - strlen($command['name'])) + 4 ), 'green'); - $this->output->writeLn($command['description'] . "\n"); + foreach ($commands as $command) { + $this->output->writeLn(' '.$command['name'].str_repeat(' ', ($max - strlen($command['name'])) + 4), 'green'); + $this->output->writeLn($command['description']."\n"); } $this->output->writeLn("\n"); } } /** - * invoke method + * invoke method. + * + * @param array $command * - * @param array $command * @return void */ public function invoke(array $command) { // invoke callback - if(is_callable($command['action'])) + if (is_callable($command['action'])) { return $this->invokeCallback($command); + } // invoke class method or (execute) return $this->invokeClassMethod($command); } /** - * invoke callback method + * invoke callback method. + * + * @param array $command * - * @param array $command * @return void */ protected function invokeCallback(array $command) { - return call_user_func_array($command['action'], array($this->input, $this->output, $this->app)); + return call_user_func_array($command['action'], [$this->input, $this->output, $this->app]); } /** - * Undocumented function + * Undocumented function. * * @param array $command + * * @return void */ protected function invokeClassMethod(array $command) { $subCommand = $this->input->subCommand(); - $class = ucfirst($command['action']); + $class = ucfirst($command['action']); - if(!class_exists($class)) + if (!class_exists($class)) { throw new InvokerException("command class ($class) is not defined."); + } + $method = ($subCommand !== null) ? $subCommand : 'execute'; - $method = ($subCommand !== NULL) ? $subCommand : 'execute'; - - $obj = new $class($this->input, $this->output, $this->app); + $obj = new $class($this->input, $this->output, $this->app); - if(!method_exists($obj, $method)) + if (!method_exists($obj, $method)) { throw new InvokerException("command method ($method) is not defined."); + } - return call_user_func_array(array($obj, $method), array($this->input, $this->output, $this->app)); + return call_user_func_array([$obj, $method], [$this->input, $this->output, $this->app]); } -} \ No newline at end of file +} diff --git a/src/Conso/CommandLinker.php b/src/Conso/CommandLinker.php index 45df382..b74a3cf 100644 --- a/src/Conso/CommandLinker.php +++ b/src/Conso/CommandLinker.php @@ -1,12 +1,16 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ @@ -14,78 +18,80 @@ use Conso\Contracts\OutputInterface; use Conso\Exceptions\InputException; -use function Conso\readProtectedProperty; -use function Conso\readCommandPropertiesFromClass; /** * This class is responsible of linking input command with - * defined commands in commands table + * defined commands in commands table. */ class CommandLinker { /** - * input object + * input object. * * @var InputInterface */ protected $input; /** - * output object + * output object. * * @var OutputInterface */ protected $output; /** - * constructor + * constructor. * * @param InputInterface $input * @param OutputInterface $output */ public function __construct(InputInterface $input, OutputInterface $output) { - $this->input = $input; + $this->input = $input; $this->output = $output; } /** - * match + * match. * * @param array $commands * * @return array */ - public function link(array &$commands) : ?array + public function link(array &$commands): ?array { - $command = $this->input->command(); + $command = $this->input->command(); $subCommand = $this->input->subCommand(); - $flags = $this->input->flags(); + $flags = $this->input->flags(); // if command is a class - for ($i = 0; $i < count($commands); $i++) - readCommandPropertiesFromClass($commands[$i]); // fill array info + for ($i = 0; $i < count($commands); $i++) { + readCommandPropertiesFromClass($commands[$i]); + } // fill array info + + if ($command) { // if not null command + for ($i = 0; $i < count($commands); $i++) { + if ($command == $commands[$i]['name'] || in_array($command, $commands[$i]['aliases'])) { + if ($subCommand && !in_array($subCommand, $commands[$i]['sub'])) { // match sub commands - if ($command) // if not null command - { - for ($i = 0; $i < count($commands); $i++) - { - if ($command == $commands[$i]['name'] || in_array($command, $commands[$i]['aliases'])) - { - if ($subCommand && !in_array($subCommand, $commands[$i]['sub'])) // match sub commands throw new InputException("sub command ({$subCommand}) is not defined."); + } + if (count($flags)) { // match flags + foreach ($flags as $flag) { + if (!in_array($flag, array_merge($commands[$i]['flags'], $this->input->reservedFlags()))) { // allow only defined and reserved flags - if (count($flags)) // match flags - foreach ($flags as $flag) - if (!in_array($flag, array_merge($commands[$i]['flags'], $this->input->reservedFlags()))) // allow only defined and reserved flags throw new InputException("flag ({$flag}) is not defined."); + } + } + } return $commands[$i]; } } + throw new InputException("command ({$command}) is not defined."); } - return NULL; + return null; } -} \ No newline at end of file +} diff --git a/src/Conso/Commands/Command.php b/src/Conso/Commands/Command.php index 5600c0e..f39534d 100644 --- a/src/Conso/Commands/Command.php +++ b/src/Conso/Commands/Command.php @@ -1,121 +1,132 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ -use Conso\Conso; use Conso\Command as BaseCommand; +use Conso\Conso; +use Conso\Contracts\CommandInterface; +use Conso\Contracts\InputInterface; +use Conso\Contracts\OutputInterface; use Conso\Exceptions\InputException; -use Conso\Contracts\{CommandInterface,InputInterface,OutputInterface}; class Command extends BaseCommand implements CommandInterface { /** - * sub commands + * sub commands. * * @var array */ - protected $sub = array( - 'make','delete' - ); + protected $sub = [ + 'make', 'delete', + ]; /** - * command description + * command description. * * @var string */ protected $description = 'This command helps you to create conso commands.'; /** - * command help + * command help. * * @var string */ - protected $help = array( - "sub commands" => array( - "make" => "make a new conso command.", - "delete" => "delete conso command." - ), - "options" => array( - "name" => "command name" - ) - ); + protected $help = [ + 'sub commands' => [ + 'make' => 'make a new conso command.', + 'delete' => 'delete conso command.', + ], + 'options' => [ + 'name' => 'command name', + ], + ]; /** - * execute method + * execute method. + * + * @param InputInterface $input + * @param OutputInterface $output * - * @param InputInterface $input - * @param OutputInterface $output * @return void */ - public function execute(InputInterface $input, OutputInterface $output, Conso $app) : void + public function execute(InputInterface $input, OutputInterface $output, Conso $app): void { $this->displayCommandHelp($input, $output, $app); } /** - * make command method + * make command method. + * + * @param InputInterface $input + * @param OutputInterface $output * - * @param InputInterface $input - * @param OutputInterface $output * @return void */ - public function make(InputInterface $input, OutputInterface $output, Conso $app) : void + public function make(InputInterface $input, OutputInterface $output, Conso $app): void { $name = $input->option(0); - if(!$name) - throw new InputException("command name is required."); - - $command = $app->getCommandsPath() . '/' . ucfirst($name) . '.php'; + if (!$name) { + throw new InputException('command name is required.'); + } + $command = $app->getCommandsPath().'/'.ucfirst($name).'.php'; - if(file_exists($command)) + if (file_exists($command)) { throw new InputException("command [$name] already exists."); - - if(!is_writable($app->getCommandsPath())) + } + if (!is_writable($app->getCommandsPath())) { throw new \Exception("no permissions to create a command at this location {$app->getCommandsPath()}."); - - $content = file_get_contents(__DIR__ . "/stubs/command"); + } + $content = file_get_contents(__DIR__.'/stubs/command'); $content = str_replace('#command#', ucfirst($name), $content); $content = str_replace('#namespace#', $app->getCommandsNamespace(), $content); $create = file_put_contents($command, $content); - if($create) + if ($create) { $output->success("Command [$name] created successfully."); + } exit; } /** - * delete command method + * delete command method. + * + * @param InputInterface $input + * @param OutputInterface $output * - * @param InputInterface $input - * @param OutputInterface $output * @return void */ - public function delete(InputInterface $input, OutputInterface $output, Conso $app) : void + public function delete(InputInterface $input, OutputInterface $output, Conso $app): void { $name = $input->option(0); - if(!$name) - throw new InputException("command name is required."); - - $command = $app->getCommandsPath() . '/' . ucfirst($name) . '.php'; + if (!$name) { + throw new InputException('command name is required.'); + } + $command = $app->getCommandsPath().'/'.ucfirst($name).'.php'; - if(!file_exists($command)) + if (!file_exists($command)) { throw new InputException("command [$name] does not exists."); - - if(!is_writable($app->getCommandsPath())) + } + if (!is_writable($app->getCommandsPath())) { throw new \Exception("no permissions to delete ($name) command"); - - if(unlink($command)) + } + if (unlink($command)) { $output->success("command [$name] deleted successfully."); + } } -} \ No newline at end of file +} diff --git a/src/Conso/CommandsTable.php b/src/Conso/CommandsTable.php index 1fc10e9..5fcff6b 100644 --- a/src/Conso/CommandsTable.php +++ b/src/Conso/CommandsTable.php @@ -1,58 +1,62 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ -use function Conso\flatten; - /** * This class is responsible for defining and - * configuring commands + * configuring commands. */ class CommandsTable { /** - * commands array + * commands array. * * @var array */ - private $commands = array( + private $commands = [ - ); + ]; /** - * add command method + * add command method. + * + * @param string $name + * @param mixed $action * - * @param string $name - * @param mixed $action * @return self */ - public function add(string $name, $action) : self + public function add(string $name, $action): self { - $this->commands[] = array( + $this->commands[] = [ 'name' => $name, 'aliases' => [], 'sub' => [], 'action' => $action, 'flags' => [], 'description' => '', - 'help' => [] - ); + 'help' => [], + ]; return $this; } /** - * edit command elements + * edit command elements. * * @param string $key + * * @return void */ protected function assign(string $key, $value) @@ -61,70 +65,78 @@ protected function assign(string $key, $value) } /** - * sub commands method + * sub commands method. + * + * @param string|array ...$aliases * - * @param string|array ...$aliases * @return self */ - public function sub(...$sub) : self + public function sub(...$sub): self { $this->assign('sub', flatten($sub)); + return $this; } /** - * aliases method + * aliases method. + * + * @param string|array ...$aliases * - * @param string|array ...$aliases * @return self */ - public function alias(...$aliases) : self + public function alias(...$aliases): self { $this->assign('aliases', flatten($aliases)); + return $this; } /** - * flags method + * flags method. + * + * @param string|array ...$flags * - * @param string|array ...$flags * @return self */ - public function flags(...$flags) : self + public function flags(...$flags): self { $this->assign('flags', flatten($flags)); + return $this; } /** - * description method + * description method. * * @return void */ - public function description(string $desc) : self + public function description(string $desc): self { $this->assign('description', $desc); + return $this; } /** - * help message method + * help message method. * * @return void */ - public function help(array $help) : self + public function help(array $help): self { $this->assign('help', $help); + return $this; } /** - * get commands method + * get commands method. * * @return void */ - public function &getCommands() : array + public function &getCommands(): array { return $this->commands; } -} \ No newline at end of file +} diff --git a/src/Conso/Conso.php b/src/Conso/Conso.php index 563a5cd..9df1e22 100644 --- a/src/Conso/Conso.php +++ b/src/Conso/Conso.php @@ -1,12 +1,16 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ @@ -15,96 +19,96 @@ /** * This class is responsible for linking all components - * of conso + * of conso. */ class Conso { /** - * additional config methods + * additional config methods. */ use ConsoTrait; /** - * commands table object + * commands table object. * * @var CommandsTable */ protected $table; /** - * commands linker object + * commands linker object. * * @var CommandsTable */ protected $linker; /** - * commands invoker object + * commands invoker object. * * @var CommandInvoker */ protected $invoker; /** - * active command + * active command. * * @var ?array */ - public $activeCommand = NULL; + public $activeCommand = null; /** - * constructor + * constructor. * - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output */ public function __construct(InputInterface $input, OutputInterface $output) { - $this->output = $output; - $this->table = new CommandsTable; - $this->linker = new CommandLinker($input, $output); - $this->invoker = new CommandInvoker($input, $output, $this); + $this->output = $output; + $this->table = new CommandsTable(); + $this->linker = new CommandLinker($input, $output); + $this->invoker = new CommandInvoker($input, $output, $this); } /** - * add command method + * add command method. * - * @param string $name - * @param mixed $action + * @param string $name + * @param mixed $action * * @return CommandsTable */ - public function command(string $name, $action) : CommandsTable + public function command(string $name, $action): CommandsTable { return $this->table->add($name, $action); } /** - * run application method + * run application method. + * + * @param int $env * - * @param integer $env * @return mixed */ public function run(int $env = 0) { - try{ - + try { $commands = &$this->table->getCommands(); // pass table defined commands & match - $this->activeCommand = $this->linker->link($commands); + $this->activeCommand = $this->linker->link($commands); - if(!$this->activeCommand) + if (!$this->activeCommand) { return $this->invoker->invokeInformation($commands); + } return $this->invoker->invoke($this->activeCommand); + } catch (\Exception $e) { + if ($this->output->isTestMode()) { // is test mode - }catch(\Exception $e) - { - if($this->output->isTestMode()) // is test mode throw new \Exception($e); - + } $this->output->exception($e, $env); } } -} \ No newline at end of file +} diff --git a/src/Conso/ConsoTrait.php b/src/Conso/ConsoTrait.php index e6ac05c..3fa31da 100644 --- a/src/Conso/ConsoTrait.php +++ b/src/Conso/ConsoTrait.php @@ -1,192 +1,193 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ -use Conso\Contracts\InputInterface; -use Conso\Contracts\OutputInterface; - /** - * This trait holds conso information + * This trait holds conso information. */ trait ConsoTrait { /** - * signature + * signature. * * @var string */ protected $signature = ''; /** - * name + * name. * * @var string */ protected $name = 'Conso'; /** - * version + * version. * * @var string */ protected $version = '1.0.0'; /** - * author + * author. * * @var string */ - protected $author = 'Lotfio Lakehal'; + protected $author = 'Lotfio Lakehal'; /** - * commands path + * commands path. * * @var string */ - protected $commandsPath = __DIR__ . '/Commands'; + protected $commandsPath = __DIR__.'/Commands'; /** - * commands namespace + * commands namespace. * * @var string */ protected $commandsNamespace = 'Conso\\Commands'; /** - * set signature method + * set signature method. * * @return void */ - public function setSignature(string $signature) : void + public function setSignature(string $signature): void { $this->signature = $signature; } /** - * set version method + * set version method. * * @return void */ - public function setVersion(string $version) : void + public function setVersion(string $version): void { $this->version = $version; } /** - * set author + * set author. * * @return void */ - public function setAuthor(string $author) : void + public function setAuthor(string $author): void { $this->author = $author; } /** - * set app name + * set app name. * * @return void */ - public function setName(string $name) : void + public function setName(string $name): void { $this->name = $name; } /** - * set commands path + * set commands path. * * @return void */ - public function setCommandsPath(string $path) : void + public function setCommandsPath(string $path): void { $this->commandsPath = $path; } /** - * set commands namespace + * set commands namespace. * * @return void */ - public function setCommandsNamespace(string $namespace) : void + public function setCommandsNamespace(string $namespace): void { $this->commandsNamespace = $namespace; } /** - * set signature method + * set signature method. * * @return string */ - public function getSignature() : string + public function getSignature(): string { return $this->signature; } /** - * set app name + * set app name. * * @return string */ - public function getName() : string + public function getName(): string { return $this->name; } /** - * get version method + * get version method. * * @return string */ - public function getVersion() : string + public function getVersion(): string { return $this->version; } /** - * get version method + * get version method. * * @return string */ - public function getAuthor() : string + public function getAuthor(): string { return $this->author; } /** - * get commands path + * get commands path. * * @return void */ - public function getCommandsPath() : string + public function getCommandsPath(): string { return $this->commandsPath; } /** - * get commands namespace + * get commands namespace. * * @return void */ - public function getCommandsNamespace() : string + public function getCommandsNamespace(): string { return $this->commandsNamespace; } /** - * get available commands method + * get available commands method. * * @return array */ - public function &getCommands() : array + public function &getCommands(): array { return $this->table->getCommands(); } -} \ No newline at end of file +} diff --git a/src/Conso/Contracts/CommandInterface.php b/src/Conso/Contracts/CommandInterface.php index 63bb52b..6f75880 100644 --- a/src/Conso/Contracts/CommandInterface.php +++ b/src/Conso/Contracts/CommandInterface.php @@ -1,12 +1,16 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ @@ -16,12 +20,13 @@ interface CommandInterface { /** * execute command method - * this is main command method + * this is main command method. + * + * @param InputInterface $input + * @param OutputInterface $output + * @param Conso $app * - * @param InputInterface $input - * @param OutputInterface $output - * @param Conso $app * @return void */ - public function execute(InputInterface $input, OutputInterface $output, Conso $app) : void; -} \ No newline at end of file + public function execute(InputInterface $input, OutputInterface $output, Conso $app): void; +} diff --git a/src/Conso/Contracts/InputInterface.php b/src/Conso/Contracts/InputInterface.php index ea57517..376a341 100644 --- a/src/Conso/Contracts/InputInterface.php +++ b/src/Conso/Contracts/InputInterface.php @@ -1,42 +1,45 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ - interface InputInterface { /** - * input command + * input command. * * @return string|null */ - public function command() : ?string; + public function command(): ?string; /** - * input sub command + * input sub command. * * @return string|null */ - public function subCommand() : ?string; + public function subCommand(): ?string; /** - * input options + * input options. * * @return array */ - public function options() : array; + public function options(): array; /** - * input flags + * input flags. * * @return array */ - public function flags() : array; -} \ No newline at end of file + public function flags(): array; +} diff --git a/src/Conso/Contracts/OutputInterface.php b/src/Conso/Contracts/OutputInterface.php index 58eab1c..61a1980 100644 --- a/src/Conso/Contracts/OutputInterface.php +++ b/src/Conso/Contracts/OutputInterface.php @@ -1,15 +1,18 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ - interface OutputInterface { /** @@ -21,4 +24,4 @@ interface OutputInterface * @param int $bold */ public function writeLn(string $line, string $color, string $bg, int $bold); -} \ No newline at end of file +} diff --git a/src/Conso/Exceptions/InputException.php b/src/Conso/Exceptions/InputException.php index cde732f..e73ad84 100644 --- a/src/Conso/Exceptions/InputException.php +++ b/src/Conso/Exceptions/InputException.php @@ -1,13 +1,18 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ - -class InputException extends \Exception{} \ No newline at end of file +class InputException extends \Exception +{ +} diff --git a/src/Conso/Exceptions/InvokerException.php b/src/Conso/Exceptions/InvokerException.php index f3e3d94..e16bd77 100644 --- a/src/Conso/Exceptions/InvokerException.php +++ b/src/Conso/Exceptions/InvokerException.php @@ -1,13 +1,18 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ - -class InvokerException extends \Exception{} \ No newline at end of file +class InvokerException extends \Exception +{ +} diff --git a/src/Conso/Exceptions/OutputException.php b/src/Conso/Exceptions/OutputException.php index 3b4f6c6..1614add 100644 --- a/src/Conso/Exceptions/OutputException.php +++ b/src/Conso/Exceptions/OutputException.php @@ -1,13 +1,18 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ - -class OutputException extends \Exception{} \ No newline at end of file +class OutputException extends \Exception +{ +} diff --git a/src/Conso/Input.php b/src/Conso/Input.php index 6a4e00c..5a7d3a6 100644 --- a/src/Conso/Input.php +++ b/src/Conso/Input.php @@ -1,58 +1,61 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ use Conso\Contracts\InputInterface; -use Conso\Exceptions\InputException; /** * This class is responsible for handling user input - * either from cli argv or custom argv (handy if we want to invoke cli from http) + * either from cli argv or custom argv (handy if we want to invoke cli from http). */ class Input implements InputInterface { /** - * Input command + * Input command. * * @var ?string */ - private $command = NULL; + private $command = null; /** - * Input sub command + * Input sub command. * * @var ?string */ - private $subCommand = NULL; + private $subCommand = null; /** - * Input options + * Input options. * * @var array */ - private $options = array( + private $options = [ - ); + ]; /** - * Input flags + * Input flags. * * @var array */ - private $flags = array( + private $flags = [ - ); + ]; /** - * reserved flags + * reserved flags. * * @var array */ @@ -62,121 +65,124 @@ class Input implements InputInterface '-c', '--commands', '-q', '--quiet', '--ansi', '--no-ansi', - '-n', '--no-interaction' + '-n', '--no-interaction', ]; /** * constructor - * set up argv or user argv + * set up argv or user argv. * * @param ?string $arguments */ - public function __construct(?string $userArgv = NULL) + public function __construct(?string $userArgv = null) { // get argv and remove file name - global $argv; unset($argv[0]); + global $argv; + unset($argv[0]); // get argv or user argv $argv = (is_string($userArgv) && strlen($userArgv) > 0) - ? array_values(array_filter(explode(" ", $userArgv))) + ? array_values(array_filter(explode(' ', $userArgv))) : array_values($argv); $this->extract($argv); } /** - * extract argv array to commands, sub commands, options & flags + * extract argv array to commands, sub commands, options & flags. * * @return void */ - private function extract(array $argv) : void + private function extract(array $argv): void { // set command if no sub command - if(isset($argv[0])) - $this->command = $argv[0] ?? NULL; + if (isset($argv[0])) { + $this->command = $argv[0] ?? null; + } // if command & sub command - if(isset($argv[0]) && preg_match('/^([A-z]+)(\:)([A-z]+)$/', $argv[0])) - { - $command = explode(':', $argv[0]); - $this->command = $command[0] ?? NULL; - $this->subCommand = $command[1] ?? NULL; + if (isset($argv[0]) && preg_match('/^([A-z]+)(\:)([A-z]+)$/', $argv[0])) { + $command = explode(':', $argv[0]); + $this->command = $command[0] ?? null; + $this->subCommand = $command[1] ?? null; } - // reset argv array - unset($argv[0]); + // reset argv array + unset($argv[0]); - $this->flags = array_values(array_filter($argv, function($elem){ - return preg_match('/^[\-]{1,2}[A-z]+$/', $elem); - })); + $this->flags = array_values(array_filter($argv, function ($elem) { + return preg_match('/^[\-]{1,2}[A-z]+$/', $elem); + })); - $this->options = array_values(array_diff($argv, $this->flags)); + $this->options = array_values(array_diff($argv, $this->flags)); } /** - * get command method + * get command method. * * @return string|null */ - public function command() : ?string + public function command(): ?string { return $this->command; } /** - * get sub command method + * get sub command method. * * @return string|null */ - public function subCommand() : ?string + public function subCommand(): ?string { return $this->subCommand; } /** - * input option method + * input option method. + * + * @param int $index * - * @param integer $index * @return string|null */ - public function option(int $index) : ?string + public function option(int $index): ?string { - return isset($this->options[$index]) ? $this->options[$index] : NULL; + return isset($this->options[$index]) ? $this->options[$index] : null; } /** - * input options method + * input options method. * * @return array */ - public function options() : array + public function options(): array { return $this->options; } /** - * input flag method + * input flag method. + * + * @param int $index * - * @param integer $index * @return string|null */ - public function flag(int $index) : ?string + public function flag(int $index): ?string { - return isset($this->flags[$index]) ? $this->flags[$index] : NULL; + return isset($this->flags[$index]) ? $this->flags[$index] : null; } /** - * input flags method + * input flags method. * * @return array */ - public function flags() : array + public function flags(): array { return $this->flags; } /** - * reserved flags + * reserved flags. * * @return void */ diff --git a/src/Conso/Output.php b/src/Conso/Output.php index dc7d23e..81a9f58 100644 --- a/src/Conso/Output.php +++ b/src/Conso/Output.php @@ -1,12 +1,16 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ @@ -15,26 +19,26 @@ /** * This class is responsible for output and output - * formatting + * formatting. */ class Output implements OutputInterface { /** - * disable ansi + * disable ansi. * - * @var boolean + * @var bool */ - private $noAnsi = false; + private $noAnsi = false; /** - * test mode + * test mode. * - * @var boolean + * @var bool */ private $testMode = false; /** - * colors + * colors. * * @var array */ @@ -48,7 +52,7 @@ class Output implements OutputInterface ]; /** - * background colors + * background colors. * * @var array */ @@ -72,105 +76,114 @@ class Output implements OutputInterface */ public function writeLn(string $line, string $color = 'white', string $bg = 'trans', int $bold = 0) { - $str = $this->lineFormatter($line, $color, $bg, $bold); + $str = $this->lineFormatter($line, $color, $bg, $bold); + return ($this->testMode) ? $str : fwrite(STDOUT, $str, strlen($str)); } /** - * output line formatter + * output line formatter. * * @return void */ public function lineFormatter(string $line, string $color, string $bg, int $bold) // needs to check if colors exists and staff & validate is256 not testing & supported { - - if(!array_key_exists($color, $this->colors)) + if (!array_key_exists($color, $this->colors)) { throw new OutputException("$color color is not a defined color"); - - if(!array_key_exists($bg, $this->bg)) + } + if (!array_key_exists($bg, $this->bg)) { throw new OutputException("$color background color is not a defined color"); - - if(!$this->is256() || $this->noAnsi == true) + } + if (!$this->is256() || $this->noAnsi == true) { return $line; + } return "\e[".$bold.';'.$this->colors[$color].';'.$this->bg[$bg].'m'.$line."\e[0m"; } /** - * output success message + * output success message. * * @param string $msg + * * @return void */ public function success(string $msg) { - $this->writeLn("\n" . $msg . "\n", "green"); + $this->writeLn("\n".$msg."\n", 'green'); } /** - * exception output + * exception output. * * @return void */ - public function exception(\Exception $e, $env = 0) : void + public function exception(\Exception $e, $env = 0): void { $class = get_class($e); // output error in dev - if(php_sapi_name() == 'cli') // if cli - { - if($env == 1) - { - $this->writeLn("=>\n", "yellow"); - $this->writeLn(" - error : ", "yellow");$this->writeLn(" {$class}\n" , "red"); - $this->writeLn(" - message : ", "yellow");$this->writeLn(" {$e->getMessage()}\n", "red"); - $this->writeLn(" - file : ", "yellow");$this->writeLn(" {$e->getFile()}\n" , "red"); - $this->writeLn(" - line : ", "yellow");$this->writeLn(" {$e->getLine()}\n" , "red"); - $this->writeLn("=>", "yellow"); + if (php_sapi_name() == 'cli') { // if cli + if ($env == 1) { + $this->writeLn("=>\n", 'yellow'); + $this->writeLn(' - error : ', 'yellow'); + $this->writeLn(" {$class}\n", 'red'); + $this->writeLn(' - message : ', 'yellow'); + $this->writeLn(" {$e->getMessage()}\n", 'red'); + $this->writeLn(' - file : ', 'yellow'); + $this->writeLn(" {$e->getFile()}\n", 'red'); + $this->writeLn(' - line : ', 'yellow'); + $this->writeLn(" {$e->getLine()}\n", 'red'); + $this->writeLn('=>', 'yellow'); + return; } // output error in production $max = max([strlen($e->getMessage()), strlen($class)]) + 8; // get max - $this->writeLn("\n" .str_repeat(" ", $max + 6) . "\n", 'white', 'red'); + $this->writeLn("\n".str_repeat(' ', $max + 6)."\n", 'white', 'red'); $this->writeLn(" [{$class}]", 'white', 'red'); - $this->writeLn(str_repeat(" ", $max - strlen($class)) . "\n", 'white', 'red'); + $this->writeLn(str_repeat(' ', $max - strlen($class))."\n", 'white', 'red'); $this->writeLn(" {$e->getMessage()}", 'white', 'red'); - $this->writeLn(str_repeat(" ", $max - strlen($e->getMessage())) . "\n", 'white', 'red'); - $this->writeLn(str_repeat(" ", $max + 6) . "\n", 'white', 'red'); + $this->writeLn(str_repeat(' ', $max - strlen($e->getMessage()))."\n", 'white', 'red'); + $this->writeLn(str_repeat(' ', $max + 6)."\n", 'white', 'red'); + return; } } /** - * check if terminal supports 256 + * check if terminal supports 256. * - * @return boolean + * @return bool */ - public function is256() : bool + public function is256(): bool { - if(DIRECTORY_SEPARATOR == '\\') // windows + if (DIRECTORY_SEPARATOR == '\\') { // windows + return function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support(STDOUT); + } return strpos(getenv('TERM'), '256color') !== false; } /** - * clear line method + * clear line method. * * @return void */ public function clearLine() { - $cls = "\r";//"\033[5D\r"; or "\033[2K\r"; + $cls = "\r"; //"\033[5D\r"; or "\033[2K\r"; + return fwrite(STDOUT, $cls, strlen($cls)); } /** - * disable ansi method + * disable ansi method. * * @return void */ - public function disableAnsi() : void + public function disableAnsi(): void { $this->noAnsi = true; } @@ -178,27 +191,27 @@ public function disableAnsi() : void /** * enable testing mode * useful method for testing to avoid creating - * custom messy stream filters + * custom messy stream filters. * * @return void */ - public function enableTestMode() : void + public function enableTestMode(): void { $this->testMode = true; } /** - * is test mode method + * is test mode method. * - * @return boolean + * @return bool */ - public function isTestMode() : bool + public function isTestMode(): bool { return $this->testMode; } /** - * timer method + * timer method. * * TODO :: need more work to make percentage works correctly * @@ -206,22 +219,20 @@ public function isTestMode() : bool */ public function timer($ms = 60000) { - $str = ">"; + $str = '>'; $b = 50; for ($i = 0; $i <= 50; $i++) { - - $str = '=' . $str; + $str = '='.$str; $this->clearLine(); - $this->writeLn("[" . $str . str_repeat(' ', $b) . "] [".($i * 2)."%]"); + $this->writeLn('['.$str.str_repeat(' ', $b).'] ['.($i * 2).'%]'); usleep($ms); $b--; } - } -} \ No newline at end of file +} diff --git a/src/Conso/hlprs.php b/src/Conso/hlprs.php index ce68300..ee7ebde 100644 --- a/src/Conso/hlprs.php +++ b/src/Conso/hlprs.php @@ -1,30 +1,33 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ /** - * flatten array method (multi-dimensional to single) + * flatten array method (multi-dimensional to single). + * + * @param array $arr * - * @param array $arr * @return void */ -function flatten(array $arr) : array +function flatten(array $arr): array { $singleDimArray = []; foreach ($arr as $item) { - if (is_array($item)) { $singleDimArray = array_merge($singleDimArray, flatten($item)); - } else { $singleDimArray[] = $item; } @@ -34,18 +37,18 @@ function flatten(array $arr) : array } /** - * get private and protected properties + * get private and protected properties. + * + * @param string $class + * @param string $pr * - * @param string $class - * @param string $pr * @return ?array */ -function readProtectedProperty(string $class,string $property) +function readProtectedProperty(string $class, string $property) { - $cmd = new \ReflectionClass($class); + $cmd = new \ReflectionClass($class); - if($cmd->hasProperty($property)) // return property if exists - { + if ($cmd->hasProperty($property)) { // return property if exists $property = $cmd->getProperty($property); $property->setAccessible(true); @@ -56,17 +59,21 @@ function readProtectedProperty(string $class,string $property) } /** - * read command properties from class + * read command properties from class. + * + * @param array $command * - * @param array $command * @return void */ -function readCommandPropertiesFromClass(array &$command) : void +function readCommandPropertiesFromClass(array &$command): void { $list = ['sub', 'flags', 'description', 'help', 'aliases']; - if(is_string($command['action']) && class_exists($command['action'])) // if is controller - foreach($list as $lst) - if(empty($command[$lst])) - $command[$lst] = readProtectedProperty($command['action'], $lst); // fill from command class if not defined by method -} \ No newline at end of file + if (is_string($command['action']) && class_exists($command['action'])) { // if is controller + foreach ($list as $lst) { + if (empty($command[$lst])) { + $command[$lst] = readProtectedProperty($command['action'], $lst); + } + } + } // fill from command class if not defined by method +} diff --git a/tests/Unit/CommandInvokerTest.php b/tests/Unit/CommandInvokerTest.php index 8779897..acbd72d 100644 --- a/tests/Unit/CommandInvokerTest.php +++ b/tests/Unit/CommandInvokerTest.php @@ -1,72 +1,75 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ +use Conso\CommandInvoker; +use Conso\Conso; use Conso\Input; use Conso\Output; -use Conso\Conso; -use Conso\CommandInvoker; use PHPUnit\Framework\TestCase; -use Conso\Exceptions\InputException; class CommandInvokerTest extends TestCase { /** - * output + * output. * * @var object */ private $output; /** - * commands + * commands. * * @var array */ private $commands; /** - * set up + * set up. * * @return void */ - public function setUp() : void + public function setUp(): void { - $this->output = new Output; + $this->output = new Output(); $this->output->disableAnsi(); $this->output->enableTestMode(); - $this->commands = array( - array( + $this->commands = [ + [ 'name' => 'make', 'aliases' => [], 'sub' => ['controller'], - 'action' => "Tests\\Unit\\Mocks\\Make", + 'action' => 'Tests\\Unit\\Mocks\\Make', 'flags' => ['--form'], 'description' => 'This is make command', - 'help' => [] - ) - ); + 'help' => [], + ], + ]; } /** - * test invoke callback + * test invoke callback. * * @return void */ public function testInvokeCallback() { - $this->commands[0]['action'] = function(){ return 'from make callback'; }; + $this->commands[0]['action'] = function () { return 'from make callback'; }; - $inp = new Input('make'); + $inp = new Input('make'); $invoker = new CommandInvoker($inp, $this->output, new Conso($inp, $this->output)); $this->assertEquals( @@ -76,13 +79,13 @@ public function testInvokeCallback() } /** - * test invoke class method + * test invoke class method. * * @return void */ public function testInvokeClassMethod() { - $inp = new Input('make'); + $inp = new Input('make'); $invoker = new CommandInvoker($inp, $this->output, new Conso($inp, $this->output)); ob_start(); @@ -94,4 +97,4 @@ public function testInvokeClassMethod() $res ); } -} \ No newline at end of file +} diff --git a/tests/Unit/CommandLinkerTest.php b/tests/Unit/CommandLinkerTest.php index 5626517..15a1db3 100644 --- a/tests/Unit/CommandLinkerTest.php +++ b/tests/Unit/CommandLinkerTest.php @@ -1,75 +1,79 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ +use Conso\CommandLinker; +use Conso\Exceptions\InputException; use Conso\Input; use Conso\Output; -use Conso\CommandLinker; use PHPUnit\Framework\TestCase; -use Conso\Exceptions\InputException; class CommandLinkerTest extends TestCase { /** - * output + * output. * * @var object */ private $output; /** - * commands + * commands. * * @var array */ private $commands; /** - * set up + * set up. * * @return void */ - public function setUp() : void + public function setUp(): void { - $this->output = new Output; + $this->output = new Output(); $this->output->disableAnsi(); $this->output->enableTestMode(); - $this->commands = array( - array( + $this->commands = [ + [ 'name' => 'make', 'aliases' => [], 'sub' => ['controller'], - 'action' => "MakeClass", + 'action' => 'MakeClass', 'flags' => ['--form'], 'description' => 'This is make command', - 'help' => [] - ) - ); + 'help' => [], + ], + ]; } /** - * test link method + * test link method. * * @return void */ public function testLinkMethodNoCommand() { - $linker = new CommandLinker(new Input, $this->output); - $link = $linker->link($this->commands); - $this->assertEquals(NULL, $link); + $linker = new CommandLinker(new Input(), $this->output); + $link = $linker->link($this->commands); + $this->assertEquals(null, $link); } /** - * test wrong method + * test wrong method. * * @return void */ @@ -81,7 +85,7 @@ public function testLinkMethodWrongCommand() } /** - * test wrong sub command + * test wrong sub command. * * @return void */ @@ -93,7 +97,7 @@ public function testLinkMethodWrongSubCommand() } /** - * test wrong flag + * test wrong flag. * * @return void */ @@ -105,14 +109,14 @@ public function testLinkMethodWrongFlags() } /** - * test linked match + * test linked match. * * @return void */ public function testLinkMethodCommandMatch() { $linker = new CommandLinker(new Input('make'), $this->output); - $res = $linker->link($this->commands); + $res = $linker->link($this->commands); $this->assertSame($res, $this->commands[0]); } -} \ No newline at end of file +} diff --git a/tests/Unit/CommandsTableTest.php b/tests/Unit/CommandsTableTest.php index 5936993..b67e33e 100644 --- a/tests/Unit/CommandsTableTest.php +++ b/tests/Unit/CommandsTableTest.php @@ -1,12 +1,16 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ @@ -16,30 +20,30 @@ class CommandsTableTest extends TestCase { /** - * commands table + * commands table. * * @var object */ private $table; /** - * set up command + * set up command. * * @return void */ - public function setUp() : void + public function setUp(): void { - $this->table = new CommandsTable; + $this->table = new CommandsTable(); } /** - * test command name + * test command name. * * @return void */ public function testCommandName() { - $this->table->add('test', NULL); + $this->table->add('test', null); $this->assertEquals( 'test', $this->table->getCommands()[0]['name'] @@ -47,7 +51,7 @@ public function testCommandName() } /** - * test command action + * test command action. * * @return void */ @@ -61,7 +65,7 @@ public function testCommandAction() } /** - * test sub command + * test sub command. * * @return void */ @@ -75,7 +79,7 @@ public function testSubCommand() } /** - * test command flags + * test command flags. * * @return void */ @@ -89,7 +93,7 @@ public function testCommandFlags() } /** - * test command aliases + * test command aliases. * * @return void */ @@ -103,7 +107,7 @@ public function testCommandAlias() } /** - * test command aliases + * test command aliases. * * @return void **/ @@ -115,6 +119,7 @@ public function testCommandDescription() $this->table->getCommands()[0]['description'] ); } + /* * test command aliases * @@ -128,4 +133,4 @@ public function testCommandHelp() $this->table->getCommands()[0]['help'] ); } -} \ No newline at end of file +} diff --git a/tests/Unit/ConsoTest.php b/tests/Unit/ConsoTest.php index 7213182..4452c06 100644 --- a/tests/Unit/ConsoTest.php +++ b/tests/Unit/ConsoTest.php @@ -1,43 +1,47 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ +use Conso\Conso; use Conso\Input; use Conso\Output; -use Conso\Conso; use PHPUnit\Framework\TestCase; class ConsoTest extends TestCase { /** - * output + * output. * * @var object */ private $output; /** - * set up + * set up. * * @return void */ - public function setUp() : void + public function setUp(): void { - $this->output = new Output; + $this->output = new Output(); $this->output->disableAnsi(); $this->output->enableTestMode(); } /** - * test not found command + * test not found command. * * @return void */ @@ -49,7 +53,7 @@ public function testCommandNotFound() } /** - * test not found command + * test not found command. * * @return void */ @@ -57,12 +61,12 @@ public function testSubCommandNotFound() { $this->expectException(\Exception::class); $app = new Conso(new Input('test:hello'), $this->output); - $app->command('test', function(){}); + $app->command('test', function () {}); $app->run(); } /** - * test flag not found + * test flag not found. * * @return void */ @@ -70,18 +74,18 @@ public function testFlagNotFound() { $this->expectException(\Exception::class); $app = new Conso(new Input('test:hello --crud'), $this->output); - $app->command('test', function(){})->sub('hello'); + $app->command('test', function () {})->sub('hello'); $app->run(); } /** - * test set signature + * test set signature. * * @return void */ public function testSetSignature() { - $app = new Conso(new Input, $this->output); + $app = new Conso(new Input(), $this->output); $app->setSignature('conso'); $this->assertEquals( @@ -91,13 +95,13 @@ public function testSetSignature() } /** - * test set name + * test set name. * * @return void */ public function testSetName() { - $app = new Conso(new Input, $this->output); + $app = new Conso(new Input(), $this->output); $app->setName('console app'); $this->assertEquals( @@ -107,13 +111,13 @@ public function testSetName() } /** - * test set version + * test set version. * * @return void */ public function testSetVersion() { - $app = new Conso(new Input, $this->output); + $app = new Conso(new Input(), $this->output); $app->setVersion('1.0.0'); $this->assertEquals( @@ -123,13 +127,13 @@ public function testSetVersion() } /** - * test set author + * test set author. * * @return void */ public function testSetAuthor() { - $app = new Conso(new Input, $this->output); + $app = new Conso(new Input(), $this->output); $app->setAuthor('lotfio lakehal'); $this->assertEquals( @@ -139,13 +143,13 @@ public function testSetAuthor() } /** - * test set commands path + * test set commands path. * * @return void */ public function testSetCommandsPath() { - $app = new Conso(new Input, $this->output); + $app = new Conso(new Input(), $this->output); $app->setCommandsPath('/commands'); $this->assertEquals( @@ -155,13 +159,13 @@ public function testSetCommandsPath() } /** - * test set commands namespace + * test set commands namespace. * * @return void */ public function testSetCommandsNamespace() { - $app = new Conso(new Input, $this->output); + $app = new Conso(new Input(), $this->output); $app->setCommandsNamespace('Conso\\'); $this->assertEquals( @@ -171,13 +175,13 @@ public function testSetCommandsNamespace() } /** - * test get commands + * test get commands. * * @return void */ public function testSetGetCommands() { - $app = new Conso(new Input, $this->output); + $app = new Conso(new Input(), $this->output); $app->command('test', 'testCommandClass'); $this->assertContains( @@ -185,4 +189,4 @@ public function testSetGetCommands() $app->getCommands()[0] ); } -} \ No newline at end of file +} diff --git a/tests/Unit/InputTest.php b/tests/Unit/InputTest.php index 84acd30..e31cb88 100644 --- a/tests/Unit/InputTest.php +++ b/tests/Unit/InputTest.php @@ -1,12 +1,16 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ @@ -16,50 +20,50 @@ class InputTest extends TestCase { /** - * input + * input. * * @var object */ private $input; /** - * setting up input + * setting up input. * * @return void */ - public function setUp() : void + public function setUp(): void { - $this->input = new Input("make:controller user --crud"); + $this->input = new Input('make:controller user --crud'); } /** - * test input command + * test input command. * * @return void */ public function testInputCommand() { $this->assertEquals( - "make", + 'make', $this->input->command() ); } /** - * test input command + * test input command. * * @return void */ public function testInputSubCommand() { $this->assertEquals( - "controller", + 'controller', $this->input->subCommand() ); } /** - * test input options + * test input options. * * @return void */ @@ -67,13 +71,13 @@ public function testInputOptions() { $this->assertContains('user', $this->input->options()); $this->assertEquals( - "user", + 'user', $this->input->option(0) ); } /** - * test input options + * test input options. * * @return void */ @@ -81,8 +85,8 @@ public function testInputFlags() { $this->assertContains('--crud', $this->input->flags()); $this->assertEquals( - "--crud", + '--crud', $this->input->flag(0) ); } -} \ No newline at end of file +} diff --git a/tests/Unit/Mocks/Make.php b/tests/Unit/Mocks/Make.php index 894556c..6153c17 100644 --- a/tests/Unit/Mocks/Make.php +++ b/tests/Unit/Mocks/Make.php @@ -1,62 +1,68 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ -use Conso\Conso; use Conso\Command as BaseCommand; -use Conso\Exceptions\InputException; -use Conso\Contracts\{CommandInterface,InputInterface,OutputInterface}; +use Conso\Conso; +use Conso\Contracts\CommandInterface; +use Conso\Contracts\InputInterface; +use Conso\Contracts\OutputInterface; class Make extends BaseCommand implements CommandInterface { /** - * sub commands + * sub commands. * * @var array */ - protected $sub = array( - ); + protected $sub = [ + ]; /** - * flags + * flags. * * @var array */ - protected $flags = array( - ); + protected $flags = [ + ]; /** - * command help + * command help. * * @var string */ - protected $help = array( - ); + protected $help = [ + ]; /** - * command description + * command description. * * @var string */ protected $description = 'This is Make command description.'; /** - * execute method + * execute method. + * + * @param InputInterface $input + * @param OutputInterface $output * - * @param InputInterface $input - * @param OutputInterface $output * @return void */ - public function execute(InputInterface $input, OutputInterface $output, Conso $app) : void + public function execute(InputInterface $input, OutputInterface $output, Conso $app): void { - echo "from make class command"; + echo 'from make class command'; } -} \ No newline at end of file +} diff --git a/tests/Unit/OutputTest.php b/tests/Unit/OutputTest.php index 0ff835d..ee29451 100644 --- a/tests/Unit/OutputTest.php +++ b/tests/Unit/OutputTest.php @@ -1,12 +1,16 @@ - - * @package Conso PHP Console Creator + * * @version 1.0.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ @@ -16,26 +20,26 @@ class OutputTest extends TestCase { /** - * output + * output. * * @var object */ private $output; /** - * set up + * set up. * * @return void */ - public function setUp() : void + public function setUp(): void { - $this->output = new Output; + $this->output = new Output(); $this->output->enableTestMode(); // disable write to STDOUT useful for testing $this->output->disableAnsi(); // disable ansi no colors needed } /** - * test output write ln + * test output write ln. * * @return void */ @@ -43,7 +47,7 @@ public function testOutputWriteLn() { $this->assertEquals( 'hello', - $this->output->writeLn("hello") + $this->output->writeLn('hello') ); } -} \ No newline at end of file +}