8000 [Intl] Allow compressing emoji and data maps by nicolas-grekas · Pull Request #49970 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Intl] Allow compressing emoji and data maps #49970

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Intl/CHANGELOG.md
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
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add the special `strip` locale to `EmojiTransliterator` to strip all emojis from a string
* Add `compress` script to compress the `Resources/data` directory when disk space matters

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Intl\Data\Bundle\Reader;

use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
use Symfony\Component\Intl\Util\GzipStreamWrapper;

/**
* Reads .php resource bundles.
Expand All @@ -31,6 +32,10 @@ public function read(string $path, string $locale): mixed
throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName));
}

if (is_file($fileName.'.gz')) {
return GzipStreamWrapper::require($fileName.'.gz');
}

if (!is_file($fileName)) {
throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName));
}
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Intl/Resources/bin/compress
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env php
<?php

if ('cli' !== PHP_SAPI) {
throw new Exception('This script must be run from the command line.');
}
if (!extension_loaded('zlib')) {
throw new Exception('This script requires the zlib extension.');
}

foreach (glob(dirname(__DIR__).'/data/*/*.php') as $file) {
if (in_array(basename($file), ['en.php', 'meta.php'], true)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that this script requires zlib anyway, is there any benefit to keeping those uncompressed ?

continue;
}

file_put_contents('compress.zlib://'.$file.'.gz', $data);

unlink($file.(filesize($file.'.gz') >= filesize($file) ? '.gz' : ''));
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/Intl/Resources/emoji/build.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
Builder::cleanTarget();
$emojisCodePoints = Builder::getEmojisCodePoints();
Builder::saveRules(Builder::buildRules($emojisCodePoints));
Builder::saveRules(Builder::buildStripRules($emojisCodePoints));
Builder::saveRules(Builder::buildGitHubRules($emojisCodePoints));
Builder::saveRules(Builder::buildSlackRules($emojisCodePoints));
Builder::saveRules(Builder::buildStripRules($emojisCodePoints));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why changing the order ?


final class Builder
{
Expand Down Expand Up @@ -178,7 +178,7 @@ public static function buildStripRules(array $emojisCodePoints): iterable
$maps[$codePointsCount][$emoji] = '';
}

return ['strip' => self::createRules($maps)];
return ['emoji-strip' => self::createRules($maps)];
}

public static function cleanTarget(): void
Expand All @@ -192,7 +192,7 @@ public static function saveRules(iterable $rulesByLocale): void
{
$firstChars = [];
foreach ($rulesByLocale as $filename => $rules) {
file_put_contents(self::TARGET_DIR."/$filename.php", "<?php\n\nreturn ".VarExporter::export($rules).";\n");
file_put_contents('compress.zlib://'.self::TARGET_DIR."/$filename.php.gz", "<?php\n\nreturn ".VarExporter::export($rules).";\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it expected that you still bui 8000 ld them as compressed here ?


foreach ($rules as $k => $v) {
if (!str_starts_with($filename, 'emoji-')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Intl\Transliterator;

use Symfony\Component\Intl\Util\GzipStreamWrapper;

if (!class_exists(\Transliterator::class)) {
throw new \LogicException(sprintf('You cannot use the "%s\EmojiTransliterator" class as the "intl" extension is not installed. See https://php.net/intl.', __NAMESPACE__));
} else {
Expand Down Expand Up @@ -40,7 +42,8 @@ public static function create(string $id, int $direction = self::FORWARD): self
$id = self::REVERSEABLE_IDS[$id];
}

if (!preg_match('/^[a-z0-9@_\\.\\-]*$/', $id) || !is_file(\dirname(__DIR__)."/Resources/data/transliterator/emoji/{$id}.php")) {
$file = \dirname(__DIR__)."/Resources/data/transliterator/emoji/{$id}.php";
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/', $id) || !is_file($file) && !is_file($file .= '.gz')) {
\Transliterator::create($id); // Populate intl_get_error_*()

throw new \IntlException(intl_get_error_message(), intl_get_error_code());
Expand All @@ -57,7 +60,7 @@ public static function create(string $id, int $direction = self::FORWARD): self
$instance = unserialize(sprintf('O:%d:"%s":1:{s:2:"id";s:%d:"%s";}', \strlen(self::class), self::class, \strlen($id), $id));
}

$instance->map = $maps[$id] ??= require \dirname(__DIR__)."/Resources/data/transliterator/emoji/{$id}.php";
$instance->map = $maps[$id] ??= str_ends_with($file, '.gz') ? GzipStreamWrapper::require($file) : $require;

return $instance;
}
Expand Down Expand Up @@ -86,7 +89,9 @@ public static function listIDs(): array
}

foreach (scandir(\dirname(__DIR__).'/Resources/data/transliterator/emoji/') as $file) {
if (str_ends_with($file, '.php')) {
if (str_ends_with($file, '.php.gz')) {
$ids[] = substr($file, 0, -7);
} elseif (str_ends_with($file, '.php')) {
$ids[] = substr($file, 0, -4);
}
}
Expand Down
83 changes: 83 additions & 0 deletions src/Symfony/Component/Intl/Util/GzipStreamWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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\Intl\Util;

/**
* @internal
*/
class GzipStreamWrapper
{
/** @var resource|null */
public $context;

/** @var resource */
private $handle;
private string $path;

public static function require(string $path): array
{
if (!\extension_loaded('zlib')) {
throw new \LogicException(sprintf('The "zlib" extension is required to load the "%s/%s" map, please enable it in your php.ini file.', basename(\dirname($path)), \basename($path)));
}

if (!\function_exists('opcache_is_script_cached') || !@opcache_is_script_cached($path)) {
stream_wrapper_unregister('file');
stream_wrapper_register('file', self::class);
}

return require $path;
}

public function stream_open(string $path, string $mode): bool
{
stream_wrapper_restore('file');
$this->path = $path;

return false !== $this->handle = fopen('compress.zlib://'. $path, $mode);
}

public function stream_read(int $count): string|false
{
return fread($this->handle, $count);
}

public function stream_eof(): bool
{
return feof($this->handle);
}

public function stream_set_option(int $option, int $arg1, int $arg2): bool
{
return match ($option) {
\STREAM_OPTION_BLOCKING => stream_set_blocking($this->handle, $arg1),
\STREAM_OPTION_READ_TIMEOUT => stream_set_timeout($this->handle, $arg1, $arg2),
\STREAM_OPTION_WRITE_BUFFER => 0 === stream_set_write_buffer($this->handle, $arg2),
default => false,
};
}

public function stream_stat(): array|false
{
if (!$stat = stat($this->path)) {
return false;
}

$h = fopen($this->path, 'rb');
fseek($h, -4, \SEEK_END);
$size = unpack('V', fread($h, 4));
fclose($h);

$stat[7] = $stat['size'] = end($size);

return $stat;
}
}
0