forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Implement Static Site Generation (SSG) feature #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.3
Choose a base branch
from
static-site-generation
base: 7.3
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.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.4 | ||
--- | ||
|
||
* asd | ||
|
||
7.3 | ||
--- | ||
|
||
|
92 changes: 92 additions & 0 deletions
92
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,92 @@ | ||
<?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\Command\Command; | ||
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\HttpKernel\StaticSiteGeneration\StaticPagesGenerator; | ||
use Symfony\Component\HttpKernel\StaticSiteGeneration\StaticPageDumperInterface; | ||
use Symfony\Component\HttpKernel\Exception\RuntimeException; | ||
use Symfony\Component\Routing\StaticSiteGeneration\StaticUrisProviderInterface; | ||
|
||
/** | ||
* @author Thomas Bibaut <[email protected]> | ||
*/ | ||
#[AsCommand(name: 'static-site-generation:generate', description: 'Generates the static site')] | ||
final class StaticSiteGenerationGenerateCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly StaticPagesGenerator $staticPagesGenerator, | ||
private readonly StaticPageDumperInterface $staticPageDumper, | ||
private readonly StaticUrisProviderInterface $staticUrisProvider, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Generates static pages') | ||
->addOption( | ||
'filterPattern', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. snake case |
||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'A pattern to filter the pages to generate', | ||
) | ||
->addOption( | ||
'dryRun', | ||
null, | ||
InputOption::VALUE_NONE, | ||
'Do not dump pages', | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$filterPattern = $input->getOption('filterPattern'); | ||
$dryRun = $input->getOption('dryRun'); | ||
|
||
foreach ($this->staticUrisProvider->provide() as $uri) | ||
{ | ||
if (null !== $filterPattern && !preg_match($filterPattern, $uri)) { | ||
continue; | ||
} | ||
|
||
try { | ||
['content' => $content, 'format' => $format] = $this->staticPagesGenerator->generate($uri); | ||
} catch (RuntimeException $exception) { | ||
$io->error(sprintf('Generating page for URI "%s" failed : %s', $uri, $exception->getMessage())); | ||
|
||
// PROBLEM : even with -vvv we don't get the stack trace for this | ||
// @todo printTrace in SymfonyStyle ??? | ||
// @see src/Symfony/Component/Console/Application.php:899 | ||
// | ||
// if ($io->isVerbose()) { | ||
// $io->comment('Exception trace', OutputInterface::VERBOSITY_QUIET); | ||
// } | ||
} | ||
|
||
if (false === $dryRun) { | ||
$this->staticPageDumper->dump($uri, $content, $format); | ||
} | ||
|
||
// @todo output path ? | ||
$io->info(sprintf('Generated static page for URI "%s"', $uri)); | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.4 | ||
--- | ||
|
||
* asd | ||
|
||
|
||
7.3 | ||
--- | ||
|
||
|
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 readonly class FilesystemStaticPageDumper implements StaticPageDumperInterface | ||
{ | ||
private Filesystem $filesystem; | ||
|
||
public function __construct( | ||
private string $projectDir, | ||
) { | ||
$this->filesystem = new Filesystem(); | ||
} | ||
|
||
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->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; | ||
} | ||
|
||
$composerConfig = json_decode($this->filesystem->readFile($composerFilePath), true, flags: \JSON_THROW_ON_ERROR); | ||
|
||
return $composerConfig['extra']['public-dir'] ?? $defaultPublicDir; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
<?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; | ||
|
||
/** | ||
* @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\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\TerminableInterface; | ||
use Symfony\Component\HttpKernel\Exception\RuntimeException; | ||
|
||
/** | ||
* @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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
invokable command