diff --git a/src/Console/Code.php b/src/Console/Code.php deleted file mode 100644 index c79f5121..00000000 --- a/src/Console/Code.php +++ /dev/null @@ -1,44 +0,0 @@ - - * - * @link https://github.com/zestframework/Zest_Framework - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * @license MIT - */ - -namespace Zest\Console; - -class Code -{ - public function controller($name) - { - return << + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console; + +use Zest\Data\Arrays; + +class Colorize +{ + /** + * Foregroud colors. + * + * @since 3.0.0 + * + * @var array + */ + public $styles = [ + 'bold' => '1m', + 'faded' => '2m', + 'underlined' => '4m', + 'blinking' => '5m', + 'reversed' => '7m', + 'hidden' => '8m', + 'red' => '31m', + 'green' => '32m', + 'yellow' => '33m', + 'blue' => '34m', + 'magenta' => '35m', + 'cyan' => '36m', + 'light_gray' => '37m', + 'dark_gray' => '90m', + 'white' => '0m', + 'bg:red' => '41m', + 'bg:green' => '42m', + 'bg:yellow' => '53m', + 'bg:blue' => '44m', + 'bg:magenta' => '45m', + 'bg:cyan' => '46m', + 'bg:light_gray' => '47m', + 'bg:dark_gray' => '100m', + 'bg:white' => '0m', + ]; + + /** + * Get the color by key. + * + * @param string $color Color key + * @param bool $background + * + * @since 3.0.0 + * + * @var string + */ + public function get(string $style) + { + if (Arrays::has($this->styles, $style, '.')) { + return Arrays::get($this->styles, $style, '.'); + } + } +} diff --git a/src/Console/Command.php b/src/Console/Command.php new file mode 100644 index 00000000..b8bcbb2a --- /dev/null +++ b/src/Console/Command.php @@ -0,0 +1,156 @@ + + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console; + +abstract class Command +{ + /** + * Sign of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $sign; + + /** + * Description of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $description; + + /** + * Should the command hidden form list. + * + * @since 3.0.0 + * + * @var bool + */ + protected $hidden = false; + + /** + * Accpet flag parameter in command. + * + * @since 3.0.0 + * + * @var array + */ + protected $flags = []; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + //todo + } + + /** + * Get hidden. + * + * @return string + */ + public function getHidden(): bool + { + return $this->hidden; + } + + /** + * Get sign. + * + * @return string + */ + public function getSign(): string + { + return $this->sign ?? ''; + } + + /** + * Get description. + * + * @return string + */ + public function getDescription(): string + { + return $this->description ?? ''; + } + + /** + * Get flags. + * + * @return array + */ + public function getFlags(): array + { + return $this->flags ?? []; + } + + /** + * Function to handle the class. + * + * @param string $str + * @param bool $newLine + * + * @return void + */ + public function write(string $str, bool $newLine = true) + { + (new Output())->write($str, $newLine); + } + + /** + * Prompt user for input. + * + * @param string $str + * + * @return void + */ + public function ask(string $str) + { + $this->write("$str", false); + + return (new Input())->ask(); + } + + /** + * Terminate the console. + * + * @return void + */ + public function terminate(): void + { + exit(); + } + + /** + * Function to handle the class. + * + * @param \Zest\Console\Output $output + * @param \Zest\Console\Input $input + * @param array $param + * + * @return void + */ + abstract public function handle(Output $output, Input $input, $prams = []): void; +} diff --git a/src/Console/Commands.php b/src/Console/Commands.php new file mode 100644 index 00000000..b6790f13 --- /dev/null +++ b/src/Console/Commands.php @@ -0,0 +1,50 @@ + + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console; + +class Commands +{ + /** + * Internal commands. + * + * @since 3.0.0 + * + * @var array + */ + protected $commands = [ + ['version', \Zest\Console\Commands\Version::class], + ['list', \Zest\Console\Commands\ListCmd::class], + ['make:controller', \Zest\Console\Commands\Controller::class], + ['clear:cache', \Zest\Console\Commands\Cache::class], + ['serve', \Zest\Console\Commands\ServeCommand::class], + + ]; + + /** + * Get commands. + * + * @since 3.0.0 + * + * @var array + */ + public function getCommands(): array + { + return $this->commands; + } +} diff --git a/src/Console/Commands/Cache.php b/src/Console/Commands/Cache.php new file mode 100644 index 00000000..4f0b1357 --- /dev/null +++ b/src/Console/Commands/Cache.php @@ -0,0 +1,71 @@ + + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console\Commands; + +use Zest\Cache\Cache as CacheManager; +use Zest\Console\Command; +use Zest\Console\Output; +use Zest\Console\Input; + +class Cache extends Command +{ + /** + * Sign of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $sign = 'clear:cache'; + + /** + * Description of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $description = 'Clear the application cache'; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } + + /** + * Function to handle the class. + * + * @param \Zest\Console\Output $output + * @param \Zest\Console\Input $input + * @param array $param + * + * @return void + */ + public function handle(Output $output, Input $input, $param = []): void + { + $c = new CacheManager(); + $c->clear(); + $output->write('Cache cleared'); + } +} diff --git a/src/Console/Commands/Controller.php b/src/Console/Commands/Controller.php new file mode 100644 index 00000000..91830a0e --- /dev/null +++ b/src/Console/Commands/Controller.php @@ -0,0 +1,112 @@ + + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console\Commands; + +use Zest\Console\Command; +use Zest\Console\Output; +use Zest\Console\Input; + +class Controller extends Command +{ + /** + * Sign of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $sign = 'make:controller'; + + /** + * Description of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $description = 'Create a new Controller class'; + + /** + * Accpet flag parameter in command. + * + * @since 3.0.0 + * + * @var array + */ + protected $flags = [ + 'name' + ]; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } + + /** + * Function to handle the class. + * + * @param \Zest\Console\Output $output + * @param \Zest\Console\Input $input + * @param array $param + * + * @return void + */ + public function handle(Output $output, Input $input, $param = []): void + { + $name = $param['name'] ?? "test"; + $file = 'App/Controllers/'.$name.'.php'; + if (!file_exists($file)) { + $fh = fopen($file, 'w'); + fwrite($fh, $this->controller($name)); + fclose($fh); + } + $output->write('Controller created successfully.'); + } + + // should replace with like {stubs} + public function controller($name) + { + return << + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console\Commands; + +use Zest\Common\Version as V; +use Zest\Console\Command; +use Zest\Console\Console; +use Zest\Console\Output; +use Zest\Container\Container; +use Zest\Console\Input; + +class ListCmd extends Command +{ + /** + * Sign of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $sign = 'list'; + + /** + * Description of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $description = 'List the available commands'; + + /** + * Commands. + * + * @since 3.0.0 + * + * @var array + */ + private $cmds; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + $this->container = new Container(); + } + + /** + * Get the list of commandss. + * + * @return void + */ + public function getList(): void + { + $console = new Console(); + $this->cmds = $console->getCommands(); + } + + /** + * Function to handle the class. + * + * @param \Zest\Console\Output $output + * @param \Zest\Console\Input $input + * @param array $param + * + * @return void + */ + public function handle(Output $output, Input $input, $param = []): void + { + $output->write('Zest Framewor: ', false); + $output->write(''.V::VERSION.'', true); + $this->getList(); + $list = []; + foreach ($this->cmds as $command) { + $this->container->register([$command[1], $command[0]], new $command[1]()); + $class = $this->container->get($command[0]); + if (!$class->getHidden()) { + $sign = $class->getSign(); + $desc = $class->getDescription(); + $output->write("${sign} :", true); + $output->write("\t${desc}", true); + } + } + } +} diff --git a/src/Console/Commands/RouterListCommand.php b/src/Console/Commands/RouterListCommand.php new file mode 100644 index 00000000..223d4ba2 --- /dev/null +++ b/src/Console/Commands/RouterListCommand.php @@ -0,0 +1,79 @@ + + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console\Commands; + +use Zest\Console\Command; +use Zest\Console\Output; +use Zest\Router\App; + +class RouterListCommand extends Command +{ + /** + * Sign of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $sign = 'route:list'; + + /** + * Description of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $description = 'List all registered routes'; + + /** + * App instance. + * + * @since 3.0.0 + * + * @var \Zest\Router\App + */ + private $app; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + $this->app = new App(); + } + + /** + * Function to handle the class. + * + * @param \Zest\Console\Output $output + * @param \Zest\Console\Input $input + * @param array $param + * + * @return void + */ + public function handle(Output $output, Input $input, $param = []): void + { + $routers = $this->app->getRoutes(); + var_dump($routers); + } +} diff --git a/src/Console/Commands/ServeCommand.php b/src/Console/Commands/ServeCommand.php new file mode 100644 index 00000000..b77c4cde --- /dev/null +++ b/src/Console/Commands/ServeCommand.php @@ -0,0 +1,76 @@ + + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console\Commands; +use Zest\Common\Version as V; +use Zest\Console\Command; +use Zest\Console\Output; +use Zest\Console\Input; + +class ServeCommand extends Command +{ + /** + * Sign of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $sign = 'serve'; + + /** + * Description of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $description = 'Serve the application on the PHP development server'; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } + + /** + * Function to handle the class. + * + * @param \Zest\Console\Output $output + * @param \Zest\Console\Input $input + * @param array $param + * + * @return void + */ + public function handle(Output $output, Input $input, $param = []): void + { + // generate random 4 digit number + $port = rand(1000, 9999); + $output->write('Zest Framewor: ', false); + $output->write(''.V::VERSION.'', true); + // check if the server is running on $port. + $host = 'localhost:'.$port; + $command = 'php -S '.$host; + $output->write("\n PHP local development server has been started locat at localhost:8080. If the public directory is the root, then localhost:8080/public \n"); + shell_exec($command); + } +} diff --git a/src/Console/Commands/Version.php b/src/Console/Commands/Version.php new file mode 100644 index 00000000..07964c2a --- /dev/null +++ b/src/Console/Commands/Version.php @@ -0,0 +1,70 @@ + + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console\Commands; + +use Zest\Common\Version as V; +use Zest\Console\Command; +use Zest\Console\Output; +use Zest\Console\Input; + +class Version extends Command +{ + /** + * Sign of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $sign = 'version'; + + /** + * Description of the command. + * + * @since 3.0.0 + * + * @var string + */ + protected $description = 'Get the version of Zest framework installed'; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } + + /** + * Function to handle the class. + * + * @param \Zest\Console\Output $output + * @param \Zest\Console\Input $input + * @param array $param + * + * @return void + */ + public function handle(Output $output, Input $input, $param = []): void + { + $output->write('Zest Framewor: ', false); + $output->write(''.V::VERSION.'', true); + } +} diff --git a/src/Console/Console.php b/src/Console/Console.php index ed90f629..3b989869 100644 --- a/src/Console/Console.php +++ b/src/Console/Console.php @@ -18,101 +18,161 @@ namespace Zest\Console; -use Zest\Common\Version; +use Zest\Data\Arrays; +use Zest\Console\Commands as InternalCommands; +use Zest\Container\Container; class Console { - public function createController() + /** + * Instance of container. + * + * @since 3.0.0 + * + * @var \Zest\Container\Container + */ + private $container; + + /** + * Commanads. + * + * @since 3.0.0 + * + * @var array + */ + private $commands = []; + + /** + * Create a new console instance. + * + * @return void + */ + public function __construct() { - echo "Enter name of controller that you want create \n"; - $name = $this->cliInput(); - $code = new Code(); - $data = $code->controller($name); - $writer = new Write(); - if ($writer->controller($name, $data)) { - echo "Controller {$name} created successfully \n"; - $this->main(); - } else { - echo "Something went wrong \n"; - $this->main(); + $this->container = new Container(); + $internalCommands = (new InternalCommands())->getCommands(); + $externalCommands = []; + if (class_exists("\Config\Commands")) { + $externalCommands = (new \Config\Commands())->getCommands(); } + + $this->commands = array_merge($internalCommands, $externalCommands); } - public function createModel() + /** + * Parse the flags from command. + * + * @param array $flags Raw flags. + * + * @return array + */ + public function parseFlags($flags): array { - echo "Enter name of model that you want create \n"; - $name = $this->cliInput(); - $code = new Code(); - $data = $code->controller($name); - $writer = new Write(); - if ($writer->controller($name, $data)) { - echo "Model {$name} created successfully \n"; - $this->main(); + $params = []; + $f = explode(",", $flags); + if (Arrays::isReallyArray($f)) { + foreach ($f as $flag => $fs) { + $param = explode("=", $fs); + if (isset($param[1])) { + $params[$param[0]] = $param[1]; + } + } } else { - echo "Something went wrong \n"; - $this->main(); + $param = explode("=", $flags); + if (isset($param[1])) { + $params[$param[0]] = $param[1]; + } } + + return $params; } - public function startServer() + /** + * Get all commands. + * + * @return array + */ + public function getCommands(): array { - $host = 'localhost:8080'; - $command = 'php -S '.$host; - echo "\n PHP local development server has been started locat at localhost:8080. If the public directory is the root, then localhost:8080/public \n"; - shell_exec($command); + return $this->commands; } - public function main() + /** + * Run the Zest console. + * + * @return void + */ + public function run($param): void { - do { - echo " Zest CLI Environment. \n"; - echo " ***************************** \n"; - echo " Enter 'c' to create a controller. \n"; - echo " Enter 'v' for version information. \n"; - echo " Enter 's' to start a local development server. \n"; - echo " Enter 'x' to quit. \n"; - echo " ***************************** \n"; - $x = $this->cliInput(); - if ($x === 'c') { - $this->createController(); - } elseif ($x === 'v') { - echo 'Zest Framework Version is: '.Version::VERSION."\n"; - $this->main(); - } elseif ($x === 's') { - $this->startServer(); - } elseif ($x === 'x') { - echo 'GoodBye'; - exit(); - } else { - $this->main(); + + // registering the commands to container. + foreach ($this->commands as $command) { + $this->container->register([$command[1], $command[0]], new $command[1]()); + } + + $sign = isset($param[1]) ? $param[1] : 'list'; + $output = new Output(); + $input = new Input(); + if ($this->container->has($sign)) { + $cmd = $this->container->get($sign); + + // default. + if (!isset($param[2])) { + if (count($cmd->getFlags()) > 0) { + $output->error("You must provide the flags"); + $output->error("For Help, php zest ". $cmd->getSign() ." -h"); + exit; + } + $cmd->handle($output, $input); } - echo "Enter 'r' to repeat"; - } while ($x === 'r'); - } + // flag for quite + if (isset($param[2]) && strtolower($param[2]) == '-q') { + $cmd->handle($output->quiet(), $input); + } + if (isset($param[2]) && isset($param[3]) && strtolower($param[2]) == '-p') { + $params = $this->parseFlags($param[3]); + $command_flags = $cmd->getFlags(); + // get keys from $params. + $keys = array_keys($params); + + // check if the keys are in the command flags (check if extra flag passed). + foreach ($keys as $key => $value) { + if (!in_array($value, $command_flags)) { + $output->error("Invalid flag: $value"); + exit; + } + } - public function run() - { - $this->main(); - } + // check the keys should be in command flags. + foreach ($command_flags as $command_flag) { + if (!in_array($command_flag, $keys)) { + $output->error("Missing flag: $command_flag"); + exit; + } + } - public function oS() - { - return (new \Zest\Common\OperatingSystem())->get(); - } + $cmd->handle($output, $input, $params); + } - public function cliInput() - { - if ($this->oS() === 'WINNT' or $this->oS() === 'Windows') { - echo '? '; - $x = stream_get_line(STDIN, 9024, PHP_EOL); - if (!empty($x)) { - return $x; + // flag for help + if (isset($param[2]) && strtolower($param[2]) == '-h') { + $args = $cmd->getFlags(); + $output->write('Description:', true); + $output->write("\t".$cmd->getDescription().'', true); + $output->write("\nUsage:", true); + $output->write("\t".$cmd->getSign().'', true); + if (count($args) > 0) { + $output->write("\nArguments:", true); + $output->write("\t".implode("," , $args).'', true); + } + $output->write("\nOptions:", true); + $output->write('-h, --help'); + $output->write("\tDisplay this help message", true); + $output->write('--q, --quiet'); + $output->write("\tDo not output any message", true); } } else { - $x = readline('? '); - if (!empty($x)) { - return $x; - } + $output->error("Sorry, the given command ${sign} not found")->exit(); } } } diff --git a/src/Console/Input.php b/src/Console/Input.php new file mode 100644 index 00000000..7bd55ec0 --- /dev/null +++ b/src/Console/Input.php @@ -0,0 +1,83 @@ + + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console; + +class Input extends Colorize +{ + /** + * Prompt for input secret input like password. + * + * @param string $prompt Message to display. + * + * @since 3.0.0 + * + * @return bool + */ + public function secret(string $prompt) + { + $os = (new \Zest\Common\OperatingSystem())->get(); + $command = './resources/secretinput.exe'; + if ($os !== 'Window') { + $command = "/usr/bin/env bash -c 'read -s -p \"" + .addslashes($prompt) + ."\" mypassword && echo \$mypassword'"; + } + $value = rtrim(shell_exec($command), "\n"); + + return $value; + } + + /** + * Prompt for input confirm. + * + * @since 3.0.0 + * + * @return bool + */ + public function confirm(): bool + { + $confirm = $this->ask(); + $confirmed = ['y', 'yes']; + + return in_array(strtolower($confirm), $confirmed); + } + + /** + * Prompt for input input. + * + * @since 3.0.0 + * + * @return string + */ + public function ask(): string + { + $os = (new \Zest\Common\OperatingSystem())->get(); + if ($os === 'WINNT' or $os === 'Windows') { + $x = stream_get_line(STDIN, 9024, PHP_EOL); + if (!empty($x)) { + return $x; + } + } else { + $x = readline(''); + if (!empty($x)) { + return $x; + } + } + } +} diff --git a/src/Console/Input/Table.php b/src/Console/Input/Table.php new file mode 100644 index 00000000..aae8964c --- /dev/null +++ b/src/Console/Input/Table.php @@ -0,0 +1,123 @@ + + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console\Input; + +class Table +{ + /** + * Table header. + * + * @since 3.0.0 + * + * @var array + */ + private $header = []; + + /** + * Table item. + * + * @since 3.0.0 + * + * @var array + */ + private $items = []; + + /** + * Create a new Table instance. + * + * @param array $header + * @param array $items + * + * @return void + */ + public function __construct(array $header, array $items) + { + $this->header = $header; + $this->items = $items; + } + + /** + * Print table border. + * + * @since 3.0.0 + * + * @return array + */ + public function printBorder($row): self + { + foreach ($row as $key => $value) { + $size = mb_strlen($value); + for ($i = 0; $i < $size + 2; $i++) { + echo '-'; + } + echo '+'; + } + + return $this; + } + + /** + * Print the row. + * + * @since 3.0.0 + * + * @return self + */ + public function printRow($row, $head = false): self + { + if ($head) { + echo ' +'; + $this->printBorder($row); + echo "\n"; + } + echo ' | '; + foreach ($row as $key => $val) { + echo $val.' | '; + } + if ($head) { + echo "\n"; + echo ' +'; + $this->printBorder($row); + } + echo "\n"; + + return $this; + } + + /** + * Draw the table. + * + * @since 3.0.0 + * + * @return void + */ + public function draw(): void + { + // print the header at top + $this->printRow($this->header, true); + // for other rows + foreach ($this->items as $key => $val) { + $this->printRow($val, false); + } + // for footer + echo ' +'; + $this->printBorder($this->header); + echo "\n"; + } +} diff --git a/src/Console/Output.php b/src/Console/Output.php new file mode 100644 index 00000000..18a72952 --- /dev/null +++ b/src/Console/Output.php @@ -0,0 +1,217 @@ + + * + * @link https://github.com/zestframework/Zest_Framework + * + * @author Muhammad Umer Farooq + * @author-profile https://www.facebook.com/Muhammadumerfarooq01/ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license MIT + */ + +namespace Zest\Console; + +use Zest\Console\Input\Table; + +class Output extends Colorize +{ + /** + * Quiet. + * + * @since 3.0.0 + * + * @var bool + */ + private $quiet = false; + + /** + * Mark the console quiet. + * + * @since 3.0.0 + * + * @return self + */ + public function quiet(): self + { + $this->quiet = true; + + return $this; + } + + /** + * Ring the bell. + * + * @param int $times + * + * @since 3.0.0 + * + * @return self + */ + public function bell(int $times = 1): self + { + for ($i = 1; $i <= $times; $i++) { + echo "\x07"; + } + + return $this; + } + + /** + * Create the table on console. + * + * @since 3.0.0 + * + * @return self + */ + public function table($header, $items): self + { + $table = new Table($header, $items); + $table->draw(); + + return $this; + } + + /** + * Write on console. + * + * @param string $value + * @param bool $newLine + * + * @since 3.0.0 + * + * @return self + */ + public function write($value, $newLine = false): self + { + if (!$this->quiet) { + preg_match("/<\b[^>]*>/i", $value, $matches); + $color = $matches[0] ?? 'default'; + $color = str_replace('<', '', $color); + $color = str_replace('>', '', $color); + $regx = "/<$color\b[^>]*>(.*?)<\/$color>/i"; + $line = ($newLine) ? "\n" : ''; + + if (preg_match($regx, $value)) { + $text = preg_replace($regx, '\\1', $value); + echo "\033[".$this->get($color); + } + + $text = isset($text) ? $text : $value; + preg_match("/<\b[^>]*>/i", $text, $_bg); + $bg = $_bg[0] ?? 'bg:default'; + $bg = str_replace(['<', '>'], '', $bg); + $_regx = "/<$bg\b[^>]*>(.*?)<\/$bg>/i"; + if (preg_match($_regx, $text)) { + $text = preg_replace($_regx, '\\1', $text); + echo "\033[".$this->get($bg); + } + + echo ''.$text.$line; + + // reset to default + echo "\033[0m"; + echo "\033[39m"; + } + + return $this; + } + + /** + * Output the error. + * + * @param string $msg + * + * @since 3.0.0 + * + * @return self + */ + public function error($msg): self + { + $this->write("$msg", true); + + return $this; + } + + /** + * Output the danger. + * + * @param string $msg + * + * @since 3.0.0 + * + * @return self + */ + public function danger($msg): self + { + $this->write("$msg", true); + + return $this; + } + + /** + * Output the info. + * + * @param string $msg + * + * @since 3.0.0 + * + * @return self + */ + public function info($msg): self + { + $this->write("$msg", true); + + return $this; + } + + /** + * Output the warning. + * + * @param string $msg + * + * @since 3.0.0 + * + * @return self + */ + public function warning($msg): self + { + $this->write("$msg", true); + + return $this; + } + + /** + * Output the warning. + * + * @param string $msg + * + * @since 3.0.0 + * + * @return self + */ + public function success($msg): self + { + $this->write("$msg", true); + + return $this; + } + + /** + * Exit. + * + * @since 3.0.0 + * + * @return void + */ + public function exit(): void + { + exit(1); + } +} diff --git a/src/Console/Write.php b/src/Console/Write.php deleted file mode 100644 index 04df8887..00000000 --- a/src/Console/Write.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * @link https://github.com/zestframework/Zest_Framework - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * @license MIT - */ - -namespace Zest\Console; - -class Write -{ - public function controller($name, $data) - { - $file = './App/Controllers/'.$name.'.php'; - if (!file_exists($file)) { - $fh = fopen($file, 'w'); - fwrite($fh, $data); - fclose($fh); - - return true; - } else { - return false; - } - } - - public function model($name, $data) - { - $file = './App/Models/'.$name.'.php'; - if (!file_exists($file)) { - $fh = fopen($file, 'w'); - fwrite($fh, $data); - fclose($fh); - - return true; - } else { - return false; - } - } -}