Namespaced the fuck out of Pickles

This commit is contained in:
Josh Sherman 2014-09-27 22:28:15 -04:00
parent 48c5289060
commit 79f8da8c45
11 changed files with 59 additions and 70 deletions

View file

@ -2,7 +2,7 @@
namespace Pickles;
abstract class Auth extends \Object
abstract class Auth extends Object
{
public function basic()
{

View file

@ -84,7 +84,7 @@ class Cache extends Object
$datasources = [$datasources];
}
$this->connection = new Memcache();
$this->connection = new \Memcache();
foreach ($datasources as $name)
{

View file

@ -52,7 +52,6 @@ class Config extends Object
$environment = false;
$is_cli = !isset($_SERVER['REQUEST_METHOD']);
// Sanity checks the config file
if (file_exists($filename) && is_file($filename) && is_readable($filename))
{
@ -77,7 +76,7 @@ class Config extends Object
// @todo is checking for argc enough?
if ($is_cli && $_SERVER['argc'] < 2)
{
throw new Exception('You must pass an environment (e.g. php script.php <environment>)');
throw new \Exception('You must pass an environment (e.g. php script.php <environment>)');
}
// Loops through the environments and tries to match on IP or name
@ -172,7 +171,7 @@ class Config extends Object
// Checks if constant is already defined, and throws an error
if (defined($constant))
{
throw new Exception('The constant ' . $constant . ' is already defined');
throw new \Exception('The constant ' . $constant . ' is already defined');
}
else
{

View file

@ -43,9 +43,9 @@ class Database extends Object
* @var string
*/
protected $attributes = [
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::NULL_EMPTY_STRING => true,
\PDO::ATTR_PERSISTENT => true,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::NULL_EMPTY_STRING => true,
];
/**
@ -160,14 +160,14 @@ class Database extends Object
{
if (!isset($config->datasources[$datasource_name]))
{
throw new Exception('The specified datasource is not defined in the config.');
throw new \Exception('The specified datasource is not defined in the config.');
}
$datasource = $config->datasources[$datasource_name];
if (!isset($datasource['driver']))
{
throw new Exception('The specified datasource lacks a driver.');
throw new \Exception('The specified datasource lacks a driver.');
}
$datasource['driver'] = strtolower($datasource['driver']);
@ -194,7 +194,7 @@ class Database extends Object
break;
default:
throw new Exception('Datasource driver "' . $datasource['driver'] . '" is invalid');
throw new \Exception('Datasource driver "' . $datasource['driver'] . '" is invalid');
break;
}
@ -240,15 +240,15 @@ class Database extends Object
case 'pdo_mysql':
// Resolves "Invalid UTF-8 sequence" issues when encoding as JSON
// @todo Didn't resolve that issue, borked some other characters though
//$this->attributes[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8';
//$this->attributes[\PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8';
break;
case 'pdo_pgsql':
// This combats a bug: https://bugs.php.net/bug.php?id=62571&edit=1
$this->attributes[PDO::ATTR_PERSISTENT] = false;
$this->attributes[\PDO::ATTR_PERSISTENT] = false;
// This allows for multiple prepared queries
$this->attributes[PDO::ATTR_EMULATE_PREPARES] = true;
$this->attributes[\PDO::ATTR_EMULATE_PREPARES] = true;
break;
}
@ -265,11 +265,13 @@ class Database extends Object
$this->dsn = str_replace(['host=;', 'port=;', 'unix_socket=;'], '', $this->dsn);
// Attempts to establish a connection
$this->connection = new PDO($this->dsn, $this->username, $this->password, $this->attributes);
$this->connection = new \PDO(
$this->dsn, $this->username, $this->password, $this->attributes
);
}
else
{
throw new Exception('There was an error loading the database configuration.');
throw new \Exception('There was an error loading the database configuration.');
}
}
@ -372,7 +374,7 @@ class Database extends Object
}
else
{
throw new Exception('No query to execute.');
throw new \Exception('No query to execute.');
}
return $this->connection->lastInsertId();
@ -396,7 +398,7 @@ class Database extends Object
}
// Pulls the results based on the type
$results = $this->results->fetchAll(PDO::FETCH_ASSOC);
$results = $this->results->fetchAll(\PDO::FETCH_ASSOC);
return $results;
}

View file

@ -1,9 +0,0 @@
<?php
namespace Pickles;
class Exception extends \Exception
{
}

View file

@ -265,7 +265,7 @@ class Model extends Object
// Errors if a table is not set. You're welcome, Geoff.
if ($this->table == false)
{
throw new Exception('You must set the table variable');
throw new \Exception('You must set the table variable.');
}
// Runs the parent constructor so we have the config
@ -353,7 +353,7 @@ class Model extends Object
{
if (is_array($parameters_or_key))
{
throw new Exception('You cannot pass in 2 query parameter arrays');
throw new \Exception('You cannot pass in 2 query parameter arrays.');
}
$this->prepareParameters($type_or_parameters);
@ -830,7 +830,7 @@ class Model extends Object
// Checks the number of values, between expects 2
if (count($value) != 2)
{
throw new Exception('BETWEEN expects an array with 2 values.');
throw new \Exception('BETWEEN expects an array with 2 values.');
}
else
{
@ -849,7 +849,7 @@ class Model extends Object
}
else
{
throw new Exception('BETWEEN expects an array.');
throw new \Exception('BETWEEN expects an array.');
}
}
else

View file

@ -70,7 +70,7 @@ class Object
public function __construct($objects = null)
{
// Gets an instance of the config, unless we ARE the config
if (get_class($this) == 'Config')
if (get_class($this) == 'Pickles\\Config')
{
$this->config = true;
}
@ -122,21 +122,14 @@ class Object
*/
public static function getInstance($class = false)
{
// In < 5.3 arguments must match in child, hence defaulting $class
// @todo Remove this, as we're no longer supporting 5.3
if ($class == false)
{
return false;
}
else
{
if (!isset(self::$instances[$class]))
{
self::$instances[$class] = new $class();
}
$class = 'Pickles\\' . $class;
return self::$instances[$class];
if (!isset(self::$instances[$class]))
{
self::$instances[$class] = new $class();
}
return self::$instances[$class];
}
/**

View file

@ -222,6 +222,8 @@ class Profiler
*/
public static function timer($timer)
{
var_dump(time());
exit('EOF');
if (self::enabled('timers'))
{
// Starts the timer

View file

@ -92,7 +92,7 @@ class Resource extends Object
{
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == false)
{
throw new Exception('400 - SSL is required.');
throw new \Exception('SSL is required.', 400);
}
}
@ -102,11 +102,14 @@ class Resource extends Object
{
if (!$this->config->pickles['auth'])
{
throw new Exception('401 - Authentication is not configured properly.');
throw new \Exception('Authentication is not configured properly.', 401);
}
/*
// This class should be in the classes directory of the service
$auth = new Auth();
$auth = '\\' . $this->config->pickles['namespace'] . '\\Auth';
var_dump($auth);
$auth = new $auth();
switch ($this->config->pickles['auth'])
{
@ -119,9 +122,10 @@ class Resource extends Object
break;
default:
throw new Exception('401 - Invalid authentication scheme.');
throw new \Exception('Invalid authentication scheme.', 401);
break;
}
*/
}
$filter = isset($this->filter[$method]);
@ -204,7 +208,7 @@ class Resource extends Object
case 'filter':
if (count($rule) < 2)
{
throw new Exception('Invalid validation rule, expected: "validate:boolean|email|float|int|ip|url".');
throw new \Exception('Invalid validation rule, expected: "validate:boolean|email|float|int|ip|url".');
}
else
{
@ -220,7 +224,7 @@ class Resource extends Object
break;
default:
throw new Exception('Invalid filter, expecting boolean, email, float, int, ip or url.');
throw new \Exception('Invalid filter, expecting boolean, email, float, int, ip or url.');
break;
}
@ -238,13 +242,13 @@ class Resource extends Object
case 'length':
if (count($rule) < 3)
{
throw new Exception('Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer".');
throw new \Exception('Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer".');
}
else
{
if (!filter_var($rule[2], FILTER_VALIDATE_INT))
{
throw new Exception('Invalid length value, expecting an integer.');
throw new \Exception('Invalid length value, expecting an integer.');
}
else
{
@ -277,7 +281,7 @@ class Resource extends Object
break;
default:
throw new Exception('Invalid operator, expecting <, <=, ==, !=, >= or >.');
throw new \Exception('Invalid operator, expecting <, <=, ==, !=, >= or >.');
break;
}
@ -296,7 +300,7 @@ class Resource extends Object
case 'regex':
if (count($rule) < 3)
{
throw new Exception('Invalid validation rule, expected: "regex:is|not:string".');
throw new \Exception('Invalid validation rule, expected: "regex:is|not:string".');
}
else
{
@ -332,17 +336,17 @@ class Resource extends Object
if ($this->errors)
{
throw new Exception('400 - Missing or invalid parameters.');
throw new \Exception('Missing or invalid parameters.', 400);
}
parent::__construct(['cache', 'db']);
// Checks if the request method has been implemented
//if (get_class($this) != 'Resource')
if (get_class($this) != 'Pickles\\Resource')
{
if (!method_exists($this, $method))
{
throw new Exception('405 - Method not allowed.');
throw new \Exception('Method not allowed.', 405);
}
else
{
@ -368,7 +372,7 @@ class Resource extends Object
}
}
}
catch (Exception $e)
catch (\Exception $e)
{
$this->status = 400;
$this->message = $e->getMessage();

View file

@ -63,23 +63,19 @@ class Router extends Object
}
// Creates our class name
array_unshift($nouns, $version);
$class = implode('_', $nouns);
// Creates our filename
array_unshift($nouns, SITE_RESOURCE_PATH);
$filename = implode('/', $nouns) . '.php';
array_unshift($nouns, '', $this->config->pickles['namespace'], 'Resources', $version);
$class = implode('\\', $nouns);
// Checks that the file is present and contains our class
if (!file_exists($filename) || !class_exists($class))
if (!class_exists($class))
{
throw new Exception('404 - Not Found.');
throw new \Exception('Not Found.', 404);
}
// Instantiates our resource with the UIDs
$resource = new $class($uids);
}
catch (Exception $e)
catch (\Exception $e)
{
// Creates a resource object if we don't have one
if (!isset($resource))
@ -87,7 +83,9 @@ class Router extends Object
$resource = new Resource();
}
$resource->status = 400;
$code = $e->getCode();
$resource->status = $code ? $code : 400;
$resource->message = $e->getMessage();
}

View file

@ -66,7 +66,7 @@ ini_set('session.hash_function', 1);
// {{{ Loads the configuration file and sets any configuration options
// Loads the base config
$config = Config::getInstance();
$config = Pickles\Config::getInstance();
// Injects PICKLES variables into the config
$config->data['pickles']['path'] = dirname(__FILE__) . '/';