Skip to content

Instantly share code, notes, and snippets.

@gotohr
Created March 13, 2012 09:33
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 gotohr/2027834 to your computer and use it in GitHub Desktop.
Save gotohr/2027834 to your computer and use it in GitHub Desktop.
symfony2 console application demo (with class loader, using Console component independently)
<?php
require_once 'Symfony/Component/ClassLoader/UniversalClassLoader.php';
$loader = new Symfony\Component\ClassLoader\UniversalClassLoader();
$loader->registerNamespaces(array(
'Symfony' => __DIR__.'/../_libs'
));
$loader->register();
use Symfony\Component\Console as Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class GreetCommand extends Command
{
protected function configure()
{
$this
->setName('demo:greet')
->setDescription('Greet someone')
->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?')
->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
if ($name) {
$text = 'Hello '.$name;
} else {
$text = 'Hello';
}
if ($input->getOption('yell')) {
$text = strtoupper($text);
}
$output->writeln($text);
}
}
$application = new Console\Application('Demo', '1.0.0');
$application->add(new GreetCommand('greet-cmd'));
$application->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment