PHP code example of consolidation / annotated-command
1. Go to this page and download the library: Download consolidation/annotated-command library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
consolidation / annotated-command example snippets
class MyCommandClass
{
/**
* This is the my:echo command
*
* This command will concatenate two parameters. If the --flip flag
* is provided, then the result is the concatenation of two and one.
*
* @command my:echo
* @param string $one The first parameter.
* @param string $two The other parameter.
* @param bool $flip The "flip" option
* @option flip Whether or not the second parameter should come first in the result.
* @aliases c
* @usage bet alpha --flip
* Concatenate "alpha" and "bet".
*/
public function myEcho($one, $two, $flip = false)
{
if ($flip) {
return "{$two}{$one}";
}
return "{$one}{$two}";
}
}
#[CLI\Name(name: 'my:echo', aliases: ['c'])]
#[CLI\Help(description: 'This is the my:echo command', synopsis: "This command will concatenate two parameters. If the --flip flag\nis provided, then the result is the concatenation of two and one.",)]
#[CLI\Param(name: 'one', description: 'The first parameter')]
#[CLI\Param(name: 'two', description: 'The other parameter')]
#[CLI\Option(name: 'flip', description: 'Whether or not the second parameter should come first in the result.')]
#[CLI\Usage(name: 'bet alpha --flip', description: 'Concatenate "alpha" and "bet".')]
public function myEcho($one, $two = '', $flip = false)
{
if ($options['flip']) {
return "{$two}{$one}";
}
return "{$one}{$two}";
}
class MyCommandClass
{
/**
* This is the my:echo command
*
* This command will concatenate two parameters. If the --flip flag
* is provided, then the result is the concatenation of two and one.
*
* @command my:echo
* @param integer $one The first parameter.
* @param integer $two The other parameter.
* @param array $options An option that takes multiple values.
* @option flip Whether or not the second parameter should come first in the result.
* @aliases c
* @usage bet alpha --flip
* Concatenate "alpha" and "bet".
*/
public function myEcho($one, $two, $options = ['flip' => false])
{
if ($options['flip']) {
return "{$two}{$one}";
}
return "{$one}{$two}";
}
}
class MyReplaceCommandHook {
/**
* @hook replace-command foo:bar
*
* Parameters must match original command method.
*/
public function myFooBarReplacement($value) {
print "Hello $value!";
}
}
/**
* @command example
* @option string $file
* @default $file -
*/
public function example(InputInterface $input)
{
$data = StdinHandler::selectStream($input, 'file')->contents();
}
$myCommandClassInstance = new MyCommandClass();
$commandFactory = new AnnotatedCommandFactory();
$commandFactory->setIncludeAllPublicMethods(true);
$commandFactory->commandProcessor()->setFormatterManager(new FormatterManager());
$commandList = $commandFactory->createCommandsFromClass($myCommandClassInstance);
foreach ($commandList as $command) {
$application->add($command);
}
$discovery = new CommandFileDiscovery();
$myCommandFiles = $discovery->discover($path, '\Drupal');
foreach ($myCommandFiles as $myCommandClass) {
$myCommandClassInstance = new $myCommandClass();
// ... as above
}
$terminalWidthOption = new PrepareTerminalWidthOption();
$terminalWidthOption->setApplication($application);
$commandFactory->commandProcessor()->addPrepareFormatter($terminalWidthOption);
use Consolidation\AnnotatedCommand\AnnotationData;
use Symfony\Component\Console\Input\InputInterface;
/**
* @hook init some:command
*/
public function initSomeCommand(InputInterface $input, AnnotationData $annotationData)
{
$value = $input->getOption('some-option');
if (!$value) {
$input->setOption('some-option', $this->generateRandomOptionValue());
}
}
use Consolidation\AnnotatedCommand\CommandData;
/**
* @hook validate some:command
*/
public function validatePassword(CommandData $commandData)
{
$input = $commandData->input();
$password = $input->getOption('password');
if (strpbrk($password, '!;$`') === false) {
throw new \Exception("Your password MUST contain at least one of the characters ! ; ` or $, for no rational reason whatsoever.");
}
}
use Consolidation\AnnotatedCommand\CommandData;
/**
* @hook process some:command
*/
public function process($result, CommandData $commandData)
{
if ($result instanceof MyInterimType) {
$result = $this->convertInterimResult($result);
}
}
/**
* @hook on-event custom-event
*/
public function handlerForCustomEvent(/* arbitrary parameters, as defined by custom-event */)
{
// do the needful, return what custom-event expects
}
public function AnnotatedCommandFactory::addListener(CommandCreationListenerInterface $listener);
public function AnnotatedCommandFactory::addAutomaticOptionProvider(AutomaticOptionsProviderInterface $listener);
public function alterCommandInfo(CommandInfo $commandInfo, $commandFileInstance);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.