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\PostsBundle\ProxySourceCollection; |
13
|
|
|
|
14
|
|
|
use Symplify\PHP7_Sculpin\DataProvider\DataProviderInterface; |
15
|
|
|
use Symplify\PHP7_Sculpin\Event\ConvertEvent; |
16
|
|
|
use Symplify\PHP7_Sculpin\Event\SculpinEvents; |
17
|
|
|
use Symplify\PHP7_Sculpin\Event\SourceSetEvent; |
18
|
|
|
use Symplify\PHP7_Sculpin\Formatter\FormatterManager; |
19
|
|
|
use Symplify\PHP7_Sculpin\Source\Filter\FilterInterface; |
20
|
|
|
use Symplify\PHP7_Sculpin\Source\Map\MapInterface; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
22
|
|
|
|
23
|
|
|
final class ProxySourceCollectionDataProvider implements DataProviderInterface, EventSubscriberInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var FormatterManager |
27
|
|
|
*/ |
28
|
|
|
private $formatterManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $dataProviderName; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array|ProxySourceCollection |
37
|
|
|
*/ |
38
|
|
|
private $collection = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var FilterInterface |
42
|
|
|
*/ |
43
|
|
|
private $filter; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var MapInterface |
47
|
|
|
*/ |
48
|
|
|
private $map; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var SimpleProxySourceItemFactory |
52
|
|
|
*/ |
53
|
|
|
private $factory; |
54
|
|
|
|
55
|
|
|
public function __construct( |
56
|
|
|
FormatterManager $formatterManager, |
57
|
|
|
string $dataProviderName, |
58
|
|
|
ProxySourceCollection $collection, |
59
|
|
|
FilterInterface $filter, |
60
|
|
|
MapInterface $map, |
61
|
|
|
SimpleProxySourceItemFactory $factory |
62
|
|
|
) { |
63
|
|
|
$this->formatterManager = $formatterManager; |
64
|
|
|
$this->dataProviderName = $dataProviderName; |
65
|
|
|
$this->collection = $collection; // ?: new ProxySourceCollection(); |
|
|
|
|
66
|
|
|
$this->filter = $filter; |
67
|
|
|
$this->map = $map; |
68
|
|
|
$this->factory = $factory; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function provideData() : ProxySourceCollection |
75
|
|
|
{ |
76
|
|
|
return $this->collection; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function getSubscribedEvents() : array |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
SculpinEvents::BEFORE_RUN => [ |
83
|
|
|
['beforeRun', 0], |
84
|
|
|
['beforeRunPost', -100], |
85
|
|
|
], |
86
|
|
|
SculpinEvents::AFTER_CONVERT => 'afterConvert', |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function beforeRun(SourceSetEvent $sourceSetEvent) |
91
|
|
|
{ |
92
|
|
|
foreach ($sourceSetEvent->updatedSources() as $source) { |
93
|
|
|
if ($source->isGenerated()) { |
94
|
|
|
// We want to skip generated sources in case someone is |
95
|
|
|
// doing something like a redirect where virtual sources are |
96
|
|
|
// created like a redirect plugin. |
97
|
|
|
|
98
|
|
|
// NOTE: This means that a generator cannot create proxy |
99
|
|
|
// source collection items. This could be limiting in the |
100
|
|
|
// future... |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($this->filter->match($source)) { |
105
|
|
|
$this->map->process($source); |
106
|
|
|
$this->collection[$source->sourceId()] = $this->factory->createProxySourceItem($source); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
$foudAtLeastOne = false; |
110
|
|
|
|
111
|
|
|
foreach ($sourceSetEvent->allSources() as $source) { |
112
|
|
|
if ($this->filter->match($source)) { |
113
|
|
|
$foudAtLeastOne = true; |
114
|
|
|
break; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!$foudAtLeastOne) { |
119
|
|
|
echo 'Didn\'t find at least one of this type : '.$this->dataProviderName.PHP_EOL; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->collection->init(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function beforeRunPost(SourceSetEvent $sourceSetEvent) |
126
|
|
|
{ |
127
|
|
|
$anItemHasChanged = false; |
128
|
|
|
foreach ($this->collection as $item) { |
129
|
|
|
if ($item->hasChanged()) { |
130
|
|
|
$anItemHasChanged = true; |
131
|
|
|
$this->collection->init(); |
132
|
|
|
break; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
if ($anItemHasChanged) { |
136
|
|
|
foreach ($sourceSetEvent->allSources() as $source) { |
137
|
|
|
if ($source->data()->get('use') and in_array($this->dataProviderName, $source->data()->get('use'))) { |
|
|
|
|
138
|
|
|
$source->forceReprocess(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function afterConvert(ConvertEvent $convertEvent) |
145
|
|
|
{ |
146
|
|
|
$sourceId = $convertEvent->source()->sourceId(); |
147
|
|
|
if (isset($this->collection[$sourceId])) { |
148
|
|
|
$item = $this->collection[$sourceId]; |
149
|
|
|
$item->setBlocks($this->formatterManager->formatSourceBlocks($convertEvent->source())); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.