Close search
Hoa

Hack book of Hoa\Registry

Static registries are arrays containing any kinds of informations. The Hoa\Registry library provides a static registry and some facilities.

Table of contents

  1. Introduction
  2. Manipulate the registry
  3. Integration to the hoa:// protocol
  4. Conclusion

Introduction

A static registry allows to store any kinds of informations, called entries. A registry behaves like a PHP array with keys and values, it means like an hashmap.

Manipulate the registry

The Hoa\Registry\Registry class provides only four static methods:

The use is trivial. We will see an example:

Hoa\Registry\Registry::set('foo', 'bar');
var_dump(Hoa\Registry\Registry::get('foo'));

/**
 * Will output:
 *     string(3) "bar"
 */

Keys must be a boolean, an integer or a string. There is no restriction, no forbidden value or character. The user is free to define its own naming conventions for the keys. We are able for example to use . (point) or / (slash) to create sort of a linearized structure; for example group.subgroup.entry. No matter, Hoa\Registry\Registry will never make any difference.

Let's see the two last methods through examples:

var_dump(Hoa\Registry\Registry::isRegistered('foo'));
Hoa\Registry\Registry::remove('foo');
var_dump(Hoa\Registry\Registry::isRegistered('foo'));

/**
 * Will output:
 *     bool(true)
 *     bool(false)
 */

Simple. The registry lives during one execution. It is globally accessible from anywhere.

Integration to the hoa:// protocol

The hoa:// protocol allows to abstract resources of any kinds and the hoa://Library branch abstracts resources related to libraries. The Hoa\Registry library provides an access to its entries thanks to hoa://Library/Registry#key where key represents the key of an entry. The simplest way to resolve such a path is to use the resolve function:

var_dump(resolve('hoa://Library/Registry#foo'));

/**
 * Will output:
 *     string(3) "bar"
 */

The use of the protocol is really useful to represent a resource stored in the registry through a URI. Since we can store any kinds of data, we can imagine to store a stream; thus:

// A dummy stream.
$stream = new Hoa\File\Read(__FILE__);
Hoa\Registry\Registry::set('stream.dummy', $stream);

And in our application, thanks to the naming conventions, we can retrieve our stream:

$stream = resolve('hoa://Library/Registry#stream.dummy');
echo $stream->readAll();

We can also store anonymous functions or anything else. The possibilities are interesting.

Conclusion

The Hoa\Registry library provides a static registry. Its integration to the hoa:// protocol propels it to new usages. Its behavior is pretty trivial but it can be often useful.

An error or a suggestion about the documentation? Contributions are welcome!

Comments

menu