Skip to content

Instantly share code, notes, and snippets.

@hacfi
Created July 22, 2021 12:15
Show Gist options
  • Save hacfi/cf8b58bf663e47ce56317986358f3f9f to your computer and use it in GitHub Desktop.
Save hacfi/cf8b58bf663e47ce56317986358f3f9f to your computer and use it in GitHub Desktop.
Symfony 5 Custom runtime to have custom request object
<?php
// public/index.php
use App\Kernel;
use App\SymfonyRuntime;
$_SERVER['APP_RUNTIME'] = SymfonyRuntime::class;
require_once dirname(__DIR__) . '/vendor/autoload_runtime.php';
$company = $_SERVER['APP_COMPANY'] ?? false;
if (!$company) {
$hostWithoutPort = \strtok($_SERVER['HTTP_HOST'], ':');
$host = \explode('.', $hostWithoutPort);
$company = \count($host) > 2 ? $host[0] : null;
}
return function (array $context) use ($company) {
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG'], $company);
};
<?php
// src/Kernel.php
declare(strict_types=1);
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
private ?string $company;
public function __construct(string $environment, bool $debug, ?string $company)
{
parent::__construct($environment, $debug);
$this->company = $company;
}
public function boot()
{
parent::boot();
}
protected function getKernelParameters()
{
$parameters = parent::getKernelParameters();
$parameters['kernel.company'] = $this->company;
return $parameters;
}
protected function configureContainer(ContainerConfigurator $container): void
{
$container->import('../config/{packages}/*.yaml');
$container->import('../config/{packages}/' . $this->environment . '/*.yaml');
if (is_file(\dirname(__DIR__) . '/config/services.yaml')) {
$container->import('../config/services.yaml');
$container->import('../config/{services}_' . $this->environment . '.yaml');
} else {
$container->import('../config/{services}.php');
}
}
protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->import('../config/{routes}/' . $this->environment . '/*.yaml');
$routes->import('../config/{routes}/*.yaml');
if (is_file(\dirname(__DIR__) . '/config/routes.yaml')) {
$routes->import('../config/routes.yaml');
} else {
$routes->import('../config/{routes}.php');
}
}
public function getCompany(): ?string
{
return $this->company;
}
}
<?php
src/Request.php
declare(strict_types=1);
namespace App;
use Symfony\Component\HttpFoundation\Request as BaseRequest;
class Request extends BaseRequest
{
private ?string $company;
public function getCompany(): ?string
{
return $this->company;
}
public function setCompany(?string $company): void
{
$this->company = $company;
}
}
<?php
// src/SymfonyRuntime.php
declare(strict_types=1);
namespace App;
use Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner;
use Symfony\Component\Runtime\RunnerInterface;
use Symfony\Component\Runtime\SymfonyRuntime as BaseSymfonyRuntime;
class SymfonyRuntime extends BaseSymfonyRuntime
{
public function getRunner(?object $application): RunnerInterface
{
if ($application instanceof Kernel) {
$request = Request::createFromGlobals();
$request->setCompany($application->getCompany());
return new HttpKernelRunner($application, $request);
}
return parent::getRunner($application);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment