From 4a127fd1acaf03951c37c09fa0973c4e29f4788e Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 9 Apr 2016 10:44:12 +0200 Subject: [PATCH] Fixing remaining bugs for route-restructure --- Autoloader.php | 2 - Config/SettingsAbstract.php | 36 ++++++----- .../Connection/ConnectionException.php | 0 .../Database/DatabaseExceptionFactory.php | 57 +++++++++++++++++ .../Schema/Exception/TableException.php | 62 +++++++++++++++++++ Dispatcher/Dispatcher.php | 9 ++- Message/Http/Request.php | 5 +- Message/Http/Response.php | 1 + Module/InfoManager.php | 14 ++--- Module/InstallerAbstract.php | 12 ++-- Module/ModuleAbstract.php | 4 +- Module/ModuleManager.php | 46 ++++++++------ Router/RouteVerb.php | 1 + Router/Router.php | 3 +- Views/View.php | 1 - 15 files changed, 192 insertions(+), 61 deletions(-) create mode 100644 DataStorage/Database/Connection/ConnectionException.php create mode 100644 DataStorage/Database/DatabaseExceptionFactory.php create mode 100644 DataStorage/Database/Schema/Exception/TableException.php diff --git a/Autoloader.php b/Autoloader.php index 8d5f545bf..6907dfad7 100644 --- a/Autoloader.php +++ b/Autoloader.php @@ -73,8 +73,6 @@ class Autoloader return $class; } - echo $class; - return false; } diff --git a/Config/SettingsAbstract.php b/Config/SettingsAbstract.php index 7cf6a8eb1..a0ead306e 100644 --- a/Config/SettingsAbstract.php +++ b/Config/SettingsAbstract.php @@ -18,6 +18,7 @@ namespace phpOMS\Config; use phpOMS\DataStorage\Database\DatabaseType; use phpOMS\DataStorage\Database\Query\Builder; +use phpOMS\DataStorage\Database\DatabaseExceptionFactory; /** * Settings class. @@ -90,25 +91,30 @@ abstract class SettingsAbstract implements OptionsInterface */ public function get(array $columns) { - $options = []; + try { + $options = []; - switch ($this->connection->getType()) { - case DatabaseType::MYSQL: - $query = new Builder($this->connection); - $sql = $query->select(...static::$columns) - ->from($this->connection->prefix . static::$table) - ->where(static::$columns[0], 'in', $columns) - ->toSql(); + switch ($this->connection->getType()) { + case DatabaseType::MYSQL: + $query = new Builder($this->connection); + $sql = $query->select(...static::$columns) + ->from($this->connection->prefix . static::$table) + ->where(static::$columns[0], 'in', $columns) + ->toSql(); - $sth = $this->connection->con->prepare($sql); - $sth->execute(); + $sth = $this->connection->con->prepare($sql); + $sth->execute(); - $options = $sth->fetchAll(\PDO::FETCH_KEY_PAIR); - $this->setOptions($options); - break; + $options = $sth->fetchAll(\PDO::FETCH_KEY_PAIR); + $this->setOptions($options); + break; + } + + return $options; + } catch (\PDOException $e) { + // todo does it mean that the recognition isn't here but at the place where the new happens? + throw DatabaseExceptionFactory::create($e); } - - return $options; } /** diff --git a/DataStorage/Database/Connection/ConnectionException.php b/DataStorage/Database/Connection/ConnectionException.php new file mode 100644 index 000000000..e69de29bb diff --git a/DataStorage/Database/DatabaseExceptionFactory.php b/DataStorage/Database/DatabaseExceptionFactory.php new file mode 100644 index 000000000..1524f84b2 --- /dev/null +++ b/DataStorage/Database/DatabaseExceptionFactory.php @@ -0,0 +1,57 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database; + +use phpOMS\DataStorage\Database\Schema\Exception\TableException; + +/** + * Path exception class. + * + * @category System + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class DatabaseExceptionFactory +{ + /** + * Constructor. + * + * @param string $message Exception message + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function create(\PDOException $e) : \PDOException + { + switch($e->getCode()) { + case '42S02': + return self::createTableViewException($e); + default: + return $e; + } + } + + private static function createTableViewException(\PDOException $e) : \PDOException + { + return new TableException(TableException::findTable($e->getMessage())); + } +} diff --git a/DataStorage/Database/Schema/Exception/TableException.php b/DataStorage/Database/Schema/Exception/TableException.php new file mode 100644 index 000000000..836ad46c9 --- /dev/null +++ b/DataStorage/Database/Schema/Exception/TableException.php @@ -0,0 +1,62 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database\Schema\Exception; + +/** + * Path exception class. + * + * @category System + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class TableException extends \PDOException +{ + /** + * Constructor. + * + * @param string $message Exception message + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(string $message, int $code = 0, \Exception $previous = null) + { + parent::__construct('The table "' . $message . '" doesn\'t exist.', $code, $previous); + } + + public static function findTable(string $message) : string + { + $pos1 = strpos($message, '\''); + + if($pos1 === false) { + return $message; + } + + $pos2 = strpos($message, '\'', $pos1+1); + + if($pos2 === false) { + return $message; + } + + return substr($message, $pos1+1, $pos2-$pos1-1); + } +} diff --git a/Dispatcher/Dispatcher.php b/Dispatcher/Dispatcher.php index bd961a9c9..dd113d76b 100644 --- a/Dispatcher/Dispatcher.php +++ b/Dispatcher/Dispatcher.php @@ -85,8 +85,7 @@ class Dispatcher $views = []; $type = ViewLayout::UNDEFINED; - if (is_array($controller) && isset($controller['type'])) { - $type = $controller['type']; + if (is_array($controller) && isset($controller['dest'])) { $controller = $controller['dest']; } @@ -95,7 +94,7 @@ class Dispatcher } elseif (is_array($controller)) { $views += $this->dispatchArray($controller, $request, $response, $data); } elseif ($controller instanceof \Closure) { - $views[$type][] = $this->dispatchClosure($controller, $request, $response, $data); + $views[] = $this->dispatchClosure($controller, $request, $response, $data); } else { throw new \UnexpectedValueException('Unexpected controller type.'); } @@ -111,9 +110,9 @@ class Dispatcher if (($c = count($dispatch)) == 3) { /* Handling static functions */ - $views[$type][$controller] = $dispatch[0]::$dispatch[2](); + $views[$controller] = $dispatch[0]::$dispatch[2](); } elseif ($c == 2) { - $views[$type][$controller] = $this->controllers[$dispatch[0]]->{$dispatch[1]}($request, $response, $data); + $views[$controller] = $this->controllers[$dispatch[0]]->{$dispatch[1]}($request, $response, $data); } else { throw new \UnexpectedValueException('Unexpected function.'); } diff --git a/Message/Http/Request.php b/Message/Http/Request.php index 6780bc4ee..629f9a8c6 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -20,6 +20,7 @@ use phpOMS\Message\RequestAbstract; use phpOMS\Uri\Http; use phpOMS\Uri\UriFactory; use phpOMS\Uri\UriInterface; +use phpOMS\Router\RouteVerb; /** * Request class. @@ -387,7 +388,7 @@ class Request extends RequestAbstract public function getMethod() : string { if (!isset($this->method)) { - $this->method = $_SERVER['REQUEST_METHOD']; + $this->method = $_SERVER['REQUEST_METHOD'] ?? RequestMethod::GET; } return $this->method; @@ -432,7 +433,7 @@ class Request extends RequestAbstract public function getRouteVerb() : int { - switch($this->method) { + switch($this->getMethod()) { case RequestMethod::GET: return RouteVerb::GET; case RequestMethod::PUT: diff --git a/Message/Http/Response.php b/Message/Http/Response.php index 3f5a1c812..752937651 100644 --- a/Message/Http/Response.php +++ b/Message/Http/Response.php @@ -157,6 +157,7 @@ class Response extends ResponseAbstract implements RenderableInterface $render .= json_encode($response); // TODO: remove this. This should never happen since then someone forgot to set the correct header. it should be json header! } else { + var_dump($response); throw new \Exception('Wrong response type'); } } diff --git a/Module/InfoManager.php b/Module/InfoManager.php index ecc436153..343491d2a 100644 --- a/Module/InfoManager.php +++ b/Module/InfoManager.php @@ -59,18 +59,18 @@ class InfoManager * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct(string $module) + public function __construct($path) { $this->path = $path; } public function load() { - if (($path = realpath($oldPath = ModuleAbstract::MODULE_PATH . '/' . $module . '/info.json')) === false || Validator::startsWith($path, ModuleAbstract::MODULE_PATH)) { - throw new PathException($oldPath); + if (($path = realpath($this->path)) === false) { + throw new PathException($this->path); } - $this->info = json_decode(file_get_contents($this->path), true); + $this->info = json_decode(file_get_contents($path), true); } /** @@ -121,12 +121,12 @@ class InfoManager return $this->info['name']['internal']; } - public function getDependencies() : string + public function getDependencies() : array { return $this->info['dependencies']; } - public function getProviding() : string + public function getProviding() : array { return $this->info['providing']; } @@ -141,7 +141,7 @@ class InfoManager return $this->info['version']; } - public function getLoad() : string + public function getLoad() : array { return $this->info['load']; } diff --git a/Module/InstallerAbstract.php b/Module/InstallerAbstract.php index 09ff3a34b..8fb39a65e 100644 --- a/Module/InstallerAbstract.php +++ b/Module/InstallerAbstract.php @@ -16,6 +16,9 @@ namespace phpOMS\Module; use phpOMS\DataStorage\Database\Pool; +use phpOMS\Module\InfoManager; +use phpOMS\Router\RouteVerb; +use phpOMS\Utils\Parser\Php\ArrayParser; /** * Installer Abstract class. @@ -44,10 +47,9 @@ class InstallerAbstract */ public static function install(Pool $dbPool, InfoManager $info) { - - self::$installRoutes(ROOT_PATH . '/Web/Routes.php', ROOT_PATH . '/Modules/' . $info['directory'] . '/Admin/Routes/http.php'); - self::$installRoutes(ROOT_PATH . '/Socket/Routes.php', ROOT_PATH . '/Modules/' . $info['directory'] . '/Admin/Routes/socket.php'); - self::$installRoutes(ROOT_PATH . '/Console/Routes.php', ROOT_PATH . '/Modules/' . $info['directory'] . '/Admin/Routes/console.php'); + self::installRoutes(ROOT_PATH . '/Web/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/http.php'); + self::installRoutes(ROOT_PATH . '/Socket/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/socket.php'); + self::installRoutes(ROOT_PATH . '/Console/Routes.php', ROOT_PATH . '/Modules/' . $info->getDirectory() . '/Admin/Routes/console.php'); } private static function installRoutes(string $appRoutePath, string $moduleRoutePath) @@ -58,7 +60,7 @@ class InstallerAbstract $appRoutes = array_merge_recursive($appRoutes, $moduleRoutes); if(is_writable($appRoutePath)) { - file_put_contents('loadInfo(); + try { + $info = $this->loadInfo($module); - $this->registerInDatabase(); - $this->installDependencies($info->getDependencies()); - $this->installModule($module); - $this->installed[$module] = true; + $this->registerInDatabase($info); + $this->installed[$module] = $info; + $this->installDependencies($info->getDependencies()); + $this->installModule($info); - /* Install providing */ - $providing = $info->getProviding(); - foreach ($providing as $key => $version) { - $this->installProviding($module, $key); - } + /* Install providing */ + $providing = $info->getProviding(); + foreach ($providing as $key => $version) { + $this->installProviding($module, $key); + } - /* Install receiving */ - foreach ($installed as $key => $value) { - $this->installProviding($key, $module); + /* Install receiving */ + foreach ($installed as $key => $value) { + $this->installProviding($key, $module); + } + } catch(PathException $e) { + // todo: handle module doesn't exist or files are missing + //echo $e->getMessage(); + } catch(\Exception $e) { + //echo $e->getMessage(); } } @@ -365,13 +373,11 @@ class ModuleManager } } - private function installModule(string $module) + private function installModule(InfoManager $info) { + $class = '\\Modules\\' . $info->getDirectory() . '\\Admin\\Installer'; - - $class = '\\Modules\\' . $module . '\\Admin\\Installer'; - - if(!Autoloader::exist($class)) { + if(!Autoloader::exists($class)) { throw new \Exception('Module installer does not exist'); } @@ -384,10 +390,10 @@ class ModuleManager $path = realpath($oldPath = self::MODULE_PATH . '/' . $module . '/' . 'info.json'); if ($path === false || strpos($path, self::MODULE_PATH) === false) { - throw new PathException($module); + throw new PathException($oldPath); } - $info = InfoManager($module); + $info = new InfoManager($path); $info->load(); return $info; diff --git a/Router/RouteVerb.php b/Router/RouteVerb.php index 5cb9493a1..7fe08ddfb 100644 --- a/Router/RouteVerb.php +++ b/Router/RouteVerb.php @@ -33,4 +33,5 @@ abstract class RouteVerb extends Enum const GET = 1; const PUT = 2; const SET = 3; + const ANY = 4; } diff --git a/Router/Router.php b/Router/Router.php index bb1703aeb..8723951ee 100644 --- a/Router/Router.php +++ b/Router/Router.php @@ -16,6 +16,7 @@ namespace phpOMS\Router; use phpOMS\Views\ViewLayout; +use phpOMS\Message\RequestAbstract; /** * Router class. @@ -92,7 +93,7 @@ class Router foreach ($this->routes as $route => $destination) { foreach ($destination as $d) { if ($this->match($route, $d['verb'], $request->getUri(), $request->getRouteVerb())) { - $bound[$route][] = ['dest' => $d['dest'], 'type' => $d['type']]; + $bound[] = ['dest' => $d['dest']]; } } } diff --git a/Views/View.php b/Views/View.php index c58fed212..9e08b94a3 100644 --- a/Views/View.php +++ b/Views/View.php @@ -16,7 +16,6 @@ namespace phpOMS\Views; use phpOMS\ApplicationAbstract; -use phpOMS\Contract\RenderableInterface; use phpOMS\Localization\Localization; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract;