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