Continued with renaming-season:
[core.git] / framework / main / classes / request / console / class_ConsoleRequest.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Request;
4
5 // Import framework stuff
6 use CoreFramework\Request\Requestable;
7
8 /**
9  * A concrete request class
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class ConsoleRequest extends BaseRequest implements Requestable {
31         /**
32          * Protected constructor
33          *
34          * @return      void
35          */
36         protected function __construct () {
37                 // Call parent constructor
38                 parent::__construct(__CLASS__);
39         }
40
41         /**
42          * Creates an instance of this class and prepares it a little
43          *
44          * @return      $consoleInstance        An instance of this class
45          */
46         public static final function createConsoleRequest () {
47                 // Create an instance
48                 $consoleInstance = new ConsoleRequest();
49
50                 // Prepare the console request data for usage
51                 $consoleInstance->prepareRequestData();
52
53                 // Return the prepared instance
54                 return $consoleInstance;
55         }
56
57         /**
58          * Prepares the request data for usage
59          *
60          * @return      void
61          */
62         public function prepareRequestData () {
63                 // 'argv' and 'argc' should be there
64                 if ((!isset($_SERVER['argv'])) || (!isset($_SERVER['argc']))) {
65                         // Maybe not right PHP mode? (needs CLI?)
66                         trigger_error(sprintf('[%s:%d]: argv/argc not set: %s', __METHOD__, __LINE__, print_r($_SERVER, TRUE)), E_USER_ERROR);
67                 } // END - if
68
69                 // Get the "request data" from the command-line argument list
70                 $args = $_SERVER['argv'];
71
72                 // Are there less than two parameters?
73                 if ($_SERVER['argc'] < 2) {
74                         // Skip this
75                         return;
76                 } // END - if
77
78                 // Is the first element "index.php" ?
79                 if ($args[0] == 'index.php') {
80                         // Then remove it
81                         array_shift($args);
82                 } // END - if
83
84                 // Try to determine next parameters
85                 foreach ($args as $arg) {
86                         // Seperate arguemnt name from value
87                         $argArray = explode('=', $arg);
88
89                         // Is the second one set?
90                         if (!isset($argArray[1])) {
91                                 // Add it likewise, but empty value
92                                 $this->setRequestElement($argArray[0], '');
93                         } else {
94                                 // Set a name=value pair escaped and secured
95                                 $this->setRequestElement($argArray[0], escapeshellcmd($argArray[1]));
96                         }
97                 } // END - foreach
98         }
99
100         /**
101          * Getter for a header element or 'null' if header was not found
102          *
103          * @param       $headerName             Name of the header
104          * @return      $headerValue    Value of the header or 'null' if not found
105          * @throws      UnsupportedOperationException   This method should never be called
106          */
107         public function getHeaderElement ($headerName) {
108                 // Console doesn't have any headers
109                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
110         }
111
112         /**
113          * Getter for request method. This getter might be useful for security filters
114          *
115          * @return      $requestMethod  Used request method
116          */
117         public final function getRequestMethod () {
118                 // @TODO Can't this be 'CONSOLE' ?
119                 return 'LOCAL';
120         }
121
122         /**
123          * Reads a cookie and returns it's value or null if not found
124          *
125          * @param       $cookieName             Name of cookie we shall read
126          * @return      $cookieValue    Value of cookie or null if not found
127          * @throws      UnsupportedOperationException   This method should never be called
128          */
129         public final function readCookie ($cookieName) {
130                 // There are no cookies on console
131                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
132         }
133
134         /**
135          * Checks if the request method is GET.
136          *
137          * @return      $isGet  Whether the request method is GET
138          * @throws      UnsupportedOperationException   This method should never be called
139          */
140         public function isGetRequestMethod () {
141                 // Not support method
142                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
143         }
144
145         /**
146          * Checks if the request method is HEAD.
147          *
148          * @return      $isHead         Whether the request method is HEAD
149          * @throws      UnsupportedOperationException   This method should never be called
150          */
151         public function isHeadRequestMethod () {
152                 // Not support method
153                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
154         }
155
156         /**
157          * Checks if the request method is POST.
158          *
159          * @return      $isPost         Whether the request method is POST
160          * @throws      UnsupportedOperationException   This method should never be called
161          */
162         public function isPostRequestMethod () {
163                 // Not support method
164                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
165         }
166
167 }