Tutorial: integrating the Yii command line tool with PhpStorm
In PhpStorm, you can run commands of several third-party command-line tools, as well as define your own ones. As an example, let's integrate the Yii command line tool with PhpStorm.
-
Create a new Composer project and choose to install the yiisoft/yii2-app-basic package.
-
In the Settings/Preferences dialog (Ctrl+Alt+S), go to .
-
Click on the toolbar. In the Command Line Tools dialog, select Custom tool from the Choose tool list and set its visibility level to project.
-
Click OK. In the Tool Settings dialog, provide the tool's main parameters. In our case, the tool is located under the project root, and we use the
yii
alias for it. -
Apply the changes and close the Settings/Preferences dialog. The tool xml definition will open in the editor.
-
By now, the tool is already operational: from the main menu, choose Input pane, type
or press Ctrl+Shift+X. In theyii
and press Enter. -
To use code completion and quick documentation for yii commands, we need to define them first in the xml tool descriptor.
A command's definition is organized as follows:
<command> < !--the command's container--> <name> !--the command itself, mandatory, and non-empty--> </name> <help> !--the command's help message, optional--> </help> <params> !--the command's parameters and their default values--> </params> <optionsBefore> < !--the command's options container--> <option name="" shortcut=""> !--the option itself, mandatory, and non-empty; you can also provide a shorthand abbreviation and the usage pattern via attributes--> <help> !--the command's help message, optional--> </help> </option> </optionsBefore> </command>Let's add a definition for a simple
hello
command, which echoes the entered argument. The command is executed via the following syntax:yii hello [message] [...options...]The resulting definition for this command should look as follows:
<command> <name>hello</name> <help>Echoes the entered argument</help> <params>message</params> <optionsBefore> <option name="--appconfig"> <help> Custom application configuration file path. If not set, default application configuration is used. </help> </option> <option name="--color" pattern="equals"> <help> Boolean, 0 or 1. Enables or disables ANSI color in the output. If not set, ANSI color will only be enabled for terminals that support it. </help> </option> <option name="--help" shortcut="-h" pattern="equals"> <help> Boolean, 0 or 1. Defines whether to display help information about current command. </help> </option> <option name="--interactive" pattern="equals"> <help> Boolean, 0 or 1. Defines whether to run the command interactively. </help> </option> </optionsBefore> </command> -
Our basic tool definition is ready. We can now press Ctrl+Shift+X and run it, while PhpStorm will provide us with code completion and documentation.
You can find the full yii tool definition file in the yii2-phpstorm-commandlinetool GitHub repository. Have a look at other notable examples, too!