Created
March 13, 2012 09:33
-
-
Save gotohr/2027834 to your computer and use it in GitHub Desktop.
symfony2 console application demo (with class loader, using Console component independently)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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