Download the PHP package phpixie/console without Composer
On this page you can find all versions of the php package phpixie/console. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phpixie/console
More information about phpixie/console
Files in phpixie/console
Package console
Short Description Console command library for PHPixie
License BSD-3-Clause
Homepage http://phpixie.com
Informations about the package console
Console
PHPixie console command component
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.
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:
- https://github.com/PHPixie/Project/blob/master/console
- https://github.com/PHPixie/Project/blob/master/src/Project/Framework/Bundles.php
- Add
"phpixie/framework-bundle": "~3.0"
tocomposer.json
Optionally also copy the Console factory and the example Greet Command:
- https://github.com/PHPixie/Project/blob/master/bundles/app/src/Project/App/Console.php
- https://github.com/PHPixie/Project/blob/master/bundles/app/src/Project/App/Console/Greet.php
- 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:
This will give you the list of available commands and their descriptions, e.g.:
You can get extended information about a command by using the help
command:
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.
framework:generateBundle This command generates and registers a new bundle within your project.
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.
You can configure your command to add a description and define options and arguments. Let's look at the default Greet
command:
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:
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.
Let's look at defining options:
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:
If we were to run the help
command now, we would see the following:
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:
Input and Output
The easiest way to return output from the command is by return
ing 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:
To further control input and out you can use the CLI context, in fact the above methods are just shortcuts to CLI context calls:
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: