annotate vendor/psy/psysh/src/Psy/Command/BufferCommand.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2017 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Command;
Chris@0 13
Chris@0 14 use Psy\Output\ShellOutput;
Chris@0 15 use Symfony\Component\Console\Input\InputInterface;
Chris@0 16 use Symfony\Component\Console\Input\InputOption;
Chris@0 17 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Interact with the current code buffer.
Chris@0 21 *
Chris@0 22 * Shows and clears the buffer for the current multi-line expression.
Chris@0 23 */
Chris@0 24 class BufferCommand extends Command
Chris@0 25 {
Chris@0 26 /**
Chris@0 27 * {@inheritdoc}
Chris@0 28 */
Chris@0 29 protected function configure()
Chris@0 30 {
Chris@0 31 $this
Chris@0 32 ->setName('buffer')
Chris@0 33 ->setAliases(array('buf'))
Chris@0 34 ->setDefinition(array(
Chris@0 35 new InputOption('clear', '', InputOption::VALUE_NONE, 'Clear the current buffer.'),
Chris@0 36 ))
Chris@0 37 ->setDescription('Show (or clear) the contents of the code input buffer.')
Chris@0 38 ->setHelp(
Chris@0 39 <<<'HELP'
Chris@0 40 Show the contents of the code buffer for the current multi-line expression.
Chris@0 41
Chris@0 42 Optionally, clear the buffer by passing the <info>--clear</info> option.
Chris@0 43 HELP
Chris@0 44 );
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * {@inheritdoc}
Chris@0 49 */
Chris@0 50 protected function execute(InputInterface $input, OutputInterface $output)
Chris@0 51 {
Chris@0 52 $buf = $this->getApplication()->getCodeBuffer();
Chris@0 53 if ($input->getOption('clear')) {
Chris@0 54 $this->getApplication()->resetCodeBuffer();
Chris@0 55 $output->writeln($this->formatLines($buf, 'urgent'), ShellOutput::NUMBER_LINES);
Chris@0 56 } else {
Chris@0 57 $output->writeln($this->formatLines($buf), ShellOutput::NUMBER_LINES);
Chris@0 58 }
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * A helper method for wrapping buffer lines in `<urgent>` and `<return>` formatter strings.
Chris@0 63 *
Chris@0 64 * @param array $lines
Chris@0 65 * @param string $type (default: 'return')
Chris@0 66 *
Chris@0 67 * @return array Formatted strings
Chris@0 68 */
Chris@0 69 protected function formatLines(array $lines, $type = 'return')
Chris@0 70 {
Chris@0 71 $template = sprintf('<%s>%%s</%s>', $type, $type);
Chris@0 72
Chris@0 73 return array_map(function ($line) use ($template) {
Chris@0 74 return sprintf($template, $line);
Chris@0 75 }, $lines);
Chris@0 76 }
Chris@0 77 }