ConsoleRequest::startup()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace Agavi\Request;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// |                                                                           |
8
// | For the full copyright and license information, please view the LICENSE   |
9
// | file that was distributed with this source code. You can also view the    |
10
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
11
// |   vi: set noexpandtab:                                                    |
12
// |   Local Variables:                                                        |
13
// |   indent-tabs-mode: t                                                     |
14
// |   End:                                                                    |
15
// +---------------------------------------------------------------------------+
16
use Agavi\Core\Context;
17
18
/**
19
 * AgaviConsoleRequest provides support for console-only request information
20
 * such as command-line parameters.
21
 *
22
 * @package    agavi
23
 * @subpackage request
24
 *
25
 * @copyright  Authors
26
 * @copyright  The Agavi Project
27
 *
28
 * @since      0.9.0
29
 *
30
 * @version    $Id$
31
 */
32
class ConsoleRequest extends Request
33
{
34
    /**
35
     * @var        string The command given on the command line (without parameters)
36
     */
37
    protected $input = null;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @author     David Zülke <[email protected]>
43
     * @since      1.0.0
44
     */
45
    public function __construct()
46
    {
47
        parent::__construct();
48
        $this->setParameters(array(
49
            'request_data_holder_class' => 'AgaviConsoleRequestDataHolder',
50
        ));
51
    }
52
53
    /**
54
     * Initialize this Request.
55
     *
56
     * @param      Context $context An Context instance.
57
     * @param      array   $parameters An associative array of initialization parameters.
58
     *
59
     * @throws     <b>AgaviInitializationException</b> If an error occurs while
60
     *                                                 initializing this Request.
61
     *
62
     * @author     David Zülke <[email protected]>
63
     * @since      1.0.0
64
     */
65
    public function initialize(Context $context, array $parameters = array())
66
    {
67
        parent::initialize($context, $parameters);
68
        
69
        $argv = self::getSourceValue('argv', array());
70
        // get rid of the script name
71
        array_shift($argv);
72
        
73
        $parameters = array();
74
        $input = array();
75
        
76
        $prev = '';
77
        foreach ($argv as $arg) {
78
            if ($arg[0] == '-') {
79
                // name
80
                $parameters[$arg] = true;
81
            } else {
82
                if ($prev && $prev[0] == '-') {
83
                    $parameters[$prev] = $arg;
84
                } else {
85
                    $input[] = $arg;
86
                }
87
            }
88
            $prev = $arg;
89
        }
90
        
91
        $files = array();
92
        if ($this->getParameter('read_stdin', true) && defined('STDIN') && ($stdinMeta = stream_get_meta_data(STDIN)) && !$stdinMeta['seekable']) {
93
            // if stream_get_meta_data() reports STDIN as not seekable, that means something was piped into our process, and we should put that into a file
94
            // the alternative method to determine this is via posix_isatty(STDIN) which returns false in the same situation, but that requires the posix extension and also doesn't work on Windows
95
            $stdinName = $this->getParameter('stdin_file_name', 'stdin_file');
96
            
97
            $ufc = $this->getParameter('uploaded_file_class', 'AgaviUploadedFile');
98
            $files = array(
99
                $stdinName => new $ufc(array(
100
                    'name' => $stdinName,
101
                    'type' => 'application/octet-stream',
102
                    'size' => -1, // we're not buffering, so -1 is a good choice probably (better than 0 anyway)
103
                    'stream' => STDIN,
104
                    'error' => UPLOAD_ERR_OK,
105
                    'is_uploaded_file' => false,
106
                ))
107
            );
108
        }
109
110
        $rdhc = $this->getParameter('request_data_holder_class');
111
        $this->setRequestData(new $rdhc(array(
112
            constant("$rdhc::SOURCE_PARAMETERS") => array(),
113
            constant("$rdhc::SOURCE_FILES") => $files,
114
        )));
115
        $rd = $this->getRequestData();
116
        
117
        foreach ($parameters as $name => $value) {
118
            $rd->setParameter(substr($name, 1), $value);
119
        }
120
        
121
        $this->input = implode(' ', $input);
122
        
123
        $this->setMethod($this->getParameter('default_method', 'read'));
124
    }
125
    
126
    /**
127
     * Get the command given on the command line (without parameters)
128
     *
129
     * @return     string The command.
130
     *
131
     * @author     David Zülke <[email protected]>
132
     * @since      1.0.0
133
     */
134
    public function getInput()
135
    {
136
        return $this->input;
137
    }
138
139
    /**
140
     * Do any necessary startup work after initialization.
141
     *
142
     * This method is not called directly after initialize().
143
     *
144
     * @author     David Zülke <[email protected]>
145
     * @since      1.0.0
146
     */
147
    public function startup()
0 ignored issues
show
Coding Style introduced by
startup uses the super-global variable $_SERVER 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...
Coding Style introduced by
startup uses the super-global variable $_ENV 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...
Coding Style introduced by
startup uses the super-global variable $GLOBALS 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...
148
    {
149
        parent::startup();
150
        
151
        if ($this->getParameter('unset_input', true)) {
152
            $_SERVER['argv'] = $_ENV['argv'] = $GLOBALS['argv'] = array();
153
            $_SERVER['argc'] = $_ENV['argc'] = $GLOBALS['argc'] = 0;
154
        }
155
    }
156
}
157