diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c83655..fb22e9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver]. +## Unreleased + +### Added + +- Store references data in memory after first open + ## v3.10.0 ### Added diff --git a/sdk/php/src/ReferencesData/StaticReference.php b/sdk/php/src/ReferencesData/StaticReference.php index b8ddfc9..2d41f2b 100644 --- a/sdk/php/src/ReferencesData/StaticReference.php +++ b/sdk/php/src/ReferencesData/StaticReference.php @@ -16,6 +16,13 @@ class StaticReference implements StaticReferenceInterface */ protected $file_path; + /** + * Source data. + * + * @var mixed[]|object[]|object|null + */ + protected $data; + /** * Create a new reference instance. * @@ -68,9 +75,10 @@ public function getFilePath(): string */ public function getData(bool $as_array = true, int $options = 0) { - /** @var mixed[]|object[]|object $data */ - $data = \json_decode((string) \file_get_contents($this->file_path), $as_array, 512, $options); + if (! isset($this->data)) { + $this->data = \json_decode((string) \file_get_contents($this->file_path), $as_array, 512, $options); + } - return $data; + return $this->data; } } diff --git a/sdk/php/src/StaticReferencesData.php b/sdk/php/src/StaticReferencesData.php index a2f6892..792be5a 100644 --- a/sdk/php/src/StaticReferencesData.php +++ b/sdk/php/src/StaticReferencesData.php @@ -5,9 +5,27 @@ namespace AvtoDev\StaticReferencesData; use AvtoDev\StaticReferencesData\ReferencesData\StaticReference; +use AvtoDev\StaticReferencesData\ReferencesData\StaticReferenceInterface; class StaticReferencesData { + /** + * @var array + */ + private static $references; + + /** + * Path to files with static data. Should be unique. + */ + private const + CADASTRAL_DISTRICTS_PATH = '/data/cadastral/districts.json', + SUBJECT_CODES_PATH = '/data/subject/codes.json', + VEHICLE_FINE_ARTICLES_PATH = '/data/vehicle/fine/articles.json', + VEHICLE_REGISTRATION_ACTIONS_PATH = '/data/vehicle/registration/actions.json', + VEHICLE_REPAIR_METHODS_PATH = '/data/vehicle/repair/methods.json', + VEHICLE_CATEGORIES_PATH = '/data/vehicle/categories.json', + VEHICLE_TYPES_PATH = '/data/vehicle/types.json'; + /** * Get root references directory path. * @@ -27,11 +45,13 @@ public static function getRootDirectoryPath(?string $additional_path = null): st /** * Get cadastral districts reference. * - * @return StaticReference + * @return StaticReferenceInterface */ - public static function cadastralDistricts(): StaticReference + public static function cadastralDistricts(): StaticReferenceInterface { - return new StaticReference(static::getRootDirectoryPath('/data/cadastral/districts.json')); + return static::$references[static::CADASTRAL_DISTRICTS_PATH] + ?? static::$references[static::CADASTRAL_DISTRICTS_PATH] = + new StaticReference(static::getRootDirectoryPath(static::CADASTRAL_DISTRICTS_PATH)); } /** @@ -39,60 +59,72 @@ public static function cadastralDistricts(): StaticReference * * @link * - * @return StaticReference + * @return StaticReferenceInterface */ - public static function subjectCodes(): StaticReference + public static function subjectCodes(): StaticReferenceInterface { - return new StaticReference(static::getRootDirectoryPath('/data/subject/codes.json')); + return static::$references[static::SUBJECT_CODES_PATH] + ?? static::$references[static::SUBJECT_CODES_PATH] = + new StaticReference(static::getRootDirectoryPath(static::SUBJECT_CODES_PATH)); } /** * Get vehicle fine articles reference. * - * @return StaticReference + * @return StaticReferenceInterface */ - public static function vehicleFineArticles(): StaticReference + public static function vehicleFineArticles(): StaticReferenceInterface { - return new StaticReference(static::getRootDirectoryPath('/data/vehicle/fine/articles.json')); + return static::$references[static::VEHICLE_FINE_ARTICLES_PATH] + ?? static::$references[static::VEHICLE_FINE_ARTICLES_PATH] = + new StaticReference(static::getRootDirectoryPath(static::VEHICLE_FINE_ARTICLES_PATH)); } /** * Get vehicle registration actions reference. * - * @return StaticReference + * @return StaticReferenceInterface */ - public static function vehicleRegistrationActions(): StaticReference + public static function vehicleRegistrationActions(): StaticReferenceInterface { - return new StaticReference(static::getRootDirectoryPath('/data/vehicle/registration/actions.json')); + return static::$references[static::VEHICLE_REGISTRATION_ACTIONS_PATH] + ?? static::$references[static::VEHICLE_REGISTRATION_ACTIONS_PATH] = + new StaticReference(static::getRootDirectoryPath(static::VEHICLE_REGISTRATION_ACTIONS_PATH)); } /** * Get vehicle repair methods reference. * - * @return StaticReference + * @return StaticReferenceInterface */ - public static function vehicleRepairMethods(): StaticReference + public static function vehicleRepairMethods(): StaticReferenceInterface { - return new StaticReference(static::getRootDirectoryPath('/data/vehicle/repair/methods.json')); + return static::$references[static::VEHICLE_REPAIR_METHODS_PATH] + ?? static::$references[static::VEHICLE_REPAIR_METHODS_PATH] = + new StaticReference(static::getRootDirectoryPath(static::VEHICLE_REPAIR_METHODS_PATH)); } /** * Get vehicle categories reference. * - * @return StaticReference + * @return StaticReferenceInterface */ - public static function vehicleCategories(): StaticReference + public static function vehicleCategories(): StaticReferenceInterface { - return new StaticReference(static::getRootDirectoryPath('/data/vehicle/categories.json')); + return static::$references[static::VEHICLE_CATEGORIES_PATH] + ?? static::$references[static::VEHICLE_CATEGORIES_PATH] = + new StaticReference(static::getRootDirectoryPath(static::VEHICLE_CATEGORIES_PATH)); } /** * Get vehicle types reference. * - * @return StaticReference + * @return StaticReferenceInterface */ - public static function vehicleTypes(): StaticReference + public static function vehicleTypes(): StaticReferenceInterface { - return new StaticReference(static::getRootDirectoryPath('/data/vehicle/types.json')); + return static::$references[static::VEHICLE_TYPES_PATH] + ?? static::$references[static::VEHICLE_TYPES_PATH] = + new StaticReference(static::getRootDirectoryPath(static::VEHICLE_TYPES_PATH)); } }