From 50e23645ca6034d030804e1159d2cbda814ea1c5 Mon Sep 17 00:00:00 2001 From: ChiperSoft Date: Sun, 16 Sep 2012 15:56:12 -0700 Subject: [PATCH] Initial commit --- README.md | 24 +++++++++++ composer.json | 22 ++++++++++ lib/Primal/Environment.php | 85 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 README.md create mode 100644 composer.json create mode 100644 lib/Primal/Environment.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..98cad85 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +#Primal.Environment + +Created and Copyright 2012 by Jarvis Badgley, chiper at chipersoft dot com. + +Environment is a very simple data storage class for saving and retrieving environmental configuration variables, such as would be defined in a project's config file. + +* Any value can be stored or retrieved by calling a function with the name, prefixed by get or set. +* Example: `$env->setConvertPath('/path/to/convert')` or `$env->getConvertPath()` +* Values can also be set or retrieved directly by name: `$env->convertPath` +* Set functions return $this to allow chaining +* Any undefined value will retrieve as `null` + +##Example: + +```php +//config.php +Environment::Singleton() + ->setName('development') + ->setDBName('PrimalTest') + ->setDBUser('php') + ->setDBPassword('Ornare856') + ->setDebug(true) +; +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..59fce6b --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "Primal/Environment", + "description": "Very basic environmental settings management library", + "homepage": "http://github.com/Primal/Environment", + "license": "MIT", + "authors": [ + { + "name": "Jarvis Badgley", + "homepage": "http://chipersoft.com", + "email": "primal@chipersoft.com" + } + ], + "require": { + "php": ">=5.3.10" + }, + "type": "library", + "autoload": { + "psr-0": { + "Primal": "lib\/" + } + } +} \ No newline at end of file diff --git a/lib/Primal/Environment.php b/lib/Primal/Environment.php new file mode 100644 index 0000000..e174dbf --- /dev/null +++ b/lib/Primal/Environment.php @@ -0,0 +1,85 @@ + 'development', + ); + + /** + * Constructor, declared private to prevent external initialization + * + * @package default + */ + private function __construct() {} + + /** + * Singleton static variable + * + * @author Jarvis Badgley + */ + protected static $singleton; + + + /** + * Static Singleton retrieval function + * + * @return Request + */ + static function Singleton() { + return (!static::$singleton) ? static::$singleton = new static() : static::$singleton; + } + + + /** + * Getter & Setter Implementation + * Any value can be stored or retrieved by calling a function with the name, prefixed by get or set. + * Example: $env->setConvertPath('/path/to/convert') or $env->getConvertPath() + * Set functions return $this to allow chaining + * + * @param string $name + * @param array $args + * @return void|$this + */ + public function __call($name, $args) { + switch (substr($name, 0, 3)) { + + case 'get': + $index = strtolower(substr($name, 3)); + return isset($this->_data[$index]) ? $this->_data[$index] : null; + + case 'set': + $index = strtolower(substr($name, 4)); + $this->_data[$index] = $args[0]; + return $this; + + default: + throw new \BadMethodCallException("Unknown function, Environment->$name."); + + } + } + + + + public function __get($index) { + $index = strtolower($index); + return isset($this->_data[$index]) ? $this->_data[$index] : null; + } + + public function __set($index, $value) { + $this->_data[strtolower($index)] = $value; + } + + public function __isset($index) { + return isset($this->_data[strtolower($index)]); + } + + +}