-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[AssetMapper] add support for assets pre-compression #59020
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
Merged
Merged
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
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.3 | ||
--- | ||
|
||
* Add support for assets pre-compression | ||
|
||
7.2 | ||
--- | ||
|
||
|
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
63 changes: 63 additions & 0 deletions
63
src/Symfony/Component/AssetMapper/Command/CompressAssetsCommand.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,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\AssetMapper\Command; | ||
|
||
use Symfony\Component\AssetMapper\Compressor\CompressorInterface; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* Pre-compresses files to serve through a web server. | ||
* | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
*/ | ||
#[AsCommand(name: 'assets:compress', description: 'Pre-compresses files to serve through a web server')] | ||
final class CompressAssetsCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly CompressorInterface $compressor, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->addArgument('paths', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'The files to compress') | ||
->setHelp(<<<'EOT' | ||
The <info>%command.name%</info> command compresses the given file in Brotli, Zstandard and gzip formats. | ||
This is especially useful to serve pre-compressed files through a web server. | ||
|
||
The existing file will be kept. The compressed files will be created in the same directory. | ||
The extension of the compression format will be appended to the original file name. | ||
EOT | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$paths = $input->getArgument('paths'); | ||
foreach ($paths as $path) { | ||
$this->compressor->compress($path); | ||
} | ||
|
||
$io->success(\sprintf('File%s compressed successfully.', \count($paths) > 1 ? 's' : '')); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/AssetMapper/Compressor/BrotliCompressor.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,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\AssetMapper\Compressor; | ||
|
||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* Compresses a file using Brotli. | ||
* | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
*/ | ||
final class BrotliCompressor implements SupportedCompressorInterface | ||
{ | ||
use CompressorTrait; | ||
|
||
private const WRAPPER = 'compress.brotli'; | ||
private const COMMAND = 'brotli'; | ||
private const PHP_EXTENSION = 'brotli'; | ||
private const FILE_EXTENSION = 'br'; | ||
|
||
public function __construct( | ||
?string $executable = null, | ||
) { | ||
$this->executable = $executable; | ||
} | ||
|
||
/** | ||
* @return resource | ||
*/ | ||
private function createStreamContext() | ||
{ | ||
return stream_context_create(['brotli' => ['level' => BROTLI_COMPRESS_LEVEL_MAX]]); | ||
} | ||
|
||
private function compressWithBinary(string $path): void | ||
{ | ||
(new Process([$this->executable, '--best', '--force', "--output=$path.".self::FILE_EXTENSION, '--', $path]))->mustRun(); | ||
dunglas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/AssetMapper/Compressor/ChainCompressor.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,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\AssetMapper\Compressor; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Calls multiple compressors in a chain. | ||
* | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
*/ | ||
final class ChainCompressor implements CompressorInterface | ||
{ | ||
/** | ||
* @param CompressorInterface[] $compressors | ||
*/ | ||
public function __construct( | ||
private ?array $compressors = null, | ||
private readonly ?LoggerInterface $logger = null, | ||
) { | ||
} | ||
|
||
public function compress(string $path): void | ||
{ | ||
if (null === $this->compressors) { | ||
$this->compressors = []; | ||
foreach ([new BrotliCompressor(), new ZstandardCompressor(), new GzipCompressor()] as $compressor) { | ||
$unsupportedReason = $compressor->getUnsupportedReason(); | ||
if (null === $unsupportedReason) { | ||
$this->compressors[] = $compressor; | ||
} else { | ||
$this->logger?->warning($unsupportedReason); | ||
} | ||
} | ||
} | ||
|
||
foreach ($this->compressors as $compressor) { | ||
$compressor->compress($path); | ||
} | ||
} | ||
} |
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.