|
| 1 | +<?php |
| 2 | +namespace PackageFactory\AtomicFusion\PresentationObjects\Infrastructure; |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of the PackageFactory.AtomicFusion.PresentationObjects package. |
| 6 | + */ |
| 7 | + |
| 8 | +use Neos\ContentRepository\Domain\Projection\Content\TraversableNodeInterface; |
| 9 | +use Neos\ContentRepository\Domain\Service\Context as ContentContext; |
| 10 | +use Neos\Flow\Annotations as Flow; |
| 11 | +use Neos\Flow\Mvc\Controller\ControllerContext; |
| 12 | +use Neos\Flow\ResourceManagement\ResourceManager; |
| 13 | +use Neos\Flow\Http; |
| 14 | +use Neos\Media\Domain\Model\Asset; |
| 15 | +use Neos\Media\Domain\Model\AssetInterface; |
| 16 | +use Neos\Media\Domain\Repository\AssetRepository; |
| 17 | +use Neos\Neos\Service\LinkingService; |
| 18 | +use Neos\Flow\Mvc; |
| 19 | +use Neos\Flow\Core\Bootstrap; |
| 20 | + |
| 21 | +/** |
| 22 | + * The URI service |
| 23 | + */ |
| 24 | +final class UriService |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @Flow\Inject |
| 28 | + * @var ResourceManager |
| 29 | + */ |
| 30 | + protected $resourceManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * @Flow\Inject |
| 34 | + * @var LinkingService |
| 35 | + */ |
| 36 | + protected $linkingService; |
| 37 | + |
| 38 | + /** |
| 39 | + * @Flow\Inject |
| 40 | + * @var AssetRepository |
| 41 | + */ |
| 42 | + protected $assetRepository; |
| 43 | + |
| 44 | + /** |
| 45 | + * @Flow\Inject |
| 46 | + * @var Bootstrap |
| 47 | + */ |
| 48 | + protected $bootstrap; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ControllerContext |
| 52 | + */ |
| 53 | + protected $controllerContext; |
| 54 | + |
| 55 | + /** |
| 56 | + * @param TraversableNodeInterface $documentNode |
| 57 | + * @param bool $absolute |
| 58 | + * @return string |
| 59 | + * @throws Mvc\Routing\Exception\MissingActionNameException |
| 60 | + * @throws \Neos\Flow\Property\Exception |
| 61 | + * @throws \Neos\Flow\Security\Exception |
| 62 | + * @throws \Neos\Neos\Exception |
| 63 | + */ |
| 64 | + public function getNodeUri(TraversableNodeInterface $documentNode, bool $absolute = false): string |
| 65 | + { |
| 66 | + return $this->linkingService->createNodeUri($this->getControllerContext(), $documentNode, null, null, $absolute); |
| 67 | + } |
| 68 | + |
| 69 | + public function getResourceUri(string $packageKey, string $resourcePath): string |
| 70 | + { |
| 71 | + return $this->resourceManager->getPublicPackageResourceUri($packageKey, $resourcePath); |
| 72 | + } |
| 73 | + |
| 74 | + public function getAssetUri(AssetInterface $asset): string |
| 75 | + { |
| 76 | + return $this->resourceManager->getPublicPersistentResourceUri($asset->getResource()); |
| 77 | + } |
| 78 | + |
| 79 | + public function getDummyImageBaseUri(): string |
| 80 | + { |
| 81 | + $uriBuilder = $this->getControllerContext()->getUriBuilder(); |
| 82 | + |
| 83 | + return $uriBuilder->uriFor( |
| 84 | + 'image', |
| 85 | + [], |
| 86 | + 'dummyImage', |
| 87 | + 'Sitegeist.Kaleidoscope' |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + protected function getControllerContext(): ControllerContext |
| 92 | + { |
| 93 | + if (is_null($this->controllerContext)) { |
| 94 | + $requestHandler = $this->bootstrap->getActiveRequestHandler(); |
| 95 | + if ($requestHandler instanceof Http\RequestHandler) { |
| 96 | + $request = $requestHandler->getHttpRequest(); |
| 97 | + $response = $requestHandler->getHttpResponse(); |
| 98 | + } else { |
| 99 | + $request = Http\Request::createFromEnvironment(); |
| 100 | + $response = new Http\Response(); |
| 101 | + } |
| 102 | + $actionRequest = new Mvc\ActionRequest($request); |
| 103 | + $uriBuilder = new Mvc\Routing\UriBuilder(); |
| 104 | + $uriBuilder->setRequest($actionRequest); |
| 105 | + $this->controllerContext = new Mvc\Controller\ControllerContext( |
| 106 | + $actionRequest, |
| 107 | + $response, |
| 108 | + new Mvc\Controller\Arguments(), |
| 109 | + $uriBuilder |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + return $this->controllerContext; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param string $rawLinkUri |
| 118 | + * @param ContentContext $subgraph |
| 119 | + * @return string |
| 120 | + * @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException |
| 121 | + * @throws \Neos\Flow\Property\Exception |
| 122 | + * @throws \Neos\Flow\Security\Exception |
| 123 | + * @throws \Neos\Neos\Exception |
| 124 | + */ |
| 125 | + public function resolveLinkUri(string $rawLinkUri, ContentContext $subgraph): string |
| 126 | + { |
| 127 | + if (\mb_substr($rawLinkUri, 0, 7) === 'node://') { |
| 128 | + $nodeIdentifier = \mb_substr($rawLinkUri, 7); |
| 129 | + $node = $subgraph->getNodeByIdentifier($nodeIdentifier); |
| 130 | + $linkUri = $node ? $this->getNodeUri($node) : '#'; |
| 131 | + } elseif (\mb_substr($rawLinkUri, 0, 8) === 'asset://') { |
| 132 | + $assetIdentifier = \mb_substr($rawLinkUri, 8); |
| 133 | + /** @var Asset $asset */ |
| 134 | + $asset = $this->assetRepository->findByIdentifier($assetIdentifier); |
| 135 | + $linkUri = $this->getAssetUri($asset); |
| 136 | + } elseif (\mb_substr($rawLinkUri, 0, 8) === 'https://' || \mb_substr($rawLinkUri, 0, 7) === 'http://') { |
| 137 | + $linkUri = $rawLinkUri; |
| 138 | + } else { |
| 139 | + $linkUri = '#'; |
| 140 | + } |
| 141 | + |
| 142 | + return $linkUri; |
| 143 | + } |
| 144 | +} |
0 commit comments