diff options
Diffstat (limited to 'src/classes/ContainerUtils.php')
-rw-r--r-- | src/classes/ContainerUtils.php | 625 |
1 files changed, 323 insertions, 302 deletions
diff --git a/src/classes/ContainerUtils.php b/src/classes/ContainerUtils.php index 10498c57..21ae51b1 100644 --- a/src/classes/ContainerUtils.php +++ b/src/classes/ContainerUtils.php @@ -1,102 +1,113 @@ <?php -/** - * PHPPgAdmin 6.0.0 - */ - + namespace PHPPgAdmin; -use Slim\App; -use Slim\Container; -use requestInstance; - -\defined('BASE_PATH') || \define('BASE_PATH', \dirname(__DIR__, 2)); -\defined('THEME_PATH') || \define('THEME_PATH', BASE_PATH . '/assets/themes'); - -\defined('DEBUGMODE') || \define('DEBUGMODE', false); -\defined('IN_TEST') || \define('IN_TEST', false); +use Kint\Kint; +use Psr\Container\ContainerInterface; +use Slim\Collection; +use Slim\DefaultServicesProvider; /** - * A class that adds convenience methods to the container. + * @property array $deploy_info + * @property \Slim\Flash\Messages $flash + * @property \GuzzleHttp\Client $fcIntranetClient + * @property \GuzzleHttp\Client $apiDteClient + * @property \Slim\Views\Twig $view + * @property \Slim\Http\Request $request + * @property \Slim\Http\Response $response + * @property string $BASE_PATH + * @property string $THEME_PATH + * @property string $subFolder + * @property bool $DEBUGMODE + * @property bool $IN_TEST + * @property string $server + * @property string $database + * @property string $schema */ -class ContainerUtils -{ - use \PHPPgAdmin\Traits\HelperTrait; - /** - * @var string - */ - const BASE_PATH = BASE_PATH; - /** - * @var string - */ - const SUBFOLDER = PHPPGA_SUBFOLDER; - /** - * @var string - */ - const DEBUGMODE = DEBUGMODE; +class ContainerUtils extends \Slim\Container implements ContainerInterface +{ use \PHPPgAdmin\Traits\HelperTrait; /** - * @var string + * @var self|null */ - const THEME_PATH = THEME_PATH; + private static $instance; - /** - * @var \Slim\Container - */ - protected $container; /** - * @var App + * $appInstance. + * + * @var null|\Slim\App */ - protected $_app; + private static $appInstance; /** + * Default settings. + * * @var array */ - protected $conf; + private $defaultSettings = [ + 'httpVersion' => '1.1', + 'responseChunkSize' => 4096, + 'outputBuffering' => 'append', + 'determineRouteBeforeAppMiddleware' => false, + 'displayErrorDetails' => false, + 'addContentLengthHeader' => true, + 'routerCacheFile' => false, + ]; + /** + * Undocumented variable + * + * @var array + */ + private static $envConfig=[ + 'BASE_PATH'=>'', + 'subFolder'=>'', + 'DEBUGMODE'=>false, + 'THEME_PATH'=>'', + + ]; /** - * @var self + * @param array $values the parameters or objects */ - protected static $_instance; + public function __construct(array $values = []) + { + parent::__construct($values); - /** - * Constructor of the ContainerUtils class. + $userSettings = $values['settings'] ?? []; + $this->registerDefaultServices($userSettings); + $this->container = $this; + self::$instance = $this; + } + /** + * Gets the subfolder. + * + * @param string $path The path + * + * @return string the subfolder */ - public function __construct() + public function getSubfolder(string $path = ''): string { - $composerinfo = \json_decode(\file_get_contents(BASE_PATH . '/composer.json')); - $appVersion = $composerinfo->extra->version; - - $phpMinVer = (\str_replace(['<', '>', '='], '', $composerinfo->require->php)); - //$this->prtrace($appVersion); - //$this->dump($composerinfo); - $settings = [ - 'determineRouteBeforeAppMiddleware' => true, - 'base_path' => self::BASE_PATH, - 'subfolder' => self::SUBFOLDER, - 'debug' => self::DEBUGMODE, - - // Configuration file version. If this is greater than that in config.inc.php, then - // the app will refuse to run. This and $conf['version'] should be incremented whenever - // backwards incompatible changes are made to config.inc.php-dist. - 'base_version' => 61, - // Application version - 'appVersion' => 'v' . $appVersion, - // Application name - 'appName' => 'phpPgAdmin6', - - // PostgreSQL and PHP minimum version - 'postgresqlMinVer' => '9.3', - 'phpMinVer' => $phpMinVer, - 'displayErrorDetails' => self::DEBUGMODE, - 'addContentLengthHeader' => false, - ]; + + return \implode(\DIRECTORY_SEPARATOR, [$this->subFolder, $path]); + } + public static function getAppInstance(array $config = []): \Slim\App + { + + $config = \array_merge(self::getDefaultConfig(), $config); + $container = self::getContainerInstance($config); - if (!self::DEBUGMODE && !IN_TEST) { - $settings['routerCacheFile'] = self::BASE_PATH . '/temp/route.cache.php'; + if (!self::$appInstance) { + self::$appInstance = new \Slim\App($container); } - $config = [ + + return self::$appInstance; + } + + public static function getContainerInstance(array $config = []): self + { + self::$envConfig = [ 'msg' => '', 'appThemes' => [ 'default' => 'Default', @@ -104,286 +115,296 @@ class ContainerUtils 'gotar' => 'Blue/Green', 'bootstrap' => 'Bootstrap3', ], - 'settings' => $settings, + 'BASE_PATH'=>$config['BASE_PATH']??dirname(__DIR__,2), + 'subFolder'=>$config['subfolder']??'', + 'DEBUGMODE'=>$config['debugmode']??false, + 'THEME_PATH'=>$config['theme_path']??dirname(__DIR__,2).'/assets/themes', + 'IN_TEST'=>$config['IN_TEST']??false, + 'webdbLastTab'=>[] ]; - $this->_app = new App($config); - // Fetch DI Container - $container = $this->_app->getContainer(); - $container['utils'] = $this; - $container['version'] = 'v' . $appVersion; - $container['errors'] = []; - $container['requestobj'] = $container['request']; - $container['responseobj'] = $container['response']; + self::$envConfig=array_merge( self::$envConfig,$config); + if (!self::$instance) { + self::$instance = new static(self::$envConfig); + + self::$instance + ->withConf(self::$envConfig) + ->setExtra() + ->setMisc() + ->setViews(); + } + //ddd($container->subfolder); + return self::$instance; +} - $this->container = $container; +/** + * Determines the redirection url according to query string. + * + * @return string the redirect url + */ +public function getRedirectUrl() +{$container=self::getContainerInstance(); + $query_string = $container->request->getUri()->getQuery(); + + // 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; + $destinationurl = self::$envConfig['subFolder'] . '/src/views/login' . ($query_string ? '?' . $query_string : ''); } - /** - * Gets the container instance. - * - * @throws \Exception (description) - * - * @return \Slim\Container the container instance - */ - public static function getContainerInstance() - { - $_instance = self::getInstance(); + return $destinationurl; +} - if (!$container = $_instance->container) { - throw new \Exception('Could not get a container'); - } +/** + * 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(); + // $this->dump(__METHOD__ . ': addMessage ' . $key . ' ' . json_encode($content)); + if ($container->flash) { + $container->flash->addMessage($key, $content); + } +} - return $container; +/** + * 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(); + $_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 + $url = $container->misc->getLastTabURL($subject) ?? ['url' => 'alldb', 'urlvars' => ['subject' => 'server']]; + $destinationurl = $this->getRedirectUrl(); + + if (!isset($_server_info['username'])) { + return $destinationurl; } - /** - * Gets the instance. - */ - public static function getInstance(): self - { - if (!$_instance = self::$_instance) { - self::$_instance = new self(); - $_instance = self::$_instance; + 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); } - - return $_instance; + $_REQUEST = \array_merge($_REQUEST, $urlvars); + $_GET = \array_merge($_GET, $urlvars); } + $actionurl = \PHPPgAdmin\Decorators\Decorator::actionurl($url['url'], $_GET); + $destinationurl = $actionurl->value($_GET); - /** - * Creates a container. - * - * @param array $conf The conf - * - * @return \Slim\App ( description_of_the_return_value ) - */ - public static function createApp($conf) - { - $_instance = self::getInstance(); - - $_instance - ->withConf($conf) - ->setExtra() - ->setMisc() - ->setViews(); + return \str_replace('views/?', "views/{$subject}?", $destinationurl); +} - //ddd($container->subfolder); - return $_instance->_app; - } +/** + * 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; + $container->offsetSet('errors', $errors); - /** - * Determines the redirection url according to query string. - * - * @return string the redirect url - */ - public function getRedirectUrl() - { - $query_string = requestInstance()->getUri()->getQuery(); + return $container; +} - // if server_id isn't set, then you will be redirected to intro - if (null === requestInstance()->getQueryParam('server')) { - $destinationurl = self::SUBFOLDER . '/src/views/intro'; +/** + * @param array $conf + */ +private function withConf($conf): self +{ + $container = self::getContainerInstance(); + $conf['plugins'] = []; + + $container->BASE_PATH=$conf['BASE_PATH']; + $container->subFolder=$conf['subfolder']; + $container->DEBUGMODE=$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 { - // otherwise, you'll be redirected to the login page for that server; - $destinationurl = self::SUBFOLDER . '/src/views/login' . ($query_string ? '?' . $query_string : ''); + $conf['display_sizes'] = [ + 'schemas' => (bool) $display_sizes, + 'tables' => (bool) $display_sizes, + ]; } - 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(); + if (!isset($conf['theme'])) { + $conf['theme'] = 'default'; } - // $this->dump(__METHOD__ . ': addMessage ' . $key . ' ' . json_encode($content)); - if ($this->container->flash) { - $this->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) - { - $_server_info = $this->container->misc->getServerInfo(); - $this->addFlash($subject, 'getDestinationWithLastTab'); - //$this->prtrace('$_server_info', $_server_info); - // If username isn't set in server_info, you should login - $url = $this->container->misc->getLastTabURL($subject) ?? ['url' => 'alldb', 'urlvars' => ['subject' => 'server']]; - $destinationurl = $this->getRedirectUrl(); - - if (!isset($_server_info['username'])) { - return $destinationurl; - } + foreach ($conf['servers'] as &$server) { + if (!isset($server['port'])) { + $server['port'] = 5432; + } - 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 (!isset($server['sslmode'])) { + $server['sslmode'] = 'unspecified'; } - $_REQUEST = \array_merge($_REQUEST, $urlvars); - $_GET = \array_merge($_GET, $urlvars); } - $actionurl = \PHPPgAdmin\Decorators\Decorator::actionurl($url['url'], $_GET); - $destinationurl = $actionurl->value($_GET); + //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; + }; + + $container->subfolder = $conf['subfolder']; + + + return $this; +} - return \str_replace('views/?', "views/{$subject}?", $destinationurl); - } +/** + * Sets the views. + * + * @return self ( description_of_the_return_value ) + */ +private function setViews() +{ + $container = self::getContainerInstance(); /** - * Adds an error to the errors array property of the container. - * - * @param string $errormsg The error msg - * - * @return\Slim\Container The app container + * @return \PHPPgAdmin\ViewManager */ - public function addError(string $errormsg): \Slim\Container - { - //dump($errormsg); - $errors = $this->container->get('errors'); - $errors[] = $errormsg; - $this->container->offsetSet('errors', $errors); + $container['view'] = static function (\Slim\Container $c): \PHPPgAdmin\ViewManager { + $misc = $c->misc; + $view = new ViewManager(BASE_PATH . '/assets/templates', [ + 'cache' => BASE_PATH . '/temp/twigcache', + 'auto_reload' => $c->get('settings')['debug'], + 'debug' => $c->get('settings')['debug'], + ], $c); - return $this->container; - } + $misc->setView($view); + + return $view; + }; + return $this; +} + +/** + * Sets the instance of Misc class. + * + * @return self ( description_of_the_return_value ) + */ +private function setMisc() +{ + $container = self::getContainerInstance(); /** - * @param array $conf + * @return \PHPPgAdmin\Misc */ - private function withConf($conf): self - { - $container = self::getContainerInstance(); - $conf['plugins'] = []; - - $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, - ]; - } + $container['misc'] = static function (\Slim\Container $c): \PHPPgAdmin\Misc { + $misc = new \PHPPgAdmin\Misc($c); - if (!isset($conf['theme'])) { - $conf['theme'] = 'default'; - } + $conf = $c->get('conf'); - foreach ($conf['servers'] as &$server) { - if (!isset($server['port'])) { - $server['port'] = 5432; - } + // 4. Check for theme by server/db/user + $_server_info = $misc->getServerInfo(); - if (!isset($server['sslmode'])) { - $server['sslmode'] = 'unspecified'; - } - } + /* starting with PostgreSQL 9.0, we can set the application name */ + if (isset($_server_info['pgVersion']) && 9 <= $_server_info['pgVersion']) { + \putenv('PGAPPNAME=' . $c->get('settings')['appName'] . '_' . $c->get('settings')['appVersion']); + } - return $conf; - }; - $container->subfolder = self::SUBFOLDER; + return $misc; + }; - return $this; - } + return $this; +} - /** - * Sets the views. - * - * @return self ( description_of_the_return_value ) - */ - private function setViews() - { - $container = self::getContainerInstance(); +private function setExtra() +{ + $container = self::getContainerInstance(); + $container['flash'] = static function (): \Slim\Flash\Messages { + return new \Slim\Flash\Messages(); + }; - /** - * @return \PHPPgAdmin\ViewManager - */ - $container['view'] = static function (\Slim\Container $c): \PHPPgAdmin\ViewManager { - $misc = $c->misc; - $view = new ViewManager(BASE_PATH . '/assets/templates', [ - 'cache' => BASE_PATH . '/temp/twigcache', - 'auto_reload' => $c->get('settings')['debug'], - 'debug' => $c->get('settings')['debug'], - ], $c); + $container['lang'] = static function (\Slim\Container $c): array { + $translations = new \PHPPgAdmin\Translations($c); - $misc->setView($view); + return $translations->lang; + }; - return $view; - }; + return $this; +} - return $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' + ], + ]; } /** - * Sets the instance of Misc class. + * This function registers the default services that Slim needs to work. * - * @return self ( description_of_the_return_value ) + * 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 setMisc() + private function registerDefaultServices($userSettings): void { - $container = self::getContainerInstance(); + $defaultSettings = $this->defaultSettings; + /** - * @return \PHPPgAdmin\Misc + * This service MUST return an array or an instance of ArrayAccess. + * + * @return array|ArrayAccess */ - $container['misc'] = static function (\Slim\Container $c): \PHPPgAdmin\Misc { - $misc = new \PHPPgAdmin\Misc($c); - - $conf = $c->get('conf'); - - // 4. Check for theme by server/db/user - $_server_info = $misc->getServerInfo(); - - /* starting with PostgreSQL 9.0, we can set the application name */ - if (isset($_server_info['pgVersion']) && 9 <= $_server_info['pgVersion']) { - \putenv('PGAPPNAME=' . $c->get('settings')['appName'] . '_' . $c->get('settings')['appVersion']); - } - - return $misc; - }; - - return $this; - } - - private function setExtra() - { - $container = self::getContainerInstance(); - $container['flash'] = static function (): \Slim\Flash\Messages { - return new \Slim\Flash\Messages(); - }; - - $container['lang'] = static function (\Slim\Container $c): array { - $translations = new \PHPPgAdmin\Translations($c); - - return $translations->lang; + $this['settings'] = static function () use ($userSettings, $defaultSettings):\Slim\Collection { + return new Collection(\array_merge($defaultSettings, $userSettings)); }; - return $this; + $defaultProvider = new DefaultServicesProvider(); + $defaultProvider->register($this); } } |