/
SiteTreePublishingEngine.php
212 lines (184 loc) · 5.87 KB
/
SiteTreePublishingEngine.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
namespace SilverStripe\StaticPublishQueue\Extension\Engine;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\CMS\Model\SiteTreeExtension;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Resettable;
use SilverStripe\ORM\ValidationException;
use SilverStripe\StaticPublishQueue\Contract\StaticPublishingTrigger;
use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree;
use SilverStripe\StaticPublishQueue\Job\DeleteStaticCacheJob;
use SilverStripe\StaticPublishQueue\Job\GenerateStaticCacheJob;
use SilverStripe\StaticPublishQueue\Service\UrlBundleInterface;
use Symbiote\QueuedJobs\Services\QueuedJobService;
/**
* This extension couples to the StaticallyPublishable and StaticPublishingTrigger implementations
* on the SiteTree objects and makes sure the actual change to SiteTree is triggered/enqueued.
*
* Provides the following information as a context to StaticPublishingTrigger:
* * action - name of the executed action: publish or unpublish
*
* @see PublishableSiteTree
*/
class SiteTreePublishingEngine extends SiteTreeExtension implements Resettable
{
/**
* Queued job service injection property
* Used for unit tests only to cover edge cases where Injector doesn't cover
*
* @var QueuedJobService|null
*/
protected static $queueService = null;
/**
* Queues the urls to be flushed into the queue.
*
* @var array
*/
private $toUpdate = [];
/**
* Queues the urls to be deleted as part of a next flush operation.
*
* @var array
*/
private $toDelete = [];
public static function reset(): void
{
static::$queueService = null;
}
/**
* Force inject queue service
* Used for unit tests only to cover edge cases where Injector doesn't cover
*
*
* @param QueuedJobService $service
*/
public static function setQueueService(QueuedJobService $service): void
{
static::$queueService = $service;
}
/**
* @return array
*/
public function getToUpdate()
{
return $this->toUpdate;
}
/**
* @return array
*/
public function getToDelete()
{
return $this->toDelete;
}
/**
* @param array $toUpdate
* @return $this
*/
public function setToUpdate($toUpdate)
{
$this->toUpdate = $toUpdate;
return $this;
}
/**
* @param array $toDelete
* @return $this
*/
public function setToDelete($toDelete)
{
$this->toDelete = $toDelete;
return $this;
}
/**
* @param SiteTree|SiteTreePublishingEngine|null $original
*/
public function onAfterPublishRecursive(&$original)
{
// If the site tree has been "reorganised" (ie: the parentID has changed)
// then this is the equivalent of an un-publish and publish as far as the
// static publisher is concerned
if ($original && (
// Intentionally using loose comparison as ParentID may contain either string or integer
$original->ParentID != $this->getOwner()->ParentID
|| $original->URLSegment !== $this->getOwner()->URLSegment
)
) {
$context = [
'action' => 'unpublish',
];
$original->collectChanges($context);
$original->flushChanges();
}
$context = [
'action' => 'publish',
];
$this->collectChanges($context);
$this->flushChanges();
}
public function onBeforeUnpublish()
{
$context = [
'action' => 'unpublish',
];
$this->collectChanges($context);
}
public function onAfterUnpublish()
{
$this->flushChanges();
}
/**
* Collect all changes for the given context.
*
* @param array $context
*/
public function collectChanges($context)
{
Environment::increaseMemoryLimitTo();
Environment::increaseTimeLimitTo();
if ($this->getOwner()->hasExtension(PublishableSiteTree::class)
|| $this->getOwner() instanceof StaticPublishingTrigger
) {
$toUpdate = $this->getOwner()->objectsToUpdate($context);
$this->setToUpdate($toUpdate);
$toDelete = $this->getOwner()->objectsToDelete($context);
$this->setToDelete($toDelete);
}
}
/**
* Execute URL deletions, enqueue URL updates.
*/
public function flushChanges()
{
$queueService = static::$queueService ?? QueuedJobService::singleton();
if (!empty($this->toUpdate)) {
/** @var UrlBundleInterface $urlService */
$urlService = Injector::inst()->create(UrlBundleInterface::class);
foreach ($this->toUpdate as $item) {
$urls = $item->urlsToCache();
ksort($urls);
$urls = array_keys($urls);
$urlService->addUrls($urls);
}
$jobs = $urlService->getJobsForUrls(GenerateStaticCacheJob::class, 'Building URLs', $this->owner);
foreach ($jobs as $job) {
$queueService->queueJob($job);
}
$this->toUpdate = [];
}
if (!empty($this->toDelete)) {
/** @var UrlBundleInterface $urlService */
$urlService = Injector::inst()->create(UrlBundleInterface::class);
foreach ($this->toDelete as $item) {
$urls = $item->urlsToCache();
ksort($urls);
$urls = array_keys($urls);
$urlService->addUrls($urls);
}
$jobs = $urlService->getJobsForUrls(DeleteStaticCacheJob::class, 'Purging URLs', $this->owner);
foreach ($jobs as $job) {
$queueService->queueJob($job);
}
$this->toDelete = [];
}
}
}