atelierspierrot/library

View on GitHub
demo/console

Summary

Maintainability
Test Coverage
#!/usr/bin/env php
<?php
/**
 * Show errors at least initially
 *
 * `E_ALL` => for hard dev
 * `E_ALL & ~E_STRICT` => for hard dev in PHP5.4 avoiding strict warnings
 * `E_ALL & ~E_NOTICE & ~E_STRICT` => classic setting
 */
//@ini_set('display_errors','1'); @error_reporting(E_ALL);
//@ini_set('display_errors','1'); @error_reporting(E_ALL & ~E_STRICT);
@ini_set('display_errors','1'); @error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);

/**
 * Set a default timezone to avoid PHP5 warnings
 */
$dtmz = @date_default_timezone_get();
date_default_timezone_set($dtmz?:'Europe/Paris');

/**
 * For security, transform a realpath as '/[***]/package_root/...'
 *
 * @param string $path
 * @param int $depth_from_root
 *
 * @return string
 */
function _getSecuredRealPath($path, $depth_from_root = 1)
{
    $ds = DIRECTORY_SEPARATOR;
    $parts = explode($ds, realpath('.'));
    for ($i=0; $i<=$depth_from_root; $i++) array_pop($parts);
    return str_replace(join($ds, $parts), $ds.'[***]', $path);
}

require_once __DIR__."/../src/SplClassLoader.php";
$classLoader = new SplClassLoader("Library", __DIR__."/../src");
$classLoader->register();

class ConsoleController extends \Library\CommandLine\AbstractCommandLineController
{

    public static $_name = 'Command line demonstration';
    public static $_version = '1-dev';

    public function __construct()
    {
        parent::__construct(array(
            'argv_options'=>array(
                't::'=>'test',
                'o:'=>'othertest'
            ),
            'argv_long_options'=>array(
                'test::'=>'test',
                'other:'=>'othertest'
            ),
            'commands'=>array(
                'env::'=>'environment',
            ),
            'aliases'=>array(
            ),
        ));
    }

    /**
     * Run a test command
     */
    public function runTestCommand($arg = null)
    {
        // do stuff here ...
        $name = !empty($arg) ? ucfirst($arg) : 'World';

        // then write and out ...
        $this
            ->writeInfo('this is the result of the "runTestCommand" method with an optional parameter:', true)
            ->write('Hello '.$name)
            ->writeStop();
    }

    /**
     * Run another test command
     */
    public function runOthertestCommand($arg)
    {
        // do stuff here ...
        $name = ucfirst($arg);

        // then write and out ...
        $this
            ->writeInfo('this is the result of the "runTestCommand" method with a required parameter:', true)
            ->write('Hello '.$name)
            ->writeStop();
    }

    /**
     * Get an information about current environment
     *
     * Get an information about current environment ; optional arguments are :
     *     - '<option>php</option>' : get PHP version (default option),
     *     - '<option>apache</option>' : get Apache version,
     *     - '<option>apache-modules</option>' : get Apache modules list,
     *     - '<option>gd</option>' : get GD library version,
     *     - '<option>all</option>' : get all above information.
     */
    public function runEnvironmentCommand($which = null)
    {
        switch($which) {
            case 'apache':
                if (function_exists('apache_get_version'))
                    self::write( 'Apache version: '.apache_get_version() );
                else
                    self::writeInfo( 'Apache version not available !' );
                break;
            case 'apache-modules':
                if (function_exists('apache_get_modules'))
                    self::write( 'Apache modules: '.var_export(apache_get_modules(),1) );
                else
                    self::writeInfo( 'Apache modules not available !' );
                break;
            case 'gd':
                if (function_exists('gd_info'))
                    self::write( 'GD library information: '.var_export(gd_info(),1) );
                else
                    self::writeInfo( 'GD library not available !' );
                break;
            case 'php': default:
                self::write( 'PHP version: '.phpversion() );
                break;
        }
    }

}

$test = new ConsoleController;
$test->distribute();

exit(PHP_EOL);