-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# v1.0.0, 2018-04-12 | ||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,37 @@ | ||
# registry | ||
The Registry component provides a mechanism for storing data globally in a well managed fashion, helping to prevent global meltdown. | ||
# Registry Component | ||
![version](https://img.shields.io/badge/version-1.0.0-brightgreen.svg?style=flat-square "Version") | ||
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/flextype-components/registry/blob/master/LICENSE) | ||
|
||
Registry component provides a mechanism for storing data globally in a well managed fashion, helping to prevent global meltdown. | ||
|
||
### Installation | ||
|
||
``` | ||
composer require flextype-components/registry | ||
``` | ||
|
||
### Usage | ||
|
||
```php | ||
use Flextype\Component\Registry\Registry; | ||
``` | ||
|
||
Checks if an object with this name is in the registry. | ||
```php | ||
if (Registry::exists('var')) { | ||
// Do something... | ||
} | ||
``` | ||
|
||
Registers a given value under a given name. | ||
```php | ||
Registry::set('var', 'value'); | ||
``` | ||
|
||
Fetch an item from the registry. | ||
```php | ||
$var = Registry::get('var', 'value'); | ||
``` | ||
|
||
## License | ||
See [LICENSE](https://github.com/flextype-components/registry/blob/master/LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/** | ||
* @package Flextype Components | ||
* | ||
* @author Sergey Romanenko <awilum@yandex.ru> | ||
* @link http://components.flextype.org | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flextype\Component\Registry; | ||
|
||
class Registry | ||
{ | ||
/** | ||
* Registry of variables | ||
* | ||
* @var array | ||
*/ | ||
private static $registry = []; | ||
|
||
/** | ||
* Checks if an object with this name is in the registry. | ||
* | ||
* if (Registry::exists('var')) { | ||
* // Do something... | ||
* } | ||
* | ||
* @param string $name The name of the registry item to check for existence. | ||
* @return bool | ||
*/ | ||
public static function exists(string $name) : bool | ||
{ | ||
return isset(Registry::$registry[(string) $name]); | ||
} | ||
|
||
/** | ||
* Registers a given value under a given name. | ||
* | ||
* Registry::set('var', 'value'); | ||
* | ||
* @param string $name The name of the value to store. | ||
* @param mixed $value The value that needs to be stored. | ||
* @return mixed | ||
*/ | ||
public static function set(string $name, $value = null) | ||
{ | ||
// delete item | ||
if ($value === null) { | ||
unset(Registry::$registry[$name]); | ||
} else { | ||
Registry::$registry[$name] = $value; | ||
|
||
return Registry::get($name); | ||
} | ||
} | ||
|
||
/** | ||
* Fetch an item from the registry. | ||
* | ||
* $var = Registry::get('var', 'value'); | ||
* | ||
* @param string $name The name of the item to fetch. | ||
* @return mixed | ||
*/ | ||
public static function get(string $name) | ||
{ | ||
if ( ! isset(Registry::$registry[$name])) { | ||
throw new RuntimeException('No item "' . $name . '" exists in the registry.'); | ||
} | ||
|
||
return Registry::$registry[$name]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "flextype-components/registry", | ||
"keywords": ["registry", "storage"], | ||
"type": "library", | ||
"license": "MIT", | ||
"homepage": "https://github.com/flextype-components/registry", | ||
"description": "Registry component provides a mechanism for storing data globally in a well managed fashion, helping to prevent global meltdown.", | ||
"authors": [ | ||
{ | ||
"name": "Sergey Romanenko", | ||
"email": "awilum@yandex.ru" | ||
} | ||
], | ||
"support": { | ||
"issues": "https://github.com/flextype-components/registry/issues" | ||
}, | ||
"require": { | ||
"php": "^7.1.3" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Flextype\\Component\\Registry\\": "" } | ||
} | ||
} |