Test Failed
Pull Request — develop (#340)
by Felipe
03:40
created

ContainerHandlers::setExtra()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 10
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.0.0
5
 */
6
7
namespace PHPPgAdmin;
8
9
/**
10
 * Auxiliary class to handle injection of dependencies to avoid
11
 * declaring them in the container class.
12
 */
13
class ContainerHandlers
14
{
15
    /**
16
     * @var \PHPPgAdmin\ContainerUtils
17
     * */
18
    private $container;
19
20
    /**
21
     * @param \PHPPgAdmin\ContainerUtils $container
22
     */
23
    public function __construct(\PHPPgAdmin\ContainerUtils $container)
24
    {
25
        $this->container = $container;
26
    }
27
28
    public function storeMainRequestParams(): self
29
    {
30
        // This should be deprecated once we're sure no php scripts are required directly
31
        $this->container->offsetSet('server', $_REQUEST['server'] ?? null);
32
        $this->container->offsetSet('database', $_REQUEST['database'] ?? null);
33
        $this->container->offsetSet('schema', $_REQUEST['schema'] ?? null);
34
35
        return $this;
36
    }
37
38
    /**
39
     * Sets the views.
40
     *
41
     * @return self ( description_of_the_return_value )
42
     */
43
    public function setViews(): self
44
    {
45
        $container = $this->container;
46
47
        /**
48
         * @return \PHPPgAdmin\ViewManager
49
         */
50
        $container['view'] = static function (\PHPPgAdmin\ContainerUtils $c): \PHPPgAdmin\ViewManager {
51
            $misc = $c->misc;
52
            $view = new ViewManager(BASE_PATH . '/assets/templates', [
53
                'cache' => BASE_PATH . '/temp/twigcache',
54
                'auto_reload' => $c->get('settings')['debug'],
55
                'debug' => $c->get('settings')['debug'],
56
            ], $c);
57
58
            $misc->setView($view);
59
60
            return $view;
61
        };
62
63
        return $this;
64
    }
65
66
    /**
67
     * Sets the instance of Misc class.
68
     *
69
     * @return self ( description_of_the_return_value )
70
     */
71
    public function setMisc(): self
72
    {
73
        $container = $this->container;
74
        /**
75
         * @return \PHPPgAdmin\Misc
76
         */
77
        $container['misc'] = static function (\PHPPgAdmin\ContainerUtils $c): \PHPPgAdmin\Misc {
78
            $misc = new \PHPPgAdmin\Misc($c);
79
80
            $conf = $c->get('conf');
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
81
82
            // 4. Check for theme by server/db/user
83
            $_server_info = $misc->getServerInfo();
84
85
            /* starting with PostgreSQL 9.0, we can set the application name */
86
            if (isset($_server_info['pgVersion']) && 9 <= $_server_info['pgVersion']) {
87
                \putenv('PGAPPNAME=' . $c->get('settings')['appName'] . '_' . $c->get('settings')['appVersion']);
88
            }
89
90
            return $misc;
91
        };
92
93
        return $this;
94
    }
95
96
    public function setExtra(): self
97
    {
98
        $container = $this->container;
99
        $container['flash'] = static function (): \Slim\Flash\Messages {
100
            return new \Slim\Flash\Messages();
101
        };
102
103
        $container['lang'] = static function (\PHPPgAdmin\ContainerUtils $c): array {
104
            $translations = new \PHPPgAdmin\Translations($c);
105
106
            return $translations->lang;
107
        };
108
109
        return $this;
110
    }
111
112
    public function setHaltHandler(): self
113
    {
114
        $this->container['haltHandler'] = static function (\PHPPgAdmin\ContainerUtils $c) {
115
            return static function ($request, $response, $exits, $status = 500) {
116
                $title = 'PHPPgAdmin Error';
117
118
                $html = '<p>The application could not run because of the following error:</p>';
119
120
                $output = \sprintf(
121
                    "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" .
122
                        '<title>%s</title><style>' .
123
                        'body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}' .
124
                        'h3{margin:0;font-size:28px;font-weight:normal;line-height:30px;}' .
125
                        'span{display:inline-block;font-size:16px;}' .
126
                        '</style></head><body><h3>%s</h3><p>%s</p><span>%s</span></body></html>',
127
                    $title,
128
                    $title,
129
                    $html,
130
                    \implode('<br>', $exits)
131
                );
132
133
                $body = $response->getBody(); //new \Slim\Http\Body(fopen('php://temp', 'r+'));
134
                $body->write($output);
135
136
                return $response
137
                    ->withStatus($status)
138
                    ->withHeader('Content-type', 'text/html')
139
                    ->withBody($body);
140
            };
141
        };
142
143
        return $this;
144
    }
145
}
146