Location: PHPKode > scripts > SnPayPalApi > snpaypalapi/lib/SnCliPayPal.class.php
<?php
 /**
 * 
 * 
 * @author Michael J. Burgess
 * @package SnPayPal
 * @subpackage SnCliPayPal
 * @copyright Michael J. Burgess, 2008
 * @version 1.0
 * @licence MIT
 */
 
 /**
 * 
 *
 */
class SnCliPayPal
{
    private $commands;
    private $commandObject;
    private $cliArgument;
    private $outputMessages;
    private $sendOutput;
    private $requireReturn;
    
    /**
     * Constructor
     *
     * @param SnPayPalMethod $objects
     */
    public function __construct($object = null)
    {
        $this->requireReturn = true;
        $this->sendOutput    = true;
        $this->commands      = array();        
        $this->iniCliArgs();
        $this->iniOutputMessages();
        $this->loadCurl();
        $this->registerObject($object);
    }
    
    /**
     * A functional approach to registering commands
     * Register a funtion rather than a class,
     * use repeatadly to make available many  commands
     *
     * @param string $cliArgument argument which calls the function (e.g. h)
     * @param string $command function name
     */
    public function registerCommand($cliArgument, $command)
    {
        $this->commands[$cliArgument] = $command;
    }
    
    /**
     * Register and obejct and initialse the cli
     *
     * @param SnPayPalMethod $object
     */
    public function registerObject($object = null)
    {
        if(isset($object))
        {
            $this->commands      = $object->getCliCommands();
            $this->commandObject = $object;
            $this->handleCli();
        }
    }
    
    /**
     * Request a registered command by the argument supplied at the command line
     *
     * @param string $cliArgument
     */
    public function requestCmd($cliArgument)
    {
        $this->cliArgument = $cliArgument;
        $this->handleCli();
    }
    
    /**
     * Write to the console, 
     * use $inline = true for 'Loading...' messages (if autoOutput is still active)
     * 
     * Will write the plain text supplied if ->disableAutoOutput is called 
     * from an object extending from SnPayPalCLiMethod
     * 
     * @param string $string message
     * @param boolean $inline
     */
    public function write($string, $inline = false)
    {
        if($this->sendOutput()) $string = $inline ? "\n\t\t $string" : "\t   >>  $string";
        
        fwrite(STDOUT, "\n$string");
        
        if($inline)
        {
            fwrite(STDOUT, '.');
            usleep(700000);
            fwrite(STDOUT, '.');
            usleep(700000);
            fwrite(STDOUT, ".\n");
        }
        
    }
    
    public function read()
    {
        return fgets(STDIN, 250);
    }
    
    /**
     * Write a predefined message (see iniOutputMessages)
     *
     * @param string $key
     * @return boolean
     */
    private function writeMessage($key)
    {
        if($this->sendOutput()) return fwrite(STDOUT, $this->outputMessages[$key]);
    }
    
    /**
     * Check if enter should be pressed to exit
     *
     * @return boolean
     */
    private function requireReturn()
    {
        $require = isset($this->commandObject) ? isset($this->commandObject->SnCliRequireReturn) : false;
        return $require ? $this->commandObject->SnCliRequireReturn : $this->requireReturn; 
    }
    
    /**
     * Check if the class should send it's own output
     *
     * @return boolean
     */
    private function sendOutput()
    {
        $require = isset($this->commandObject) ? isset($this->commandObject->SnCliSendOutput) : false;
        return $require ? $this->commandObject->SnCliSendOutput : $this->sendOutput; 
    }
    
    /**
     * Run the commands requested
     *
     */
    private function handleCli()
    {
        $this->writeMessage('initial');
                
        if(isset($this->commands[$this->cliArgument]))
        {
            $arguments = $this->cleanArguments();
            $function  = $this->commands[$this->cliArgument];
           
            try 
            {
                $return = (isset($this->commandObject)) ? $this->commandObject->$function($this, new SnPayPalApi(), $arguments) : 
                                                                                $function($this, new SnPayPalApi(), $arguments);
            }
        
            catch (Exception $e)
            {
                $this->writeMessage($e->getMessage());
                $return = false;
            }
        
        }
        
        else 
        {
            $this->writeMessage('nocmd');
            $return = false;
        }
        
        $this->writeMessage($return ? 'success' : 'failure');
        
        if($this->requireReturn())
        {
            $this->writeMessage('end');
            $this->read();
        }
        
        else $this->writeMessage('quit');
    }
    
    /**
     * Parse commandline arguments passed
     *
     * @return stdClass
     */
    private function cleanArguments()
    {        
        $return  = array();
        foreach ($this->argv as $argument)
        {
            $cmdArg = explode('=', $argument);
            $return[$cmdArg[0]] = strtolower($cmdArg[1]);
        }
        
        return (object) $return;
    }
    
    private function loadCurl()
    {
        if(!function_exists('curl_init')) 
        {
            dl('php_curl.dll');
        }
    }
    
    /**
     * Setup cli arguments for parsing
     *
     */
    private function iniCliArgs()
    {
                             array_shift($_SERVER['argv']);
        $this->cliArgument = array_shift($_SERVER['argv']);
        $this->argv        = $_SERVER['argv'];
    }
    
    /**
     * Add some default messages to ->outputMessages
     *
     */
    private function iniOutputMessages()
    {
        $this->outputMessages = array();
        
        $this->outputMessages['initial']  =   "\n\t+---------------------------------------------------------------+";
        $this->outputMessages['initial'] .=   "\n\t|        >> SnPayPalCli - Written by Michael J. Burgess <<      |";
        $this->outputMessages['initial'] .=   "\n\t+---------------------------------------------------------------+\n";
        
        $this->outputMessages['success'] .= "\n\n\t+---------------------------------------------------------------+";
        $this->outputMessages['success'] .=   "\n\t|                   >> Command successful! <<                   |";
        $this->outputMessages['success'] .=   "\n\t+---------------------------------------------------------------+";
        
        $this->outputMessages['failure'] .= "\n\n\t+---------------------------------------------------------------+";
        $this->outputMessages['failure'] .=   "\n\t|                    >> Command Failed! <<                      |";
        $this->outputMessages['failure'] .=   "\n\t+---------------------------------------------------------------+";
        
        $this->outputMessages['nocmd'] = "\n\n\t\t ! The command you requested does not exist !\n";
        $this->outputMessages['end']   = "\n\n\nPress Return (Enter) to end...";
        $this->outputMessages['quit']  = "\n\n\nExiting...";
    }
}

?>
Return current item: SnPayPalApi