From 77928c548f60d4f003359877653e3e2bea5f0e87 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 20 Feb 2021 12:58:54 +0300 Subject: [PATCH 1/9] Move to PHP 7.4; Extend Atomastic Arrays; Code refactoring; Update tests --- README.md | 154 +---------------------------------------- composer.json | 10 +-- src/Registry.php | 84 ++-------------------- tests/RegistryTest.php | 47 ++----------- 4 files changed, 19 insertions(+), 276 deletions(-) diff --git a/README.md b/README.md index 4806eda..7a27339 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,6 @@ Registry Component provides a fluent, object-oriented interface for storing data
-* [Installation](#installation) -* [Usage](#usage) -* [Methods](#methods) -* [Tests](#tests) -* [License](#license) - ### Installation #### With [Composer](https://getcomposer.org) @@ -23,152 +17,8 @@ Registry Component provides a fluent, object-oriented interface for storing data composer require atomastic/registry ``` -### Usage - -```php -use Atomastic\Registry\Registry; - -// Using public static method getInstance() -$registry = Registry::getInstance(); - -// Using global helper function registry() thats returns Registry::getInstance() -$registry = registry(); -``` - -### Methods - -| Method | Description | -|---|---| -| `getInstance()` | Gets the instance via lazy initialization (created on first usage) | -| `set()` | Set an registry data to a given value using "dot" notation. If no key is given to the method, the entire registry data will be replaced. | -| `get()` | Get item from the registry. | -| `has()` | Determine if the registry has a value for the given name. | -| `delete()` | Get item from the registry. | -| `all()` | Get all items from the registry. | -| `flush()` | Flush all items from the registry. | - -#### Methods Details - -##### Method: `getInstance()` - -```php -/** - * Gets the instance via lazy initialization (created on first usage) - */ -public static function getInstance(): Registry -``` - -**Examples** - -```php -$registry = Registry::getInstance(); -``` - -##### Method: `set()` - -```php -/** - * Set an registry data to a given value using "dot" notation. - * - * If no key is given to the method, the entire registry data will be replaced. - * - * @param string $key Key - * @param mixed $value Value - */ -public function set(string $key, $value): self -``` - -**Examples** - -```php -$registry->set('movies.the-thin-red-line.title', 'The Thin Red Line'); -``` - -##### Method: `get()` - -```php -/** - * Get item from the registry. - * - * @param string $key The name of the item to fetch. - * @param mixed $default Default value - */ -public function get(string $key, $default = null) -``` - -**Examples** - -```php -$title = $registry->get('movies.the-thin-red-line.title', 'The Thin Red Line'); -``` - -##### Method: `has()` - -```php -/** - * Determine if the registry has a value for the given name. - * - * @param string|array $keys The keys of the registry item to check for existence. - */ -public function has($keys): bool -``` - -**Examples** - -```php -if ($registry->has('movies.the-thin-red-line')) { - // Do something... -} -``` - -##### Method: `delete()` - -```php -/** - * Delete a items from the registry. - * - * @param array|string $keys Keys - */ -public function delete($keys): self -``` - -**Examples** - -```php -$registry->delete('movies.the-thin-red-line'); -``` - -##### Method: `all()` - -```php -/** - * Get all items from the registry. - */ -public function all(): array -``` - -**Examples** - -```php -foreach ($registry->all() as $key => $value) { - // code... -} -``` - -##### Method: `flush()` - -```php -/** - * Flush all items from the registry. - */ -public function flush(): void -``` - -**Examples** - -```php -$registry->flush(); -``` +### Resources +* [Documentation](https://atomastic.com/components/registry) ### Tests diff --git a/composer.json b/composer.json index 31c694c..8febfa0 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,8 @@ } ], "require": { - "php": "^7.3 || ^8.0", - "atomastic/arrays": "^2.0" + "php": "^7.4 || ^8.0", + "atomastic/arrays": "^3.0" }, "autoload":{ "psr-4": { @@ -29,8 +29,8 @@ ] }, "require-dev": { - "doctrine/coding-standard": "8.1.0", - "pestphp/pest": "^0.3.3", - "phpstan/phpstan": "^0.12.42" + "doctrine/coding-standard": "8.2.0", + "pestphp/pest": "^1.0.2", + "phpstan/phpstan": "^0.12.77" } } diff --git a/src/Registry.php b/src/Registry.php index 153b7bf..939ef40 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -7,21 +7,17 @@ use Atomastic\Arrays\Arrays; use Exception; -final class Registry +class Registry extends Arrays { /** * Registry instance - * - * @var Registry */ - private static $instance = null; + private static ?Registry $instance = null; /** * Registry storage - * - * @var Arrays */ - private static $storage = null; + private static ?Arrays $storage = null; /** * Gets the instance via lazy initialization (created on first usage) @@ -45,7 +41,6 @@ public static function getInstance(): Registry */ protected function __construct() { - } /** @@ -53,82 +48,13 @@ protected function __construct() */ protected function __clone() { - } /** * Prevent from being unserialized (which would create a second instance of it) */ - public function __wakeup() - { - throw new Exception("Cannot unserialize a Registry."); - } - - /** - * Set an registry data to a given value using "dot" notation. - * - * If no key is given to the method, the entire registry data will be replaced. - * - * @param string $key Key - * @param mixed $value Value - */ - public function set(string $key, $value): self - { - static::$storage->set($key, $value); - - return $this; - } - - /** - * Determine if the registry has a value for the given name. - * - * @param string|array $keys The keys of the registry item to check for existence. - */ - public function has($keys): bool - { - if (static::$storage->has($keys)) { - return true; - } - - return false; - } - - /** - * Get item from the registry. - * - * @param string $key The keys of the registry item to get. - * @param mixed $default Default value - */ - public function get(string $key, $default = null) - { - return static::$storage->get($key, $default); - } - - /** - * Delete a items from the registry. - * - * @param array|string $keys Keys. - */ - public function delete($keys): self - { - static::$storage->delete($keys); - - return $this; - } - - /** - * Flush all items from the registry. - */ - public function flush(): void - { - static::$storage = null; - } - - /** - * Get all items from the registry. - */ - public function all(): array + public function __wakeup(): void { - return static::$storage->all(); + throw new Exception('Cannot unserialize a Registry.'); } } diff --git a/tests/RegistryTest.php b/tests/RegistryTest.php index 891e616..66765d6 100644 --- a/tests/RegistryTest.php +++ b/tests/RegistryTest.php @@ -5,51 +5,18 @@ use Atomastic\Registry\Registry; test('test getInstance() method', function() { - $this->assertEquals(Registry::getInstance(), Registry::getInstance()); + $this->assertInstanceOf(Registry::class, Registry::getInstance()); }); test('test registry() helper', function() { $this->assertEquals(Registry::getInstance(), registry()); + $this->assertInstanceOf(Registry::class, registry()); }); -test('test set() method', function() { - Registry::getInstance()->set('movies.the-thin-red-line.title', 'The Thin Red Line'); +test('test registry uniqueness', function() { + $firstCall = Registry::getInstance(); + $secondCall = Registry::getInstance(); - $this->assertEquals('The Thin Red Line', - Registry::getInstance()->get('movies.the-thin-red-line.title')); -}); - -test('test get() method', function() { - Registry::getInstance()->set('movies.the-thin-red-line.title', 'The Thin Red Line'); - - $this->assertEquals('The Thin Red Line', - Registry::getInstance()->get('movies.the-thin-red-line.title')); -}); - -test('test has() method', function() { - $this->assertTrue(Registry::getInstance()->has('movies.the-thin-red-line.title')); - $this->assertFalse(Registry::getInstance()->has('movies.the-thin-red-line.description')); -}); - -test('test all() method', function() { - $this->assertEquals(['movies' => ['the-thin-red-line' => ['title' => 'The Thin Red Line']]],Registry::getInstance()->all()); -}); - -test('test delete() method', function() { - Registry::getInstance()->delete('movies.the-thin-red-line.title'); - - $this->assertFalse(Registry::getInstance()->has('movies.the-thin-red-line.title')); -}); - -test('test flush() method', function() { - Registry::getInstance()->set('movies.the-thin-red-line.title', 'The Thin Red Line') - ->set('movies.the-thin-red-line.description', 'Lorem ipsum dolor sit amet'); - - $this->assertTrue(Registry::getInstance()->has('movies.the-thin-red-line')); - $this->assertEquals(null, Registry::getInstance()->flush()); - - Registry::getInstance()->set('movies.the-thin-red-line.title', 'The Thin Red Line') - ->set('movies.the-thin-red-line.description', 'Lorem ipsum dolor sit amet'); - - $this->assertTrue(Registry::getInstance()->has('movies.the-thin-red-line')); + $this->assertInstanceOf(Registry::class, $firstCall); + $this->assertSame($firstCall, $secondCall); }); From c6545ba4d6f73de11e2a306c6e6c6ba79829bdcf Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 20 Feb 2021 12:59:09 +0300 Subject: [PATCH 2/9] Move to PHP 7.4; Extend Atomastic Arrays; Code refactoring; Update tests --- .gitattributes | 10 ++++++++++ .github/workflows/tests.yml | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1736554 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,10 @@ +* text=auto +* eol=lf + +.git export-ignore +.gitattributes export-ignore +.gitignore export-ignore +/.github export-ignore +phpunit.xml export-ignore +CHANGELOG.md export-ignore +/tests export-ignore diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7773bc0..696d7f7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] - php: ['7.3', '7.4', '8.0'] + php: ['7.4', '8.0'] dependency-version: [prefer-lowest, prefer-stable] steps: From d9a1017b081eb605eb7f3318324dd28dae43b883 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 20 Feb 2021 13:00:37 +0300 Subject: [PATCH 3/9] Move to PHP 7.4; Extend Atomastic Arrays; Code refactoring; Update tests --- src/Registry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Registry.php b/src/Registry.php index 939ef40..e31d1f2 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -37,7 +37,7 @@ public static function getInstance(): Registry /** * Is not allowed to call from outside to prevent from creating multiple instances, - * to use the singleton, you have to obtain the instance from Singleton::getInstance() instead + * to use the Registry, you have to obtain the instance from Registry::getInstance() instead. */ protected function __construct() { From 044ba48bf3925025e1abc210fdfcea673e07952b Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 20 Feb 2021 13:15:01 +0300 Subject: [PATCH 4/9] revert back final --- src/Registry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Registry.php b/src/Registry.php index e31d1f2..9ff06be 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -7,7 +7,7 @@ use Atomastic\Arrays\Arrays; use Exception; -class Registry extends Arrays +final class Registry extends Arrays { /** * Registry instance From c34290baec4d2dd5e3caa03f0b5641c872d0f564 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 20 Feb 2021 13:15:37 +0300 Subject: [PATCH 5/9] code refactoring --- src/Registry.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Registry.php b/src/Registry.php index 9ff06be..92fa1d1 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -55,6 +55,5 @@ protected function __clone() */ public function __wakeup(): void { - throw new Exception('Cannot unserialize a Registry.'); } } From e3c164615878bc2118726b32a1155e272ef31fb6 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 20 Feb 2021 13:19:44 +0300 Subject: [PATCH 6/9] add macroable and remove for now final class --- composer.json | 3 ++- src/Registry.php | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 8febfa0..b8cab84 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ ], "require": { "php": "^7.4 || ^8.0", - "atomastic/arrays": "^3.0" + "atomastic/arrays": "^3.0", + "atomastic/macroable": "^2.0" }, "autoload":{ "psr-4": { diff --git a/src/Registry.php b/src/Registry.php index 92fa1d1..1f2d3dc 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -5,10 +5,13 @@ namespace Atomastic\Registry; use Atomastic\Arrays\Arrays; +use Atomastic\Macroable\Macroable; use Exception; -final class Registry extends Arrays +class Registry extends Arrays { + use Macroable; + /** * Registry instance */ @@ -25,7 +28,7 @@ final class Registry extends Arrays public static function getInstance(): Registry { if (static::$instance === null) { - static::$instance = new static(); + static::$instance = new self(); } if (static::$storage === null) { From 702518c770757c660e6abde2480eedb6ade8357a Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 20 Feb 2021 13:38:45 +0300 Subject: [PATCH 7/9] add tests for macro --- tests/RegistryTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/RegistryTest.php b/tests/RegistryTest.php index 66765d6..c79a45d 100644 --- a/tests/RegistryTest.php +++ b/tests/RegistryTest.php @@ -20,3 +20,14 @@ $this->assertInstanceOf(Registry::class, $firstCall); $this->assertSame($firstCall, $secondCall); }); + +test('test macro() method', function (): void { + Registry::getInstance()->set('foo', 'bar'); + + Registry::macro('customMethod', function() { + return $this->count(); + }); + + $registry = Registry::getInstance(); + $this->assertEquals(1, $registry->customMethod()); +}); From 267c5c73675efaa400d71e46ee74d75c6de7f52f Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 20 Feb 2021 14:17:53 +0300 Subject: [PATCH 8/9] Registry 3.0.0 --- CHANGELOG.md | 9 +++++++++ LICENSE | 2 +- README.md | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f898fb..847a7a2 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ + +# [3.0.0](https://github.com/atomastic/registry) (2021-02-20) +* Move to PHP 7.4 +* add macroable functionality +* use full power of Arrays Component by extending it. +* remove final class +* general code refactoring. +* improve tests workflow. + # [2.0.0](https://github.com/atomastic/registry) (2020-12-02) * use new Arrays 2.0.0 diff --git a/LICENSE b/LICENSE index cacc3f0..6b05ac2 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2020 Sergey Romanenko +Copyright (c) 2021 Sergey Romanenko Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 7a27339..6320af6 100644 --- a/README.md +++ b/README.md @@ -30,4 +30,4 @@ Run tests ### License [The MIT License (MIT)](https://github.com/atomastic/registry/blob/master/LICENSE.txt) -Copyright (c) 2020 [Sergey Romanenko](https://github.com/Awilum) +Copyright (c) 2021 [Sergey Romanenko](https://github.com/Awilum) From a9b7b92c63d03872592501667ecdfc4f81beb388 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 20 Feb 2021 14:18:33 +0300 Subject: [PATCH 9/9] Registry 3.0.0 --- src/Registry.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Registry.php b/src/Registry.php index 1f2d3dc..722ed22 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -6,7 +6,6 @@ use Atomastic\Arrays\Arrays; use Atomastic\Macroable\Macroable; -use Exception; class Registry extends Arrays {