From 79f8da8c45b15d67c4f043fb5d889af1184c8f75 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sat, 27 Sep 2014 22:28:15 -0400 Subject: [PATCH] Namespaced the fuck out of Pickles --- src/Auth.php | 2 +- src/Cache.php | 2 +- src/Config.php | 5 ++--- src/Database.php | 28 +++++++++++++++------------- src/Exception.php | 9 --------- src/Model.php | 8 ++++---- src/Object.php | 21 +++++++-------------- src/Profiler.php | 2 ++ src/Resource.php | 32 ++++++++++++++++++-------------- src/Router.php | 18 ++++++++---------- src/pickles.php | 2 +- 11 files changed, 59 insertions(+), 70 deletions(-) delete mode 100644 src/Exception.php diff --git a/src/Auth.php b/src/Auth.php index 785e6bc..147c0a3 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -2,7 +2,7 @@ namespace Pickles; -abstract class Auth extends \Object +abstract class Auth extends Object { public function basic() { diff --git a/src/Cache.php b/src/Cache.php index 1f8d7fc..b896e6d 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -84,7 +84,7 @@ class Cache extends Object $datasources = [$datasources]; } - $this->connection = new Memcache(); + $this->connection = new \Memcache(); foreach ($datasources as $name) { diff --git a/src/Config.php b/src/Config.php index 3e301e4..57d4ac9 100644 --- a/src/Config.php +++ b/src/Config.php @@ -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 )'); + throw new \Exception('You must pass an environment (e.g. php script.php )'); } // 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 { diff --git a/src/Database.php b/src/Database.php index cf69e51..ce44b43 100644 --- a/src/Database.php +++ b/src/Database.php @@ -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; } diff --git a/src/Exception.php b/src/Exception.php deleted file mode 100644 index f83fbf8..0000000 --- a/src/Exception.php +++ /dev/null @@ -1,9 +0,0 @@ -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 diff --git a/src/Object.php b/src/Object.php index 184eabf..589512d 100644 --- a/src/Object.php +++ b/src/Object.php @@ -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]; } /** diff --git a/src/Profiler.php b/src/Profiler.php index 0e4d3ee..a9ab939 100644 --- a/src/Profiler.php +++ b/src/Profiler.php @@ -222,6 +222,8 @@ class Profiler */ public static function timer($timer) { + var_dump(time()); + exit('EOF'); if (self::enabled('timers')) { // Starts the timer diff --git a/src/Resource.php b/src/Resource.php index 55b4388..ac22de6 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -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(); diff --git a/src/Router.php b/src/Router.php index 6468f2f..4a067e4 100644 --- a/src/Router.php +++ b/src/Router.php @@ -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(); } diff --git a/src/pickles.php b/src/pickles.php index 505f5e4..2fb1e66 100644 --- a/src/pickles.php +++ b/src/pickles.php @@ -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__) . '/';