Skip to content

Commit

Permalink
feat: generate routes without /index.html by default (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarlovi authored Feb 25, 2022
1 parent cf71562 commit 6b0a43f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 52 deletions.
5 changes: 0 additions & 5 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ when@prod:
$stripParameters: '%sigwin_yassg.routes.strip_parameters%'
$urlGenerator: '@Sigwin\YASSG\Bridge\Symfony\Routing\Generator\FilenameUrlGenerator.inner'

router.request_context:
class: Symfony\Component\Routing\RequestContext
factory: [ '@Sigwin\YASSG\Bridge\Symfony\Routing\BuildRequestContextFactory', 'create' ]
shared: false

twig.extension.routing:
class: Symfony\Bridge\Twig\Extension\RoutingExtension
arguments:
Expand Down
28 changes: 21 additions & 7 deletions src/Bridge/Symfony/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,39 @@

namespace Sigwin\YASSG\Bridge\Symfony\Command;

use Sigwin\YASSG\Bridge\Symfony\Routing\BuildRequestContextFactory;
use Sigwin\YASSG\Generator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;

final class GenerateCommand extends Command
{
protected static $defaultName = 'yassg:generate';

private Generator $generator;
private BuildRequestContextFactory $contextFactory;
private UrlGeneratorInterface $urlGenerator;

public function __construct(Generator $generator, BuildRequestContextFactory $contextFactory)
public function __construct(Generator $generator, UrlGeneratorInterface $urlGenerator)
{
parent::__construct('yassg:generate');

$this->generator = $generator;
$this->contextFactory = $contextFactory;
$this->urlGenerator = $urlGenerator;
}

protected function configure(): void
{
$this
->setDescription('Generate the site')
->addArgument('url', InputArgument::REQUIRED, 'Base URL to generate for');
->addArgument('url', InputArgument::REQUIRED, 'Base URL to generate for')
->addOption('index-file', null, InputOption::VALUE_NONE, 'Add the index.html to generated routes');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -55,9 +58,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* @psalm-suppress UnnecessaryVarAnnotation Psalm's Symfony plugin solves this, but not PHPStan's
*/
$buildUrl = $input->getArgument('url');
$this->contextFactory->setBuildUrl($buildUrl);
$buildUrl = rtrim($buildUrl, '/');

$this->generator->generate($buildUrl, static function (Request $request, Response $response, string $path) use ($style, $buildUrl): void {
/**
* @phpstan-var bool $indexFile
* @psalm-suppress UnnecessaryVarAnnotation Psalm's Symfony plugin solves this, but not PHPStan's
*/
$indexFile = $input->getOption('index-file');

$context = RequestContext::fromUri($buildUrl);
$context->setParameter('index-file', $indexFile);
$this->urlGenerator->setContext($context);

$this->generator->generate(static function (Request $request, Response $response, string $path) use ($style, $buildUrl): void {
// TODO: this looks like a bug in Symfony (?)
$style->writeln(str_replace('http://localhost', $buildUrl, $request->getUri()));

if ($style->isDebug()) {
Expand Down
31 changes: 0 additions & 31 deletions src/Bridge/Symfony/Routing/BuildRequestContextFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ public function __construct(private UrlGeneratorInterface $urlGenerator, private
public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string
{
$this->stripParameters($this->stripParameters[$name] ?? [], $parameters);
if (($this->getContext()->getParameter('index-file') ?? false) === true) {
$parameters += ['_filename' => 'index.html'];
}

$url = $this->urlGenerator->generate($name, $parameters + ['_filename' => 'index.html'], $referenceType);
$url = $this->urlGenerator->generate($name, $parameters, $referenceType);
if (parse_url($url, \PHP_URL_QUERY) !== null) {
throw new \LogicException(sprintf('Query string found while generating route "%1$s", query strings are forbidden: %2$s', $name, $url));
}
Expand Down
17 changes: 9 additions & 8 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;

/**
* @internal
*/
final class Generator
{
private string $buildDir;
Expand All @@ -38,18 +40,17 @@ public function __construct(string $buildDir, Permutator $permutator, UrlGenerat
$this->filesystem = $filesystem;
}

public function generate(string $baseUrl, callable $callable): void
public function generate(callable $callable): void
{
// TODO: extract to factory
$this->urlGenerator->setContext(RequestContext::fromUri($baseUrl));
$indexFile = (bool) ($this->urlGenerator->getContext()->getParameter('index-file') ?? false);

foreach ($this->permutator->permute() as $routeName => $parameters) {
$this->dumpFile($callable, $this->urlGenerator->generate($routeName, $parameters + ['_filename' => 'index.html'], UrlGeneratorInterface::RELATIVE_PATH));
$this->dumpFile($callable, $this->urlGenerator->generate($routeName, $parameters + ($indexFile ? ['_filename' => 'index.html'] : []), UrlGeneratorInterface::RELATIVE_PATH), ! $indexFile);
}
$this->dumpFile($callable, '/404.html');
$this->dumpFile($callable, '/404.html', false);
}

private function dumpFile(callable $callable, string $url): void
private function dumpFile(callable $callable, string $url, bool $doesNotHaveIndexFile): void
{
$request = Request::create($url);
try {
Expand All @@ -62,7 +63,7 @@ private function dumpFile(callable $callable, string $url): void
if ($body === false) {
throw new \RuntimeException('No body in response');
}
$path = $this->buildDir.$request->getPathInfo();
$path = $this->buildDir.$request->getPathInfo().($doesNotHaveIndexFile ? '/index.html' : '');

$this->filesystem->dumpFile($path, $body);

Expand Down

0 comments on commit 6b0a43f

Please sign in to comment.