joomla / registry
Joomla Registry Package
Fund package maintenance!
joomla
community.joomla.org/sponsorship-campaigns.html
Installs: 343 468
Dependents: 34
Suggesters: 7
Security: 0
Stars: 17
Watchers: 15
Forks: 20
Open Issues: 3
Type:joomla-package
Requires
- php: ^8.1.0
- joomla/utilities: ^3.0
Requires (Dev)
- phan/phan: ^5.4.2
- phpstan/phpstan: ^1.10.7
- phpunit/phpunit: ^9.5.28
- squizlabs/php_codesniffer: ^3.7.2
- symfony/yaml: ^5.0
Suggests
- ext-json: ext-json is needed for JSON support
- ext-simplexml: ext-simplexml is needed for XML support
- symfony/yaml: Install symfony/yaml if you require YAML support.
- dev-3.x-dev / 3.0.x-dev
- 3.0.0
- dev-2.0-dev / 2.0.x-dev
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-rc
- 2.0.0-beta
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0
- 1.0-beta3
- 1.0-beta2
- 1.0-beta
- 1.0-alpha
- dev-4.x-dev
- dev-php-8.2
- dev-stringToObject
- dev-1.x-dev
- dev-return-value
This package is auto-updated.
Last update: 2024-12-10 07:12:34 UTC
README
The Registry package provides an indexed key-value data store and an API for importing/exporting this data to several formats.
Load config by Registry
use Joomla\Registry\Registry; $registry = new Registry(); // Load by string $registry->loadString('{"foo" : "bar"}'); $registry->loadString('<root></root>', 'xml'); // Load by object or array $registry->loadObject($object); $registry->loadArray($array); // Load by file $registry->loadFile($root . '/config/config.json', 'json');
Accessing a Registry by getter & setter
Get value
$registry->get('foo'); // Get a non-exists value and return default $registry->get('foo', 'default'); // OR $registry->get('foo') ?: 'default';
Set value
// Set value $registry->set('bar', $value); // Sets a default value if not already assigned. $registry->def('bar', $default);
Accessing children value by path
$json = '{ "parent" : { "child" : "Foo" } }'; $registry = new Registry($json); $registry->get('parent.child'); // return 'Foo' $registry->set('parent.child', $value);
Removing values from Registry
// Set value $registry->set('bar', $value); // Remove the key $registry->remove('bar'); // Works for nested keys too $registry->set('nested.bar', $value); $registry->remove('nested.bar');
Accessing a Registry as an Array
The Registry
class implements ArrayAccess
so the properties of the registry can be accessed as an array. Consider the following examples:
// Set a value in the registry. $registry['foo'] = 'bar'; // Get a value from the registry; $value = $registry['foo']; // Check if a key in the registry is set. if (isset($registry['foo'])) { echo 'Say bar.'; }
Merge Registry
Using load* methods to merge two config files.
$json1 = '{ "field" : { "keyA" : "valueA", "keyB" : "valueB" } }'; $json2 = '{ "field" : { "keyB" : "a new valueB" } }'; $registry->loadString($json1); $registry->loadString($json2);
Output
Array(
field => Array(
keyA => valueA
keyB => a new valueB
)
)
Merge another Registry
$object1 = '{ "foo" : "foo value", "bar" : { "bar1" : "bar value 1", "bar2" : "bar value 2" } }'; $object2 = '{ "foo" : "foo value", "bar" : { "bar2" : "new bar value 2" } }'; $registry1 = new Registry(json_decode($object1)); $registry2 = new Registry(json_decode($object2)); $registry1->merge($registry2);
If you just want to merge first level, do not hope recursive:
$registry1->merge($registry2, false); // Set param 2 to false that Registry will only merge first level
Dump to one dimension
$array = array( 'flower' => array( 'sunflower' => 'light', 'sakura' => 'samurai' ) ); $registry = new Registry($array); // Make data to one dimension $flatted = $registry->flatten(); print_r($flatted);
The result:
Array
(
[flower.sunflower] => light
[flower.sakura] => samurai
)
Installation via Composer
Add "joomla/registry": "~3.0"
to the 'require' block in your composer.json and then run composer install
.
{ "require": { "joomla/registry": "~3.0" } }
Alternatively, you can simply run the following from the command line:
composer require joomla/registry "~3.0"