Fixing remaining bugs for route-restructure

This commit is contained in:
Dennis Eichhorn 2016-04-09 10:44:12 +02:00
parent bab6abfd0d
commit 4a127fd1ac
15 changed files with 192 additions and 61 deletions

View File

@ -73,8 +73,6 @@ class Autoloader
return $class;
}
echo $class;
return false;
}

View File

@ -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;
}
/**

View File

@ -0,0 +1,57 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <d.eichhorn@oms.com>
*/
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()));
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <d.eichhorn@oms.com>
*/
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);
}
}

View File

@ -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.');
}

View File

@ -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:

View File

@ -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');
}
}

View File

@ -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'];
}

View File

@ -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('<?php return ' . ArrayParser::serializeArray($appRoutes), $appRoutePath);
file_put_contents($appRoutePath, '<?php return ' . ArrayParser::serializeArray($appRoutes) . ';', LOCK_EX);
} else {
throw new PermissionException($appRoutePath);
}

View File

@ -139,9 +139,7 @@ abstract class ModuleAbstract
}
/** @noinspection PhpIncludeInspection */
include $path;
/** @var array $MODLANG */
$lang += $MODLANG;
$lang = include $path;
return $lang;
}

View File

@ -20,6 +20,7 @@ use phpOMS\DataStorage\Database\DatabaseType;
use phpOMS\Log\FileLogger;
use phpOMS\Message\Http\Request;
use phpOMS\System\File\PathException;
use phpOMS\Autoloader;
use phpOMS\Utils\IO\Json\InvalidJsonException;
/**
@ -296,22 +297,29 @@ class ModuleManager
// todo download;
}
$info = $this->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;

View File

@ -33,4 +33,5 @@ abstract class RouteVerb extends Enum
const GET = 1;
const PUT = 2;
const SET = 3;
const ANY = 4;
}

View File

@ -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']];
}
}
}

View File

@ -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;