Skip to content

Commit

Permalink
Properly respect router.request_context settings (#444)
Browse files Browse the repository at this point in the history
* Properly respect router.request_context settings

* Fixed failing tests
  • Loading branch information
norberttech authored Feb 10, 2024
1 parent c9a653f commit c400d5f
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 494 deletions.
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@
"php": "~8.1 || ~8.2 || ~8.3",
"ext-json": "*",
"norberttech/symfony-process-executor": "^2.0.4",
"symfony/browser-kit": "^4.4|^5.0|^6.0|^7.0",
"symfony/dependency-injection": "^4.4|^5.0|^6.0|^7.0",
"symfony/http-kernel": "^4.4|^5.0|^6.0|^7.0"
"symfony/browser-kit": "^5.4|^6.4|^7.0",
"symfony/dependency-injection": "^5.4|^6.4|^7.0",
"symfony/http-kernel": "^5.4|^6.4|^7.0"
},
"require-dev": {
"doctrine/annotations": "^1.11.1 || ^2.0.0",
"symfony/dotenv": "^5.4|^6",
"symfony/framework-bundle": "^5.4|^6",
"symfony/dotenv": "^5.4|^6.4|^7.0",
"symfony/framework-bundle": "^5.4|^6.4|^7.0",
"symfony/maker-bundle": "^1.36.4",
"symfony/stopwatch": "^5.4|^6",
"symfony/stopwatch": "^5.4|^6.4|^7.0",
"symfony/twig-pack": "^1.0",
"symfony/web-profiler-bundle": "^5.4|^6",
"symfony/yaml": "^5.4|^6"
"symfony/web-profiler-bundle": "^5.4|^6.4|^7.0",
"symfony/yaml": "^5.4|^6.4|^7.0",
"nikic/php-parser": "^4.18"
},
"config": {
"optimize-autoloader": true,
Expand Down
446 changes: 214 additions & 232 deletions composer.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
use NorbertTech\StaticContentGeneratorBundle\Content\Source;
use NorbertTech\StaticContentGeneratorBundle\Content\Transformer;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RouterInterface;

final class HttpKernelTransformer implements Transformer
{
private HttpKernelInterface $kernel;
private KernelInterface $kernel;

private RouterInterface $router;

public function __construct(HttpKernelInterface $kernel, RouterInterface $router)
public function __construct(KernelInterface $kernel, RouterInterface $router)
{
$this->kernel = $kernel;
$this->router = $router;
Expand All @@ -26,7 +26,16 @@ public function __construct(HttpKernelInterface $kernel, RouterInterface $router
public function transform(Source $source) : Content
{
$kernelBrowser = new HttpKernelBrowser($this->kernel);
$kernelBrowser->request('GET', $path = $this->router->generate($source->routerName(), $source->parameters()));
$kernelBrowser->request(
'GET',
$path = $this->router->generate($source->routerName(), $source->parameters()),
[],
[],
[
'HTTP_HOST' => $this->kernel->getContainer()->getParameter('router.request_context.host') ?? 'localhost',
'HTTPS' => $this->kernel->getContainer()->getParameter('router.request_context.scheme') === 'https',
]
);

if ($kernelBrowser->getResponse()->getStatusCode() > 299) {
throw new \RuntimeException('Can\'t generate static content for route ' . $source->routerName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function test_generating_content_from_all_routes() : void
$this->assertFileExists(self::$kernel->getContainer()->getParameter('static_content_generator.output_directory') . '/named/route/index.html');
$this->assertFileExists(self::$kernel->getContainer()->getParameter('static_content_generator.output_directory') . '/version/1.x/index.html');
$this->assertFileExists(self::$kernel->getContainer()->getParameter('static_content_generator.output_directory') . '/parametrized/first-param/second-param/index.html');

$this->assertStringContainsString(
self::$kernel->getContainer()->getParameter('router.request_context.scheme') . '://' . self::$kernel->getContainer()->getParameter('router.request_context.host'),
\file_get_contents(self::$kernel->getContainer()->getParameter('static_content_generator.output_directory') . '/index.html')
);
}

public function test_generating_content_from_specific_route() : void
Expand Down
29 changes: 26 additions & 3 deletions tests/fixtures/project/config/routes/annotations.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
controllers:
resource: ../../src/Controller/
type: annotation
index:
path: /
controller: FixtureProject\Controller\StaticRoutesController::index

index_html:
path: /index.html
controller: FixtureProject\Controller\StaticRoutesController::indexHtml

version_1_x:
path: /version/1.x
controller: FixtureProject\Controller\StaticRoutesController::versionHtml

api:
path: /api.json
controller: FixtureProject\Controller\StaticRoutesController::apiJson

api_xml:
path: /api.xml
controller: FixtureProject\Controller\StaticRoutesController::apiXml

named_route:
path: /named/route
controller: FixtureProject\Controller\StaticRoutesController::namedRoute

parametrized_route:
path: /parametrized/{param1}/{param2}
controller: FixtureProject\Controller\StaticRoutesController::withParameters
2 changes: 2 additions & 0 deletions tests/fixtures/project/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
router.request_context.scheme: 'https'
router.request_context.host: 'static-content-generator.wip'

services:
# default configuration for services in *this* file
Expand Down
22 changes: 0 additions & 22 deletions tests/fixtures/project/src/Controller/StaticRoutesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,41 @@

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class StaticRoutesController extends AbstractController
{
/**
* @Route("/", name="index")
*/
public function index() : Response
{
return $this->render('index.html.twig');
}

/**
* @Route("index.html", name="index_html")
*/
public function indexHtml() : Response
{
return $this->render('index.html.twig');
}

/**
* @Route("/version/1.x", name="version_1_x")
*/
public function versionHtml() : Response
{
return $this->render('index.html.twig');
}

/**
* @Route("api.json", name="api")
*/
public function apiJson() : Response
{
return $this->json([
'content' => 'json',
]);
}

/**
* @Route("api.xml", name="api_xml")
*/
public function apiXML() : Response
{
return $this->render('api.xml.twig');
}

/**
* @Route("/named/route", name="named_route")
*/
public function namedRoute() : Response
{
return $this->render('named/route.html.twig');
}

/**
* @Route("/parametrized/{param1}/{param2}", name="parametrized_route")
*/
public function withParameters(string $param1, string $param2) : Response
{
return $this->render('parametrized.html.twig', ['param1' => $param1, 'param2' => $param2]);
Expand Down
45 changes: 4 additions & 41 deletions tests/fixtures/project/src/Kernel.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,18 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace FixtureProject;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

class Kernel extends BaseKernel
{
use MicroKernelTrait;

private const CONFIG_EXTS = '.{php,xml,yaml,yml}';

public function registerBundles() : iterable
{
$contents = require $this->getProjectDir() . '/config/bundles.php';

foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class();
}
}
}

public function getProjectDir() : string
{
return \dirname(__DIR__);
}

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader) : void
{
$container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
$container->setParameter('container.dumper.inline_class_loader', \PHP_VERSION_ID < 70400 || $this->debug);
$container->setParameter('container.dumper.inline_factories', true);
$confDir = $this->getProjectDir() . '/config';

$loader->load($confDir . '/{packages}/*' . self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{packages}/' . $this->environment . '/*' . self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob');
}

protected function configureRoutes(RoutingConfigurator $routes) : void
{
$confDir = $this->getProjectDir() . '/config';

$routes->import($confDir . '/{routes}/' . $this->environment . '/*' . self::CONFIG_EXTS, 'glob');
$routes->import($confDir . '/{routes}/*' . self::CONFIG_EXTS, 'glob');
$routes->import($confDir . '/{routes}' . self::CONFIG_EXTS, 'glob');
return \realpath(__DIR__ . '/../');
}
}
1 change: 1 addition & 0 deletions tests/fixtures/project/templates/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

{% block content %}
This is content of index.html
<a id="index-url" href="{{ url('index') }}">index url</a>
{% endblock %}
2 changes: 1 addition & 1 deletion tools/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "aeon-php/static-content-generator-bundle",
"description": "Symfony Static Content Generator Bundle - Tools",
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.6.16",
"vimeo/psalm": "^5.0",
"phpstan/phpstan": "^1.1.2",
"infection/infection": "^0.26.0",
Expand Down
Loading

0 comments on commit c400d5f

Please sign in to comment.