本文整理汇总了PHP中Symfony\Component\Console\Command\Command::setCode方法的典型用法代码示例。如果您正苦于以下问题:PHP Command::setCode方法的具体用法?PHP Command::setCode怎么用?PHP Command::setCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Console\Command\Command
的用法示例。
在下文中一共展示了Command::setCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testInteractiveOutputs
/**
* @dataProvider inputInteractiveCommandToOutputFilesProvider
*/
public function testInteractiveOutputs($inputCommandFilepath, $outputFilepath)
{
$code = (require $inputCommandFilepath);
$this->command->setCode($code);
$this->tester->execute(array(), array('interactive' => true, 'decorated' => false));
$this->assertStringEqualsFile($outputFilepath, $this->tester->getDisplay(true));
}
示例2: testLongWordsBlockWrapping
public function testLongWordsBlockWrapping()
{
$word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygovgollhjvhvljfezefeqifzeiqgiqzhrsdgihqzridghqridghqirshdghdghieridgheirhsdgehrsdvhqrsidhqshdgihrsidvqhneriqsdvjzergetsrfhgrstsfhsetsfhesrhdgtesfhbzrtfbrztvetbsdfbrsdfbrn';
$wordLength = strlen($word);
$maxLineLength = SymfonyStyle::MAX_LINE_LENGTH - 3;
$this->command->setCode(function (InputInterface $input, OutputInterface $output) use($word) {
$sfStyle = new SymfonyStyleWithForcedLineLength($input, $output);
$sfStyle->block($word, 'CUSTOM', 'fg=white;bg=blue', ' § ', false);
});
$this->tester->execute(array(), array('interactive' => false, 'decorated' => false));
$expectedCount = (int) ceil($wordLength / $maxLineLength) + (int) ($wordLength > $maxLineLength - 5);
$this->assertSame($expectedCount, substr_count($this->tester->getDisplay(true), ' § '));
}
示例3: testLongWordsBlockWrapping
public function testLongWordsBlockWrapping()
{
$word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygon';
$wordLength = strlen($word);
$maxLineLength = SymfonyStyle::MAX_LINE_LENGTH - 3;
$this->command->setCode(function (InputInterface $input, OutputInterface $output) use($word) {
$sfStyle = new SymfonyStyle($input, $output);
$sfStyle->block($word, 'CUSTOM', 'fg=white;bg=blue', ' § ', false);
});
$this->tester->execute(array(), array('interactive' => false, 'decorated' => false));
$expectedCount = (int) ceil($wordLength / $maxLineLength) + (int) ($wordLength > $maxLineLength - 5);
$this->assertSame($expectedCount, substr_count($this->tester->getDisplay(true), ' § '));
}
示例4: command
/**
* Mount console command.
*
* @param string $name Command name
* @param Closure $closure Closure to mount as command code
*
* @return Symfony\Component\Console\Command\Command
*/
public function command($name, \Closure $closure = null)
{
$cmd = new Command($name);
if (null !== $closure) {
$cmd->setCode($closure);
}
return $this['console']->add($cmd);
}
示例5: __construct
/**
* @inheritdoc
*/
public function __construct($name = null)
{
parent::__construct($name);
parent::setCode([$this, 'background']);
$this->backgroundExecute = [$this, 'execute'];
// add stop signals
$this->addSignalCallback(SIGTERM, [$this, 'shutdown']);
$this->addSignalCallback(SIGINT, [$this, 'shutdown']);
}
示例6: generateCommand
/**
* creates a Command based on an Api Method.
*
* @param string $name
* @param \ReflectionMethod $method
* @param string $token
*
* @return Command
*/
private function generateCommand($name, \ReflectionMethod $method, $token = null)
{
$methodName = $this->transformer->transform($method->getName());
$command = new Command(strtolower($name . ':' . $methodName));
$docBlock = DocBlockFactory::createInstance()->create($method->getDocComment());
$command->setDefinition($this->buildDefinition($method, $token));
$command->setDescription($docBlock->getSummary());
$command->setCode($this->createCode($name, $method));
return $command;
}
示例7: testSetCommandEnvironment
public function testSetCommandEnvironment()
{
$command = new Command('acme');
$command->setCode(function ($input, $output) {
$output->writeln('acme');
});
$commandPlugin = new CommandPlugin();
$commandPlugin->setCommandEnvironment(new SymfonyCommandEnvironment($command, new ConsoleOutput()));
$this->assertInstanceOf('Yosymfony\\Spress\\Plugin\\Environment\\CommandEnvironmentInterface', $commandPlugin->getCommandEnvironment());
}
示例8: setCode
/**
* @param callable $code
* @return $this|Command
*/
public function setCode($code)
{
$this->assertCallable($code);
$command = $this;
parent::setCode(function (InputInterface $input) use($code, $command) {
$input = $command->filterByOriginalDefinition($input, $command->getApplication()->getDefinition());
$args = $input->getArguments();
$args += $input->getOptions();
return call_user_func_array($code, $args);
});
return $this;
}
示例9: testRunCommandNotFound
/**
* @expectedException Yosymfony\Spress\Plugin\Environment\CommandNotFoundException
* @expectedExceptionMessage Command "foo" not found.
*/
public function testRunCommandNotFound()
{
$application = new Application();
$application->setAutoExit(false);
$command = new Command('acme');
$command->setCode(function ($input, $output) {
$output->writeln('acme');
});
$application->add($command);
$environment = new SymfonyCommandEnvironment($command, new ConsoleOutput());
$environment->runCommand('foo', []);
}
示例10: addInitRoboFileCommand
public function addInitRoboFileCommand($roboFile, $roboClass)
{
$createRoboFile = new Command('init');
$createRoboFile->setDescription("Intitalizes basic RoboFile in current dir");
$createRoboFile->setCode(function () use($roboClass, $roboFile) {
$output = Config::get('output');
$output->writeln("<comment> ~~~ Welcome to Robo! ~~~~ </comment>");
$output->writeln("<comment> " . $roboFile . " will be created in current dir </comment>");
file_put_contents($roboFile, '<?php' . "\n/**" . "\n * This is project's console commands configuration for Robo task runner." . "\n *" . "\n * @see http://robo.li/" . "\n */" . "\nclass " . $roboClass . " extends \\Robo\\Tasks\n{\n // define public methods as commands\n}");
$output->writeln("<comment> Edit RoboFile.php to add your commands! </comment>");
});
$this->add($createRoboFile);
}
示例11: testCommandFromApplication
public function testCommandFromApplication()
{
$application = new Application();
$application->setAutoExit(false);
$command = new Command('foo');
$command->setCode(function ($input, $output) {
$output->writeln('foo');
});
$application->add($command);
$tester = new CommandTester($application->find('foo'));
// check that there is no need to pass the command name here
$this->assertEquals(0, $tester->execute(array()));
}
示例12: __construct
/**
* @see Symfony\Component\Console\Command\Command::__construct()
*/
public function __construct($name = null)
{
// Construct our context
$this->shutdownRequested = false;
$this->setTimeout(self::DEFAULT_TIMEOUT);
$this->setReturnCode(0);
$this->lastUsage = 0;
$this->lastPeakUsage = 0;
// Construct parent context (also calls configure)
parent::__construct($name);
// Set our runloop as the executable code
parent::setCode(array($this, 'runloop'));
}
示例13: __construct
/**
* DaemonCommand constructor
* @param string $name
* @throws \InvalidArgumentException
* @throws \Symfony\Component\Console\Exception\LogicException
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException
*/
public function __construct($name = Version::NAME)
{
$this->lastPeakUsage = 0;
$this->lastUsage = 0;
$this->pid = getmypid();
$this->pidFilePath = getcwd();
$this->returnCode = 0;
$this->setTimeout(self::DEFAULT_TIMEOUT);
$this->shutdownRequested = false;
parent::__construct($name);
parent::setCode([$this, 'runLoop']);
$this->addOption('run-once', null, InputOption::VALUE_NONE, 'Run the command just once, do not go into an endless loop');
$this->addOption('detect-leaks', null, InputOption::VALUE_NONE, 'Output information about memory usage');
}
示例14: buildCommand
/**
* Build a Symfony Console commands.
*
* @param \Yosymfony\Spress\Plugin\CommandPluginInterface $commandPlugin
*
* @return \Symfony\Component\Console\Command\Command Symfony Console command.
*/
protected function buildCommand(CommandPluginInterface $commandPlugin)
{
$definition = $commandPlugin->getCommandDefinition();
$argumentsAndOptions = [];
$consoleComand = new Command($definition->getName());
$consoleComand->setDescription($definition->getDescription());
$consoleComand->setHelp($definition->getHelp());
foreach ($definition->getArguments() as list($name, $mode, $description, $defaultValue)) {
$argumentsAndOptions[] = new InputArgument($name, $mode, $description, $defaultValue);
}
foreach ($definition->getOptions() as list($name, $shortcut, $mode, $description, $defaultValue)) {
$argumentsAndOptions[] = new InputOption($name, $shortcut, $mode, $description, $defaultValue);
}
$consoleComand->setDefinition($argumentsAndOptions);
$consoleComand->setCode(function (InputInterface $input, OutputInterface $output) use($commandPlugin) {
$io = new ConsoleIO($input, $output);
$arguments = $input->getArguments();
$options = $input->getOptions();
$commandPlugin->executeCommand($io, $arguments, $options);
});
return $consoleComand;
}
示例15: use
$parse_countries->setCode(function (InputInterface $input, ConsoleOutputInterface $output) use($project) {
$output->writeLn('Starting Parse of Country this may <info>take a while</info>');
# create a csv parser
$parser = $project['parser'];
$parser_options = $project['parser_options'];
$parser_options->setParser('csv');
$parser_options->setHasHeaderRow(false);
$parser_options->setFieldSeperator(59);
$parser_options->setSkipRows(0);
# register for the generate event
$event = $project['event_dispatcher'];
$connection = $project['faker_database'];
$event->addListener('row_parsed', function (Faker\Parser\Event\RowParsed $event) use($connection, $output, $input) {
/* Example of data from names file
Array
(
[FIELD1] => UNITED STATES MINOR OUTLYING ISLANDS
[FIELD2] => UM
)
*/
$data = $event->getData();
//$output->writeLn(print_r($data,true));
$connection->transactional(function ($conn) use($output, $data, $input) {
$conn->insert('countries', array('code' => $data['FIELD2'], 'name' => ucwords(strtolower($data['FIELD1']))));
if ($input->getOption('verbose')) {
$output->writeLn(sprintf('Parsed Country <info>%s</info> with code <info>%s</info>', $data['FIELD1'], $data['FIELD2']));
} else {
$output->write('.');
}
});
});
# parse data and load into table
$parser->parse(__DIR__ . '/list-en1-semic-3.csv', $parser_options);
$output->writeLn('Finished <info>parsing countries</info> into database');
});