Develop Simply

Ivan K's development musings

Command Line Interfaces Rule

All the proper web framework those days have some sort of Command line tools - the first that I’ve personally encountered where rails and symfony and I haven’t been able to live without them ever since, but Kohana 3 seems to be awfully backwards in that respect - no CLI in 2.3 and now no CLI in Kohana 3. They did structure it very well to create those tools - its just the tools themselves seem to be missing. Anyway here’s my take on this.

Kohana-cli

1
git clone git://github.com/OpenBuildings/kohana-cli.git modules/kohana-cli

Or

1
2
git submodule git://github.com/OpenBuildings/kohana-cli.git modules/kohana-cli
git submodule update --init

To use it properly after you’ve downloaded the module you have to symlink or copy the kohana file from the root module directory to your root directory.

1
2
3
4
cp modules/kohana-cli/kohana ./

php kohana help
./kohana help

Or if you want system-wide support

1
2
3
cp modules/kohana-cli/kohana /usr/local/bin/

kohana help

Alright - so now you’re set to explore what commands you have

1
kohana list

This will go through all your modules and search for specially designated “command” folders which contain cli scripts. Now several of my own modules have predefined command line utilities but kohana-cli comes with some useful ones itself.

1
2
kohana cache:clear
kohana module:generate

You can get help for the commands with the usual syntax

1
2
kohana help cache:clear
kohana help module:generate

And it’s ridiculously easy to write your own CLI commands with color output and all that check out cache:clear’s source:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
class Command_Cache extends Command
{
  const CLEAR_BRIEF = "Clear system cache and Cache";
  public function clear()
  {
      //Call function and profile its start and end, last argument is the color of the output.
      self::log_func(array(Cache::instance(), 'delete_all'), null, Command::OK);
      self::log_func("system", array("rm -rf ".Kohana::$cache_dir."/*"), Command::OK);
  }
}
?>