Completed
Push — develop ( 79e753...179711 )
by Peter
01:44
created

Module::sessionStart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDebug/ for the canonical source repository
6
 * @copyright   Copyright (c) 2014-2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDebug;
11
12
use WebinoDebug\Factory\DebuggerFactory;
13
use WebinoDebug\Factory\ModuleOptionsFactory;
14
use WebinoDebug\Options\ModuleOptions;
15
use WebinoDebug\Service\NullDebugger;
16
use Zend\Http\PhpEnvironment\Request;
17
use Zend\ModuleManager\Feature;
18
use Zend\ModuleManager\ModuleEvent;
19
use Zend\ModuleManager\ModuleManagerInterface;
20
use Zend\ServiceManager\ServiceManager;
21
22
/**
23
 * WebinoDebug module
24
 */
25
class Module implements Feature\InitProviderInterface
26
{
27
    /**
28
     * @param ModuleManagerInterface $modules
29
     */
30
    public function init(ModuleManagerInterface $modules)
31
    {
32
        /** @var \Zend\ModuleManager\ModuleManager $modules */
33
        /** @var ServiceManager $services */
34
        $services = $modules->getEvent()->getParam('ServiceManager');
0 ignored issues
show
Bug introduced by
The method getEvent() does not exist on Zend\ModuleManager\ModuleManagerInterface. Did you maybe mean getEventManager()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
35
        $services->setFactory(ModuleOptions::class, ModuleOptionsFactory::class);
36
        $services->setFactory(DebuggerFactory::SERVICE, DebuggerFactory::class);
37
38
        /** @var ModuleOptions $options */
39
        $options = $services->get(ModuleOptions::class);
40
41
        // set error log path
42
        ini_set('error_log', $options->getPhpErrorLog());
43
44
        // return early when disabled
45
        if ($options->isDisabled()) {
46
            return;
47
        }
48
49
        // init debugger
50
        $this->sessionStart();
51
        /** @var Service\Debugger $debugger */
52
        $debugger = $services->get(DebuggerFactory::SERVICE);
53
54
        // add a service initializer
55
        // TODO something better
56
        $modules->getEventManager()->attach(
57
            ModuleEvent::EVENT_LOAD_MODULES_POST,
58
            function () use ($services, $debugger) {
59
60
                $initializer = function ($obj) use ($debugger) {
61
                    if ($obj instanceof Service\DebuggerAwareInterface) {
62
                        /** @var Service\Debugger $debugger */
63
                        $obj->setDebugger($debugger);
64
                    }
65
                };
66
67
                $services->addInitializer($initializer);
68
                foreach ($services->getRegisteredServices()['instances'] as $name) {
69
                    $instance = $services->get($name);
70
                    ($instance instanceof ServiceManager) and $instance->addInitializer($initializer);
71
                }
72
            }
73
        );
74
75
        // return early on null debugger
76
        if ($debugger instanceof NullDebugger) {
77
            return;
78
        }
79
80
        // create bar panels
81
        $showBar   = $options->showBar();
82
        $barPanels = $options->getBarPanels();
83
84
        if ($showBar) {
85
            // set core bar panels
86
            foreach ($barPanels as $id => $barPanel) {
87
                $debugger->setBarPanel(new $barPanel($modules), $id);
88
            }
89
        }
90
91
        // finish debugger init
92
        $modules->getEventManager()->attach(
93
            ModuleEvent::EVENT_LOAD_MODULES_POST,
94
            function () use ($services, $debugger, $options, $showBar, $barPanels) {
95
96
                // update module options
97
                /** @var ModuleOptions $newOptions */
98
                $newOptions = $services->create(ModuleOptions::class);
99
                $options->setFromArray($newOptions->toArray());
100
101
                if ($showBar) {
102
                    // set additional bar panels
103
                    $newBarPanels = array_diff($options->getBarPanels(), $barPanels);
104
                    foreach ($newBarPanels as $id => $barPanel) {
105
                        $debugger->setBarPanel($services->get($barPanel), $id);
0 ignored issues
show
Documentation introduced by
$services->get($barPanel) is of type object|array, but the function expects a null|object<WebinoDebug\Debugger\PanelInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
                    }
107
108
                    // init bar panels
109
                    foreach ($debugger->getBarPanels() as $barPanel) {
110
                        ($barPanel instanceof Debugger\PanelInitInterface) and $barPanel->init($services);
111
                    }
112
                }
113
114
                // debugger templates
115
                $templateMap = $options->getTemplateMap();
116
                empty($templateMap) or $services->get('ViewTemplateMapResolver')->merge($templateMap);
117
            }
118
        );
119
    }
120
121
    /**
122
     * Start session for Ajax debug support
123
     *
124
     * Simplified session start with cookie path support at least.
125
     *
126
     * @return void
127
     */
128
    private function sessionStart()
129
    {
130
        if (session_status() !== PHP_SESSION_ACTIVE) {
131
            session_set_cookie_params(0, (new Request)->getBasePath());
132
            session_start();
133
        }
134
    }
135
}
136