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]: Allow multiple distinct url prefixes to act as "early routes" #164

Closed
1 task done
calvinalkan opened this issue Oct 31, 2022 · 2 comments
Closed
1 task done
Assignees
Labels
feature New feature or request released

Comments

@calvinalkan
Copy link
Member

Thanks for taking the time to fill out this feature request!"

  • I have searched the issue tracker for similar requests

Which @snicco/* package(s) are relevant/related of the feature request?

http-routing-bundle

Description

Currently, the HttpKernelRunner class takes a single "api" prefix to determine if a route should be loaded early.

 private function isApiRequest(ServerRequestInterface $request): bool
    {
        return $this->api_prefix && Str::startsWith($request->getUri()->getPath(), $this->api_prefix);
   }

It would be good to allow multiple distinct prefixes from acting as "API" routes that should be loaded early in the WP loading process.

I.E.

  • /my-plugin/rest-api
  • /my-plugin/pages

Proposed solution

Change the constructor arguments of HttpKernelRunner to accept string|string[]

/**
 * @param non-empty-string|non-empty-string[]|null $api_prefix
*/
public function __construct(
        HttpKernel $http_kernel,
        ServerRequestCreator $request_creator,
        EventDispatcherInterface $event_dispatcher,
        ResponseEmitter $emitter,
        StreamFactoryInterface $stream_factory,
        ? $api_prefix
    ) {
        $this->http_kernel = $http_kernel;
        $this->request_creator = $request_creator;
        $this->event_dispatcher = $event_dispatcher;
        $this->emitter = $emitter;
        $this->stream_factory = $stream_factory;

        if (null !== $api_prefix) {
            $api_prefix = '/' . ltrim($api_prefix, '/');
        }

        $this->api_prefix = $api_prefix;
}

Alternatives

Currently the above is only possible by structuring URLs like so:

  • /my-plugin/EARLY/rest-api
  • /my-plugin/EARLY/pages

(EARLY is some shared "sub" prefix that is currently needed")

@calvinalkan calvinalkan added the feature New feature or request label Oct 31, 2022
@calvinalkan calvinalkan self-assigned this Oct 31, 2022
calvinalkan added a commit to calvinalkan/snicco that referenced this issue Nov 13, 2022
Instead of one fixed "api prefix", we now allow an array of prefixes.
If one prefix matches the current request path the request will
be dispatched as an api request early in the WP loading cycle.

Fix: snicco#164
@calvinalkan
Copy link
Member Author

To clarify,

What we want to achieve here is that:

/my-plugin/rest-api/*
/my-plugin/pages/*

can be loaded early as API-routes

while /my-plugin/shared/*

can be loaded late.

This is currently not possible.

calvinalkan added a commit to calvinalkan/snicco that referenced this issue Dec 6, 2022
Instead of one fixed "api prefix", we now allow an array of prefixes.
If one prefix matches the current request path the request will
be dispatched as an api request early in the WP loading cycle.

Fix: snicco#164
@snicco-bot
Copy link
Collaborator

🎉 This issue has been resolved in version 1.5.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request released
Projects
None yet
Development

No branches or pull requests

2 participants