2017 © Pedro Peláez
 

library console

Console command library for PHPixie

image

phpixie/console

Console command library for PHPixie

  • Friday, February 16, 2018
  • by dracony
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6,706 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 13 Versions
  • 9 % Grown

The README.md

Console

PHPixie console command component, (*1)

Author Source Code Software License, (*2)

PHPixie Console allows you to define and run commands in the command line. The main benefit as opposed to just keeping a folder with PHP scripts are the ability to define options and arguments, validate them, generate command usage etc., (*3)

Migrating an Existing PHPixie Project

If you create a fresh PHPixie project you are already ready to go, otherwise you need to do a few small modifications to the project, by updating some files from the new project skeleton:, (*4)

  1. https://github.com/PHPixie/Project/blob/master/console
  2. https://github.com/PHPixie/Project/blob/master/src/Project/Framework/Bundles.php
  3. Add "phpixie/framework-bundle": "~3.0" to composer.json

Optionally also copy the Console factory and the example Greet Command:, (*5)

  1. https://github.com/PHPixie/Project/blob/master/bundles/app/src/Project/App/Console.php
  2. https://github.com/PHPixie/Project/blob/master/bundles/app/src/Project/App/Console/Greet.php
  3. Register the Console class in your Builder, like here: https://github.com/PHPixie/Project/blob/master/bundles/app/src/Project/App/Builder.php

Running commands

Now try running the console script:, (*6)

cd your_project_directory/
./console

This will give you the list of available commands and their descriptions, e.g.:, (*7)

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

You can get extended information about a command by using the help command:, (*8)

./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

Default commands

framework:installWebAssets Its purpose is creating symlinks from bundle directories to the /web/bundles folder, e.g. /web/bundles/app -> /bundles/app/web. The idea behind it is that we can have bundles that are installable and updatable via composer that provide their own web assets. The --copy flag will copy the directories instead of symlinking them. This is usefull if you want to deploy the files to some CDN network afterwards., (*9)

framework:generateBundle This command generates and registers a new bundle within your project., (*10)

Adding your own commands

There is a sample app:greet command provided in the skeleton project. They are added in the same was as HTTP Processors, using the \Project\App\Console class. To add a new command you have to add it's name to the array returned by the commandNames method, and create a build<command_name>Command method., (*11)

You can configure your command to add a description and define options and arguments. Let's look at the default Greet command:, (*12)

namespace Project\App\Console;

class Greet extends \PHPixie\Console\Command\Implementation
{
    public function __construct($config)
    {
        // Specify command description
        $config->description('Greet the user');

        //Define a 'message' argument
        $config->argument('message')
            ->description("Message to display");

        parent::__construct($config);
    }

    /**
     * Gets called when the command is executed.
     * $argumentData and $optionData work in the same
     * way as HTTP $request->query() and $request->data()
     */
    public function run($argumentData, $optionData)
    {
        $message = $argumentData->get('message', "Have fun coding!");
        $this->writeLine($message);
    }
}

Arguments and options

Let's say we want to define a command that dumps some tables from the database, a typical call might look like this:, (*13)

sqldump --user=root --skip-missing -f myDatabase users items

Here myDatabase is the name of the database, followed by the names of the tables we want to dump. These are the arguments of our command. The user, skip-missing and f are options. Note that for arguments the order in which they are specified matters, but for the options it does not, also short one letter options can be referenced with a single - instead of two., (*14)

Let's look at defining options:, (*15)

$config->option('user')

    //Mark option as required
    ->required()

    //Describe what the option does.
    //this is displayed by the 'help' command
    ->description("User to connect to the database with");

$config->option('skip-missing')
    ->description("Don't throw an error if the tables are missing")

    //mark option as flag,
    //flag options don't accept a value,
    //but are set to 'true' if they are present.
    ->flag();

$config->option('f')
    ->flag()
    ->description("Force database dump");

When defining arguments you have to keep in mind that they should be defined in the same order in which they should be specified. In out case this means we have to define a database argument before the tables one:, (*16)

$config->argument('database')
    ->required()
    ->description("Which database to dump the tables from");

$config->argument('tables')
    ->description("Tables to dump")

    // Can accept more than one value.
    // There can be only one argument marked with `arrayOf`
    // and it has to be the last one.
    ->arrayOf();

If we were to run the help command now, we would see the following:, (*17)

./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    Don't throw an error if the tables are missing

Arguments:
DATABASE  Which database to dump the tables from
TABLES    Tables to dump

When the command is executed the run method of the command receives the passed options and arguments, which can be accessed in the same way as when working with HTTP requests:, (*18)

public function run($argumentData, $optionData)
{
    $database = $argumentData->get('database');

    // specifying default value
    $user = $optionData->get('user', 'phpixie');
}

Input and Output

The easiest way to return output from the command is by returning a string. But some commands take a while to process and you may want to provide users with intermediate status. There are some additional methods you can use:, (*19)

public function run($argumentData, $optionData)
{
    // Write text without line break
    $this->write("Hello ");

    // Write text with new line
    $this->writeLine("World");

    // Read a line of user input
    $str = $this->readLine();

    // Throwing a CommandException will output the error message
    // and ensure that the command exits with a non-zero exit code
    throw new \PHPixie\Console\Exception\CommandException("Something bad happened");
}

To further control input and out you can use the CLI context, in fact the above methods are just shortcuts to CLI context calls:, (*20)

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); // set the exit code
}

The exit code matters for if you want to check externally if the command was successful or not, e.g. in Bash if you do something like:, (*21)

if ./console app:somecommand ; then
    echo "Command succeeded"
else
    echo "Command failed"
fi

The Versions

16/02 2018

dev-master

9999999-dev http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD BSD-3-Clause

The Requires

 

The Development Requires

console cli

16/02 2018

3.4.3

3.4.3.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

console cli

16/01 2017

3.4.2

3.4.2.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli

04/12 2016

3.4.1

3.4.1.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli

14/11 2016

3.4

3.4.0.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli

11/11 2016

3.3.4

3.3.4.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli

09/09 2016

3.3.3

3.3.3.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli

06/09 2016

3.3.2

3.3.2.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli

04/09 2016

3.3.1

3.3.1.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli

04/09 2016

3.3

3.3.0.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli

02/09 2016

3.2

3.2.0.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli

01/09 2016

3.1

3.1.0.0 http://phpixie.com

Console command library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli

13/06 2016

3.0

3.0.0.0 http://phpixie.com

ORM library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

console cli command line