Skip to content

Instantly share code, notes, and snippets.

@kjdev
Created September 11, 2014 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjdev/956c7837d7fd50c45f12 to your computer and use it in GitHub Desktop.
Save kjdev/956c7837d7fd50c45f12 to your computer and use it in GitHub Desktop.
<?php
// Using the CLI factory default services container
$di = new \Phalcon\DI\FactoryDefault\Cli;
// Register the autoloader and tell it to register the tasks directory
$loader = new \Phalcon\Loader;
$loader->registerDirs(array(dirname(__FILE__) . '/tasks'))->register();
// Create a console application
$console = new \Phalcon\Cli\Console;
$console->setDI($di);
// Create a console application routing
$di->setShared('router', function() {
$router = new \Phalcon\Cli\Router(true);
$router->add('world', array(
'task' => 'main',
'action' => 'hello',
'params' => array('WORLD', '###')
));
return $router;
});
try {
$console->setArgument($argv)->handle();
} catch (\Phalcon\Exception $e) {
echo $e->getMessage(), PHP_EOL;
exit(255);
}
<?php
class echoTask extends \Phalcon\CLI\Task
{
public function mainAction()
{
echo "echoMainAction\n";
}
}
$ php cli.php
mainAction
$ php cli.php main
mainAction
$ php cli.php main hello
Hello !
$ php cli.php main hello world @
Hello world@
$ php cli.php echo
echoMainAction
$ php cli.php world
Hello WORLD###
$ php cli.php --strtoupper main hello world
HELLO WORLD!
$ php cli.php -u main hello world
HELLO WORLD!
$ php cli.php main hello world --strtolower
hello world!
$ php cli.php main -l hello world
hello world!
$ php cli.php main hello world --string=hoge
Hello world!hoge
<?php
class mainTask extends \Phalcon\CLI\Task
{
public function mainAction()
{
echo "mainAction\n";
}
public function helloAction($world = "", $symbol = "!")
{
$retval = "Hello " . $world . $symbol;
$options = $this->dispatcher->getOptions();
if (isset($options['strtoupper']) || isset($options['u'])) {
$retval = strtoupper($retval);
}
if (isset($options['strtolower']) || isset($options['l'])) {
$retval = strtolower($retval);
}
if (isset($options['string'])) {
$retval .= $options['string'];
}
echo $retval, PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment