Ejemplo n.º 1
0
 /**
  * It all happens here!
  *
  * @param array $argv
  */
 public function main($argv)
 {
     // step 0: create some plumbing
     $this->lastContext = $context = new Context();
     $context->argvZero = $argv[0];
     // Register error handler
     \set_error_handler(array($this, 'errorHandler'));
     // step 1: parse the command-line args
     // we do this first because it may change where we look
     // for our commands
     $context->phixDefinedSwitches = PhixSwitches::buildSwitches();
     list($phixParsedSwitches, $argsIndex) = $this->parsePhixArgs($context, $argv);
     // step 2: process the switches we have just parsed
     //
     // we parse the switches twice; once to find out where to
     // look for extensions, and then once more to decide what
     // to do once the extensions are loaded
     $errCode = $this->processPhixSwitchesBeforeCommandLoad($context, $phixParsedSwitches);
     if ($errCode !== null) {
         return $errCode;
     }
     // step 3: load the phix commands
     //
     // we do this here in case anyone has used the -I switch
     $context->commandsList = $this->loadPhixCommands($context, $phixParsedSwitches);
     // step 4: process the switches a second time
     //
     // just in case anything ever wants to happen after we have
     // loaded a list of available commands
     $errCode = $this->processPhixSwitchesAfterCommandLoad($context, $phixParsedSwitches);
     if ($errCode !== null) {
         return $errCode;
     }
     // step 5: do we have a valid command to execute?
     if (!isset($argv[$argsIndex])) {
         // no command given - special case
         $argv[$argsIndex] = 'help';
     }
     $errCode = $this->validateCommand($argv, $argsIndex, $context);
     if ($errCode !== null) {
         return $errCode;
     }
     // step 6: execute the validated command
     $errCode = $this->processCommand($argv, $argsIndex, $context);
     // all done
     return $errCode;
 }
Ejemplo n.º 2
0
 public function testCanBuildListOfSwitches()
 {
     // make the call
     $switches = PhixSwitches::buildSwitches();
     // test the results
     $builtSwitches = $switches->getSwitches();
     foreach ($builtSwitches as $switch) {
         // make sure we have a switch
         $this->assertTrue($switch instanceof DefinedSwitch);
         if ($switch instanceof DefinedSwitch) {
             // skip the remaining tests
             continue;
         }
         // make sure the switch has either a short or a
         // long option
         $shortSwitches = $switch->getShortSwitches();
         $longSwitches = $switch->getLongSwitches();
         $this->assertTrue(count($shortSwitches) + count($longSwitches) > 0);
     }
 }
Ejemplo n.º 3
0
    public function testShowsHelpOnACommand()
    {
        // setup
        $obj = new HelpCommand();
        $context = new Context();
        $context->argvZero = 'phix';
        $context->phixDefinedSwitches = PhixSwitches::buildSwitches();
        $context->commandsList->addClass('Phix_Project\\PhixCommands\\HelpCommand');
        $context->stdout = new DevString();
        $context->stderr = new DevString();
        $args = array('phix', 'help', 'help');
        $argsIndex = 2;
        // do the test
        $retVal = $obj->validateAndExecute($args, $argsIndex, $context);
        // did the general help get shown?
        $stdoutOutput = $context->stdout->_getOutput();
        $stderrOutput = $context->stderr->_getOutput();
        $this->assertEquals(0, strlen($stderrOutput));
        $this->assertNotEquals(0, strlen($stdoutOutput));
        $expectedString = <<<EOS
NAME
    phix help - get detailed help about a specific phix command

SYNOPSIS
    phix help

IMPLEMENTATION
    This command is implemented in the PHP class:

    * Phix_Project\\PhixCommands\\HelpCommand

    which is defined in the file:

    * /home/stuarth/Devel/GWC/phix/src/php/Phix_Project/PhixCommands
      /HelpCommand.php

EOS;
        $this->assertEquals($expectedString, $stdoutOutput);
    }
Ejemplo n.º 4
0
    public function testOutputsGeneralHelp()
    {
        // setup the test
        $context = new Context();
        $context->argvZero = 'phix';
        $context->phixDefinedSwitches = PhixSwitches::buildSwitches();
        $context->stdout = new DevString();
        $context->stderr = new DevString();
        $args = array();
        // perform the test
        $return = LongHelpSwitch::processBeforeCommandLoad($context, $args);
        // check the results
        $this->assertEquals(0, $return);
        $stdoutOutput = $context->stdout->_getOutput();
        $stderrOutput = $context->stderr->_getOutput();
        $this->assertNotEquals(0, strlen($stdoutOutput));
        $this->assertEquals(0, strlen($stderrOutput));
        $expectedString = <<<EOS
phix @@PACKAGE_VERSION@@ http://gradwell.github.com
Copyright (c) 2010 Gradwell dot com Ltd. Released under the BSD license

SYNOPSIS
    phix [ -? -d -h -v ] [ --? --debug --help --version ] [ -I <path> ] [
    --include=<path> ] [ command ] [ command-options ]

OPTIONS
    Use the following switches in front of any <command> to have the following
    effects.

    -? | -h
        display a summary of the command-line structure

    -I <path> | --include=<path>
        add a folder to load commands from

        phix finds all of its commands by searching PHP's include_path for PHP
        files in folders called 'PhixCommands'. If you want to phix to look in
        other folders without having to add them to PHP's include_path, use
        --include to tell phix to look in these folders.

        phix expects '<path>' to point to a folder that conforms to the PSR0
        standard for autoloaders.

        For example, if your command is the class '\\Me\\Tools\\PhixCommands
        \\ScheduledTask', phix would expect to autoload this class from the 'Me
        /Tools/PhixCommands/ScheduledTask.php' file.

        If your class lives in the './myApp/lib/Me/Tools/PhixCommands' folder,
        you would call phix with 'phix --include=./myApp/lib'

    -d | --debug
        enable debugging output

    -v | --version
        display phix version number

    --? | --help
        display a full list of supported commands

COMMANDS

    See phix help <command> for detailed help on <command>

EOS;
        $this->assertEquals($expectedString, $stdoutOutput);
    }