Skip to content

Instantly share code, notes, and snippets.

@jzawadzki
Last active January 22, 2021 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jzawadzki/9858ed12153b4adb3063a2a99f59f892 to your computer and use it in GitHub Desktop.
Save jzawadzki/9858ed12153b4adb3063a2a99f59f892 to your computer and use it in GitHub Desktop.
<?php
namespace App\Router;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\NoConfigurationException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
class OwnRouter implements WarmableInterface, ServiceSubscriberInterface, RouterInterface, RequestMatcherInterface
{
/**
* @var Router original
*/
private $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public function getRouteCollection()
{
return $this->router->getRouteCollection();
}
public function warmUp(string $cacheDir)
{
return $this->router->warmUp($cacheDir);
}
public static function getSubscribedServices()
{
return [
'routing.loader' => LoaderInterface::class,
];
}
public function setContext(RequestContext $context)
{
return $this->router->setContext($context);
}
public function getContext()
{
return $this->router->getContext();
}
public function matchRequest(Request $request)
{
return $this->router->matchRequest($request);
}
public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH)
{
// if it's not internal _profiler/_wdt routes
// (best would be to have a allow list of routes to change)
if(substr($name,0,1) !== '_'){
// then add additional parameter
$parameters['tenant'] = "5"; //you probably would like to take it from different service instead const ;)
}
return $this->router->generate($name, $parameters, $referenceType);
}
public function match(string $pathinfo)
{
return $this->router->match($pathinfo);
}
}
services:
...
App\Router\OwnRouter:
decorates: router
arguments: ['@.inner']
@jzawadzki
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment