3 namespace Org\Mxchange\CoreFramework\Request;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
7 use Org\Mxchange\CoreFramework\Request\Requestable;
10 * A concrete request class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class ConsoleRequest extends BaseRequest implements Requestable {
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Creates an instance of this class and prepares it a little
45 * @return $consoleInstance An instance of this class
47 public static final function createConsoleRequest () {
49 $consoleInstance = new ConsoleRequest();
51 // Prepare the console request data for usage
52 $consoleInstance->prepareRequestData();
54 // Return the prepared instance
55 return $consoleInstance;
59 * Prepares the request data for usage
63 protected function prepareRequestData () {
64 // 'argv' and 'argc' should be there
65 if ((!isset($_SERVER['argv'])) || (!isset($_SERVER['argc']))) {
66 // Maybe not right PHP mode? (needs CLI?)
67 trigger_error(sprintf('[%s:%d]: argv/argc not set: %s', __METHOD__, __LINE__, print_r($_SERVER, true)), E_USER_ERROR);
70 // Get the "request data" from the command-line argument list
71 $args = $_SERVER['argv'];
73 // Are there less than two parameters?
74 if ($_SERVER['argc'] < 2) {
79 // Is the first element "index.php" ?
80 if ($args[0] == 'index.php') {
85 // Try to determine next parameters
86 foreach ($args as $arg) {
87 // Seperate argument key from value
88 $argArray = explode('=', $arg);
90 // Is the second one set?
91 if (!isset($argArray[1])) {
92 // Add it likewise, but empty value
93 $this->setRequestElement($argArray[0], '');
95 // Set a name=value pair escaped and secured
96 $this->setRequestElement($argArray[0], escapeshellcmd($argArray[1]));
102 * Getter for a header element or 'null' if header was not found
104 * @param $headerName Name of the header
105 * @return $headerValue Value of the header or 'null' if not found
106 * @throws UnsupportedOperationException This method should never be called
108 public function getHeaderElement ($headerName) {
109 // Console doesn't have any headers
110 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
114 * Getter for request method. This getter might be useful for security filters
116 * @return $requestMethod Used request method
118 public final function getRequestMethod () {
119 // @TODO Can't this be 'CONSOLE' ?
124 * Reads a cookie and returns it's value or null if not found
126 * @param $cookieName Name of cookie we shall read
127 * @return $cookieValue Value of cookie or null if not found
128 * @throws UnsupportedOperationException This method should never be called
130 public final function readCookie ($cookieName) {
131 // There are no cookies on console
132 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
136 * Checks if the request method is GET.
138 * @return $isGet Whether the request method is GET
139 * @throws UnsupportedOperationException This method should never be called
141 public function isGetRequestMethod () {
142 // Not support method
143 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
147 * Checks if the request method is HEAD.
149 * @return $isHead Whether the request method is HEAD
150 * @throws UnsupportedOperationException This method should never be called
152 public function isHeadRequestMethod () {
153 // Not support method
154 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
158 * Checks if the request method is POST.
160 * @return $isPost Whether the request method is POST
161 * @throws UnsupportedOperationException This method should never be called
163 public function isPostRequestMethod () {
164 // Not support method
165 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);