-
Notifications
You must be signed in to change notification settings - Fork 175
/
Path.php
128 lines (109 loc) · 3.65 KB
/
Path.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
<?php
namespace Drupal\next\Plugin\Next\Revalidator;
use Drupal\Core\Form\FormStateInterface;
use Drupal\next\Event\EntityActionEvent;
use Drupal\next\Plugin\ConfigurableRevalidatorBase;
use Drupal\next\Plugin\RevalidatorInterface;
use Symfony\Component\HttpFoundation\Response;
/**
* Provides a revalidator for paths.
*
* @Revalidator(
* id = "path",
* label = "Path",
* description = "Path-based on-demand revalidation."
* )
*/
class Path extends ConfigurableRevalidatorBase implements RevalidatorInterface {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'revalidate_page' => NULL,
'additional_paths' => NULL,
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['revalidate_page'] = [
'#title' => $this->t('Revalidate page'),
'#description' => $this->t('Revalidate the page for the entity on update.'),
'#type' => 'checkbox',
'#default_value' => $this->configuration['revalidate_page'],
];
$form['additional_paths'] = [
'#type' => 'textarea',
'#title' => $this->t('Additional paths'),
'#default_value' => $this->configuration['additional_paths'],
'#description' => $this->t('Additional paths to revalidate on entity update. Enter one path per line. Example %example.', [
'%example' => '/blog',
]),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['revalidate_page'] = $form_state->getValue('revalidate_page');
$this->configuration['additional_paths'] = $form_state->getValue('additional_paths');
}
/**
* {@inheritdoc}
*/
public function revalidate(EntityActionEvent $event): bool {
$revalidated = FALSE;
$sites = $event->getSites();
if (!count($sites)) {
return FALSE;
}
$paths = [];
if (!empty($this->configuration['revalidate_page'])) {
$paths[] = $event->getEntityUrl();
}
if (!empty($this->configuration['additional_paths'])) {
$paths = array_merge($paths, array_map('trim', explode("\n", $this->configuration['additional_paths'])));
}
if (!count($paths)) {
return FALSE;
}
foreach ($paths as $path) {
foreach ($sites as $site) {
try {
$revalidate_url = $site->getRevalidateUrlForPath($path);
if (!$revalidate_url) {
throw new \Exception('No revalidate url set.');
}
if ($this->nextSettingsManager->isDebug()) {
$this->logger->notice('(@action): Revalidating path %path for the site %site. URL: %url', [
'@action' => $event->getAction(),
'%path' => $path,
'%site' => $site->label(),
'%url' => $revalidate_url->toString(),
]);
}
$response = $this->httpClient->request('GET', $revalidate_url->toString());
if ($response && $response->getStatusCode() === Response::HTTP_OK) {
if ($this->nextSettingsManager->isDebug()) {
$this->logger->notice('(@action): Successfully revalidated path %path for the site %site. URL: %url', [
'@action' => $event->getAction(),
'%path' => $path,
'%site' => $site->label(),
'%url' => $revalidate_url->toString(),
]);
}
$revalidated = TRUE;
}
}
catch (\Exception $exception) {
$this->logger->error($exception->getMessage());
$revalidated = FALSE;
}
}
}
return $revalidated;
}
}