Completed
Pull Request — master (#8)
by
unknown
02:31 queued 01:21
created

LegacyController::runScript()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.288

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 12
cts 15
cp 0.8
rs 8.8817
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 6.288
1
<?php
2
3
/**
4
 * @author Matthias Glaub <[email protected]>
5
 * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
6
 */
7
8
namespace MaglLegacyApplication\Controller;
9
10
use Laminas\Http\Response;
11
use Laminas\Mvc\Controller\AbstractActionController;
12
use Laminas\View\Model\ViewModel;
13
use MaglLegacyApplication\Application\MaglLegacy;
14
use MaglLegacyApplication\Options\LegacyControllerOptions;
15
16
class LegacyController extends AbstractActionController
17
{
18
19
    /**
20
     *
21
     * @var LegacyControllerOptions
22
     */
23
    private $options;
24
25
    /**
26
     *
27
     * @var MaglLegacy
28
     */
29
    private $legacy;
30
31 8
    public function __construct(LegacyControllerOptions $options, MaglLegacy $legacy)
32
    {
33 8
        $this->options = $options;
34 8
        $this->legacy = $legacy;
35 8
    }
36
37 7
    public function indexAction()
38
    {
39 7
        $docRoots = $this->options->getDocRoots();
40 7
        foreach ($docRoots as $key => $docRoot) {
41 7
            $docRoots[$key] = rtrim(getcwd() . '/' . $docRoot);
42
        }
43
44 7
        $scriptName = $this->params('script');
45
46 7
        if (empty($scriptName)) {
47
            $path = $this->params(('path')) ? $this->params('path') : '';
48
            foreach ($this->options->getIndexFiles() as $indexFile) {
49
                foreach ($docRoots as $docRoot) {
50
                    if (is_file($docRoot . '/' . $path . $indexFile)) {
51
                        $this->legacy->setLegacyScriptName($path . $indexFile);
52
                        return $this->runScript($docRoot . '/' . $path . $indexFile);
53
                    }
54
                }
55
            }
56
        }
57
58
        $scriptUri = '/' . ltrim($scriptName, '/'); // force leading '/'
59 7
        foreach ($docRoots as $docRoot) {
60 7
            $legacyScriptFilename = $docRoot . $scriptUri;
61 7
            if (is_file($legacyScriptFilename)) {
62 7
                //inform the application about the used script
63
                $this->legacy->setLegacyScriptName($scriptUri);
64 5
65
                //inject get and request variables
66
                $this->setGetVariables();
67 5
68
                return $this->runScript($legacyScriptFilename);
69 5
            }
70
        }
71
72
        /** @var Response $response */
73
        $response = $this->getResponse();
74 2
        $response->setStatusCode(404);
75 2
    }
76 2
77
    private function setGetVariables()
78 5
    {
79
        $globals_options = $this->options->getGlobals();
80 5
81
        // if we should not set any global vars, we can return safely
82
        if (!$globals_options['get'] && !$globals_options['request']) {
83 5
            return;
84
        }
85
86
        // check if $_GET is used at all (ini - variables_order ?)
87
        // check if $_GET is written to $_REQUEST (ini - variables_order / request_order)
88
        // depending on request_order, check if $_REQUEST is already written and decide if we are allowed to override
89
90
        $request_order = ini_get('request_order');
91 5
92
        if ($request_order === false) {
93 5
            $request_order = ini_get('variables_order');
94
        }
95
96
        $get_prio = stripos($request_order, 'g');
97 5
        $post_prio = stripos($request_order, 'p');
98 5
99
        $forceOverrideRequest = $get_prio > $post_prio;
100 5
101
        $routeParams = $this->getEvent()->getRouteMatch()->getParams();
102 5
103
        foreach ($routeParams as $paramName => $paramValue) {
104 5
            if ($globals_options['get'] && !isset($_GET[$paramName])) {
105
                $_GET[$paramName] = $paramValue;
106 5
            }
107 5
108
            if ($globals_options['request'] && ($forceOverrideRequest || !isset($_REQUEST[$paramName]))) {
109
                $_REQUEST[$paramName] = $paramValue;
110 5
            }
111 5
        }
112
    }
113
114 5
    private function runScript($scriptFileName)
115
    {
116 5
        $this->legacy->setLegacyScriptFilename($scriptFileName);
117
118 5
        ob_start();
119
        $result = include $scriptFileName;
120 5
        $output = ob_get_clean();
121 5
122 5
        if ($result instanceof ViewModel || $result instanceof Response) {
123
            if ($this->options->getPrependOutputBufferToResponse()) {
124 5
                echo $output;
125 2
            }
126
            return $result;
127
        }
128 3
129 3
        $result = $this->getEventManager()->trigger(MaglLegacy::EVENT_SHORT_CIRCUIT_RESPONSE, $this);
130
        if ($result->stopped()) {
131
            if ($this->options->getPrependOutputBufferToResponse()) {
132
                $result->last()->setContent($output . $result->last()->getContent());
133
            }
134
            return $result->last();
135
        }
136 3
137 3
        $this->getResponse()->setContent($output);
138
        return $this->getResponse();
139
    }
140
}
141