9. Symfony Console
namespace
AcmeDemoBundleCommand;
use
SymfonyComponentConsoleCommandCommand;
use
SymfonyComponentConsoleInputInputArgument;
use
SymfonyComponentConsoleInputInputInterface;
use
SymfonyComponentConsoleInputInputOp^on;
use
SymfonyComponentConsoleOutputOutputInterface;
class
GreetCommand
extends
Command
{
protected
func^on
configure()
{
$this
-‐>setName('demo:greet')
-‐>setDescrip^on('Greet
someone')
-‐>addArgument(
'name',
InputArgument::OPTIONAL,
'Who
do
you
want
to
greet?'
)
-‐>addOp^on(
'yell',
null,
InputOp^on::VALUE_NONE,
'If
set,
the
task
will
yell
in
uppercase
lelers'
);
}
…
12. Silex
//Bootstrap our Silex application
$app = require __DIR__ . '/src/bootstrap.php';
//Include the namespaces of the components we plan to use
use SymfonyComponentConsoleApplication;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleOutputOutputInterface;
//Instantiate our Console application
$console = new Application('ProjectName', '0.1');
…
13. …
Silex
//Register a command to run from the command line
//Our command will be started with "./console.php sync"
$console->register( 'sync' )
->setDefinition( array(
//Create a "--test" optional parameter
new InputOption('test', '', InputOption::VALUE_NONE, 'Test mode'),
) )
->setDescription('Synchronize with an external data source')
->setHelp('Usage: <info>./console.php sync [--test]</info>')
->setCode(
function(InputInterface $input, OutputInterface $output) use ($app)
{
if ($input->getOption('test'))
{
$output->write("ntTest Mode Enablednn");
}
}
);
$output->write( "Contacting external data source ...n");
//Do work here
//Example:
// $app[ 'myExtension' ]->doStuff();
$console->run();
14. Silex
$> ./console.php
ProjectName version ProjectVersion
Usage:
[options] command [arguments]
Options:
--help
--quiet
--verbose
--version
--ansi
--no-ansi
--no-interaction
-h
-q
-v
-V
Display this help message.
Do not output any message.
Increase verbosity of messages.
Display this program version.
Force ANSI output.
Disable ANSI output.
-n Do not ask any interactive question.
Available commands:
help
Displays help for a command
list
Lists commands
sync
Synchronize with an external data source
15.
16. Zend Console
//
modules/applica.on/config/module.config.php
array(
'router' => array(
'routes' => array(
// HTTP routes are defined here
)
),
'console' => array(
'router' => array(
'routes' => array(
// Console routes go here
)
)
),
)
20. Console Command Line
…
// add an option to delete original files after zipping
$parser->addOption(
'delete',
array(
'short_name' => '-d',
'long_name' => '--delete',
'action' => 'StoreTrue',
'description' => 'delete original files after zip operation'
)
);
// run the parser
try {
$result = $parser->parse();
// write your program here...
print_r($result->options);
print_r($result->args);
} catch (Exception $exc) {
$parser->displayError($exc->getMessage());
}