From 353d56c83404e51c290ad032682393f8357a4b60 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Wed, 7 Aug 2024 23:33:26 +0200 Subject: [PATCH] A CLI for ICanBoogie/CLDR --- .editorconfig | 15 +++ .gitattributes | 13 +++ .github/workflows/code-style.yml | 21 ++++ .github/workflows/static-analysis.yml | 32 +++++++ .github/workflows/test.yml | 43 +++++++++ .gitignore | 5 + CHANGELOG.md | 31 ++++++ CODE_OF_CONDUCT.md | 132 ++++++++++++++++++++++++++ CONTRIBUTING.md | 32 +++++++ Dockerfile | 40 ++++++++ LICENSE | 30 ++++++ Makefile | 44 +++++++++ README.md | 59 ++++++++++++ cldr | 55 +++++++++++ composer.json | 50 ++++++++++ docker-compose.yaml | 35 +++++++ phpcs.xml | 19 ++++ phpstan.neon | 4 + phpunit.xml | 24 +++++ src/App.php | 44 +++++++++ src/WarmUpCommand.php | 108 +++++++++++++++++++++ tests/bootstrap.php | 52 ++++++++++ 22 files changed, 888 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/workflows/code-style.yml create mode 100644 .github/workflows/static-analysis.yml create mode 100644 .github/workflows/test.yml create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100755 cldr create mode 100644 composer.json create mode 100644 docker-compose.yaml create mode 100644 phpcs.xml create mode 100644 phpstan.neon create mode 100644 phpunit.xml create mode 100644 src/App.php create mode 100644 src/WarmUpCommand.php create mode 100644 tests/bootstrap.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3199870 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +indent_style = space +indent_size = 4 + +[{*.yaml,*.yml,*.neon,*.json}] +indent_size = 2 + +[*.md] +max_line_length = 100 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..2391959 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,13 @@ +* text=auto + +.editorconfig export-ignore +.gitattributes export-ignore +.github export-ignore +.gitignore export-ignore +docker-compose.yml export-ignore +Dockerfile export-ignore +Makefile export-ignore +phpcs.xml +phpstan.neon export-ignore +phpunit.xml export-ignore +tests/ export-ignore diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml new file mode 100644 index 0000000..66a1b3d --- /dev/null +++ b/.github/workflows/code-style.yml @@ -0,0 +1,21 @@ +name: code-style + +on: +- push +- pull_request + +jobs: + phpcs: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + coverage: none + php-version: "8.3" + ini-values: memory_limit=-1 + tools: phpcs, cs2pr + - name: Run PHP Code Sniffer + run: phpcs -q --report=checkstyle | cs2pr diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..6a87b5e --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,32 @@ +name: static-analysis + +on: +- push +- pull_request + +jobs: + phpstan: + name: phpstan + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.3" + ini-values: memory_limit=-1 + tools: composer:v2 + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: | + ~/.composer/cache + vendor + key: phpstan-deps + + - name: Install dependencies + run: composer install --no-interaction --no-progress + + - name: Analyze + run: vendor/bin/phpstan diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..63e282b --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,43 @@ +name: test + +on: +- push +- pull_request + +jobs: + phpunit: + name: phpunit + runs-on: ubuntu-latest + strategy: + matrix: + php-version: + - "8.1" + - "8.2" + - "8.3" + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php-version }}" + ini-values: memory_limit=-1 + tools: composer:v2 + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: | + ~/.composer/cache + .cldr-cache + vendor + key: "php-${{ matrix.php-version }}" + restore-keys: "php-${{ matrix.php-version }}" + + - name: Install dependencies + run: make vendor + + - name: Warm up + run: make warm-up + + - name: Run PHPUnit + run: make test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..14756a3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.cldr-cache +.phpunit.result.cache +build +composer.lock +vendor diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..24aaddc --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,31 @@ +# Changelog + +## v6.0 + +The version starts at v6.0 to match ICanBoogie/CLDR. + + diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..5ef93b0 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,132 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, caste, color, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +[https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0]. + +Community Impact Guidelines were inspired by +[Mozilla's code of conduct enforcement ladder][Mozilla CoC]. + +For answers to common questions about this code of conduct, see the FAQ at +[https://www.contributor-covenant.org/faq][FAQ]. Translations are available +at [https://www.contributor-covenant.org/translations][translations]. + +[homepage]: https://www.contributor-covenant.org +[v2.0]: https://www.contributor-covenant.org/version/2/0/code_of_conduct.html +[Mozilla CoC]: https://github.com/mozilla/diversity +[FAQ]: https://www.contributor-covenant.org/faq +[translations]: https://www.contributor-covenant.org/translations diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..33bc016 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,32 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +We accept contributions via Pull Requests. + +## Pull Requests + +- **Code style** — We're following a [Coding Standard][]. Check the code style with `make lint`. +- **Code health** — We're using [PHPStan][] to analyse the code, with maximum scrutiny. Check the code with `make lint`. +- **Add tests!** — Your contribution won't be accepted if it does not have tests. +- **Document any change in behaviour** — Make sure the `README.md` and any other relevant documentation are kept + up-to-date. +- **Consider our release cycle** — We follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not + an option. +- **Create feature branches** — We won't pull from your main branch. +- **One pull request per feature** — If you want to do more than one thing, send multiple pull requests. +- **Send coherent history** — Make sure each individual commit in your pull request is meaningful. If you had to make + multiple intermediate commits while developing, please [squash them][git-squash] before submitting. + +## Running Tests + +We provide a Docker container for local development. Run `make test-container` to create a new session. Inside the +container run `make test` to run the test suite. Alternatively, run `make test-coverage` for a breakdown of the code +coverage. The coverage report is available in `build/coverage/index.html`. + +**Thanks for your contribution**! + + +[Coding Standard]: phpcs.xml +[git-squash]: http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages +[PHPStan]: https://phpstan.org/user-guide/getting-started diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..69998c2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,40 @@ +ARG PHP_VERSION +FROM php:${PHP_VERSION}-cli-bookworm + +RUN <<-EOF + apt-get update + apt-get install -y autoconf pkg-config + pecl channel-update pecl.php.net + pecl install xdebug + docker-php-ext-enable opcache xdebug +EOF + +RUN <<-EOF + cat <<-SHELL >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini + xdebug.client_host=host.docker.internal + xdebug.mode=develop + xdebug.start_with_request=yes + SHELL + + cat <<-SHELL >> /usr/local/etc/php/conf.d/php.ini + display_errors=On + error_reporting=E_ALL + date.timezone=UTC + SHELL +EOF + +ENV COMPOSER_ALLOW_SUPERUSER 1 + +RUN <<-EOF + apt-get update + apt-get install unzip + curl -s https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer | php -- --quiet + mv composer.phar /usr/local/bin/composer + cat <<-SHELL >> /root/.bashrc + export PATH="$HOME/.composer/vendor/bin:$PATH" + SHELL +EOF + +RUN composer global require squizlabs/php_codesniffer + +RUN apt-get install -y git diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68d6689 --- /dev/null +++ b/LICENSE @@ -0,0 +1,30 @@ +The icanboogie/cldr-cli package is free software. +It is released under the terms of the following BSD License. + +Copyright (c) 2024-present by Olivier Laviale +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of Olivier Laviale nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..964c43c --- /dev/null +++ b/Makefile @@ -0,0 +1,44 @@ +CACHE_DIR = .cldr-cache +PHPUNIT = vendor/bin/phpunit + +vendor: + @composer install + @rm -rf vendor/icanboogie/cldr + @composer install --prefer-install=source + +# testing + +.PHONY: warm-up +warm-up: + mkdir -p .cldr-cache + ./cldr warm-up -vvv de en en-001 es fr fr-BE it ja ru zh + +.PHONY: test-dependencies +test-dependencies: vendor + +.PHONY: test +test: test-dependencies + $(PHPUNIT) + +.PHONY: test-container +test-container: test-container-81 + +.PHONY: test-container-81 +test-container-81: + @-docker-compose run --rm app81 bash + @docker-compose down -v + +.PHONY: test-container-82 +test-container-82: + @-docker-compose run --rm app82 bash + @docker-compose down -v + +.PHONY: test-container-83 +test-container-83: + @-docker-compose run --rm app83 bash + @docker-compose down -v + +.PHONY: lint +lint: + @XDEBUG_MODE=off phpcs -s + @XDEBUG_MODE=off vendor/bin/phpstan diff --git a/README.md b/README.md new file mode 100644 index 0000000..03bb72e --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# CLDR CLI + +[![Release](https://img.shields.io/packagist/v/icanboogie/cldr-cli.svg)](https://packagist.org/packages/icanboogie/cldr-cli) +[![Downloads](https://img.shields.io/packagist/dt/icanboogie/cldr-cli.svg)](https://packagist.org/packages/icanboogie/cldr-cli) + +A CLI for [ICanBoogie/CLDR][]. + + + +#### Usage + +```shell +./vendor/bin/cldr warm-up us fr de +``` + + + +#### Installation + +```shell +composer require icanboogie/cldr-cli +``` + + + +---------- + + + +## Continuous Integration + +The project is continuously tested by [GitHub actions](https://github.com/ICanBoogie/CLDR-CLI/actions). + +[![Tests](https://github.com/ICanBoogie/CLDR-CLI/workflows/test/badge.svg)](https://github.com/ICanBoogie/CLDR-CLI/actions?query=workflow%3Atest) +[![Static Analysis](https://github.com/ICanBoogie/CLDR-CLI/workflows/static-analysis/badge.svg)](https://github.com/ICanBoogie/CLDR-CLI/actions?query=workflow%3Astatic-analysis) +[![Code Style](https://github.com/ICanBoogie/CLDR-CLI/workflows/code-style/badge.svg)](https://github.com/ICanBoogie/CLDR-CLI/actions?query=workflow%3Acode-style) + + + +## Code of Conduct + +This project adheres to a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in +this project and its community, you're expected to uphold this code. + + + +## Contributing + +See [CONTRIBUTING](CONTRIBUTING.md) for details. + + + +## License + +**icanboogie/cldr-cli** is released under the [BSD-3-Clause](LICENSE). + + + +[ICanBoogie/CLDR]: https://github.com/ICanBoogie/CLDR/ diff --git a/cldr b/cldr new file mode 100755 index 0000000..1526e79 --- /dev/null +++ b/cldr @@ -0,0 +1,55 @@ +#!/usr/bin/env php +')) { + fwrite( + STDERR, + sprintf( + 'This version of CLDR requires PHP >= 8.1.' . PHP_EOL . + 'You are using PHP %s (%s).' . PHP_EOL, + PHP_VERSION, + PHP_BINARY + ) + ); + + die(1); +} + +if (isset($GLOBALS['_composer_autoload_path'])) { + define('CLDR_COMPOSER_INSTALL', $GLOBALS['_composer_autoload_path']); + + unset($GLOBALS['_composer_autoload_path']); +} else { + foreach ( + [ + __DIR__ . '/../../autoload.php', + __DIR__ . '/../vendor/autoload.php', + __DIR__ . '/vendor/autoload.php' + ] as $file + ) { + if (file_exists($file)) { + define('CLDR_COMPOSER_INSTALL', $file); + + break; + } + } + + unset($file); +} + +if (!defined('CLDR_COMPOSER_INSTALL')) { + fwrite( + STDERR, + 'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL . + ' composer install' . PHP_EOL . PHP_EOL . + 'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL + ); + + die(1); +} + +require CLDR_COMPOSER_INSTALL; + +App::run(); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..57ad0a2 --- /dev/null +++ b/composer.json @@ -0,0 +1,50 @@ +{ + "name": "icanboogie/cldr-cli", + "type": "library", + "description": "A CLI for ICanBoogie/CLDR", + "version": "6.0", + "keywords": [ + ], + "homepage": "https://icanboogie.org/", + "license": "BSD-3-Clause", + "authors": [ + { + "name": "Olivier Laviale", + "email": "olivier.laviale@gmail.com", + "homepage": "https://olvlvl.com/", + "role": "Developer" + } + ], + "support": { + "issues": "https://github.com/ICanBoogie/CLDR-CLI/issues", + "source": "https://github.com/ICanBoogie/CLDR-CLI" + }, + "config": { + "sort-packages": true + }, + "minimum-stability": "dev", + "prefer-stable": true, + "require": { + "php": ">=8.1", + "icanboogie/cldr": "^6.0", + "symfony/console": "^6.3|^7.0", + "symfony/dependency-injection": "^6.3|^7.0" + }, + "require-dev": { + "phpstan/phpstan": "^1.11", + "phpunit/phpunit": "^10.5" + }, + "autoload": { + "psr-4": { + "ICanBoogie\\CLDR\\CLI\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Test\\ICanBoogie\\CLDR\\": "vendor/icanboogie/cldr/tests" + } + }, + "bin": [ + "cldr" + ] +} diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..8aac285 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,35 @@ +--- +services: + app81: + build: + context: . + args: + PHP_VERSION: "8.1" + environment: + PHP_IDE_CONFIG: 'serverName=icanboogie-cldr-cli' + volumes: + - .:/app:delegated + - ~/.composer:/root/.composer:delegated + working_dir: /app + app82: + build: + context: . + args: + PHP_VERSION: "8.2" + environment: + PHP_IDE_CONFIG: 'serverName=icanboogie-cldr-cli' + volumes: + - .:/app:delegated + - ~/.composer:/root/.composer:delegated + working_dir: /app + app83: + build: + context: . + args: + PHP_VERSION: "8.3" + environment: + PHP_IDE_CONFIG: 'serverName=icanboogie-cldr-cli' + volumes: + - .:/app:delegated + - ~/.composer:/root/.composer:delegated + working_dir: /app diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..aca4756 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,19 @@ + + + src + + + + + + + tests/bootstrap.php + + + tests/* + + + tests/* + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..a1c637a --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + level: max + paths: + - src diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..111cf47 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,24 @@ + + + + + ./vendor/icanboogie/cldr/tests + ./vendor/icanboogie/cldr/tests/Cache/RedisCacheTest.php + + + + + ./vendor/icanboogie/cldr/lib + + + diff --git a/src/App.php b/src/App.php new file mode 100644 index 0000000..61d2004 --- /dev/null +++ b/src/App.php @@ -0,0 +1,44 @@ +get('console.command_loader'); + + $app = new Application(); + $app->setCommandLoader($commandLoader); + $app->run(); + } + + private static function container(): ContainerInterface + { + $container = new ContainerBuilder(); + $container->addCompilerPass(new AddConsoleCommandPass()); + + foreach (self::COMMANDS as $command) { + $container + ->register($command) + ->addTag('console.command') + ->setAutowired(true); + } + + $container->compile(); + + return $container; + } +} diff --git a/src/WarmUpCommand.php b/src/WarmUpCommand.php new file mode 100644 index 0000000..7cb2ac4 --- /dev/null +++ b/src/WarmUpCommand.php @@ -0,0 +1,108 @@ +addOption( + name: 'cache-dir', + shortcut: 'd', + mode: InputOption::VALUE_OPTIONAL, + description: "The directory of the CLDR cache", + default: FileCache::RECOMMENDED_DIR, + ); + + $this->addOption( + name: 'clear', + mode: InputOption::VALUE_NONE, + description: "Clear the cache directory before the warm up", + ); + + $this->addArgument( + name: 'locales', + mode: InputArgument::REQUIRED | InputArgument::IS_ARRAY + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $cacheDir = $input->getOption('cache-dir'); + assert(is_string($cacheDir)); + $this->assertCacheDir($cacheDir); + + $locales = $input->getArgument('locales'); + assert(is_array($locales)); + $this->assertLocales($locales); + + if ($input->getOption('clear')) { + $this->clearCacheDir($cacheDir); + } + + $provider = new CachedProvider( + new WebProvider(), + new FileCache($cacheDir) + ); + + $repository = new Repository($provider); + $w = $output->writeln(...); + + $w("Data will be stored in '$cacheDir'"); + + $repository->supplemental->warm_up($w); + + foreach ($locales as $locale) { + $repository->locale_for($locale)->warm_up($w); + } + + $repository->currencies->warm_up($w); + + return self::SUCCESS; + } + + private function assertCacheDir(string $cacheDir): void + { + if (!is_writable($cacheDir)) { + throw new RuntimeException("The directory '$cacheDir' is not writable"); + } + } + + /** + * @param string[] $locales + */ + private function assertLocales(array $locales): void + { + foreach ($locales as $locale) { + LocaleId::from($locale); + } + } + + private function clearCacheDir(string $cacheDir): void + { + $di = new DirectoryIterator($cacheDir); + + foreach ($di as $file) { + if (!$file->isFile()) { + continue; + } + + unlink($file->getPathname()); + } + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..260ea50 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Test\ICanBoogie\CLDR; + +use ICanBoogie\CLDR\Cache\FileCache; +use ICanBoogie\CLDR\Locale; +use ICanBoogie\CLDR\LocaleId; +use ICanBoogie\CLDR\Provider; +use ICanBoogie\CLDR\Provider\CachedProvider; +use ICanBoogie\CLDR\Provider\FailingProvider; +use ICanBoogie\CLDR\Repository; + +require __DIR__ . '/../vendor/autoload.php'; + +const CACHE_DIR = __DIR__ . '/../' . FileCache::RECOMMENDED_DIR; + +if (!file_exists(CACHE_DIR)) { + mkdir(CACHE_DIR); +} + +function create_provider(): Provider +{ + static $provider; + + return $provider ??= new CachedProvider( + new FailingProvider(), + new FileCache(CACHE_DIR), + ); +} + +function get_repository(): Repository +{ + static $repository; + + return $repository ??= new Repository(create_provider()); +} + +function locale_for(string|LocaleId $id): Locale +{ + return get_repository()->locale_for($id); +} + +date_default_timezone_set('Europe/Madrid');