Skip to content

Instantly share code, notes, and snippets.

@kjdev kjdev/EchoTask.php
Created Sep 11, 2014

Embed
What would you like to do?
<?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
You can’t perform that action at this time.