Command Line Tool Support is designed to enable usage of command line tools of various frameworks inside the IDE.
From this tutorial you will learn:
To complete this tutorial you need PHPStorm 2.0 or higher installed on your computer.
First of all, let's consider some examples of running console commands. Console commands can be executed, with the working folder in the root of your project. Invoke the tool with Ctrl+Shift+X and try:
The command output is shown in the dedicated tab:
Macros are also supported, so you can use $ProjectFileDir$ instead of the full path to the project, or $PhpExecutable$ instead of the php configured in the project settings, and so on. Another useful macro $Prompt$ which displays a string input dialog. Example of macro usage:
Macro is replaced with actual value:
Now it’s time to let’s add support of a framework to get use of its commands. Let’s, for example, load Symfony.
First of all, we need to create a Symfony project, which is a mandatory prerequisite for executing Symfony commands.
Press Ctrl +Shift+X and type:
The command has two parameters:
Before the command execution, the $ProjectName$ macro will be exploded to the actual project name, and the framework alias s will be changed to Invoke Path. In our case, it is ""C:\php\php.exe" C:\sf\data\bin\symfony”.
Now that we have a Symfony project, let’s see how we can use Symfony commands.
To start, let’s create a frontend application without escaping output.
Press Ctrl +Shift+X and type:
Note that options after commands are also supported. In our case the --escaping-strategy=false option defines that output is not escaped.
The command execution result is displayed in the Output console.
The Output console is interactive. To see how to derive advantage from this possibility, let’s run the command that removes the data available through a configured data source.
Press Ctrl+Shift+X and type:
The Output console informs about the possible consequences of executing the command and asks you to confirm or reject your decision by pressing y or N:
As you can see, command loaders are provided only for Symfony and Zend Framework Tool. You might ask, “What about other frameworks? “. Other frameworks and tools are supported via custom framework descriptions. You can create and load an xml file where the required commands will be stored, just like Zend Framework Tool and Symfony commands. Let’s create a definition for the Pear tool.
The file contains only a framework element with the following contents:
The attributes invoke, alias and enabled correspond to the values specified in the Command Line Tool Support dialog box. They are mandatory and should not be empty.
The name and version attributes should never be changed manually.
Now let’s add a command definition to the description.
Any command is described in the command child of the framework element. The command element itself has the following children:
Let’s define the config-get command.
The text of the command is:
<help>pear config-get [options] parameter [layer]<br/>Displays the value of one configuration parameter. The<br/>first argument is the name of the parameter, an optional second argument<br/>may be used to tell which configuration layer to look in. Valid configuration<br/>layers are "user", "system" and "default". If no layer is specified, a value<br/>will be picked from the first layer that defines the parameter, in the order<br/>just specified. The configuration value will be retrieved for the channel<br/>specified by the default_channel configuration variable.<br/><br/>Options:<br/> -c CHAN, --channel=CHAN<br/> show configuration variables for another channel<br/></help>
In this example, the command element has the following children:
Commands without parameters may omit the params element. The default value of a parameter may use IDE macro. Then all omitted parameters before it will be provided by their default values, and the macro will be exploded before execution. These are the only circumstances when provided default values are used. That`s why the Symfony description is safe to set the default values of all optional parameters to null.
Let’s invoke the new command to make sure its definition works:
Then let’s get the help on the command with Ctrl+Q:
Note that files with commands are saved and reloaded each time you invoke auto completion in the Command Line Tool Support popup.
The only thing left is copy other commands and framework help from the attached file. Now Pear support is ready to use.
Some frameworks, for example, Symfony allow creating custom tasks. As a result, you need to change the framework description accordingly.
Let’s create a simple hello task inside a Symfony project. We suppose that you have previously loaded and enabled Symfony and created a Symfony project.
A new task class helloTask is created inside the lib/task directory.
Command description :
<help>My first task with no parameters.</help>
The result of executing the command is shown in the Output console:
If you get any troubles while editing description file and auto completion does not offer you expected commands, open the Validation tab of Command Line Tools Console. This tab shows info about problems with parsing description files. You can also get information about the description format from schema file at .idea/commandlinetools/schemas/frameworkDescriptionVersion1.1.xsd. Its path is provided in the generated description file, and the editor validates the file against the schema automatically.