Yii 1.1: Adding Bash Command Completion for Yii Commands

5 followers

Introduction

This article discusses the installation/configuration of the YiiBash project (https://github.com/dsoprea/YiiBash).

Those of us whom write a lot of Yii-based console functionality quickly realize the need for command-completion with Yii.. With the couple of quick-taps of the TAB, finishing the name of a command or command-action, or parameter to either.

To do this, we will insert a script-stub into Bash's library of existing command-completion stubs, which will then call a Yii command that we'll create, to determine what options to show. Most of the functionality has been written for you, but you'll have to set stuff like the path of your Yii project.

Since this functionality needs to use PHP via CGI, not the normal PHP executable, you'll have to use your package manager to install it. In Ubuntu, it's the "php5-cgi" package, or whatever the appropriate name would be at the time that you are reading this article. Then, we'll copy the normal "yiic" console bootstrap code to call the CGI version of the PHP executable. You'll obviously also need Bash completion installed (the "bash-completion" package, under Ubuntu).

Steps

  1. Get a copy of the YiiBash project at https://github.com/dsoprea/YiiBash.
  2. Put the "BashCompleteCommand.php" file in your project's protected/commands/ directory.
  3. Put the "yiic-cgi" file in your project's protected/ directory.

    This file invokes the php5-cgi executable at the top, and does a couple of things required for normal Yii operation that aren't available by default with the CGI. Most notably, it establishes a couple of SERVER values, including the arguments to the script. Without knowing if there might be other things missing that peoples' projects depend on, just report any bugs in GitHub and I'll try to take care of it.

  4. Put the "yiic" file in /etc/bash_completion.d .

    This file defines and loads the function that is called when you summon command-completion for your Yii bootstrap script, what determines the context for the value to be completed, and what then invokes the PHP code to get the options. Make sure you update the path of your project at the top.

    If your Yii console bootstrap command is named something other then "yiic", set something other than the default command-name of "yiic" sent to the "complete" command at the bottom of the stub, and change the name of the function to match (just prefix it with an underscore to keep the convention). If you have more than one project that you wish to set-up completion for, you can make copies of this function and the "complete" call for each project, and keep them all in this same file.

After you're done, run "source /etc/bash_completion.d/" to activate your changes.

Dustin Oprea

Be the first person to leave a comment

Please to leave your comment.