Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Dynamic resource uri host (baseUri) for CLI #3334

Open
mhsdesign opened this issue Mar 21, 2024 · 0 comments
Open

FEATURE: Dynamic resource uri host (baseUri) for CLI #3334

mhsdesign opened this issue Mar 21, 2024 · 0 comments

Comments

@mhsdesign
Copy link
Member

mhsdesign commented Mar 21, 2024

Building a resource uri with the default configuration will crash on CLI:

No base URI could be provided.
This probably means a call was made outside of an HTTP request and a base
URI was neither configured nor specified as $fallbackRequest.

The entry point ResourceManager::getPublicPackageResourceUri
calls TargetInterface::getPublicStaticResourceUri
which will use the configured target like localWebDirectoryStaticResourcesTarget
which in case of the FileSystemTarget and FileSystemSymlinkTarget
will call the BaseUriProvider::getConfiguredBaseUriOrFallbackToCurrentRequest
which uses $bootstrap->getActiveRequestHandler->getHttpRequest which is not possible on CLI.

Possible workarounds include:

To actually fix this issue we should pass the current base uri to the target:

interface TargetInterface
{
    public function getPublicStaticResourceUri(string $relativePathAndFilename, UriInterface $baseUri): string;

    public function getPublicPersistentResourceUri(PersistentResource $resource, UriInterface $baseUri): string;
    
    // ...
}

The methods are currently only used by the resource manager Neos & Flow.
And there we could allow a backwards compatible nullable parameter which will default to the current request:

class ResourceManager
{
    public function getPublicPackageResourceUri(string $packageKey, string $relativePathAndFilename, ?UriInterface $baseUri = null): string
    {
        $baseUri ??= $this->generateBaseUriFromHttpRequest();
        // ...
    }

    public function getPublicPersistentResourceUri(PersistentResource $resource, ?UriInterface $baseUri = null)
    {
        $baseUri ??= $this->generateBaseUriFromHttpRequest();
        // ...
    }

    private function generateBaseUriFromHttpRequest(): ?UriInterface
    {
        // copied from the base uri provider without `Neos.Flow.http.baseUri` support
        $activeRequestHandler = $this->bootstrap->getActiveRequestHandler();
        if (!$activeRequestHandler instanceof HttpRequestHandlerInterface) {
            return new Uri();
        }
        $request = $activeRequestHandler->getHttpRequest();
        return RequestInformationHelper::generateBaseUri($request);
    }
}

(i left out the demonstration for ResourceManager::getPublicPersistentResourceUriByHash as its unused)

In Fusion we could start inside our prototypes to call getPublicPersistentResourceUri with the base uri of the current
request and thus make the life a little easier ;)

Alternative fixes

  1. Add a context switcher like BaseUriProvider::applyBaseUriToClosure($newBaseUri, fn () => $resourcemanger->...(...))
  2. Add an environment variable like FLOW_BASE_URI see FEATURE: Make base URI configuration obsolete #3002
    • (like option 1 but less hacky to the eye, as it could be specified from outside flow ^^)
  3. Use empty base uri (host relative) and dont crash BUGFIX: Prevent getPublicStaticResourceUri from crashing in cli con… #3314
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant