-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProxySourceCollectionDataProvider.php
135 lines (122 loc) · 4.52 KB
/
ProxySourceCollectionDataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/*
* This file is a part of Sculpin.
*
* (c) Dragonfly Development Inc.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sculpin\Contrib\ProxySourceCollection;
use Doctrine\Common\Inflector\Inflector;
use Sculpin\Core\DataProvider\DataProviderInterface;
use Sculpin\Core\Event\ConvertEvent;
use Sculpin\Core\Event\SourceSetEvent;
use Sculpin\Core\Formatter\FormatterManager;
use Sculpin\Core\Sculpin;
use Sculpin\Core\Source\Filter\FilterInterface;
use Sculpin\Core\Source\Filter\NullFilter;
use Sculpin\Core\Source\Map\MapInterface;
use Sculpin\Core\Source\Map\NullMap;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProxySourceCollectionDataProvider implements DataProviderInterface, EventSubscriberInterface
{
private $formatterManager;
private $dataProviderName;
private $dataSingularName;
private $collection;
private $filter;
private $map;
private $factory;
public function __construct(
FormatterManager $formatterManager,
$dataProviderName,
$dataSingularName = null,
ProxySourceCollection $collection = null,
FilterInterface $filter,
MapInterface $map,
ProxySourceItemFactoryInterface $factory = null
) {
$this->formatterManager = $formatterManager;
$this->dataProviderName = $dataProviderName;
$this->dataSingularName = $dataSingularName ?: Inflector::singularize($dataProviderName);
$this->collection = $collection ?: new ProxySourceCollection;
$this->filter = $filter ?: new NullFilter;
$this->map = $map ?: new NullMap;
$this->factory = $factory ?: new SimpleProxySourceItemFactory;
}
public function provideData()
{
return $this->collection;
}
public static function getSubscribedEvents()
{
return array(
Sculpin::EVENT_BEFORE_RUN => array(
array('beforeRun', 0),
array('beforeRunPost', -100),
),
Sculpin::EVENT_AFTER_CONVERT => 'afterConvert',
);
}
public function beforeRun(SourceSetEvent $sourceSetEvent)
{
foreach ($sourceSetEvent->updatedSources() as $source) {
if ($source->isGenerated()) {
// We want to skip generated sources in case someone is
// doing something like a redirect where virtual sources are
// created like a redirect plugin.
//
// NOTE: This means that a generator cannot create proxy
// source collection items. This could be limiting in the
// future...
continue;
}
if ($this->filter->match($source)) {
$this->map->process($source);
$this->collection[$source->sourceId()] = $this->factory->createProxySourceItem($source);
}
}
$foudAtLeastOne = false;
foreach ($sourceSetEvent->allSources() as $source) {
if ($this->filter->match($source)) {
$foudAtLeastOne = true;
break;
}
}
if (!$foudAtLeastOne) {
echo 'Didnt find at least one of this type : ' . $this->dataProviderName . PHP_EOL;
}
$this->collection->init();
}
public function beforeRunPost(SourceSetEvent $sourceSetEvent)
{
$anItemHasChanged = false;
foreach ($this->collection as $item) {
if ($item->hasChanged()) {
$anItemHasChanged = true;
$this->collection->init();
break;
}
}
if ($anItemHasChanged) {
foreach ($sourceSetEvent->allSources() as $source) {
if ($source->data()->get('use') and in_array($this->dataProviderName, $source->data()->get('use'))) {
$source->forceReprocess();
}
}
}
foreach ($this->collection as $item) {
$item->data()->set('next_'.$this->dataSingularName, $item->nextItem());
$item->data()->set('previous_'.$this->dataSingularName, $item->previousItem());
}
}
public function afterConvert(ConvertEvent $convertEvent)
{
$sourceId = $convertEvent->source()->sourceId();
if (isset($this->collection[$sourceId])) {
$item = $this->collection[$sourceId];
$item->setBlocks($this->formatterManager->formatSourceBlocks($convertEvent->source()));
}
}
}