-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http-routing-bundle,testing-bundle): multiple url prefixes can t…
…rigger early route-loading Fix: #164
- Loading branch information
1 parent
7a34a60
commit b95f50c
Showing
16 changed files
with
795 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Snicco\Bundle\HttpRouting; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Snicco\Component\StrArr\Arr; | ||
use Snicco\Component\StrArr\Str; | ||
use Webmozart\Assert\Assert; | ||
|
||
use function array_map; | ||
use function is_string; | ||
use function ltrim; | ||
|
||
/** | ||
* @psalm-internal Snicco | ||
*/ | ||
final class ApiRequestDetector | ||
{ | ||
/** | ||
* @var non-empty-string[] | ||
*/ | ||
private array $api_prefixes; | ||
|
||
/** | ||
* @param non-empty-string[]|non-empty-string $early_route_prefixes | ||
*/ | ||
public function __construct($early_route_prefixes) | ||
{ | ||
$early_route_prefixes = Arr::toArray($early_route_prefixes); | ||
|
||
Assert::allNotEq($early_route_prefixes, '/'); | ||
|
||
$early_route_prefixes = array_map(fn (string $prefix) => '/' . ltrim($prefix, '/'), $early_route_prefixes); | ||
|
||
$this->api_prefixes = $early_route_prefixes; | ||
} | ||
|
||
/** | ||
* @param string|ServerRequestInterface $path_or_request | ||
*/ | ||
public function isAPIRequest($path_or_request): bool | ||
{ | ||
$path = is_string($path_or_request) ? $path_or_request : $path_or_request->getUri() | ||
->getPath(); | ||
|
||
foreach ($this->api_prefixes as $prefix) { | ||
if (Str::startsWith($path, $prefix)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.