-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorPageCommandController.php
More file actions
174 lines (147 loc) · 6.48 KB
/
ErrorPageCommandController.php
File metadata and controls
174 lines (147 loc) · 6.48 KB
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
<?php
declare(strict_types=1);
namespace Netlogix\ErrorHandler\Command;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\StreamWrapper;
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePoint;
use Neos\ContentRepository\Core\Feature\SubtreeTagging\Dto\SubtreeTag;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAddress;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\Core\Bootstrap;
use Neos\Neos\Domain\Model\Site;
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\FrontendRouting\Exception\NodeNotFoundException;
use Neos\Neos\FrontendRouting\NodeUriBuilderFactory;
use Neos\Neos\FrontendRouting\Options;
use Neos\Neos\FrontendRouting\SiteDetection\SiteDetectionResult;
use Neos\Utility\Files;
use Netlogix\ErrorHandler\Configuration\ErrorHandlerConfiguration;
use Netlogix\ErrorHandler\Service\ActionRequestFactory;
use Netlogix\ErrorHandler\Service\DestinationResolver;
use Psr\Http\Message\UriInterface;
use RuntimeException;
use Symfony\Component\Yaml\Yaml;
use function dirname;
use function fopen;
use function is_dir;
use function stream_copy_to_stream;
/**
* @Flow\Scope("singleton")
*/
class ErrorPageCommandController extends CommandController
{
#[Flow\Inject]
protected Bootstrap $bootstrap;
#[Flow\Inject]
protected ErrorHandlerConfiguration $configuration;
#[Flow\Inject]
protected DestinationResolver $destinationResolver;
#[Flow\Inject]
protected SiteRepository $siteRepository;
#[Flow\Inject]
protected NodeUriBuilderFactory $nodeUriBuilderFactory;
#[Flow\Inject]
protected ContentRepositoryRegistry $contentRepositoryRegistry;
#[Flow\Inject]
protected ActionRequestFactory $actionRequestFactory;
/**
* Generate Error Pages for configured Sites
*
* @param bool $verbose
* @throws \Exception
*/
public function generateCommand(bool $verbose = false)
{
$client = new Client([
'verify' => $this->bootstrap->getContext()->isProduction()
]);
$hadError = false;
foreach ($this->configuration->getConfiguration() as $siteNodeName => $configurations) {
foreach ($configurations as $configuration) {
$site = $this->siteRepository->findOneByNodeName($siteNodeName);
assert($site instanceof Site);
if (!$site->isOnline()) {
$verbose && $this->outputLine('Site %s is not online', [$siteNodeName]);
continue;
}
try {
$requestUri = $this->getSiteUri($site, $configuration);
} catch (\Exception $e) {
$verbose && $this->outputLine('Could not resolve Error Page Uri for %s, "%s"', [$siteNodeName, $e->getMessage()]);
continue;
}
$verbose && $this->outputLine('Downloading Error Page for %s from %s', [$siteNodeName, $requestUri]);
try {
$response = $client->get($requestUri);
} catch (\Exception $e) {
$hadError = true;
$this->outputLine('Could not fetch Error Page for %s, "%s"', [$siteNodeName, $e->getMessage()]);
continue;
}
$destination = $this->destinationResolver->getDestinationForConfiguration($configuration, $siteNodeName);
$directory = dirname($destination);
if (!is_dir($directory)) {
try {
Files::createDirectoryRecursively($directory);
} catch (\Exception $e) {
$hadError = true;
$this->outputLine('Could not create target directory for %s, "%s"', [$siteNodeName, $e->getMessage()]);
continue;
}
}
$verbose && $this->outputLine('Saving Error Page for %s to %s', [$siteNodeName, $destination]);
$file = fopen($destination, 'w+');
stream_copy_to_stream(StreamWrapper::getResource($response->getBody()), $file);
}
}
if ($hadError) {
$this->sendAndExit(1);
}
}
/**
* Export current error page configuration in YAML format.
* This combines both, Node Type based settings, and YAML based settings.
*
* @param bool $verbose
* @throws \Exception
*/
public function showConfigurationCommand(): void
{
$result = [
'Netlogix' => [
'ErrorHandler' => [
'pages' => $this->configuration->getConfiguration()
]
]
];
$this->output(Yaml::dump($result, 10, 2));
}
protected function getSiteUri(Site $site, array $configuration): UriInterface
{
$domain = $site->getPrimaryDomain();
if (!$domain) {
throw new RuntimeException(sprintf('Site %s has no primary domain', $site->getNodeName()), 1708944032);
}
$contentRepository = $this->contentRepositoryRegistry->get($site->getConfiguration()->contentRepositoryId);
$contentGraph = $contentRepository->getContentGraph(WorkspaceName::fromString(WorkspaceName::WORKSPACE_NAME_LIVE));
$nodeAggregate = $contentGraph->findNodeAggregateById(NodeAggregateId::fromString(substr($configuration['source'], 1)));
$dimensionSpacePoint = DimensionSpacePoint::fromJsonString(json_encode($configuration['dimensions']));
$source = $nodeAggregate->getNodeByCoveredDimensionSpacePoint($dimensionSpacePoint);
if (!$this->isNodeVisible($source)) {
throw new NodeNotFoundException('Node ' . $source->aggregateId . ' is not visible', 1552492532);
}
$siteDetectionResult = SiteDetectionResult::create($site->getNodeName(), $contentRepository->id);
$actionRequest = $this->actionRequestFactory->buildActionRequest($siteDetectionResult);
$nodeUriBuilder = $this->nodeUriBuilderFactory->forActionRequest($actionRequest);
return $nodeUriBuilder->uriFor(NodeAddress::fromNode($source), Options::createForceAbsolute());
}
protected function isNodeVisible(Node $node): bool
{
return !$node->tags->contain(SubtreeTag::disabled());
}
}