Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Feb 20, 2021
2 parents 3c6bd40 + a9b7b92 commit 3fa3aa0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 278 deletions.
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
<a name="3.0.0"></a>
# [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.

<a name="2.0.0"></a>
# [2.0.0](https://github.com/atomastic/registry) (2020-12-02)
* use new Arrays 2.0.0
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
156 changes: 3 additions & 153 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ Registry Component provides a fluent, object-oriented interface for storing data

<br>

* [Installation](#installation)
* [Usage](#usage)
* [Methods](#methods)
* [Tests](#tests)
* [License](#license)

### Installation

#### With [Composer](https://getcomposer.org)
Expand All @@ -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 |
|---|---|
| <a href="#registry_getInstance">`getInstance()`</a> | Gets the instance via lazy initialization (created on first usage) |
| <a href="#registry_set">`set()`</a> | 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. |
| <a href="#registry_get">`get()`</a> | Get item from the registry. |
| <a href="#registry_has">`has()`</a> | Determine if the registry has a value for the given name. |
| <a href="#registry_delete">`delete()`</a> | Get item from the registry. |
| <a href="#registry_all">`all()`</a> | Get all items from the registry. |
| <a href="#registry_flush">`flush()`</a> | Flush all items from the registry. |

#### Methods Details

##### <a name="registry_getInstance"></a> Method: `getInstance()`

```php
/**
* Gets the instance via lazy initialization (created on first usage)
*/
public static function getInstance(): Registry
```

**Examples**

```php
$registry = Registry::getInstance();
```

##### <a name="registry_set"></a> 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');
```

##### <a name="registry_get"></a> 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');
```

##### <a name="registry_has"></a> 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...
}
```

##### <a name="registry_delete"></a> 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');
```

##### <a name="registry_all"></a> Method: `all()`

```php
/**
* Get all items from the registry.
*/
public function all(): array
```

**Examples**

```php
foreach ($registry->all() as $key => $value) {
// code...
}
```

##### <a name="registry_flush"></a> 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

Expand All @@ -180,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)
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
}
],
"require": {
"php": "^7.3 || ^8.0",
"atomastic/arrays": "^2.0"
"php": "^7.4 || ^8.0",
"atomastic/arrays": "^3.0",
"atomastic/macroable": "^2.0"
},
"autoload":{
"psr-4": {
Expand All @@ -29,8 +30,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"
}
}
91 changes: 9 additions & 82 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@
namespace Atomastic\Registry;

use Atomastic\Arrays\Arrays;
use Exception;
use Atomastic\Macroable\Macroable;

final class Registry
class Registry extends Arrays
{
use Macroable;

/**
* 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)
*/
public static function getInstance(): Registry
{
if (static::$instance === null) {
static::$instance = new static();
static::$instance = new self();
}

if (static::$storage === null) {
Expand All @@ -41,94 +39,23 @@ 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()
{

}

/**
* Prevent the instance from being cloned (which would create a second instance of it)
*/
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();
}
}
Loading

0 comments on commit 3fa3aa0

Please sign in to comment.