1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Bigwhoop\Trumpet\Presentation\SlideRendering; |
4
|
|
|
|
5
|
|
|
use Bigwhoop\Trumpet\Commands\CommandExecutionContext; |
6
|
|
|
use Bigwhoop\Trumpet\Commands\CommandHandler; |
7
|
|
|
use Bigwhoop\Trumpet\Commands\CommandParams; |
8
|
|
|
use Bigwhoop\Trumpet\Config\Slide; |
9
|
|
|
|
10
|
|
|
final class CommandsRenderer implements SlideRenderer |
11
|
|
|
{ |
12
|
|
|
const COMMAND_PREFIX = '!'; |
13
|
|
|
|
14
|
|
|
/** @var CommandHandler */ |
15
|
|
|
private $commandHandler; |
16
|
|
|
|
17
|
|
|
/** @var CommandExecutionContext */ |
18
|
|
|
private $executionContext; |
19
|
|
|
|
20
|
1 |
|
public function __construct(CommandHandler $handler, CommandExecutionContext $executionContext) |
21
|
|
|
{ |
22
|
1 |
|
$this->commandHandler = $handler; |
23
|
1 |
|
$this->executionContext = $executionContext; |
24
|
1 |
|
} |
25
|
|
|
|
26
|
1 |
|
public function render(Slide $slide): string |
27
|
|
|
{ |
28
|
1 |
|
$lines = explode("\n", str_replace("\r", '', $slide->content)); |
29
|
|
|
|
30
|
1 |
|
$out = []; |
31
|
1 |
|
foreach ($lines as $line) { |
32
|
1 |
|
if (0 === strpos($line, self::COMMAND_PREFIX)) { |
33
|
1 |
|
$command = substr($line, strlen(self::COMMAND_PREFIX)); |
34
|
1 |
|
list($commandName, $commandParams) = explode(' ', $command, 2); |
35
|
|
|
|
36
|
1 |
|
if ($this->commandHandler->hasCommand($commandName)) { |
37
|
1 |
|
$line = $this->commandHandler->execute($commandName, new CommandParams($commandParams), $this->executionContext); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
$out[] = $line; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
return implode("\n", $out); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|