forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
[Routing] [HttpKernel] [FrameworkBundle] Static Site Generation #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kocal
wants to merge
1
commit into
7.4
Choose a base branch
from
static-site-generation
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+804
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Symfony/Bundle/FrameworkBundle/Command/StaticSiteGenerationGenerateCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Attribute\Option; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\HttpKernel\Exception\RuntimeException; | ||
use Symfony\Component\HttpKernel\StaticSiteGeneration\StaticPageDumperInterface; | ||
use Symfony\Component\HttpKernel\StaticSiteGeneration\StaticPagesGenerator; | ||
use Symfony\Component\Routing\StaticSiteGeneration\StaticPageUrisProviderInterface; | ||
|
||
/** | ||
* @author Thomas Bibaut <[email protected]> | ||
*/ | ||
#[AsCommand(name: 'static-site-generation:generate', description: 'Generates static pages')] | ||
final class StaticSiteGenerationGenerateCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly StaticPagesGenerator $staticPagesGenerator, | ||
private readonly StaticPageDumperInterface $staticPageDumper, | ||
private readonly StaticPageUrisProviderInterface $staticPageUrisProvider, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function __invoke( | ||
SymfonyStyle $io, | ||
#[Option(description: 'A pattern to filter the pages to generate')] ?string $filterPattern = null, | ||
#[Option(description: 'Do not dump pages')] bool $dryRun = false, | ||
): int { | ||
$successful = true; | ||
|
||
foreach ($this->staticPageUrisProvider->provide() as $uri) { | ||
if (null !== $filterPattern && !preg_match($filterPattern, $uri)) { | ||
continue; | ||
} | ||
|
||
try { | ||
['content' => $content, 'format' => $format] = $this->staticPagesGenerator->generate($uri); | ||
|
||
if (false === $dryRun) { | ||
$this->staticPageDumper->dump($uri, $content, $format); | ||
} | ||
|
||
$io->info(\sprintf('Generated static page for URI "%s"', $uri)); | ||
} catch (RuntimeException $exception) { | ||
$io->error(\sprintf('Generating page for URI "%s" failed : %s', $uri, $exception->getMessage())); | ||
$successful = false; | ||
} | ||
} | ||
|
||
return $successful ? Command::SUCCESS : Command::FAILURE; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Symfony/Bundle/FrameworkBundle/Resources/config/http_kernel.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Symfony\Component\HttpKernel\StaticSiteGeneration\FilesystemStaticPageDumper; | ||
use Symfony\Component\HttpKernel\StaticSiteGeneration\StaticPageDumperInterface; | ||
use Symfony\Component\HttpKernel\StaticSiteGeneration\StaticPagesGenerator; | ||
|
||
return static function (ContainerConfigurator $container) { | ||
$container->services() | ||
->set('http_kernel.static_site.pages_generator', StaticPagesGenerator::class) | ||
->args([ | ||
service('http_kernel'), | ||
service('routing.static_site.pages_uri_provider'), | ||
]) | ||
|
||
->set('http_kernel.static_site.page_dumper.filesystem', FilesystemStaticPageDumper::class) | ||
->args([ | ||
param('kernel.project_dir'), | ||
]) | ||
->alias('http_kernel.static_site.page_dumper', 'http_kernel.static_site.page_dumper.filesystem') | ||
->alias(StaticPageDumperInterface::class, 'http_kernel.static_site.page_dumper') | ||
; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Symfony/Component/HttpKernel/Exception/RuntimeException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Exception; | ||
|
||
class RuntimeException extends \RuntimeException | ||
{ | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Symfony/Component/HttpKernel/StaticSiteGeneration/FilesystemStaticPageDumper.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\StaticSiteGeneration; | ||
|
||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
/** | ||
* @author Thomas Bibaut <[email protected]> | ||
*/ | ||
final class FilesystemStaticPageDumper implements StaticPageDumperInterface | ||
{ | ||
private ?Filesystem $filesystem = null; | ||
|
||
public function __construct( | ||
private string $projectDir, | ||
) { | ||
} | ||
|
||
public function dump(string $uri, string $content, ?string $format = null): void | ||
{ | ||
$fileName = '/' === $uri ? 'index.html' : $uri; | ||
|
||
if ($format && !str_ends_with($uri, '.'.$format)) { | ||
$fileName = \sprintf('%s.%s', $uri, $format); | ||
} | ||
|
||
$staticPagesDirectory = \sprintf('%s/%s/static-pages', $this->projectDir, $this->getPublicDirectory()); | ||
|
||
$this->filesystem ??= new Filesystem(); | ||
$this->filesystem->dumpFile(\sprintf('%s/%s', $staticPagesDirectory, $fileName), $content); | ||
} | ||
|
||
private function getPublicDirectory(): string | ||
{ | ||
$defaultPublicDir = 'public'; | ||
$composerFilePath = \sprintf('%s/composer.json', $this->projectDir); | ||
|
||
if (!file_exists($composerFilePath)) { | ||
return $defaultPublicDir; | ||
} | ||
|
||
$this->filesystem ??= new Filesystem(); | ||
$composerConfig = json_decode($this->filesystem->readFile($composerFilePath), true, flags: \JSON_THROW_ON_ERROR); | ||
|
||
return $composerConfig['extra']['public-dir'] ?? $defaultPublicDir; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Symfony/Component/HttpKernel/StaticSiteGeneration/StaticPageDumperInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\StaticSiteGeneration; | ||
|
||
/** | ||
* Dump a static page content into a storage. | ||
* | ||
* @author Thomas Bibaut <[email protected]> | ||
*/ | ||
interface StaticPageDumperInterface | ||
{ | ||
public function dump(string $uri, string $content, ?string $format): void; | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Symfony/Component/HttpKernel/StaticSiteGeneration/StaticPagesGenerator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\StaticSiteGeneration; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\RuntimeException; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\TerminableInterface; | ||
|
||
/** | ||
* @author Thomas Bibaut <[email protected]> | ||
*/ | ||
final readonly class StaticPagesGenerator | ||
{ | ||
public function __construct( | ||
private HttpKernelInterface $kernel, | ||
) { | ||
} | ||
|
||
/** | ||
* Generate page content for URI. | ||
* | ||
* @return array{content: string, format: ?string} | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
public function generate(string $uri): array | ||
{ | ||
$request = Request::create($uri); | ||
try { | ||
$response = $this->kernel->handle($request, HttpKernelInterface::MAIN_REQUEST); | ||
|
||
if ($this->kernel instanceof TerminableInterface) { | ||
$this->kernel->terminate($request, $response); | ||
} | ||
} catch (\Exception $e) { | ||
throw new RuntimeException(\sprintf('Cannot generate page for URI "%s".', $uri), $e->getCode(), $e); | ||
} | ||
|
||
if (Response::HTTP_OK !== $response->getStatusCode()) { | ||
throw new RuntimeException(\sprintf('Expected URI "%s" to return status code 200, got %d.', $uri, $response->getStatusCode())); | ||
} | ||
|
||
return [ | ||
'content' => $response->getContent(), | ||
'format' => $request->getFormat($response->headers->get('Content-Type')), | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.