Skip to content

Instantly share code, notes, and snippets.

@sorenmalling
Created July 8, 2022 11:20
Show Gist options
  • Save sorenmalling/0420a7afdf9b71a8ddb6a943459df5b1 to your computer and use it in GitHub Desktop.
Save sorenmalling/0420a7afdf9b71a8ddb6a943459df5b1 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace Dafis\Application\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Utility\MathUtility;
class CompanyIdentifierResolver implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$path = ltrim($request->getUri()->getPath(), '/');
if (empty($path)) {
return $handler->handle($request);
}
$pathParts = explode('/', $path);
$companyCandidate = $pathParts[0];
if (MathUtility::canBeInterpretedAsInteger($companyCandidate)) {
$request = $request->withAttribute('company_id', (int) $companyCandidate);
$uri = $request->getUri();
array_shift($pathParts);
$uri = $uri->withPath('/' . implode('/', $pathParts));
$request = $request->withUri($uri);
}
return $handler->handle($request);
}
}
<?php
return [
'frontend' => [
'dafis/application/company-identifier-resolver' => [
'target' => \Dafis\Application\Middleware\CompanyIdentifierResolver::class,
'before' => [
'typo3/cms-frontend/site'
]
]
]
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment