1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the SexyField package. |
5
|
|
|
* |
6
|
|
|
* (c) Dion Snoeijen <[email protected]> |
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
|
|
|
declare (strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace Tardigrades\SectionField\Service; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17
|
|
|
use Tardigrades\Helper\FullyQualifiedClassNameConverter; |
18
|
|
|
use Tardigrades\SectionField\Event\SectionBeforeRead; |
19
|
|
|
use Tardigrades\SectionField\Event\SectionDataRead; |
20
|
|
|
use Tardigrades\SectionField\ValueObject\SectionConfig; |
21
|
|
|
|
22
|
|
|
class ReadSection implements ReadSectionInterface |
23
|
|
|
{ |
24
|
|
|
/** @var array */ |
25
|
|
|
private $readers; |
26
|
|
|
|
27
|
|
|
/** @var SectionManagerInterface */ |
28
|
|
|
private $sectionManager; |
29
|
|
|
|
30
|
|
|
/** @var EventDispatcherInterface */ |
31
|
|
|
private $dispatcher; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
array $readers, |
35
|
|
|
SectionManagerInterface $sectionManager, |
36
|
|
|
EventDispatcherInterface $dispatcher |
37
|
|
|
) { |
38
|
|
|
$this->readers = $readers; |
39
|
|
|
$this->sectionManager = $sectionManager; |
40
|
|
|
$this->dispatcher = $dispatcher; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Read from one or more data-sources |
45
|
|
|
* |
46
|
|
|
* @param ReadOptionsInterface $options |
47
|
|
|
* @param SectionConfig|null $sectionConfig |
48
|
|
|
* @return \ArrayIterator |
49
|
|
|
*/ |
50
|
|
|
public function read( |
51
|
|
|
ReadOptionsInterface $options, |
52
|
|
|
SectionConfig $sectionConfig = null |
53
|
|
|
): \ArrayIterator { |
54
|
|
|
$sectionData = new \ArrayIterator(); |
55
|
|
|
|
56
|
|
|
$this->dispatcher->dispatch( |
57
|
|
|
SectionBeforeRead::NAME, |
58
|
|
|
new SectionBeforeRead($sectionData, $options, $sectionConfig) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
if ($sectionConfig === null) { |
62
|
|
|
$sectionConfig = $this->sectionManager->readByHandle( |
63
|
|
|
FullyQualifiedClassNameConverter::toHandle( |
64
|
|
|
$options->getSection()[0] |
|
|
|
|
65
|
|
|
) |
66
|
|
|
)->getConfig(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Make sure we are passing the fully qualified class name as the section |
70
|
|
|
$optionsArray = $options->toArray(); |
71
|
|
|
$optionsArray[ReadOptions::SECTION] = (string) $sectionConfig->getFullyQualifiedClassName(); |
72
|
|
|
// For now, we call DoctrineRead options, this will of course be fixed in a later release. |
73
|
|
|
$options = ReadOptions::fromArray($optionsArray); |
74
|
|
|
|
75
|
|
|
/** @var ReadSectionInterface $reader */ |
76
|
|
|
foreach ($this->readers as $reader) { |
77
|
|
|
foreach ($reader->read($options, $sectionConfig) as $entry) { |
78
|
|
|
$sectionData->append($entry); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->dispatcher->dispatch( |
83
|
|
|
SectionDataRead::NAME, |
84
|
|
|
new SectionDataRead($sectionData, $options, $sectionConfig) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return $sectionData; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|