Passed
Pull Request — develop (#340)
by Felipe
04:56
created

ContainerUtils::withConf()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 54
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 28
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 54
rs 8.4444

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
 
4
namespace PHPPgAdmin;
5
6
use Kint\Kint;
7
use Psr\Container\ContainerInterface;
8
use Slim\Collection;
9
use Slim\DefaultServicesProvider;
10
11
/**
12
 * @property array $deploy_info
13
 * @property \Slim\Flash\Messages $flash
14
 * @property \GuzzleHttp\Client $fcIntranetClient
15
 * @property \GuzzleHttp\Client $apiDteClient
16
 * @property \Slim\Views\Twig $view
17
 * @property \Slim\Http\Request $request
18
 * @property \Slim\Http\Response $response
19
 * @property string $BASE_PATH
20
 * @property string $THEME_PATH
21
 * @property string $subFolder
22
 * @property bool $DEBUGMODE
23
 * @property bool $IN_TEST
24
 * @property string  $server
25
 * @property string $database
26
 * @property string  $schema
27
 */
28
class ContainerUtils extends \Slim\Container implements ContainerInterface
29
{    use \PHPPgAdmin\Traits\HelperTrait;
0 ignored issues
show
Coding Style introduced by
Opening class brace must be on a line by itself
Loading history...
30
31
    /**
32
     * @var self|null
33
     */
34
    private static $instance;
0 ignored issues
show
Coding Style introduced by
Private member variable "instance" must be prefixed with an underscore
Loading history...
35
36
37
    /**
38
     * $appInstance.
39
     *
40
     * @var null|\Slim\App
41
     */
42
    private static $appInstance;
0 ignored issues
show
Coding Style introduced by
Private member variable "appInstance" must be prefixed with an underscore
Loading history...
43
44
    /**
45
     * Default settings.
46
     *
47
     * @var array
48
     */
49
    private $defaultSettings = [
0 ignored issues
show
Coding Style introduced by
Private member variable "defaultSettings" must be prefixed with an underscore
Loading history...
50
        'httpVersion' => '1.1',
51
        'responseChunkSize' => 4096,
52
        'outputBuffering' => 'append',
53
        'determineRouteBeforeAppMiddleware' => false,
54
        'displayErrorDetails' => false,
55
        'addContentLengthHeader' => true,
56
        'routerCacheFile' => false,
57
    ];
58
        /**
59
     * Undocumented variable
0 ignored issues
show
Coding Style introduced by
Expected 9 space(s) before asterisk; 5 found
Loading history...
60
     *
0 ignored issues
show
Coding Style introduced by
Expected 9 space(s) before asterisk; 5 found
Loading history...
61
     * @var array
0 ignored issues
show
Coding Style introduced by
Expected 9 space(s) before asterisk; 5 found
Loading history...
62
     */
0 ignored issues
show
Coding Style introduced by
Expected 9 space(s) before asterisk; 5 found
Loading history...
63
    private static $envConfig=[
0 ignored issues
show
Coding Style introduced by
Private member variable "envConfig" must be prefixed with an underscore
Loading history...
64
        'BASE_PATH'=>'',
65
        'subFolder'=>'',
66
        'DEBUGMODE'=>false,
67
        'THEME_PATH'=>'',
68
69
    ];
70
71
    /**
72
     * @param array $values the parameters or objects
73
     */
74
    public function __construct(array $values = [])
75
    {
76
        parent::__construct($values);
77
78
        $userSettings = $values['settings'] ?? [];
79
        $this->registerDefaultServices($userSettings);
80
        $this->container = $this;
81
        self::$instance = $this;
82
    }
83
   /**
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 3
Loading history...
84
     * Gets the subfolder.
0 ignored issues
show
Coding Style introduced by
Expected 4 space(s) before asterisk; 5 found
Loading history...
85
     *
0 ignored issues
show
Coding Style introduced by
Expected 4 space(s) before asterisk; 5 found
Loading history...
86
     * @param string $path The path
0 ignored issues
show
Coding Style introduced by
Expected 4 space(s) before asterisk; 5 found
Loading history...
87
     *
0 ignored issues
show
Coding Style introduced by
Expected 4 space(s) before asterisk; 5 found
Loading history...
88
     * @return string the subfolder
0 ignored issues
show
Coding Style introduced by
Expected 4 space(s) before asterisk; 5 found
Loading history...
89
     */
0 ignored issues
show
Coding Style introduced by
Expected 4 space(s) before asterisk; 5 found
Loading history...
90
    public function getSubfolder(string $path = ''): string
91
    {
92
       
93
        return \implode(\DIRECTORY_SEPARATOR, [$this->subFolder, $path]);
94
    }
95
    public static function getAppInstance(array $config = []): \Slim\App
96
    {
97
98
        $config = \array_merge(self::getDefaultConfig(), $config);
99
        $container = self::getContainerInstance($config);
100
101
        if (!self::$appInstance) {
102
            self::$appInstance = new \Slim\App($container);
103
        }
104
105
        return self::$appInstance;
106
    }
107
108
    public static function getContainerInstance(array $config = []): self
109
    {
110
        self::$envConfig = [
111
            'msg' => '',
112
            'appThemes' => [
113
                'default' => 'Default',
114
                'cappuccino' => 'Cappuccino',
115
                'gotar' => 'Blue/Green',
116
                'bootstrap' => 'Bootstrap3',
117
            ],
118
            'BASE_PATH'=>$config['BASE_PATH']??dirname(__DIR__,2),
119
            'subFolder'=>$config['subfolder']??'',
120
            'DEBUGMODE'=>$config['debugmode']??false,
121
            'THEME_PATH'=>$config['theme_path']??dirname(__DIR__,2).'/assets/themes',
122
            'IN_TEST'=>$config['IN_TEST']??false,
123
            'webdbLastTab'=>[]
124
        ];
125
126
127
        self::$envConfig=array_merge( self::$envConfig,$config);
0 ignored issues
show
Coding Style introduced by
Space after opening parenthesis of function call prohibited
Loading history...
128
          if (!self::$instance) {
129
            self::$instance = new static(self::$envConfig);
130
       
131
        self::$instance
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
132
        ->withConf(self::$envConfig)
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 8
Loading history...
133
        ->setExtra()
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 8
Loading history...
134
        ->setMisc()
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 8
Loading history...
135
        ->setViews();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 8
Loading history...
136
    }
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 10 spaces, found 4
Loading history...
137
    //ddd($container->subfolder);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
138
    return self::$instance;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
139
}
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 4 spaces, found 0
Loading history...
140
141
/**
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
142
 * Determines the redirection url according to query string.
143
 *
144
 * @return string the redirect url
145
 */
146
public function getRedirectUrl()
147
{$container=self::getContainerInstance();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
148
    $query_string = $container->request->getUri()->getQuery();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
149
150
    // if server_id isn't set, then you will be redirected to intro
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
151
    if (null === $container->request->getQueryParam('server')) {
152
        $destinationurl = self::$envConfig['subFolder'] . '/src/views/intro';
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
153
    } else {
154
        // otherwise, you'll be redirected to the login page for that server;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
155
        $destinationurl = self::$envConfig['subFolder'] . '/src/views/login' . ($query_string ? '?' . $query_string : '');
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
156
    }
157
158
    return $destinationurl;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
159
}
160
161
/**
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
162
 * Adds a flash message to the session that will be displayed on the next request.
163
 *
164
 * @param mixed  $content msg content (can be object, array, etc)
165
 * @param string $key     The key to associate with the message. Defaults to the stack
166
 *                        trace of the closure or method that called addFlassh
167
 */
168
public function addFlash($content, $key = ''): void
169
{
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
170
    if ('' === $key) {
171
        $key = self::getBackTrace();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
172
    }$container=self::getContainerInstance();
173
    // $this->dump(__METHOD__ . ': addMessage ' . $key . '  ' . json_encode($content));
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
174
    if ($container->flash) {
175
        $container->flash->addMessage($key, $content);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type array<string,mixed|string>; however, parameter $key of Slim\Flash\Messages::addMessage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

175
        $container->flash->addMessage(/** @scrutinizer ignore-type */ $key, $content);
Loading history...
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
176
    }
177
}
178
179
/**
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
180
 * Gets the destination with the last active tab selected for that controller
181
 * Usually used after going through a redirect route.
182
 *
183
 * @param string $subject The subject, usually a view name like 'server' or 'table'
184
 *
185
 * @return string The destination url with last tab set in the query string
186
 */
187
public function getDestinationWithLastTab($subject)
188
{$container=self::getContainerInstance();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
189
    $_server_info = $container->misc->getServerInfo();
0 ignored issues
show
Bug Best Practice introduced by
The property misc does not exist on PHPPgAdmin\ContainerUtils. Since you implemented __get, consider adding a @property annotation.
Loading history...
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
190
    $this->addFlash($subject, 'getDestinationWithLastTab');
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
191
    //$this->prtrace('$_server_info', $_server_info);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
192
    // If username isn't set in server_info, you should login
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
193
    $url = $container->misc->getLastTabURL($subject) ?? ['url' => 'alldb', 'urlvars' => ['subject' => 'server']];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
194
    $destinationurl = $this->getRedirectUrl();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
195
196
    if (!isset($_server_info['username'])) {
197
        return $destinationurl;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
198
    }
199
200
    if (!\is_array($url)) {
201
        return $this->getRedirectUrl($subject);
0 ignored issues
show
Unused Code introduced by
The call to PHPPgAdmin\ContainerUtils::getRedirectUrl() has too many arguments starting with $subject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
        return $this->/** @scrutinizer ignore-call */ getRedirectUrl($subject);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
202
    }
203
    $this->addFlash($url, 'getLastTabURL for ' . $subject);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
204
    // Load query vars into superglobal arrays
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
205
    if (isset($url['urlvars'])) {
206
        $urlvars = [];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
207
208
        foreach ($url['urlvars'] as $key => $urlvar) {
209
            //$this->prtrace($key, $urlvar);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 16 spaces, found 12
Loading history...
210
            $urlvars[$key] = \PHPPgAdmin\Decorators\Decorator::get_sanitized_value($urlvar, $_REQUEST);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 16 spaces, found 12
Loading history...
211
        }
212
        $_REQUEST = \array_merge($_REQUEST, $urlvars);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
213
        $_GET = \array_merge($_GET, $urlvars);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
214
    }
215
    $actionurl = \PHPPgAdmin\Decorators\Decorator::actionurl($url['url'], $_GET);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
216
    $destinationurl = $actionurl->value($_GET);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
217
218
    return \str_replace('views/?', "views/{$subject}?", $destinationurl);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
219
}
220
221
/**
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
222
 * Adds an error to the errors array property of the container.
223
 *
224
 * @param string $errormsg The error msg
225
 *
226
 * @return\Slim\Container The app container
227
 */
228
public function addError(string $errormsg): \Slim\Container
229
{
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
230
    $container=self::getContainerInstance();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
231
    $errors = $container->get('errors');
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
232
    $errors[] = $errormsg;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
233
    $container->offsetSet('errors', $errors);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
234
235
    return $container;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
236
}
237
238
/**
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
239
 * @param array $conf
240
 */
241
private function withConf($conf): self
242
{
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
243
    $container = self::getContainerInstance();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
244
    $conf['plugins'] = [];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
245
    
246
        $container->BASE_PATH=$conf['BASE_PATH'];
247
        $container->subFolder=$conf['subfolder'];
248
        $container->DEBUGMODE=$conf['debugmode'];
249
        $container->THEME_PATH=$conf['theme_path'];
250
        $container->IN_TEST=$conf['IN_TEST'];
251
        $container['errors'] = [];
252
    $container['conf'] = static function (\Slim\Container $c) use ($conf): array {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
253
        $display_sizes = $conf['display_sizes'];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
254
255
        if (\is_array($display_sizes)) {
256
            $conf['display_sizes'] = [
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 16 spaces, found 12
Loading history...
257
                'schemas' => (bool) isset($display_sizes['schemas']) && true === $display_sizes['schemas'],
258
                'tables' => (bool) isset($display_sizes['tables']) && true === $display_sizes['tables'],
259
            ];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 16 spaces, found 12
Loading history...
260
        } else {
261
            $conf['display_sizes'] = [
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 16 spaces, found 12
Loading history...
262
                'schemas' => (bool) $display_sizes,
263
                'tables' => (bool) $display_sizes,
264
            ];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 16 spaces, found 12
Loading history...
265
        }
266
267
        if (!isset($conf['theme'])) {
268
            $conf['theme'] = 'default';
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 16 spaces, found 12
Loading history...
269
        }
270
271
        foreach ($conf['servers'] as &$server) {
272
            if (!isset($server['port'])) {
273
                $server['port'] = 5432;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 20 spaces, found 16
Loading history...
274
            }
275
276
            if (!isset($server['sslmode'])) {
277
                $server['sslmode'] = 'unspecified';
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 20 spaces, found 16
Loading history...
278
            }
279
        }
280
        //self::$envConfig=[
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
281
            //'BASE_PATH'=>$conf['BASE_PATH'],
282
            //'subFolder'=>$conf['subfolder'],
283
            //'DEBUGMODE'=>$conf['debugmode'],
284
            //'THEME_PATH'=>$conf['theme_path'],
285
            //'IN_TEST'=>$conf['IN_TEST']
286
        //];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
287
288
        return $conf;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
289
    };
290
  
291
    $container->subfolder = $conf['subfolder'];
292
    
293
294
    return $this;
295
}
296
297
/**
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
298
 * Sets the views.
299
 *
300
 * @return self ( description_of_the_return_value )
301
 */
302
private function setViews()
303
{
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
304
    $container = self::getContainerInstance();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
305
306
    /**
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
307
     * @return \PHPPgAdmin\ViewManager
308
     */
309
    $container['view'] = static function (\Slim\Container $c): \PHPPgAdmin\ViewManager {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
310
        $misc = $c->misc;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
311
        $view = new ViewManager(BASE_PATH . '/assets/templates', [
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
312
            'cache' => BASE_PATH . '/temp/twigcache',
313
            'auto_reload' => $c->get('settings')['debug'],
314
            'debug' => $c->get('settings')['debug'],
315
        ], $c);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
316
317
        $misc->setView($view);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
318
319
        return $view;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
320
    };
321
322
    return $this;
323
}
324
325
/**
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
326
 * Sets the instance of Misc class.
327
 *
328
 * @return self ( description_of_the_return_value )
329
 */
330
private function setMisc()
331
{
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
332
    $container = self::getContainerInstance();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
333
    /**
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
334
     * @return \PHPPgAdmin\Misc
335
     */
336
    $container['misc'] = static function (\Slim\Container $c): \PHPPgAdmin\Misc {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
337
        $misc = new \PHPPgAdmin\Misc($c);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
338
339
        $conf = $c->get('conf');
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
340
341
        // 4. Check for theme by server/db/user
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
342
        $_server_info = $misc->getServerInfo();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
343
344
        /* starting with PostgreSQL 9.0, we can set the application name */
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
345
        if (isset($_server_info['pgVersion']) && 9 <= $_server_info['pgVersion']) {
346
            \putenv('PGAPPNAME=' . $c->get('settings')['appName'] . '_' . $c->get('settings')['appVersion']);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 16 spaces, found 12
Loading history...
347
        }
348
349
        return $misc;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
350
    };
351
352
    return $this;
353
}
354
355
private function setExtra()
356
{
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 4 spaces, found 0
Loading history...
357
    $container = self::getContainerInstance();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
358
    $container['flash'] = static function (): \Slim\Flash\Messages {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 4
Loading history...
359
        return new \Slim\Flash\Messages();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 12 spaces, found 8
Loading history...
360
    };
361
362
    $container['lang'] = static function (\Slim\Container $c): array {
363
        $translations = new \PHPPgAdmin\Translations($c);
364
365
        return $translations->lang;
366
    };
367
368
    return $this;
369
}
370
371
    public static function getDefaultConfig():array
372
    {
373
        return  [
374
            'settings' => [
375
                'displayErrorDetails' => self::$envConfig['DEBUGMODE'],
376
                'determineRouteBeforeAppMiddleware' => true,
377
                'base_path' => \dirname(__DIR__, 2),
378
                'debug' => self::$envConfig['DEBUGMODE'],
379
                'phpMinVer' => '7.1', // PHP minimum version
380
                'addContentLengthHeader' => false,
381
                'appName' => 'PHPPgAdmin6'
382
            ],
383
        ];
384
    }
385
386
    /**
387
     * This function registers the default services that Slim needs to work.
388
     *
389
     * All services are shared, they are registered such that the
390
     * same instance is returned on subsequent calls.
391
     *
392
     * @param array $userSettings Associative array of application settings
393
     */
394
    private function registerDefaultServices($userSettings): void
395
    {
396
        $defaultSettings = $this->defaultSettings;
397
398
        /**
399
         * This service MUST return an array or an instance of ArrayAccess.
400
         *
401
         * @return array|ArrayAccess
402
         */
403
        $this['settings'] = static function () use ($userSettings, $defaultSettings):\Slim\Collection {
404
            return new Collection(\array_merge($defaultSettings, $userSettings));
405
        };
406
407
        $defaultProvider = new DefaultServicesProvider();
408
        $defaultProvider->register($this);
409
    }
410
}
411