Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Twipped committed Sep 16, 2012
0 parents commit 50e2364
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
;
```
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"require": {
"php": ">=5.3.10"
},
"type": "library",
"autoload": {
"psr-0": {
"Primal": "lib\/"
}
}
}
85 changes: 85 additions & 0 deletions lib/Primal/Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Primal;

/**
* Very simple class for managing environmental variables.
*
* @package default
*/

class Environment {
protected $_data = array(
'name' => '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)]);
}


}

0 comments on commit 50e2364

Please sign in to comment.