[eluser]ivantcholakov[/eluser]
@ InsiteFX
Yes, I have mine too.
Code: <?php defined('BASEPATH') OR exit('No direct script access allowed.');
/**
* Registry library for CodeIgniter
*
* You may use this library for storing and accessing application-level global data.
*
* Installation:
*
* Put this file Registry.php within application/libraries/ folder of your project.
*
* Usage Example:
*
* //---------------------------------------------------------------------------
* // Context #1:
*
* $ci = get_instance(); // Use $this instead of $ci inside a controller's method.
* $ci->load->library('registry'); // You may autoload this library at will.
*
* $title = 'Page Title';
* $subtitle = 'Page Subtitle';
* $metatitle = 'Page Title (Meta)';
* $metadescription = 'Page Description (Meta)';
* $metakeywords = 'page, keywords, meta';
*
* $ci->registry
* // Method chaining is possible.
* // Set values individually:
* ->set('page_title', $title)
* ->set('page_subtitle', $subtitle)
* // Set multiple values.
* ->set(compact('metatitle', 'metadescription', 'metakeywords'))
* ;
*
* unset($title, $subtitle, $metatitle, $metadescription, $metakeywords);
*
* //---------------------------------------------------------------------------
* // Context #2:
*
* $ci = get_instance();
* $ci->load->library('registry');
*
* // Get values individually.
* $title = $ci->registry->get('page_title');
* $subtitle = $ci->registry->get('page_subtitle');
*
* // Get multiple values.
* extract($ci->registry->get(array('metatitle', 'metadescription', 'metakeywords')));
*
* // Test:
* var_dump(compact('title', 'subtitle', 'metatitle', 'metadescription', 'metakeywords'));
*
* //---------------------------------------------------------------------------
* // Also:
*
* // Check whether a particular value is present.
* $test = $ci->registry->has('test_key');
* var_dump($test);
*
* // Gets everything from the registry (for debugging purpose).
* $registry = $ci->registry->get_all();
* var_dump($registry);
*
* // Unset values.
* $ci->registry
* ->delete('page_title')
* ->delete('page_subtitle')
* ->delete(array('metatitle', 'metadescription', 'metakeywords'))
* ;
* var_dump($ci->registry->get_all());
*
* // Use destroy method only for testing purposes
* $ci->registry->destroy();
* var_dump($ci->registry->get_all());
*
* //---------------------------------------------------------------------------
*
* @author Ivan Tcholakov <[email protected]>, 2014
* @license The MIT License, http://opensource.org/licenses/MIT
*
* Code repository: https://github.com/ivantcholakov/codeigniter-registry
*/
class Registry {
protected static $data = array();
public function __construct() {
log_message('debug', 'Registry class initialized');
}
public function get($key) {
if (is_array($key)) {
$result = array();
foreach ($key as $k) {
$result[$k] = $this->get($k);
}
return $result;
}
$key = (string) $key;
if ($key != '' && array_key_exists($key, self::$data)) {
return self::$data[$key];
}
return null;
}
public function get_all() {
return self::$data;
}
public function set($key, $value = null) {
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->set($k, $v);
}
return $this;
}
self::$data[(string) $key] = $value;
return $this;
}
public function has($key) {
$key = (string) $key;
if ($key != '' && array_key_exists($key, self::$data)) {
return true;
}
return false;
}
public function delete($key) {
if (is_array($key)) {
foreach ($key as $k) {
$this->delete($k);
}
return $this;
}
$key = (string) $key;
if ($key != '' && array_key_exists($key, self::$data)) {
unset(self::$data[$key]);
}
return $this;
}
// Use this method for testing purposes only!
public function destroy() {
self::$data = array();
return $this;
}
}
|