diff options
Diffstat (limited to 'src/classes/ContainerUtils.php')
-rw-r--r-- | src/classes/ContainerUtils.php | 299 |
1 files changed, 131 insertions, 168 deletions
diff --git a/src/classes/ContainerUtils.php b/src/classes/ContainerUtils.php index b4180d12..fd719cef 100644 --- a/src/classes/ContainerUtils.php +++ b/src/classes/ContainerUtils.php @@ -1,7 +1,9 @@ <?php +/** * PHPPgAdmin 6.1.3 - + */ + namespace PHPPgAdmin; use Psr\Container\ContainerInterface; @@ -24,16 +26,18 @@ use Slim\DefaultServicesProvider; * @property string $server * @property string $database * @property string $schema - * @property + * + * @method mixed get(string) + */ class ContainerUtils extends \Slim\Container implements ContainerInterface -{ use \PHPPgAdmin\Traits\HelperTrait; +{ + use \PHPPgAdmin\Traits\HelperTrait; /** * @var null|self */ private static $instance; - /** * $appInstance. * @@ -55,7 +59,8 @@ class ContainerUtils extends \Slim\Container implements ContainerInterface 'addContentLengthHeader' => true, 'routerCacheFile' => false, ]; - /** + + /** * Undocumented variable. * * @var array @@ -66,7 +71,6 @@ class ContainerUtils extends \Slim\Container implements ContainerInterface 'DEBUGMODE' => false, 'THEME_PATH' => '', ]; - ]; /** * @param array $values the parameters or objects @@ -77,16 +81,11 @@ class ContainerUtils extends \Slim\Container implements ContainerInterface $userSettings = $values['settings'] ?? []; $this->registerDefaultServices($userSettings); - - self::$instance = $this; - } - - $userSettings = $values['settings'] ?? []; - $this->registerDefaultServices($userSettings); $this->container = $this; self::$instance = $this; } - /** + + /** * Gets the subfolder. * * @param string $path The path @@ -97,7 +96,7 @@ class ContainerUtils extends \Slim\Container implements ContainerInterface { return \implode(\DIRECTORY_SEPARATOR, [$this->subFolder, $path]); } - + public static function getAppInstance(array $config = []): \Slim\App { $config = \array_merge(self::getDefaultConfig($config['debugmode'] ?? false), $config); @@ -130,12 +129,13 @@ class ContainerUtils extends \Slim\Container implements ContainerInterface ]; self::$envConfig = \array_merge(self::$envConfig, $config); + if (!self::$instance) { self::$instance = new static(self::$envConfig); self::$instance ->withConf(self::$envConfig); - + $handlers = new ContainerHandlers(self::$instance); $handlers->setExtra() ->setMisc() @@ -143,105 +143,108 @@ class ContainerUtils extends \Slim\Container implements ContainerInterface ->storeMainRequestParams() ->setHaltHandler(); } - //ddd($container->subfolder); + //ddd($container->subfolder); return self::$instance; -} + } -/** - * Determines the redirection url according to query string. - * - * @return string the redirect url - */ -public function getRedirectUrl() -{$container=self::getContainerInstance(); + /** + * Determines the redirection url according to query string. + * + * @return string the redirect url + */ + public function getRedirectUrl() + { + $container = self::getContainerInstance(); $container = self::getContainerInstance(); $query_string = $container->request->getUri()->getQuery(); - // if server_id isn't set, then you will be redirected to intro + // if server_id isn't set, then you will be redirected to intro if (null === $container->request->getQueryParam('server')) { $destinationurl = self::$envConfig['subFolder'] . '/src/views/intro'; - } else { - // otherwise, you'll be redirected to the login page for that server; + } else { + // otherwise, you'll be redirected to the login page for that server; $destinationurl = self::$envConfig['subFolder'] . '/src/views/login' . ($query_string ? '?' . $query_string : ''); - } + } - return $destinationurl; -} + return $destinationurl; + } -/** - * Adds a flash message to the session that will be displayed on the next request. - * - * @param mixed $content msg content (can be object, array, etc) - * @param string $key The key to associate with the message. Defaults to the stack - * trace of the closure or method that called addFlassh - */ -public function addFlash($content, $key = ''): void -{ - if ('' === $key) { - $key = self::getBackTrace(); - }$container=self::getContainerInstance(); + /** + * Adds a flash message to the session that will be displayed on the next request. + * + * @param mixed $content msg content (can be object, array, etc) + * @param string $key The key to associate with the message. Defaults to the stack + * trace of the closure or method that called addFlassh + */ + public function addFlash($content, $key = ''): void + { + if ('' === $key) { + $key = self::getBackTrace(); + } + $container = self::getContainerInstance(); $container = self::getContainerInstance(); - // $this->dump(__METHOD__ . ': addMessage ' . $key . ' ' . json_encode($content)); + // $this->dump(__METHOD__ . ': addMessage ' . $key . ' ' . json_encode($content)); if ($container->flash) { $container->flash->addMessage($key, $content); + } } -} -/** - * Gets the destination with the last active tab selected for that controller - * Usually used after going through a redirect route. - * - * @param string $subject The subject, usually a view name like 'server' or 'table' - * - * @return string The destination url with last tab set in the query string - */ -public function getDestinationWithLastTab($subject) -{$container=self::getContainerInstance(); + /** + * Gets the destination with the last active tab selected for that controller + * Usually used after going through a redirect route. + * + * @param string $subject The subject, usually a view name like 'server' or 'table' + * + * @return string The destination url with last tab set in the query string + */ + public function getDestinationWithLastTab($subject) + { + $container = self::getContainerInstance(); $container = self::getContainerInstance(); $_server_info = $container->misc->getServerInfo(); - $this->addFlash($subject, 'getDestinationWithLastTab'); - //$this->prtrace('$_server_info', $_server_info); - // If username isn't set in server_info, you should login + $this->addFlash($subject, 'getDestinationWithLastTab'); + //$this->prtrace('$_server_info', $_server_info); + // If username isn't set in server_info, you should login $url = $container->misc->getLastTabURL($subject) ?? ['url' => 'alldb', 'urlvars' => ['subject' => 'server']]; - $destinationurl = $this->getRedirectUrl(); + $destinationurl = $this->getRedirectUrl(); - if (!isset($_server_info['username'])) { - return $destinationurl; - } + if (!isset($_server_info['username'])) { + return $destinationurl; + } - if (!\is_array($url)) { - return $this->getRedirectUrl($subject); - } - $this->addFlash($url, 'getLastTabURL for ' . $subject); - // Load query vars into superglobal arrays - if (isset($url['urlvars'])) { - $urlvars = []; - - foreach ($url['urlvars'] as $key => $urlvar) { - //$this->prtrace($key, $urlvar); - $urlvars[$key] = \PHPPgAdmin\Decorators\Decorator::get_sanitized_value($urlvar, $_REQUEST); + if (!\is_array($url)) { + return $this->getRedirectUrl($subject); } - $_REQUEST = \array_merge($_REQUEST, $urlvars); - $_GET = \array_merge($_GET, $urlvars); - } - $actionurl = \PHPPgAdmin\Decorators\Decorator::actionurl($url['url'], $_GET); - $destinationurl = $actionurl->value($_GET); + $this->addFlash($url, 'getLastTabURL for ' . $subject); + // Load query vars into superglobal arrays + if (isset($url['urlvars'])) { + $urlvars = []; + + foreach ($url['urlvars'] as $key => $urlvar) { + //$this->prtrace($key, $urlvar); + $urlvars[$key] = \PHPPgAdmin\Decorators\Decorator::get_sanitized_value($urlvar, $_REQUEST); + } + $_REQUEST = \array_merge($_REQUEST, $urlvars); + $_GET = \array_merge($_GET, $urlvars); + } + $actionurl = \PHPPgAdmin\Decorators\Decorator::actionurl($url['url'], $_GET); + $destinationurl = $actionurl->value($_GET); - return \str_replace('views/?', "views/{$subject}?", $destinationurl); -} + return \str_replace('views/?', "views/{$subject}?", $destinationurl); + } -/** - * Adds an error to the errors array property of the container. - * - * @param string $errormsg The error msg - * - * @return\Slim\Container The app container - */ -public function addError(string $errormsg): \Slim\Container -{ + /** + * Adds an error to the errors array property of the container. + * + * @param string $errormsg The error msg + * + * @return\Slim\Container The app container + */ + public function addError(string $errormsg): \Slim\Container + { $container = self::getContainerInstance(); $errors = $container->get('errors'); - $errors[] = $errormsg; + $errors[] = $errormsg; $container->offsetSet('errors', $errors); return $container; @@ -272,104 +275,64 @@ public function addError(string $errormsg): \Slim\Container 'appName' => 'PHPPgAdmin6', ], ]; -} + } + + /** + * @param array $conf + */ + private function withConf($conf): self + { + $container = self::getContainerInstance(); + $conf['plugins'] = []; -/** - * @param array $conf - */ -private function withConf($conf): self -{ - $container = self::getContainerInstance(); - $conf['plugins'] = []; - $container->BASE_PATH = $conf['BASE_PATH']; $container->subFolder = $conf['subfolder']; $container->debug = $conf['debugmode']; $container->THEME_PATH = $conf['theme_path']; $container->IN_TEST = $conf['IN_TEST']; $container['errors'] = []; - $container['conf'] = static function (\Slim\Container $c) use ($conf): array { - $display_sizes = $conf['display_sizes']; - - if (\is_array($display_sizes)) { - $conf['display_sizes'] = [ - 'schemas' => (bool) isset($display_sizes['schemas']) && true === $display_sizes['schemas'], - 'tables' => (bool) isset($display_sizes['tables']) && true === $display_sizes['tables'], - ]; - } else { - $conf['display_sizes'] = [ - 'schemas' => (bool) $display_sizes, - 'tables' => (bool) $display_sizes, - ]; - } - - if (!isset($conf['theme'])) { - $conf['theme'] = 'default'; - } + $container['conf'] = static function (\Slim\Container $c) use ($conf): array { + $display_sizes = $conf['display_sizes']; + + if (\is_array($display_sizes)) { + $conf['display_sizes'] = [ + 'schemas' => (bool) isset($display_sizes['schemas']) && true === $display_sizes['schemas'], + 'tables' => (bool) isset($display_sizes['tables']) && true === $display_sizes['tables'], + ]; + } else { + $conf['display_sizes'] = [ + 'schemas' => (bool) $display_sizes, + 'tables' => (bool) $display_sizes, + ]; + } - foreach ($conf['servers'] as &$server) { - if (!isset($server['port'])) { - $server['port'] = 5432; + if (!isset($conf['theme'])) { + $conf['theme'] = 'default'; } - if (!isset($server['sslmode'])) { - $server['sslmode'] = 'unspecified'; + foreach ($conf['servers'] as &$server) { + if (!isset($server['port'])) { + $server['port'] = 5432; + } + + if (!isset($server['sslmode'])) { + $server['sslmode'] = 'unspecified'; + } } - } - //self::$envConfig=[ + //self::$envConfig=[ //'BASE_PATH'=>$conf['BASE_PATH'], //'subFolder'=>$conf['subfolder'], //'DEBUGMODE'=>$conf['debugmode'], //'THEME_PATH'=>$conf['theme_path'], //'IN_TEST'=>$conf['IN_TEST'] - //]; + //]; - return $conf; - }; + return $conf; + }; $container->subFolder = $conf['subfolder']; - - - return $this; -} -/** - * This function registers the default services that Slim needs to work. - * - * All services are shared, they are registered such that the - * same instance is returned on subsequent calls. - * - * @param array $userSettings Associative array of application settings - */ - private function registerDefaultServices($userSettings): void -{ - $defaultSettings = $this->defaultSettings; - - /** - * This service MUST return an array or an instance of ArrayAccess. - * - * @return array|ArrayAccess - */ - $this['settings'] = static function () use ($userSettings, $defaultSettings): \Slim\Collection { - return new Collection(\array_merge($defaultSettings, $userSettings)); - }; - - $defaultProvider = new DefaultServicesProvider(); - $defaultProvider->register($this); - - public static function getDefaultConfig():array - { - return [ - 'settings' => [ - 'displayErrorDetails' => self::$envConfig['DEBUGMODE'], - 'determineRouteBeforeAppMiddleware' => true, - 'base_path' => \dirname(__DIR__, 2), - 'debug' => self::$envConfig['DEBUGMODE'], - 'phpMinVer' => '7.1', // PHP minimum version - 'addContentLengthHeader' => false, - 'appName' => 'PHPPgAdmin6' - ], - ]; + return $this; } /** @@ -389,7 +352,7 @@ private function withConf($conf): self * * @return array|ArrayAccess */ - $this['settings'] = static function () use ($userSettings, $defaultSettings):\Slim\Collection { + $this['settings'] = static function () use ($userSettings, $defaultSettings): \Slim\Collection { return new Collection(\array_merge($defaultSettings, $userSettings)); }; |