Permalink
Browse files

major refactoring to ditch the resource locator in favor of injecting…

… specifics
  • Loading branch information...
1 parent 25c07b4 commit 5e66f3a2d4a0f3c9b5be12f703465a67fbed4484 @enygma committed Aug 10, 2012
Showing with 98 additions and 137 deletions.
  1. +13 −15 Shield/Bootstrap.php
  2. +31 −30 Shield/Config.php
  3. +11 −7 Shield/Env.php
  4. +2 −2 Shield/Filter.php
  5. +6 −7 Shield/Input.php
  6. +2 −4 Shield/Log.php
  7. +7 −8 Shield/Session.php
  8. +21 −58 Shield/Shield.php
  9. +5 −6 Shield/Template.php
View
@@ -5,59 +5,57 @@
class Bootstrap extends Base
{
- public function __construct(&$di)
+ public function __construct()
{
- parent::__construct($di);
-
// look for "_init" methods in this class and execute them
$class = new \ReflectionClass(get_class($this));
$methods = $class->getMethods();
foreach ($methods as $method) {
if (strstr($method->name, '_init') !== false) {
$name = $method->name;
- $this->$name($di);
+ $this->$name();
}
}
}
- private function _initEnvConfig(Di &$di)
+ private function _initEnvConfig()
{
error_reporting(-1);
ini_set('display_errors', 1);
ini_set('session.save_handler', 'files');
- $contentType = $di->get('Config')->get('view.content-type');
+ $contentType = Config::get('view.content-type');
$contentType = ($contentType == null) ? 'text/html' : $contentType;
- $charset = $di->get('Config')->get('view.charset');
+ $charset = Config::get('view.charset');
$charset = ($charset == null) ? 'utf-8' : $charset;
// render with the UTF-8 charset
header('Content-Type: '.$contentType.'; charset='.$charset);
}
- private function _initObjects(Di &$di)
+ private function _initObjects()
{
// set up the custom encrypted session handler
- $session = new Session($di);
- $di->register($session);
+ $session = new Session();
+ //$di->register($session);
session_start();
// see if we need to lock our session
- $sessionLock = $di->get('Config')->get('session.lock');
+ $sessionLock = Config::get('session.lock');
if ($sessionLock == true) {
$session->lock();
}
// grab our input & filter
- $di->register(new Filter($di));
- $input = new Input($di);
+ $filter = new Filter();
+ $input = new Input($filter);
session_set_cookie_params(3600, '/', $input->server('HTTP_HOST'), 1, true);
- $di->register($input);
+ //$di->register($input);
- $env = new Env($di);
+ $env = new Env($input);
$env->check();
}
}
View
@@ -8,13 +8,18 @@ class Config extends Base
* Configuration options container
* @var array
*/
- private $config = array('general'=>array());
+ private static $config = array('general'=>array());
/**
* Configuration file name (default)
* @var string
*/
- private $configFile = 'config.php';
+ private static $configFile = 'config.php';
+
+ public function __construct()
+ {
+ //nothing to see...
+ }
/**
* Load the configuration into the container (from a file)
@@ -24,10 +29,10 @@ class Config extends Base
* @throws \Exception If no config file or it's not a .php file
* @return null
*/
- public function load($path=null)
+ public static function load($path=null)
{
if ($path == null) {
- $path = './'.$this->configFile;
+ $path = './'.self::$configFile;
}
$path = realpath($path);
if (file_exists($path) && !is_readable($path)) {
@@ -43,7 +48,7 @@ public function load($path=null)
} else {
// we're good - load it!
$data = include $path;
- $this->setConfig($data);
+ self::setConfig($data);
}
}
}
@@ -53,10 +58,9 @@ public function load($path=null)
*
* @param array $config Array of configuration options
*/
- public function setConfig($config,$context='general')
+ public static function setConfig($config,$context='general')
{
- $this->config[$context] = $config;
- return $this;
+ self::$config[$context] = $config;
}
/**
@@ -66,33 +70,32 @@ public function setConfig($config,$context='general')
*
* @return null
*/
- public function setConfigFile($fileName)
+ public static function setConfigFile($fileName)
{
- $this->configFile = $fileName;
- return $this;
+ self::$configFile = $fileName;
}
/**
* Get the current value for the config filename
*
* @return null
*/
- public function getConfigFile()
+ public static function getConfigFile()
{
- return $this->configFile;
+ return self::$configFile;
}
/**
* Get the full set of config options
*
* @return array Config container (options)
*/
- public function getConfig($context=null)
- {
- if ($context !== null && isset($this->config[$context])) {
- return $this->config[$context];
+ public static function getConfig($context=null)
+ {
+ if ($context !== null && isset(self::$config[$context])) {
+ return self::$config[$context];
} else {
- return $this->config;
+ return self::$config;
}
}
@@ -103,12 +106,12 @@ public function getConfig($context=null)
*
* @return mixed Either a string value or an array
*/
- public function get($name,$context='general')
+ public static function get($name,$context='general')
{
if (strstr($name, '.') !== false) {
// an array, split it and try to find it
$parts = explode('.',$name);
- $current = $this->config[$context];
+ $current = self::$config[$context];
foreach ($parts as $p) {
if (!isset($current[$p])) { return null; }
@@ -117,8 +120,8 @@ public function get($name,$context='general')
return $current;
} else {
// just a string
- return (isset($this->config[$context][$name]))
- ? $this->config[$context][$name] : null;
+ return (isset(self::$config[$context][$name]))
+ ? self::$config[$context][$name] : null;
}
}
@@ -130,10 +133,9 @@ public function get($name,$context='general')
*
* @return object Shield\Config
*/
- public function set($name,$value,$context='general')
+ public static function set($name,$value,$context='general')
{
- $this->config[$context][$name] = $value;
- return $this;
+ self::$config[$context][$name] = $value;
}
/**
@@ -144,16 +146,15 @@ public function set($name,$value,$context='general')
*
* @return null
*/
- public function update($config,$context='general')
+ public static function update($config,$context='general')
{
foreach ($config as $option => $value) {
if (strstr($option, '.') !== false) {
$opt = explode('.',$option);
-
} else {
$opt = array($option);
}
- $this->recurseConfig($opt,$value,$this->config[$context]);
+ self::recurseConfig($opt,$value,self::$config[$context]);
}
}
@@ -166,14 +167,14 @@ public function update($config,$context='general')
*
* @return null
*/
- private function recurseConfig($opt,$value,&$config)
+ private static function recurseConfig($opt,$value,&$config)
{
$first = array_shift($opt);
if (isset($config[$first])) {
if (count($opt) == 0) {
$config[$first] = $value;
} else {
- $this->recurseConfig($opt,$value,$config[$first]);
+ self::recurseConfig($opt,$value,$config[$first]);
}
}
View
@@ -4,6 +4,13 @@
class Env extends Base
{
+ private $input = null;
+
+ public function __construct(\Shield\Input $input)
+ {
+ $this->input = $input;
+ }
+
/**
* Execute the checks for various environment issues
*
@@ -34,16 +41,13 @@ private function setFrameHeader()
*/
private function checkHttps()
{
- $config = $this->di->get('Config');
- $input = $this->di->get('Input');
-
// see if we need to be on HTTPS
- $httpsCfg = $config->get('force_https');
- $httpsSet = $input->server('HTTPS');
+ $httpsCfg = Config::get('force_https');
+ $httpsSet = $this->input->server('HTTPS');
if ($httpsCfg == true && empty($httpsSet)) {
- $host = $input->server('HTTP_HOST');
- $request = $input->server('REQUEST_URI');
+ $host = $this->input->server('HTTP_HOST');
+ $request = $this->input->server('REQUEST_URI');
$redirect= "https://".$host.$request;
header("Location: $redirect");
View
@@ -16,9 +16,9 @@ class Filter extends Base
*/
private $config = null;
- public function __construct(\Shield\Config $config)
+ public function __construct()
{
- $this->config = $config;
+ //nothing to see...
}
/**
View
@@ -40,17 +40,19 @@ class Input extends Base
*/
private $session = array();
+ private $filter = null;
+
/**
* Intiitalize the object and extract the superglobals
*
* @param object $di DI container
*
* @return null
*/
- public function __construct(Di $di)
+ public function __construct(\Shield\Filter $filter)
{
+ $this->filter = $filter;
$this->load();
- parent::__construct($di);
}
/**
@@ -89,14 +91,11 @@ public function load()
*/
private function filterInput($name, $value)
{
- // look for its filter(s)
- $filter = $this->di->get('Filter');
-
- if ($filter == null) {
+ if ($this->filter == null) {
throw new \Exception('No filter object defined!');
}
- $val = $filter->filter($name, $value);
+ $val = $this->filter->filter($name, $value);
return ($val !== false && $val !== null) ? $val : null;
}
View
@@ -4,8 +4,6 @@
class Log extends Base
{
- private $config = null;
-
/**
* Path to the log file
* @var string
@@ -60,10 +58,10 @@ public function makeLogPath($logPath=null)
*
* @return null
*/
- public function __construct(\Shield\Config $config)
+ public function __construct()
{
// check config for a path or set a default logging path
- $logPath = $config->get('log_path');
+ $logPath = Config::get('log_path');
if ($logPath !== null && is_dir(realpath($logPath)) && is_writable($logPath)) {
$this->setLogPath(realpath($logPath));
Oops, something went wrong.

0 comments on commit 5e66f3a

Please sign in to comment.