Completed
Pull Request — master (#268)
by
unknown
01:13
created

Xhgui_ServiceContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
use Slim\Slim;
3
use Slim\Views\Twig;
4
use Slim\Middleware\SessionCookie;
5
6
class Xhgui_ServiceContainer extends Pimple
7
{
8
    protected static $_instance;
9
10
    /**
11
     * @return \Xhgui_ServiceContainer
12
     */
13
    public static function instance()
14
    {
15
        if (empty(static::$_instance)) {
16
            static::$_instance = new self();
17
        }
18
        return static::$_instance;
19
    }
20
21
    /**
22
     * Xhgui_ServiceContainer constructor.
23
     */
24
    public function __construct()
25
    {
26
        parent::__construct();
27
        $this['config'] = Xhgui_Config::all();
28
        $this->_slimApp();
29
        $this->_services();
30
        $this->_controllers();
0 ignored issues
show
Unused Code introduced by
The call to the method Xhgui_ServiceContainer::_controllers() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
31
    }
32
33
    /**
34
     * Create the Slim app.
35
     */
36
    protected function _slimApp()
37
    {
38
        $this['view'] = function ($c) {
39
            $cacheDir = isset($c['config']['cache']) ? $c['config']['cache'] : XHGUI_ROOT_DIR . '/cache';
40
41
            // Configure Twig view for slim
42
            $view = new Twig();
43
44
            $view->twigTemplateDirs = array(dirname(__DIR__) . '/templates');
45
            $view->parserOptions = array(
46
                'charset'           => 'utf-8',
47
                'cache'             => $cacheDir,
48
                'auto_reload'       => true,
49
                'debug'             => true,
50
                'strict_variables'  => false,
51
                'autoescape'        => true
52
            );
53
54
            return $view;
55
        };
56
57
        $this['app'] = $this->share(function ($c) {
58
            $app = new Slim($c['config']);
59
60
            // Enable cookie based sessions
61
            $app->add(new SessionCookie(array(
62
                'httponly' => true,
63
            )));
64
65
            // Add renderer.
66
            $app->add(new Xhgui_Middleware_Render());
67
68
            $view = $c['view'];
69
            $view->parserExtensions = array(
70
                new Xhgui_Twig_Extension($app)
71
            );
72
            $app->view($view);
73
74
            return $app;
75
        });
76
    }
77
78
    /**
79
     * Add common service objects to the container.
80
     */
81
    protected function _services()
82
    {
83
        $this['db'] = $this->share(function ($c) {
84
            return Xhgui_Storage_Factory::factory($c['config']);
85
        });
86
87
        $this['watchFunctions'] = function ($c) {
88
89
            switch ($c['config']['save.handler']) {
90
                default:
91
                case 'pdo':
92
                case 'mongodb':
93
                case 'file':
94
                    return $c['db'];
95
            }
96
        };
97
98
        $this['profiles'] = function ($c) {
99
            return new Xhgui_Profiles($c['db']);
100
        };
101
102
        $this['storageFactory'] = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
            return new Xhgui_Storage_Factory();
104
        };
105
106
//        $this['saver'] = function($c) {
107
//            return Xhgui_Saver::factory($c['config']);
108
//        };
109
    }
110
111
    /**
112
     * Add controllers to the DI container.
113
     */
114
    protected function _controllers()
115
    {
116
        $this['watchController'] = function ($c) {
117
            return new Xhgui_Controller_Watch($c['app'], $c['watchFunctions']);
118
        };
119
120
        $this['runController'] = function ($c) {
121
            return new Xhgui_Controller_Run($c['app'], $c['profiles'], $c['watchFunctions']);
122
        };
123
124
        $this['customController'] = function ($c) {
125
            return new Xhgui_Controller_Custom($c['app'], $c['profiles']);
126
        };
127
128
        $this['waterfallController'] = function ($c) {
129
            return new Xhgui_Controller_Waterfall($c['app'], $c['profiles']);
130
        };
131
132
        $this['importController'] = function ($c) {
133
            return new Xhgui_Controller_Import($c['app'], new Xhgui_Storage_Factory(), new Xhgui_Saver());
134
        };
135
    }
136
137
}
138