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\Contrib\ProxySourceCollection; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Inflector\Inflector; |
15
|
|
|
use Symplify\PHP7_Sculpin\Core\DataProvider\DataProviderInterface; |
16
|
|
|
use Symplify\PHP7_Sculpin\Core\Event\ConvertEvent; |
17
|
|
|
use Symplify\PHP7_Sculpin\Core\Event\SculpinEvents; |
18
|
|
|
use Symplify\PHP7_Sculpin\Core\Event\SourceSetEvent; |
19
|
|
|
use Symplify\PHP7_Sculpin\Core\Formatter\FormatterManager; |
20
|
|
|
use Symplify\PHP7_Sculpin\Core\Source\Filter\FilterInterface; |
21
|
|
|
use Symplify\PHP7_Sculpin\Core\Source\Map\MapInterface; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
23
|
|
|
|
24
|
|
|
final class ProxySourceCollectionDataProvider implements DataProviderInterface, EventSubscriberInterface |
25
|
|
|
{ |
26
|
|
|
private $formatterManager; |
27
|
|
|
private $dataProviderName; |
28
|
|
|
private $dataSingularName; |
29
|
|
|
private $collection = []; |
30
|
|
|
private $filter; |
31
|
|
|
private $map; |
32
|
|
|
private $factory; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
FormatterManager $formatterManager, |
36
|
|
|
$dataProviderName, |
37
|
|
|
$dataSingularName, |
38
|
|
|
ProxySourceCollection $collection = null, |
39
|
|
|
FilterInterface $filter, |
40
|
|
|
MapInterface $map, |
41
|
|
|
ProxySourceItemFactoryInterface $factory = null |
42
|
|
|
) { |
43
|
|
|
$this->formatterManager = $formatterManager; |
44
|
|
|
$this->dataProviderName = $dataProviderName; |
45
|
|
|
$this->dataSingularName = $dataSingularName ?: Inflector::singularize($dataProviderName); |
46
|
|
|
$this->collection = $collection ?: new ProxySourceCollection(); |
|
|
|
|
47
|
|
|
$this->filter = $filter; |
48
|
|
|
$this->map = $map; |
49
|
|
|
$this->factory = $factory ?: new SimpleProxySourceItemFactory(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function provideData() : ProxySourceCollection |
56
|
|
|
{ |
57
|
|
|
return $this->collection; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function getSubscribedEvents() : array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
SculpinEvents::EVENT_BEFORE_RUN => [ |
64
|
|
|
['beforeRun', 0], |
65
|
|
|
['beforeRunPost', -100], |
66
|
|
|
], |
67
|
|
|
SculpinEvents::EVENT_AFTER_CONVERT => 'afterConvert', |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function beforeRun(SourceSetEvent $sourceSetEvent) |
72
|
|
|
{ |
73
|
|
|
foreach ($sourceSetEvent->updatedSources() as $source) { |
74
|
|
|
if ($source->isGenerated()) { |
75
|
|
|
// We want to skip generated sources in case someone is |
76
|
|
|
// doing something like a redirect where virtual sources are |
77
|
|
|
// created like a redirect plugin. |
78
|
|
|
|
79
|
|
|
// NOTE: This means that a generator cannot create proxy |
80
|
|
|
// source collection items. This could be limiting in the |
81
|
|
|
// future... |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
if ($this->filter->match($source)) { |
85
|
|
|
$this->map->process($source); |
86
|
|
|
$this->collection[$source->sourceId()] = $this->factory->createProxySourceItem($source); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
$foudAtLeastOne = false; |
90
|
|
|
|
91
|
|
|
foreach ($sourceSetEvent->allSources() as $source) { |
92
|
|
|
if ($this->filter->match($source)) { |
93
|
|
|
$foudAtLeastOne = true; |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$foudAtLeastOne) { |
99
|
|
|
echo 'Didnt find at least one of this type : '.$this->dataProviderName.PHP_EOL; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->collection->init(); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function beforeRunPost(SourceSetEvent $sourceSetEvent) |
106
|
|
|
{ |
107
|
|
|
$anItemHasChanged = false; |
108
|
|
|
foreach ($this->collection as $item) { |
109
|
|
|
if ($item->hasChanged()) { |
110
|
|
|
$anItemHasChanged = true; |
111
|
|
|
$this->collection->init(); |
|
|
|
|
112
|
|
|
break; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
if ($anItemHasChanged) { |
116
|
|
|
foreach ($sourceSetEvent->allSources() as $source) { |
117
|
|
|
if ($source->data()->get('use') and in_array($this->dataProviderName, $source->data()->get('use'))) { |
|
|
|
|
118
|
|
|
$source->forceReprocess(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
foreach ($this->collection as $item) { |
123
|
|
|
$item->data()->set('next_'.$this->dataSingularName, $item->nextItem()); |
124
|
|
|
$item->data()->set('previous_'.$this->dataSingularName, $item->previousItem()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function afterConvert(ConvertEvent $convertEvent) |
129
|
|
|
{ |
130
|
|
|
$sourceId = $convertEvent->source()->sourceId(); |
131
|
|
|
if (isset($this->collection[$sourceId])) { |
132
|
|
|
$item = $this->collection[$sourceId]; |
133
|
|
|
$item->setBlocks($this->formatterManager->formatSourceBlocks($convertEvent->source())); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..