3 namespace CoreFramework\Request;
5 // Import framework stuff
6 use CoreFramework\Request\Requestable;
9 * A concrete request class
11 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
30 class ConsoleRequest extends BaseRequest implements Requestable {
32 * Protected constructor
36 protected function __construct () {
37 // Call parent constructor
38 parent::__construct(__CLASS__);
42 * Creates an instance of this class and prepares it a little
44 * @return $consoleInstance An instance of this class
46 public static final function createConsoleRequest () {
48 $consoleInstance = new ConsoleRequest();
50 // Prepare the console request data for usage
51 $consoleInstance->prepareRequestData();
53 // Return the prepared instance
54 return $consoleInstance;
58 * Prepares the request data for usage
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);
69 // Get the "request data" from the command-line argument list
70 $args = $_SERVER['argv'];
72 // Are there less than two parameters?
73 if ($_SERVER['argc'] < 2) {
78 // Is the first element "index.php" ?
79 if ($args[0] == 'index.php') {
84 // Try to determine next parameters
85 foreach ($args as $arg) {
86 // Seperate arguemnt name from value
87 $argArray = explode('=', $arg);
89 // Is the second one set?
90 if (!isset($argArray[1])) {
91 // Add it likewise, but empty value
92 $this->setRequestElement($argArray[0], '');
94 // Set a name=value pair escaped and secured
95 $this->setRequestElement($argArray[0], escapeshellcmd($argArray[1]));
101 * Getter for a header element or 'null' if header was not found
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
107 public function getHeaderElement ($headerName) {
108 // Console doesn't have any headers
109 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
113 * Getter for request method. This getter might be useful for security filters
115 * @return $requestMethod Used request method
117 public final function getRequestMethod () {
118 // @TODO Can't this be 'CONSOLE' ?
123 * Reads a cookie and returns it's value or null if not found
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
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);
135 * Checks if the request method is GET.
137 * @return $isGet Whether the request method is GET
138 * @throws UnsupportedOperationException This method should never be called
140 public function isGetRequestMethod () {
141 // Not support method
142 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
146 * Checks if the request method is HEAD.
148 * @return $isHead Whether the request method is HEAD
149 * @throws UnsupportedOperationException This method should never be called
151 public function isHeadRequestMethod () {
152 // Not support method
153 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
157 * Checks if the request method is POST.
159 * @return $isPost Whether the request method is POST
160 * @throws UnsupportedOperationException This method should never be called
162 public function isPostRequestMethod () {
163 // Not support method
164 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);