3 namespace Org\Mxchange\CoreFramework\Request;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
7 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
8 use Org\Mxchange\CoreFramework\Request\Requestable;
11 * A concrete request class
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class ConsoleRequest extends BaseRequest implements Requestable {
34 * Protected constructor
38 private function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Creates an instance of this class and prepares it a little
46 * @return $consoleInstance An instance of this class
48 public static final function createConsoleRequest () {
50 $consoleInstance = new ConsoleRequest();
52 // Prepare the console request data for usage
53 $consoleInstance->prepareRequestData();
55 // Return the prepared instance
56 return $consoleInstance;
60 * Prepares the request data for usage
64 protected function prepareRequestData () {
65 // 'argv' and 'argc' should be there
66 if ((!isset($_SERVER['argv'])) || (!isset($_SERVER['argc']))) {
67 // Maybe not right PHP mode? (needs CLI?)
68 trigger_error(sprintf('[%s:%d]: argv/argc not set: %s', __METHOD__, __LINE__, print_r($_SERVER, true)), E_USER_ERROR);
71 // Get the "request data" from the command-line argument list
72 $args = $_SERVER['argv'];
74 // Are there less than two parameters?
75 if ($_SERVER['argc'] < 2) {
80 // Is the first element "index.php" ?
81 if ($args[0] == 'index.php') {
86 // Try to determine next parameters
87 foreach ($args as $arg) {
88 // Seperate argument key from value
89 $argArray = explode('=', $arg);
91 // Is the second one set?
92 if (!isset($argArray[1])) {
93 // Add it likewise, but empty value
94 $this->setRequestElement($argArray[0], '');
96 // Set a name=value pair escaped and secured
97 $this->setRequestElement($argArray[0], escapeshellcmd($argArray[1]));
103 * Getter for a header element or 'null' if header was not found
105 * @param $headerName Name of the header
106 * @return $headerValue Value of the header or 'null' if not found
107 * @throws UnsupportedOperationException This method should never be called
109 public function getHeaderElement (string $headerName) {
110 // Console doesn't have any headers
111 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
115 * Getter for request method. This getter might be useful for security filters
117 * @return $requestMethod Used request method
119 public final function getRequestMethod () {
120 // @TODO Can't this be 'CONSOLE' ?
125 * Reads a cookie and returns it's value or null if not found
127 * @param $cookieName Name of cookie we shall read
128 * @return $cookieValue Value of cookie or null if not found
129 * @throws UnsupportedOperationException This method should never be called
131 public final function readCookie (string $cookieName) {
132 // There are no cookies on console
133 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
137 * Checks if the request method is GET.
139 * @return $isGet Whether the request method is GET
140 * @throws UnsupportedOperationException This method should never be called
142 public function isGetRequestMethod () {
143 // Not support method
144 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
148 * Checks if the request method is HEAD.
150 * @return $isHead Whether the request method is HEAD
151 * @throws UnsupportedOperationException This method should never be called
153 public function isHeadRequestMethod () {
154 // Not support method
155 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
159 * Checks if the request method is POST.
161 * @return $isPost Whether the request method is POST
162 * @throws UnsupportedOperationException This method should never be called
164 public function isPostRequestMethod () {
165 // Not support method
166 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);