Namespaced the fuck out of Pickles
This commit is contained in:
parent
48c5289060
commit
79f8da8c45
11 changed files with 59 additions and 70 deletions
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace Pickles;
|
namespace Pickles;
|
||||||
|
|
||||||
abstract class Auth extends \Object
|
abstract class Auth extends Object
|
||||||
{
|
{
|
||||||
public function basic()
|
public function basic()
|
||||||
{
|
{
|
||||||
|
|
|
@ -84,7 +84,7 @@ class Cache extends Object
|
||||||
$datasources = [$datasources];
|
$datasources = [$datasources];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->connection = new Memcache();
|
$this->connection = new \Memcache();
|
||||||
|
|
||||||
foreach ($datasources as $name)
|
foreach ($datasources as $name)
|
||||||
{
|
{
|
||||||
|
|
|
@ -52,7 +52,6 @@ class Config extends Object
|
||||||
$environment = false;
|
$environment = false;
|
||||||
$is_cli = !isset($_SERVER['REQUEST_METHOD']);
|
$is_cli = !isset($_SERVER['REQUEST_METHOD']);
|
||||||
|
|
||||||
|
|
||||||
// Sanity checks the config file
|
// Sanity checks the config file
|
||||||
if (file_exists($filename) && is_file($filename) && is_readable($filename))
|
if (file_exists($filename) && is_file($filename) && is_readable($filename))
|
||||||
{
|
{
|
||||||
|
@ -77,7 +76,7 @@ class Config extends Object
|
||||||
// @todo is checking for argc enough?
|
// @todo is checking for argc enough?
|
||||||
if ($is_cli && $_SERVER['argc'] < 2)
|
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
|
// 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
|
// Checks if constant is already defined, and throws an error
|
||||||
if (defined($constant))
|
if (defined($constant))
|
||||||
{
|
{
|
||||||
throw new Exception('The constant ' . $constant . ' is already defined');
|
throw new \Exception('The constant ' . $constant . ' is already defined');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -43,9 +43,9 @@ class Database extends Object
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $attributes = [
|
protected $attributes = [
|
||||||
PDO::ATTR_PERSISTENT => true,
|
\PDO::ATTR_PERSISTENT => true,
|
||||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
||||||
PDO::NULL_EMPTY_STRING => true,
|
\PDO::NULL_EMPTY_STRING => true,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -160,14 +160,14 @@ class Database extends Object
|
||||||
{
|
{
|
||||||
if (!isset($config->datasources[$datasource_name]))
|
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];
|
$datasource = $config->datasources[$datasource_name];
|
||||||
|
|
||||||
if (!isset($datasource['driver']))
|
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']);
|
$datasource['driver'] = strtolower($datasource['driver']);
|
||||||
|
@ -194,7 +194,7 @@ class Database extends Object
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Exception('Datasource driver "' . $datasource['driver'] . '" is invalid');
|
throw new \Exception('Datasource driver "' . $datasource['driver'] . '" is invalid');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,15 +240,15 @@ class Database extends Object
|
||||||
case 'pdo_mysql':
|
case 'pdo_mysql':
|
||||||
// Resolves "Invalid UTF-8 sequence" issues when encoding as JSON
|
// Resolves "Invalid UTF-8 sequence" issues when encoding as JSON
|
||||||
// @todo Didn't resolve that issue, borked some other characters though
|
// @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;
|
break;
|
||||||
|
|
||||||
case 'pdo_pgsql':
|
case 'pdo_pgsql':
|
||||||
// This combats a bug: https://bugs.php.net/bug.php?id=62571&edit=1
|
// 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 allows for multiple prepared queries
|
||||||
$this->attributes[PDO::ATTR_EMULATE_PREPARES] = true;
|
$this->attributes[\PDO::ATTR_EMULATE_PREPARES] = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,11 +265,13 @@ class Database extends Object
|
||||||
$this->dsn = str_replace(['host=;', 'port=;', 'unix_socket=;'], '', $this->dsn);
|
$this->dsn = str_replace(['host=;', 'port=;', 'unix_socket=;'], '', $this->dsn);
|
||||||
|
|
||||||
// Attempts to establish a connection
|
// 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
|
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
|
else
|
||||||
{
|
{
|
||||||
throw new Exception('No query to execute.');
|
throw new \Exception('No query to execute.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->connection->lastInsertId();
|
return $this->connection->lastInsertId();
|
||||||
|
@ -396,7 +398,7 @@ class Database extends Object
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pulls the results based on the type
|
// Pulls the results based on the type
|
||||||
$results = $this->results->fetchAll(PDO::FETCH_ASSOC);
|
$results = $this->results->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Pickles;
|
|
||||||
|
|
||||||
class Exception extends \Exception
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -265,7 +265,7 @@ class Model extends Object
|
||||||
// Errors if a table is not set. You're welcome, Geoff.
|
// Errors if a table is not set. You're welcome, Geoff.
|
||||||
if ($this->table == false)
|
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
|
// Runs the parent constructor so we have the config
|
||||||
|
@ -353,7 +353,7 @@ class Model extends Object
|
||||||
{
|
{
|
||||||
if (is_array($parameters_or_key))
|
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);
|
$this->prepareParameters($type_or_parameters);
|
||||||
|
@ -830,7 +830,7 @@ class Model extends Object
|
||||||
// Checks the number of values, between expects 2
|
// Checks the number of values, between expects 2
|
||||||
if (count($value) != 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
|
else
|
||||||
{
|
{
|
||||||
|
@ -849,7 +849,7 @@ class Model extends Object
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new Exception('BETWEEN expects an array.');
|
throw new \Exception('BETWEEN expects an array.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -70,7 +70,7 @@ class Object
|
||||||
public function __construct($objects = null)
|
public function __construct($objects = null)
|
||||||
{
|
{
|
||||||
// Gets an instance of the config, unless we ARE the config
|
// 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;
|
$this->config = true;
|
||||||
}
|
}
|
||||||
|
@ -122,21 +122,14 @@ class Object
|
||||||
*/
|
*/
|
||||||
public static function getInstance($class = false)
|
public static function getInstance($class = false)
|
||||||
{
|
{
|
||||||
// In < 5.3 arguments must match in child, hence defaulting $class
|
$class = 'Pickles\\' . $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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$instances[$class];
|
if (!isset(self::$instances[$class]))
|
||||||
|
{
|
||||||
|
self::$instances[$class] = new $class();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return self::$instances[$class];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -222,6 +222,8 @@ class Profiler
|
||||||
*/
|
*/
|
||||||
public static function timer($timer)
|
public static function timer($timer)
|
||||||
{
|
{
|
||||||
|
var_dump(time());
|
||||||
|
exit('EOF');
|
||||||
if (self::enabled('timers'))
|
if (self::enabled('timers'))
|
||||||
{
|
{
|
||||||
// Starts the timer
|
// Starts the timer
|
||||||
|
|
|
@ -92,7 +92,7 @@ class Resource extends Object
|
||||||
{
|
{
|
||||||
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == false)
|
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'])
|
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
|
// 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'])
|
switch ($this->config->pickles['auth'])
|
||||||
{
|
{
|
||||||
|
@ -119,9 +122,10 @@ class Resource extends Object
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Exception('401 - Invalid authentication scheme.');
|
throw new \Exception('Invalid authentication scheme.', 401);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
$filter = isset($this->filter[$method]);
|
$filter = isset($this->filter[$method]);
|
||||||
|
@ -204,7 +208,7 @@ class Resource extends Object
|
||||||
case 'filter':
|
case 'filter':
|
||||||
if (count($rule) < 2)
|
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
|
else
|
||||||
{
|
{
|
||||||
|
@ -220,7 +224,7 @@ class Resource extends Object
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,13 +242,13 @@ class Resource extends Object
|
||||||
case 'length':
|
case 'length':
|
||||||
if (count($rule) < 3)
|
if (count($rule) < 3)
|
||||||
{
|
{
|
||||||
throw new Exception('Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer".');
|
throw new \Exception('Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer".');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!filter_var($rule[2], FILTER_VALIDATE_INT))
|
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
|
else
|
||||||
{
|
{
|
||||||
|
@ -277,7 +281,7 @@ class Resource extends Object
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Exception('Invalid operator, expecting <, <=, ==, !=, >= or >.');
|
throw new \Exception('Invalid operator, expecting <, <=, ==, !=, >= or >.');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -296,7 +300,7 @@ class Resource extends Object
|
||||||
case 'regex':
|
case 'regex':
|
||||||
if (count($rule) < 3)
|
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
|
else
|
||||||
{
|
{
|
||||||
|
@ -332,17 +336,17 @@ class Resource extends Object
|
||||||
|
|
||||||
if ($this->errors)
|
if ($this->errors)
|
||||||
{
|
{
|
||||||
throw new Exception('400 - Missing or invalid parameters.');
|
throw new \Exception('Missing or invalid parameters.', 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::__construct(['cache', 'db']);
|
parent::__construct(['cache', 'db']);
|
||||||
|
|
||||||
// Checks if the request method has been implemented
|
// Checks if the request method has been implemented
|
||||||
//if (get_class($this) != 'Resource')
|
if (get_class($this) != 'Pickles\\Resource')
|
||||||
{
|
{
|
||||||
if (!method_exists($this, $method))
|
if (!method_exists($this, $method))
|
||||||
{
|
{
|
||||||
throw new Exception('405 - Method not allowed.');
|
throw new \Exception('Method not allowed.', 405);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -368,7 +372,7 @@ class Resource extends Object
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception $e)
|
catch (\Exception $e)
|
||||||
{
|
{
|
||||||
$this->status = 400;
|
$this->status = 400;
|
||||||
$this->message = $e->getMessage();
|
$this->message = $e->getMessage();
|
||||||
|
|
|
@ -63,23 +63,19 @@ class Router extends Object
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates our class name
|
// Creates our class name
|
||||||
array_unshift($nouns, $version);
|
array_unshift($nouns, '', $this->config->pickles['namespace'], 'Resources', $version);
|
||||||
$class = implode('_', $nouns);
|
$class = implode('\\', $nouns);
|
||||||
|
|
||||||
// Creates our filename
|
|
||||||
array_unshift($nouns, SITE_RESOURCE_PATH);
|
|
||||||
$filename = implode('/', $nouns) . '.php';
|
|
||||||
|
|
||||||
// Checks that the file is present and contains our class
|
// 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
|
// Instantiates our resource with the UIDs
|
||||||
$resource = new $class($uids);
|
$resource = new $class($uids);
|
||||||
}
|
}
|
||||||
catch (Exception $e)
|
catch (\Exception $e)
|
||||||
{
|
{
|
||||||
// Creates a resource object if we don't have one
|
// Creates a resource object if we don't have one
|
||||||
if (!isset($resource))
|
if (!isset($resource))
|
||||||
|
@ -87,7 +83,9 @@ class Router extends Object
|
||||||
$resource = new Resource();
|
$resource = new Resource();
|
||||||
}
|
}
|
||||||
|
|
||||||
$resource->status = 400;
|
$code = $e->getCode();
|
||||||
|
|
||||||
|
$resource->status = $code ? $code : 400;
|
||||||
$resource->message = $e->getMessage();
|
$resource->message = $e->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,7 +66,7 @@ ini_set('session.hash_function', 1);
|
||||||
// {{{ Loads the configuration file and sets any configuration options
|
// {{{ Loads the configuration file and sets any configuration options
|
||||||
|
|
||||||
// Loads the base config
|
// Loads the base config
|
||||||
$config = Config::getInstance();
|
$config = Pickles\Config::getInstance();
|
||||||
|
|
||||||
// Injects PICKLES variables into the config
|
// Injects PICKLES variables into the config
|
||||||
$config->data['pickles']['path'] = dirname(__FILE__) . '/';
|
$config->data['pickles']['path'] = dirname(__FILE__) . '/';
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue