PHPixie Console is a new component that allows you to create, route and run console commands. Like other framework libraries, it can easily be used without PHPixie itself as a simpler alternative to a similar library from symfony. First of all, this article is designed for those who already use PHPixie and it will contain a short description of the standard framework commands, but in the end I will also give an example of how to run PHPixie Console separately.cd your_project_directory/ ./console # php ./console Available commands: app:greet Greet the user framework:installWebAssets Symlink or copy bundle web files to the projects web folder framework:generateBundle Generate a new bundle help Print command list and usage ./console help framework:installWebAssets framework:installWebAssets [ --copy ] Symlink or copy bundle web files to the projects web folder Options: copy Whether to copy web directories instead of symlinking them namespace Project\App\Console; class Greet extends \PHPixie\Console\Command\Implementation { public function __construct($config) { // $config->description('Greet the user'); // 'message' $config->argument('message') ->description("Message to display"); parent::__construct($config); } /** * . * $argumentData $optionData * HTTP $request->query() $request->data() */ public function run($argumentData, $optionData) { $message = $argumentData->get('message', "Have fun coding!"); $this->writeLine($message); } } sqldump --user=root --skip-missing -f myDatabase users items $config->option('user') // ->required() // , 'help' ->description("User to connect to the database with"); $config->option('skip-missing') ->description("Don't throw an error if the tables are missing") // . //- , // 'true' . ->flag(); $config->option('f') ->flag() ->description("Force database dump"); $config->argument('database') ->required() ->description("Which database to dump the tables from"); $config->argument('tables') ->description("Tables to dump") // // , // ->arrayOf(); ./console help app:sqldump app:sqldump --user=VALUE [ -f ] [ --skip-missing ] DATABASE [ TABLES... ] Options: user User to connect to the database with f Force database dump skip-missing Dont throw an error if the tables are missing Arguments: DATABASE Which database to dump the tables from TABLES Tables to dump public function run($argumentData, $optionData) { $database = $argumentData->get('database'); // $user = $optionData->get('user', 'phpixie'); } public function run($argumentData, $optionData) { // $this->write("Hello "); // $this->writeLine("World"); // $str = $this->readLine(); // CommandException // - ( Bash). throw new \PHPixie\Console\Exception\CommandException("Something bad happened"); } public function run($argumentData, $optionData) { $context = $this->cliContext(); $inputStream = $cliContext->inputStream(); $outputStream = $cliContext->outputStream(); $errorStream = $cliContext->errorStream(); $outputStream->write("Hello"); $errorStream->writeLine("Something bad happened"); $context->setExitCode(1); // if ./console app:somecommand ; then echo "Command succeeded" else echo "Command failed" fi class YourCommandRegistry extends \PHPixie\Console\Registry\Provider\Implementation { public function commandNames() { return ['greet']; } public function buildGreetCommand($config) { return new Greet($config); } } $slice = new \PHPixie\Slice(); $cli = new \PHPixie\CLI(); $registry = new YourCommandRegistry(); $console = new \PHPixie\Console($slice, $cli, $registry); $console->runCommand(); Source: https://habr.com/ru/post/314314/
All Articles