From 39635c1115400da28810f1fa2416a135affef1e2 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 8 Sep 2018 18:22:34 +0200 Subject: [PATCH] Remove todos and make use of query builder --- Config/SettingsAbstract.php | 1 - DataStorage/Session/SessionInterface.php | 2 - Localization/Localization.php | 2 - Module/ModuleManager.php | 123 +++++++++-------------- 4 files changed, 47 insertions(+), 81 deletions(-) diff --git a/Config/SettingsAbstract.php b/Config/SettingsAbstract.php index 01f851055..48ee145ee 100644 --- a/Config/SettingsAbstract.php +++ b/Config/SettingsAbstract.php @@ -82,7 +82,6 @@ abstract class SettingsAbstract implements OptionsInterface * @return mixed Option value * * @since 1.0.0 - * @todo: don't db request if exists. check exists() */ public function get($columns) { diff --git a/DataStorage/Session/SessionInterface.php b/DataStorage/Session/SessionInterface.php index 3e9103433..3bab2fd70 100644 --- a/DataStorage/Session/SessionInterface.php +++ b/DataStorage/Session/SessionInterface.php @@ -65,8 +65,6 @@ interface SessionInterface /** * Save session. * - * @todo : implement save type (session, cache, database) - * * @return void * * @since 1.0.0 diff --git a/Localization/Localization.php b/Localization/Localization.php index 0b1e01383..ca8224341 100644 --- a/Localization/Localization.php +++ b/Localization/Localization.php @@ -251,8 +251,6 @@ final class Localization * * @return void * - * @todo : maybe make parameter int - * * @since 1.0.0 */ public function setTimezone(string $timezone) : void diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index ef32866eb..d35aabedb 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -22,6 +22,7 @@ use phpOMS\Message\Http\Request; use phpOMS\Message\RequestAbstract; use phpOMS\System\File\PathException; use phpOMS\Module\Exception\InvalidModuleException; +use phpOMs\DataStorage\Database\Query\Builder; /** * Modules class. @@ -141,44 +142,19 @@ final class ModuleManager public function getUriLoad(RequestAbstract $request) : array { if ($this->uriLoad === null) { - switch ($this->app->dbPool->get('select')->getType()) { - case DatabaseType::MYSQL: - $uriHash = $request->getHash(); - $uriPdo = ''; + $uriHash = $request->getHash(); + $uriPdo = ''; - $i = 1; - $c = \count($uriHash); + $query = new Builder($this->app->dbPool->get('select')); + $query->prefix($this->app->dbPool->get('select')->prefix); + $sth = $query->select('module_load.module_load_type', 'module_load.*') + ->from('module_load') + ->innerJoin('module')->on('module_load.module_load_from', '=', 'module.module_id')->orOn('module_load.module_load_for', '=', 'module.module_id') + ->whereIn('module_load.module_load_pid', $uriHash) + ->andWhere('module.module_active', '=', 1) + ->execute(); - for ($k = 0; $k < $c; ++$k) { - $uriPdo .= ':pid' . $i . ','; - ++$i; - } - - $uriPdo = \rtrim($uriPdo, ','); - - /* TODO: make join in order to see if they are active */ - $sth = $this->app->dbPool->get('select')->con->prepare( - 'SELECT - `' . $this->app->dbPool->get('select')->prefix . 'module_load`.`module_load_type`, `' . $this->app->dbPool->get('select')->prefix . 'module_load`.* - FROM - `' . $this->app->dbPool->get('select')->prefix . 'module_load` - WHERE - `module_load_pid` IN(' . $uriPdo . ')' - ); - - $i = 1; - foreach ($uriHash as $hash) { - $sth->bindValue(':pid' . $i, $hash, \PDO::PARAM_STR); - ++$i; - } - - $sth->execute(); - - $this->uriLoad = $sth->fetchAll(\PDO::FETCH_GROUP); - break; - default: - throw new InvalidDatabaseTypeException($this->app->dbPool->get('select')->getType()); - } + $this->uriLoad = $sth->fetchAll(\PDO::FETCH_GROUP); } return $this->uriLoad; @@ -196,31 +172,29 @@ final class ModuleManager public function getActiveModules(bool $useCache = true) : array { if (empty($this->active) || !$useCache) { - switch ($this->app->dbPool->get('select')->getType()) { - case DatabaseType::MYSQL: - $sth = $this->app->dbPool->get('select')->con->prepare('SELECT `module_path` FROM `' . $this->app->dbPool->get('select')->prefix . 'module` WHERE `module_active` = 1'); - $sth->execute(); - $active = $sth->fetchAll(\PDO::FETCH_COLUMN); + $query = new Builder($this->app->dbPool->get('select')); + $query->prefix($this->app->dbPool->get('select')->prefix); + $sth = $query->select('module.module_path') + ->from('module') + ->where('module.module_active', '=', 1) + ->execute(); - foreach ($active as $module) { - $path = $this->modulePath . '/' . $module . '/info.json'; + $active = $sth->fetchAll(\PDO::FETCH_COLUMN); + + foreach ($active as $module) { + $path = $this->modulePath . '/' . $module . '/info.json'; - if (!\file_exists($path)) { - continue; - // throw new PathException($path); - } + if (!\file_exists($path)) { + continue; + // throw new PathException($path); + } - $content = \file_get_contents($path); - $json = \json_decode($content === false ? '[]' : $content, true); - $this->active[$json['name']['internal']] = $json === false ? [] : $json; - } - break; - default: - throw new InvalidDatabaseTypeException($this->app->dbPool->get('select')->getType()); + $content = \file_get_contents($path); + $json = \json_decode($content === false ? '[]' : $content, true); + $this->active[$json['name']['internal']] = $json === false ? [] : $json; } - } - + } return $this->active; } @@ -294,28 +268,25 @@ final class ModuleManager public function getInstalledModules(bool $useCache = true) : array { if (empty($this->installed) || !$useCache) { - switch ($this->app->dbPool->get('select')->getType()) { - case DatabaseType::MYSQL: - $sth = $this->app->dbPool->get('select')->con->prepare('SELECT `module_path` FROM `' . $this->app->dbPool->get('select')->prefix . 'module`'); - $sth->execute(); - $installed = $sth->fetchAll(\PDO::FETCH_COLUMN); + $query = new Builder($this->app->dbPool->get('select')); + $query->prefix($this->app->dbPool->get('select')->prefix); + $sth = $query->select('module.module_path') + ->from('module') + ->execute(); - foreach ($installed as $module) { - $path = $this->modulePath . '/' . $module . '/info.json'; - - if (!\file_exists($path)) { - continue; - // throw new PathException($path); - } - - $content = \file_get_contents($path); - $json = \json_decode($content === false ? '[]' : $content, true); - $this->installed[$json['name']['internal']] = $json === false ? [] : $json; - } + $installed = $sth->fetchAll(\PDO::FETCH_COLUMN); - break; - default: - throw new InvalidDatabaseTypeException($this->app->dbPool->get('select')->getType()); + foreach ($installed as $module) { + $path = $this->modulePath . '/' . $module . '/info.json'; + + if (!\file_exists($path)) { + continue; + // throw new PathException($path); + } + + $content = \file_get_contents($path); + $json = \json_decode($content === false ? '[]' : $content, true); + $this->installed[$json['name']['internal']] = $json === false ? [] : $json; } }