-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
NodeUriBuilder.php
90 lines (79 loc) · 2.86 KB
/
NodeUriBuilder.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
<?php
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
declare(strict_types=1);
namespace Neos\Neos\FrontendRouting;
use GuzzleHttp\Psr7\Uri;
use Neos\Neos\FrontendRouting\NodeAddress;
use Neos\Flow\Http\Exception as HttpException;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\Mvc\Exception\NoMatchingRouteException;
use Neos\Flow\Mvc\Routing\Exception\MissingActionNameException;
use Neos\Flow\Mvc\Routing\UriBuilder;
use Psr\Http\Message\UriInterface;
/**
* Builds URIs to nodes, taking workspace (live / shared / user) into account.
* This class can also be used in order to render "preview" URLs to nodes
* that are not in the live workspace (in the Neos Backend and shared workspaces)
*/
final class NodeUriBuilder
{
private UriBuilder $uriBuilder;
protected function __construct(UriBuilder $uriBuilder)
{
$this->uriBuilder = $uriBuilder;
}
public static function fromRequest(ActionRequest $request): static
{
$uriBuilder = new UriBuilder();
$uriBuilder->setRequest($request);
return new static($uriBuilder);
}
public static function fromUriBuilder(UriBuilder $uriBuilder): static
{
return new static($uriBuilder);
}
/**
* Renders an URI for the given $nodeAddress
* If the node belongs to the live workspace, the public URL is generated
* Otherwise a preview URI is rendered (@see previewUriFor())
*
* Note: Shortcut nodes will are resolved in the RoutePartHandler thus the resulting URI will point
* to the shortcut target (node, asset or external URI)
*
* @param NodeAddress $nodeAddress
* @return UriInterface
* @throws NoMatchingRouteException | MissingActionNameException | HttpException
*/
public function uriFor(NodeAddress $nodeAddress): UriInterface
{
if (!$nodeAddress->isInLiveWorkspace()) {
return $this->previewUriFor($nodeAddress);
}
return new Uri($this->uriBuilder->uriFor('show', ['node' => $nodeAddress], 'Frontend\Node', 'Neos.Neos'));
}
/**
* Renders a stable "preview" URI for the given $nodeAddress
* A preview URI is used to display a node that is not public yet (i.e. not in a live workspace).
*
* @param NodeAddress $nodeAddress
* @return UriInterface
* @throws NoMatchingRouteException | MissingActionNameException | HttpException
*/
public function previewUriFor(NodeAddress $nodeAddress): UriInterface
{
return new Uri($this->uriBuilder->uriFor(
'preview',
['node' => $nodeAddress->serializeForUri()],
'Frontend\Node',
'Neos.Neos'
));
}
}