Skip to content

Commit

Permalink
Registry 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Apr 12, 2018
1 parent ea38569 commit 2371cdb
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# v1.0.0, 2018-04-12
* Initial release
39 changes: 37 additions & 2 deletions README.md
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)
77 changes: 77 additions & 0 deletions Registry.php
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];
}

}
23 changes: 23 additions & 0 deletions composer.json
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\\": "" }
}
}

0 comments on commit 2371cdb

Please sign in to comment.