diff --git a/src/Cache.php b/src/Cache.php
index b896e6d..28a862b 100644
--- a/src/Cache.php
+++ b/src/Cache.php
@@ -75,9 +75,9 @@ class Cache extends Object
parent::__construct();
// @todo Shouldn't need the isset() but Travis is failing some tests
- if (isset($this->config->pickles['cache']) && $this->config->pickles['cache'])
+ if (isset($this->config['pickles']['cache']) && $this->config['pickles']['cache'])
{
- $datasources = $this->config->pickles['cache'];
+ $datasources = $this->config['pickles']['cache'];
if (!is_array($datasources))
{
@@ -88,9 +88,9 @@ class Cache extends Object
foreach ($datasources as $name)
{
- if (isset($this->config->datasources[$name]))
+ if (isset($this->config['datasources'][$name]))
{
- $datasource = $this->config->datasources[$name];
+ $datasource = $this->config['datasources'][$name];
$this->connection->addServer($datasource['hostname'], $datasource['port']);
$this->servers++;
diff --git a/src/Config.php b/src/Config.php
index 57d4ac9..bdb1255 100644
--- a/src/Config.php
+++ b/src/Config.php
@@ -26,17 +26,10 @@ namespace Pickles;
* custom config files on the fly as well. The core of PICKLES uses the class
* as a Singleton so we're not loading the configuration multiple times per
* page load.
- *
- * @usage $config = new Config($filename);
*/
-class Config extends Object
+class Config extends \ArrayObject
{
- /**
- * Config data
- *
- * @var array
- */
- public $data = [];
+ private static $_instance = false;
/**
* Constructor
@@ -45,78 +38,76 @@ class Config extends Object
*/
public function __construct()
{
- parent::__construct();
-
- $filename = SITE_PATH . 'config.php';
+ $filename = getcwd() . '/../../pickles.php';
$environments = false;
$environment = false;
- $is_cli = !isset($_SERVER['REQUEST_METHOD']);
+ $cli = PHP_SAPI == 'cli';
- // Sanity checks the config file
- if (file_exists($filename) && is_file($filename) && is_readable($filename))
- {
- require $filename;
- }
+ // Only require in case you want to reload the config
+ require $filename;
// Checks that we have the config array
- if (isset($config))
+ if (!isset($config))
{
- // Determines the environment
- if (isset($config['environment']))
- {
- $environment = $config['environment'];
- }
- else
- {
- if (isset($config['environments']) && is_array($config['environments']))
- {
- $environments = $config['environments'];
+ throw new \Exception('Missing $config array.');
+ }
- // If we're on the CLI, check an environment was even passed in
- // @todo is checking for argc enough?
- if ($is_cli && $_SERVER['argc'] < 2)
+ // Determines the environment
+ if (isset($config['environment']))
+ {
+ $environment = $config['environment'];
+ }
+ else
+ {
+ if (isset($config['environments']) && is_array($config['environments']))
+ {
+ $environments = $config['environments'];
+
+ // If we're on the CLI, check an environment was even passed in
+ if ($cli && $_SERVER['argc'] < 2)
+ {
+ throw new \Exception('You must pass an environment (e.g. php script.php )');
+ }
+
+ // Loops through the environments and looks for a match
+ foreach ($config['environments'] as $name => $hosts)
+ {
+ if (!is_array($hosts))
{
- throw new \Exception('You must pass an environment (e.g. php script.php )');
+ $hosts = [$hosts];
}
- // Loops through the environments and tries to match on IP or name
- foreach ($config['environments'] as $name => $hosts)
+ // Tries to determine the environment name
+ foreach ($hosts as $host)
{
- if (!is_array($hosts))
+ if ($cli)
{
- $hosts = [$hosts];
- }
-
- // Tries to determine the environment name
- foreach ($hosts as $host)
- {
- if ($is_cli)
+ // Checks the first argument on the command line
+ if ($_SERVER['argv'][1] == $name)
{
- // Checks the first argument on the command line
- if ($_SERVER['argv'][1] == $name)
- {
- $environment = $name;
- break;
- }
+ $environment = $name;
+ break;
}
- else
+ }
+ else
+ {
+ // Exact match
+ if ((preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $host)
+ && $_SERVER['SERVER_ADDR'] == $host)
+ || (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == $host))
{
- // Exact match
- if ((preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $host)
- && $_SERVER['SERVER_ADDR'] == $host)
- || (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == $host))
- {
- $environment = $name;
- break;
- }
- // Fuzzy match
- elseif (substr($host,0,1) == '/' && (preg_match($host, $_SERVER['SERVER_NAME'], $matches) > 0 || preg_match($host, $_SERVER['HTTP_HOST'], $matches) > 0))
- {
- $environments[$name] = $matches[0];
- $environment = $name;
- $config['environments'][$name] = $matches[0];
- break;
- }
+ $environment = $name;
+ break;
+ }
+ // Fuzzy match
+ elseif (substr($host,0,1) == '/'
+ && (preg_match($host, $_SERVER['SERVER_NAME'], $matches) > 0
+ || preg_match($host, $_SERVER['HTTP_HOST'], $matches) > 0))
+ {
+ $environments[$name] = $matches[0];
+ $environment = $name;
+ $config['environments'][$name] = $matches[0];
+ break;
}
}
}
@@ -124,66 +115,32 @@ class Config extends Object
}
// Flattens the array based on the environment
- $this->data = $this->flatten($environment, $config);
+ $config = $this->flatten($environment, $config);
// Restore environments value
if ($environments != false)
{
- $this->data['environments'] = $environments;
+ $config['environments'] = $environments;
}
// Sets the environment if it's not set already
- if (!isset($this->data['environment']))
+ if (!isset($config['environment']))
{
- $this->data['environment'] = $environment;
+ $config['environment'] = $environment;
}
- // Defaults profiler to true if it doesn't match an option exactly
- if (isset($this->data['pickles']['profiler']))
- {
- // If we have an array convert to a string
- if (is_array($this->data['pickles']['profiler']))
- {
- $this->data['pickles']['profiler'] = implode(',', $this->data['pickles']['profiler']);
- }
- }
- else
- {
- $this->data['pickles']['profiler'] = false;
- }
+ // Defaults expected Pickles options to false
+ $this['pickles'] = [
+ 'cache' => false,
+ 'profiler' => false,
+ ];
- // Defaults expected PICKLES options to false
- foreach (['cache', 'logging', 'minify'] as $variable)
+ // Assigns the config variables to the object
+ foreach ($config as $variable => $value)
{
- if (!isset($this->data['pickles'][$variable]))
- {
- $this->data['pickles'][$variable] = false;
- }
+ $this[$variable] = $value;
}
-
- // Creates constants for the security levels
- if (isset($this->data['security']['levels']) && is_array($this->data['security']['levels']))
- {
- foreach ($this->data['security']['levels'] as $value => $name)
- {
- $constant = 'SECURITY_LEVEL_' . strtoupper($name);
-
- // Checks if constant is already defined, and throws an error
- if (defined($constant))
- {
- throw new \Exception('The constant ' . $constant . ' is already defined');
- }
- else
- {
- define($constant, $value);
- }
- }
- }
-
- return true;
}
-
- return false;
}
/**
@@ -229,28 +186,14 @@ class Config extends Object
* @param string $class name of the class to instantiate
* @return object self::$instance instance of the Config class
*/
- public static function getInstance($class = 'Config')
+ public static function getInstance()
{
- return parent::getInstance($class);
- }
-
- /**
- * Magic Getter Method
- *
- * Attempts to load the config variable. If it's not set, will override
- * the variable with boolean false.
- *
- * @param string $name name of the variable requested
- * @return mixed value of the variable or boolean false
- */
- public function __get($name)
- {
- if (!isset($this->data[$name]))
+ if (!self::$_instance)
{
- $this->data[$name] = false;
+ self::$_instance = new Config();
}
- return $this->data[$name];
+ return self::$_instance;
}
}
diff --git a/src/Database.php b/src/Database.php
index ce44b43..d4e5ff7 100644
--- a/src/Database.php
+++ b/src/Database.php
@@ -135,13 +135,13 @@ class Database extends Object
// Tries to load a datasource if one wasn't specified
if (!$datasource_name)
{
- if (isset($config->pickles['datasource']))
+ if (isset($config['pickles']['datasource']))
{
- $datasource_name = $config->pickles['datasource'];
+ $datasource_name = $config['pickles']['datasource'];
}
- elseif (is_array($config->datasources))
+ elseif (is_array($config['datasources']))
{
- $datasources = $config->datasources;
+ $datasources = $config['datasources'];
foreach ($datasources as $name => $datasource)
{
@@ -158,12 +158,12 @@ class Database extends Object
{
if (!isset(self::$instances['Database'][$datasource_name]))
{
- if (!isset($config->datasources[$datasource_name]))
+ if (!isset($config['datasources'][$datasource_name]))
{
throw new \Exception('The specified datasource is not defined in the config.');
}
- $datasource = $config->datasources[$datasource_name];
+ $datasource = $config['datasources'][$datasource_name];
if (!isset($datasource['driver']))
{
@@ -328,13 +328,13 @@ class Database extends Object
$sql .= "\n" . '/* [' . implode('|', $files) . '] */';
// Establishes if we're working on an EXPLAIN
- if (Profiler::enabled('explains'))
+ if ($this->config['pickles']['profiler'])
{
$explain = preg_match('/^SELECT /i', $sql);
}
else
{
- $explain = null;
+ $explain = false;
}
// Executes a standard query
@@ -367,9 +367,9 @@ class Database extends Object
$duration = $end_time - $start_time;
// Logs the information to the profiler
- if (Profiler::enabled('explains', 'queries'))
+ if ($this->config['pickles']['profiler'])
{
- Profiler::logQuery($sql, $input_parameters, (isset($explain) ? $explain : false), $duration);
+ Profiler::logQuery($sql, $input_parameters, $explain, $duration);
}
}
else
diff --git a/src/Object.php b/src/Object.php
index b271898..c0f5d53 100644
--- a/src/Object.php
+++ b/src/Object.php
@@ -55,13 +55,6 @@ class Object
*/
public $db = null;
- /**
- * Profiler flag
- *
- * @var mixed
- */
- public $profiler = false;
-
/**
* Constructor
*
@@ -96,14 +89,8 @@ class Object
}
}
- // Assigns the profiler flag
- $this->profiler = (isset($this->config->pickles['profiler']) && $this->config->pickles['profiler'] != '' ? $this->config->pickles['profiler'] : false);
-
// Optionally logs the constructor to the profiler
- if ($this->profiler === true
- || ((is_array($this->profiler)
- && in_array('objects', $this->profiler))
- || stripos($this->profiler, 'objects') !== false))
+ if ($this->config['pickles']['profiler'])
{
Profiler::log($this, '__construct');
}
@@ -143,10 +130,7 @@ class Object
public function __destruct()
{
// Optionally logs the destructor to the profiler
- if ($this->profiler === true
- || ((is_array($this->profiler)
- && in_array('objects', $this->profiler))
- || stripos($this->profiler, 'objects') !== false))
+ if ($this->config['pickles']['profiler'])
{
Profiler::log($this, '__destruct');
}
diff --git a/src/Profiler.php b/src/Profiler.php
index 0e4d3ee..9d76d0a 100644
--- a/src/Profiler.php
+++ b/src/Profiler.php
@@ -80,7 +80,7 @@ class Profiler
public static function enabled(/* polymorphic */)
{
$config = Config::getInstance();
- $config = isset($config->pickles['profiler']) ? $config->pickles['profiler'] : false;
+ $config = $config['pickles']['profiler'];
// Checks if we're set to boolean true
if ($config === true)
diff --git a/src/Resource.php b/src/Resource.php
index 598db4b..938f2ac 100644
--- a/src/Resource.php
+++ b/src/Resource.php
@@ -98,18 +98,18 @@ class Resource extends Object
if ($this->auth === true
|| (isset($this->auth[$method]) && $this->auth[$method]))
{
- if (!$this->config->pickles['auth'])
+ if (!$this->config['pickles']['auth'])
{
throw new \Exception('Authentication is not configured properly.', 401);
}
/*
// This class should be in the classes directory of the service
- $auth = '\\' . $this->config->pickles['namespace'] . '\\Auth';
+ $auth = '\\' . $this->config['pickles']['namespace'] . '\\Auth';
var_dump($auth);
$auth = new $auth();
- switch ($this->config->pickles['auth'])
+ switch ($this->config['pickles']['auth'])
{
case 'basic':
$auth->basic();
@@ -327,11 +327,8 @@ class Resource extends Object
else
{
/*
- // Gets the profiler status
- $profiler = $this->config['pickles']['profiler'];
-
// Starts a timer before the resource is executed
- if ($profiler)
+ if ($this->config['pickles']['profiler'])
{
Profiler::timer('resource ' . $method);
}
@@ -341,7 +338,7 @@ class Resource extends Object
/*
// Stops the resource timer
- if ($profiler)
+ if ($this->config['pickles']['profiler'])
{
Profiler::timer('resource ' . $method);
}
diff --git a/src/Router.php b/src/Router.php
index 90c8cf8..7630857 100644
--- a/src/Router.php
+++ b/src/Router.php
@@ -63,7 +63,7 @@ class Router extends Object
}
// Creates our class name
- array_unshift($nouns, '', $this->config->pickles['namespace'], 'Resources', $version);
+ array_unshift($nouns, '', $this->config['pickles']['namespace'], 'Resources', $version);
$class = implode('\\', $nouns);
// Strips preceding slashs when there is no namespace
diff --git a/src/pickles.php b/src/pickles.php
index e028bcf..69b263c 100644
--- a/src/pickles.php
+++ b/src/pickles.php
@@ -21,18 +21,6 @@
* @usage require_once 'pickles.php';
*/
-// {{{ PICKLES Constants
-
-// @todo Finish reworking constants to be part of the Config object
-if (!defined('SITE_PATH'))
-{
- // Establishes our site paths, sanity check is to allow vfsStream in our tests
- define('SITE_PATH', getcwd() . '/../');
-}
-
-// }}}
-// {{{ Defaults some important configuration options
-
// Turns on error before the config is loaded to help catch parse errors
ini_set('display_errors', true);
error_reporting(-1);
@@ -43,29 +31,15 @@ if (ini_get('date.timezone') == '')
ini_set('date.timezone', 'Etc/UTC');
}
-// Sets the session variables
-ini_set('session.cache_expire', 86400);
-ini_set('session.entropy_file', '/dev/urandom');
-ini_set('session.entropy_length', 512);
-ini_set('session.gc_maxlifetime', 86400);
-ini_set('session.gc_probability', 1);
-ini_set('session.gc_divisor', 1000);
-ini_set('session.hash_function', 1);
-
-// }}}
-// {{{ Loads the configuration file and sets any configuration options
-
// Loads the base config
$config = Pickles\Config::getInstance();
// Configures any available PHP configuration options
-if (is_array($config->php) && count($config->php))
+if (isset($config['php']) && is_array($config['php']))
{
- foreach ($config->php as $variable => $value)
+ foreach ($config['php'] as $variable => $value)
{
ini_set($variable, $value);
}
}
-// }}}
-