Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy\Command;
13
 
14
use Psy\Exception\RuntimeException;
15
use Psy\Output\ShellOutput;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
 
20
/**
21
 * Interact with the current code buffer.
22
 *
23
 * Shows and clears the buffer for the current multi-line expression.
24
 */
25
class BufferCommand extends Command
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function configure()
31
    {
32
        $this
33
            ->setName('buffer')
34
            ->setAliases(['buf'])
35
            ->setDefinition([
36
                new InputOption('clear', '', InputOption::VALUE_NONE, 'Clear the current buffer.'),
37
            ])
38
            ->setDescription('Show (or clear) the contents of the code input buffer.')
39
            ->setHelp(
40
                <<<'HELP'
41
Show the contents of the code buffer for the current multi-line expression.
42
 
43
Optionally, clear the buffer by passing the <info>--clear</info> option.
44
HELP
45
            );
46
    }
47
 
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @return int 0 if everything went fine, or an exit code
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $app = $this->getApplication();
56
        if (!$app instanceof \Psy\Shell) {
57
            throw new RuntimeException('Buffer command requires a \Psy\Shell application');
58
        }
59
 
60
        $buf = $app->getCodeBuffer();
61
        if ($input->getOption('clear')) {
62
            $app->resetCodeBuffer();
63
            $output->writeln($this->formatLines($buf, 'urgent'), ShellOutput::NUMBER_LINES);
64
        } else {
65
            $output->writeln($this->formatLines($buf), ShellOutput::NUMBER_LINES);
66
        }
67
 
68
        return 0;
69
    }
70
 
71
    /**
72
     * A helper method for wrapping buffer lines in `<urgent>` and `<return>` formatter strings.
73
     *
74
     * @param array  $lines
75
     * @param string $type  (default: 'return')
76
     *
77
     * @return array Formatted strings
78
     */
79
    protected function formatLines(array $lines, string $type = 'return'): array
80
    {
81
        $template = \sprintf('<%s>%%s</%s>', $type, $type);
82
 
83
        return \array_map(function ($line) use ($template) {
84
            return \sprintf($template, $line);
85
        }, $lines);
86
    }
87
}