|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is a part of Sculpin. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dragonfly Development Inc. |
|
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 Symplify\PHP7_Sculpin\Converter; |
|
13
|
|
|
|
|
14
|
|
|
use Symplify\PHP7_Sculpin\Event\ConvertEvent; |
|
15
|
|
|
use Symplify\PHP7_Sculpin\Event\SculpinEvents; |
|
16
|
|
|
use Symplify\PHP7_Sculpin\Formatter\FormatterManager; |
|
17
|
|
|
use Symplify\PHP7_Sculpin\Source\SourceInterface; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
19
|
|
|
|
|
20
|
|
|
final class ConverterManager |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var EventDispatcherInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $eventDispatcher; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var FormatterManager |
|
29
|
|
|
*/ |
|
30
|
|
|
private $formatterManager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ConverterInterface[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $converters = []; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, FormatterManager $formatterManager) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->formatterManager = $formatterManager; |
|
40
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function registerConverter(string $name, ConverterInterface $converter) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->converters[$name] = $converter; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function converter(string $name) : ConverterInterface |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->converters[$name]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function convertSource(SourceInterface $source) |
|
54
|
|
|
{ |
|
55
|
|
|
$converters = $source->data()->get('converters'); |
|
56
|
|
|
if (!$converters || !is_array($converters)) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
foreach ($converters as $converter) { |
|
61
|
|
|
$this->eventDispatcher->dispatch( |
|
62
|
|
|
SculpinEvents::EVENT_BEFORE_CONVERT, |
|
63
|
|
|
new ConvertEvent($source, $converter, $this->formatterManager->defaultFormatter()) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$this->converter($converter)->convert(new SourceConverterContext($source)); |
|
67
|
|
|
|
|
68
|
|
|
$this->eventDispatcher->dispatch( |
|
69
|
|
|
SculpinEvents::EVENT_AFTER_CONVERT, |
|
70
|
|
|
new ConvertEvent($source, $converter, $this->formatterManager->defaultFormatter()) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|