Skip to content

Commit

Permalink
[!!!][FEATURE] Integrate Symfony/Console into CommandController
Browse files Browse the repository at this point in the history
This is a backport from the new introduced feature in Flow
https://review.typo3.org/#/c/30653/ with upstream patches.

This extends the base ``CommandController`` by some convenience
helpers from the ``symfony/console`` package:
easy output coloring through "<error>Warning!</error>"
TableHelper to render values to a grid
ProgressHelper to render and advance and progress bars
DialogHelper with numerous types of questions like: select,
ask, confirm, askHidden, etc
Additionally this change improves the
``mapRequestArgumentsToControllerArguments()`` method to ask for
missing required arguments instead of quitting with an exception.
You can make use of the new features by calling the introduced
ConsoleOutput object with its respective methods:
outputTable()
select()
ask()
askConfirmation()
askHiddenResponse()
askAndValidate()
askHiddenResponseAndValidate()
progressStart()
progressSet()
progressAdvance()
progressFinish()

This change does not alter the public API so it is not breaking
in the strict sense. But it introduces a new behavior:
Previously all outputs where collected in the ``Cli\Response``
and only rendered to the console at the end of a CLI request.
Now all methods producing output (inluding ``output()`` and
``outputLine()``) render the result directly to the console.
If you use ``$this->response`` directly or let the command method
return a string, the rendering is still deferred until the end of
the CLI request.

Resolves: #59606
Releases: master
Change-Id: I33e051f698f5cc1e204f609734280bbed69610c9
Reviewed-on: http://review.typo3.org/30743
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org>
Tested-by: Helmut Hummel <helmut.hummel@typo3.org>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
  • Loading branch information
wouter90 authored and maddy2101 committed Apr 30, 2015
1 parent 3044b74 commit be4ba97
Show file tree
Hide file tree
Showing 7 changed files with 489 additions and 48 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -37,6 +37,7 @@
"pear/http_request2": "2.2.1",
"phpwhois/idna-convert": "0.8.2",
"swiftmailer/swiftmailer": "5.2.1",
"symfony/console": "2.5.*",
"helhum/class-alias-loader": "~1.1",
"typo3/cms-composer-installers": "1.1.*@dev"
},
Expand Down
@@ -0,0 +1,93 @@
==================================================================
Feature: #59606 - Integrate Symfony/Console into CommandController
==================================================================

Description
===========

The CommandController now makes use of Symfony/Console internally and
provides various methods directly from the CommandController's ``output`` member:

* TableHelper

* outputTable($rows, $headers = NULL)

* DialogHelper

* select($question, $choices, $default = NULL, $multiSelect = false, $attempts = FALSE)
* ask($question, $default = NULL, array $autocomplete = array())
* askConfirmation($question, $default = TRUE)
* askHiddenResponse($question, $fallback = TRUE)
* askAndValidate($question, $validator, $attempts = FALSE, $default = NULL, array $autocomplete = NULL)
* askHiddenResponseAndValidate($question, $validator, $attempts = FALSE, $fallback = TRUE)

* ProgressHelper

* progressStart($max = NULL)
* progressSet($current)
* progressAdvance($step = 1)
* progressFinish()

Here's an example showing of some of those functions:

.. code-block:: php
namespace Acme\Demo\Command;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
/**
* My command
*/
class MyCommandController extends CommandController {
/**
* @return string
*/
public function myCommand() {
// render a table
$this->output->outputTable(array(
array('Bob', 34, 'm'),
array('Sally', 21, 'f'),
array('Blake', 56, 'm')
),
array('Name', 'Age', 'Gender'));
// select
$colors = array('red', 'blue', 'yellow');
$selectedColorIndex = $this->output->select('Please select one color', $colors, 'red');
$this->outputLine('You choose the color %s.', array($colors[$selectedColorIndex]));
// ask
$name = $this->output->ask('What is your name?' . PHP_EOL, 'Bob', array('Bob', 'Sally', 'Blake'));
$this->outputLine('Hello %s.', array($name));
// prompt
$likesDogs = $this->output->askConfirmation('Do you like dogs?');
if ($likesDogs) {
$this->outputLine('You do like dogs!');
}
// progress
$this->output->progressStart(600);
for ($i = 0; $i < 300; $i ++) {
$this->output->progressAdvance();
usleep(5000);
}
$this->output->progressFinish();
}
}
Impact
======

This change does not alter the public API so it is not breaking
in the strict sense. But it introduces a new behavior:
Previously all outputs where collected in the ``Cli\Response``
and only rendered to the console at the end of a CLI request.
Now all methods producing output (inluding ``output()`` and
``outputLine()``) render the result directly to the console.If you use ``$this->response`` directly or let the command method
return a string, the rendering is still deferred until the end of
the CLI request.
Expand Up @@ -84,10 +84,10 @@ protected function displayHelpIndex() {
foreach ($this->commandsByExtensionsAndControllers as $extensionKey => $commandControllers) {
$this->outputLine('');
$this->outputLine('EXTENSION "%s":', array(strtoupper($extensionKey)));
$this->outputLine(str_repeat('-', self::MAXIMUM_LINE_LENGTH));
$this->outputLine(str_repeat('-', $this->output->getMaximumLineLength()));
foreach ($commandControllers as $commands) {
foreach ($commands as $command) {
$description = wordwrap($command->getShortDescription(), self::MAXIMUM_LINE_LENGTH - 43, PHP_EOL . str_repeat(' ', 43), TRUE);
$description = wordwrap($command->getShortDescription(), $this->output->getMaximumLineLength() - 43, PHP_EOL . str_repeat(' ', 43), TRUE);
$shortCommandIdentifier = $this->commandManager->getShortestIdentifierForCommand($command);
$this->outputLine('%-2s%-40s %s', array(' ', $shortCommandIdentifier, $description));
}
Expand Down Expand Up @@ -129,7 +129,7 @@ protected function displayHelpForCommand(\TYPO3\CMS\Extbase\Mvc\Cli\Command $com
if ($command->hasArguments()) {
foreach ($commandArgumentDefinitions as $commandArgumentDefinition) {
$argumentDescription = $commandArgumentDefinition->getDescription();
$argumentDescription = wordwrap($argumentDescription, self::MAXIMUM_LINE_LENGTH - 23, PHP_EOL . str_repeat(' ', 23), TRUE);
$argumentDescription = wordwrap($argumentDescription, $this->output->getMaximumLineLength() - 23, PHP_EOL . str_repeat(' ', 23), TRUE);
if ($commandArgumentDefinition->isRequired()) {
$argumentDescriptions[] = vsprintf(' %-20s %s', array($commandArgumentDefinition->getDashedName(), $argumentDescription));
} else {
Expand Down

0 comments on commit be4ba97

Please sign in to comment.