Completed
Pull Request — master (#7037)
by Damian
09:19
created

CLIRequestBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B cleanEnvironment() 0 59 8
1
<?php
2
3
namespace SilverStripe\Control;
4
5
/**
6
 * CLI specific request building logic
7
 */
8
class CLIRequestBuilder extends HTTPRequestBuilder
9
{
10
    public static function cleanEnvironment(array $variables)
0 ignored issues
show
Coding Style introduced by
cleanEnvironment uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
11
    {
12
        // Create all blank vars
13
        foreach (['_REQUEST', '_GET', '_POST', '_SESSION', '_SERVER', '_COOKIE', '_ENV', '_FILES'] as $key) {
14
            if (!isset($variables[$key])) {
15
                $variables[$key] = [];
16
            };
17
        }
18
19
        // We update the $_SERVER variable to contain data consistent with the rest of the application.
20
        $variables['_SERVER'] = array_merge(array(
21
            'SERVER_PROTOCOL' => 'HTTP/1.1',
22
            'HTTP_ACCEPT' => 'text/plain;q=0.5',
23
            'HTTP_ACCEPT_LANGUAGE' => '*;q=0.5',
24
            'HTTP_ACCEPT_ENCODING' => '',
25
            'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1;q=0.5',
26
            'SERVER_SIGNATURE' => 'Command-line PHP/' . phpversion(),
27
            'SERVER_SOFTWARE' => 'PHP/' . phpversion(),
28
            'SERVER_ADDR' => '127.0.0.1',
29
            'REMOTE_ADDR' => '127.0.0.1',
30
            'REQUEST_METHOD' => 'GET',
31
            'HTTP_USER_AGENT' => 'CLI',
32
        ), $variables['_SERVER']);
33
34
        /**
35
         * Process arguments and load them into the $_GET and $_REQUEST arrays
36
         * For example,
37
         * sake my/url somearg otherarg key=val --otherkey=val third=val&fourth=val
38
         *
39
         * Will result in the following get data:
40
         *   args => array('somearg', 'otherarg'),
41
         *   key => val
42
         *   otherkey => val
43
         *   third => val
44
         *   fourth => val
45
         */
46
        if (isset($variables['_SERVER']['argv'][2])) {
47
            $args = array_slice($variables['_SERVER']['argv'], 2);
48
            foreach ($args as $arg) {
49
                if (strpos($arg, '=') == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($arg, '=') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
50
                    $variables['_GET']['args'][] = $arg;
51
                } else {
52
                    $newItems = array();
53
                    parse_str((substr($arg, 0, 2) == '--') ? substr($arg, 2) : $arg, $newItems);
54
                    $variables['_GET'] = array_merge($variables['_GET'], $newItems);
55
                }
56
            }
57
            $_REQUEST = array_merge($_REQUEST, $variables['_GET']);
58
        }
59
60
        // Set 'url' GET parameter
61
        if (isset($variables['_SERVER']['argv'][1])) {
62
            $variables['_GET']['url'] = $variables['_SERVER']['argv'][1];
63
            $variables['_SERVER']['REQUEST_URI'] = $variables['_SERVER']['argv'][1];
64
        }
65
66
        // Parse rest of variables as standard
67
        return parent::cleanEnvironment($variables);
68
    }
69
}
70