diff --git a/MIGRATION.md b/MIGRATION.md index d4c424d..fea9dd3 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -11,6 +11,7 @@ ### New features - `LocaleId` is an enum of available locales. +- Added the `Warmable` interface to features that can warm the CLDR cache. ### Backward Incompatible Changes diff --git a/lib/CurrencyCollection.php b/lib/CurrencyCollection.php index 502d217..b37a10a 100644 --- a/lib/CurrencyCollection.php +++ b/lib/CurrencyCollection.php @@ -11,6 +11,7 @@ namespace ICanBoogie\CLDR; +use Closure; use ICanBoogie\Accessor\AccessorTrait; use function array_combine; @@ -36,7 +37,7 @@ * * @property-read string[] $codes Alphabetic list of currency codes. */ -final class CurrencyCollection extends AbstractCollection +final class CurrencyCollection extends AbstractCollection implements Warmable { /** * @uses get_codes @@ -70,10 +71,7 @@ public function __construct( private function get_codes(): array { $make = function () { - $codes = array_keys($this->repository->fetch( - 'numbers/en-001/currencies', - 'main/en-001/numbers/currencies' - )); + $codes = array_keys($this->fetch_currencies()); return array_combine($codes, $codes); }; @@ -105,4 +103,18 @@ public function assert_defined(string $currency_code): void throw new CurrencyNotDefined($currency_code); } } + + private function fetch_currencies(): array + { + return $this->repository->fetch( + 'numbers/en-001/currencies', + 'main/en-001/numbers/currencies' + ); + } + + public function warm_up(Closure $progress): void + { + $progress("Warming up currencies"); + $this->fetch_currencies(); + } } diff --git a/lib/Locale.php b/lib/Locale.php index d42e08d..811bcab 100644 --- a/lib/Locale.php +++ b/lib/Locale.php @@ -11,8 +11,8 @@ namespace ICanBoogie\CLDR; +use Closure; use ICanBoogie\Accessor\AccessorTrait; -use InvalidArgumentException; use LogicException; use function str_replace; @@ -40,7 +40,7 @@ * @property-read Units $units * @uses self::get_units() */ -class Locale extends AbstractSectionCollection +class Locale extends AbstractSectionCollection implements Warmable { use AccessorTrait; @@ -94,6 +94,19 @@ public function offsetExists($offset): bool return isset(self::OFFSET_MAPPING[$offset]); } + /** + * Warm up with locale relevant data. + */ + public function warm_up(Closure $progress): void + { + $progress("Warming up locale '{$this->id->value}':"); + + foreach (array_keys(self::OFFSET_MAPPING) as $offset) { + $progress("- $offset"); + $this[$offset]; + } + } + protected function path_for(string $offset): string { return str_replace('{locale}', $this->id->value, self::OFFSET_MAPPING[$offset][0]); diff --git a/lib/Supplemental.php b/lib/Supplemental.php index d104d93..85f2698 100644 --- a/lib/Supplemental.php +++ b/lib/Supplemental.php @@ -11,6 +11,7 @@ namespace ICanBoogie\CLDR; +use Closure; use ICanBoogie\Accessor\AccessorTrait; use ICanBoogie\CLDR\Supplemental\CurrencyData; @@ -30,7 +31,7 @@ * @property-read CurrencyData $currency_data * @uses self::get_currency_data() */ -final class Supplemental extends AbstractSectionCollection +final class Supplemental extends AbstractSectionCollection implements Warmable { /** * @uses get_currency_data @@ -86,6 +87,16 @@ public function offsetExists($offset): bool return isset(self::OFFSET_MAPPING[$offset]); } + public function warm_up(Closure $progress): void + { + $progress("Warming up supplemental:"); + + foreach (array_keys(self::OFFSET_MAPPING) as $offset) { + $progress("- $offset"); + $this[$offset]; + } + } + protected function path_for(string $offset): string { return "core/supplemental/$offset"; diff --git a/lib/Warmable.php b/lib/Warmable.php new file mode 100644 index 0000000..8ffab29 --- /dev/null +++ b/lib/Warmable.php @@ -0,0 +1,16 @@ +sut->assert_defined('EUR'); $this->assertTrue(true); } + + public function test_warm_up(): void + { + $n = 0; + + $this->sut->warm_up(function() use (&$n) { $n++; }); + + $this->assertEquals(1, $n); + } } diff --git a/tests/LocaleTest.php b/tests/LocaleTest.php index 3f691ba..d9613a1 100644 --- a/tests/LocaleTest.php +++ b/tests/LocaleTest.php @@ -183,4 +183,13 @@ public function test_context_transform(): void ) ); } + + public function test_warm_up(): void + { + $n = 0; + + self::$locale->warm_up(function() use (&$n) { $n++; }); + + $this->assertEquals(30, $n); + } } diff --git a/tests/SupplementalTest.php b/tests/SupplementalTest.php index 52888ad..02ad1fc 100644 --- a/tests/SupplementalTest.php +++ b/tests/SupplementalTest.php @@ -121,4 +121,13 @@ public function test_should_throw_exception_in_attempt_to_unset_offset(): void $this->expectException(OffsetNotWritable::class); unset($s['timeData']); } + + public function test_warm_up(): void + { + $n = 0; + + self::$sut->warm_up(function() use (&$n) { $n++; }); + + $this->assertEquals(28, $n); + } }