3 namespace CoreFramework\Request;
6 * A concrete request class
8 * @author Roland Haeder <webmaster@shipsimu.org>
10 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.shipsimu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class ConsoleRequest extends BaseRequest implements Requestable {
29 * Protected constructor
33 protected function __construct () {
34 // Call parent constructor
35 parent::__construct(__CLASS__);
39 * Creates an instance of this class and prepares it a little
41 * @return $consoleInstance An instance of this class
43 public static final function createConsoleRequest () {
45 $consoleInstance = new ConsoleRequest();
47 // Prepare the console request data for usage
48 $consoleInstance->prepareRequestData();
50 // Return the prepared instance
51 return $consoleInstance;
55 * Prepares the request data for usage
59 public function prepareRequestData () {
60 // 'argv' and 'argc' should be there
61 if ((!isset($_SERVER['argv'])) || (!isset($_SERVER['argc']))) {
62 // Maybe not right PHP mode? (needs CLI?)
63 trigger_error(sprintf('[%s:%d]: argv/argc not set: %s', __METHOD__, __LINE__, print_r($_SERVER, TRUE)), E_USER_ERROR);
66 // Get the "request data" from the command-line argument list
67 $args = $_SERVER['argv'];
69 // Are there less than two parameters?
70 if ($_SERVER['argc'] < 2) {
75 // Is the first element "index.php" ?
76 if ($args[0] == 'index.php') {
81 // Try to determine next parameters
82 foreach ($args as $arg) {
83 // Seperate arguemnt name from value
84 $argArray = explode('=', $arg);
86 // Is the second one set?
87 if (!isset($argArray[1])) {
88 // Add it likewise, but empty value
89 $this->setRequestElement($argArray[0], '');
91 // Set a name=value pair escaped and secured
92 $this->setRequestElement($argArray[0], escapeshellcmd($argArray[1]));
98 * Getter for a header element or 'null' if header was not found
100 * @param $headerName Name of the header
101 * @return $headerValue Value of the header or 'null' if not found
102 * @throws UnsupportedOperationException This method should never be called
104 public function getHeaderElement ($headerName) {
105 // Console doesn't have any headers
106 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
110 * Getter for request method. This getter might be useful for security filters
112 * @return $requestMethod Used request method
114 public final function getRequestMethod () {
115 // @TODO Can't this be 'CONSOLE' ?
120 * Reads a cookie and returns it's value or null if not found
122 * @param $cookieName Name of cookie we shall read
123 * @return $cookieValue Value of cookie or null if not found
124 * @throws UnsupportedOperationException This method should never be called
126 public final function readCookie ($cookieName) {
127 // There are no cookies on console
128 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
132 * Checks if the request method is GET.
134 * @return $isGet Whether the request method is GET
135 * @throws UnsupportedOperationException This method should never be called
137 public function isGetRequestMethod () {
138 // Not support method
139 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
143 * Checks if the request method is HEAD.
145 * @return $isHead Whether the request method is HEAD
146 * @throws UnsupportedOperationException This method should never be called
148 public function isHeadRequestMethod () {
149 // Not support method
150 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
154 * Checks if the request method is POST.
156 * @return $isPost Whether the request method is POST
157 * @throws UnsupportedOperationException This method should never be called
159 public function isPostRequestMethod () {
160 // Not support method
161 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);