|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmaTeam\Image\Projection\Framework; |
|
4
|
|
|
|
|
5
|
|
|
use AmaTeam\Image\Projection\API\ConversionInterface; |
|
6
|
|
|
use AmaTeam\Image\Projection\API\Conversion\ListenerInterface; |
|
7
|
|
|
use AmaTeam\Image\Projection\API\Conversion\ProcessorInterface; |
|
8
|
|
|
use AmaTeam\Image\Projection\API\SpecificationInterface; |
|
9
|
|
|
use AmaTeam\Image\Projection\Tile\Tile; |
|
10
|
|
|
use AmaTeam\Image\Projection\API\Type\GeneratorInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Single-run conversion processing that encapsulates all userspace handlers. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Conversion implements ConversionInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var SpecificationInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $target; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var GeneratorInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $generator; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ListenerInterface[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $listeners = []; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ProcessorInterface[][] |
|
31
|
|
|
*/ |
|
32
|
|
|
private $processors = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param SpecificationInterface $target |
|
36
|
|
|
* @param GeneratorInterface $generator |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
SpecificationInterface $target, |
|
40
|
|
|
GeneratorInterface $generator |
|
41
|
|
|
) { |
|
42
|
|
|
$this->target = $target; |
|
43
|
|
|
$this->generator = $generator; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Runs whole pipeline. Tiles are destroyed after last listener has been |
|
48
|
|
|
* called. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function run() |
|
51
|
|
|
{ |
|
52
|
|
|
foreach ($this->generator as $tile) { |
|
53
|
|
|
$this->applyProcessors($tile, $this->target); |
|
54
|
|
|
$this->notifyListeners($tile, $this->target); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Tile $tile |
|
60
|
|
|
* @param SpecificationInterface $specification |
|
61
|
|
|
*/ |
|
62
|
|
|
private function applyProcessors( |
|
63
|
|
|
Tile $tile, |
|
64
|
|
|
SpecificationInterface $specification |
|
65
|
|
|
) { |
|
66
|
|
|
foreach ($this->processors as $processors) { |
|
67
|
|
|
foreach ($processors as $processor) { |
|
68
|
|
|
$processor->process($tile, $specification); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Tile $tile |
|
75
|
|
|
* @param SpecificationInterface $specification |
|
76
|
|
|
*/ |
|
77
|
|
|
private function notifyListeners( |
|
78
|
|
|
Tile $tile, |
|
79
|
|
|
SpecificationInterface $specification |
|
80
|
|
|
) { |
|
81
|
|
|
foreach ($this->listeners as $listener) { |
|
82
|
|
|
$listener->accept($tile, $specification); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param ProcessorInterface $processor |
|
88
|
|
|
* @param int $order Order in which processors are applied (lower order. |
|
89
|
|
|
* values force processors to be run earlier). Processor with same order |
|
90
|
|
|
* value will be run in the order they were added. |
|
91
|
|
|
* priority |
|
92
|
|
|
* |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
|
|
public function addProcessor(ProcessorInterface $processor, $order = 0) |
|
96
|
|
|
{ |
|
97
|
|
|
if (!$this->processors[$order]) { |
|
98
|
|
|
$this->processors[$order] = []; |
|
99
|
|
|
} |
|
100
|
|
|
$this->processors[$order][] = $processor; |
|
101
|
|
|
ksort($this->processors); |
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param ListenerInterface $listener |
|
107
|
|
|
* @return $this |
|
108
|
|
|
*/ |
|
109
|
|
|
public function addListener(ListenerInterface $listener) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->listeners[] = $listener; |
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|