]> git.mxchange.org Git - core.git/blob - framework/main/classes/request/console/class_ConsoleRequest.php
Continued:
[core.git] / framework / main / classes / request / console / class_ConsoleRequest.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Request;
4
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;
9
10 /**
11  * A concrete request class
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
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
18  *
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.
23  *
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.
28  *
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/>.
31  */
32 class ConsoleRequest extends BaseRequest implements Requestable {
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         private function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Creates an instance of this class and prepares it a little
45          *
46          * @return      $consoleInstance        An instance of this class
47          */
48         public static final function createConsoleRequest () {
49                 // Create an instance
50                 $consoleInstance = new ConsoleRequest();
51
52                 // Prepare the console request data for usage
53                 $consoleInstance->prepareRequestData();
54
55                 // Return the prepared instance
56                 return $consoleInstance;
57         }
58
59         /**
60          * Prepares the request data for usage
61          *
62          * @return      void
63          */
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);
69                 }
70
71                 // Get the "request data" from the command-line argument list
72                 $args = $_SERVER['argv'];
73
74                 // Are there less than two parameters?
75                 if ($_SERVER['argc'] < 2) {
76                         // Skip this
77                         return;
78                 }
79
80                 // Is the first element "index.php" ?
81                 if ($args[0] == 'index.php') {
82                         // Then remove it
83                         array_shift($args);
84                 }
85
86                 // Try to determine next parameters
87                 foreach ($args as $arg) {
88                         // Seperate argument key from value
89                         $argArray = explode('=', $arg);
90
91                         // Is the second one set?
92                         if (!isset($argArray[1])) {
93                                 // Add it likewise, but empty value
94                                 $this->setRequestElement($argArray[0], '');
95                         } else {
96                                 // Set a name=value pair escaped and secured
97                                 $this->setRequestElement($argArray[0], escapeshellcmd($argArray[1]));
98                         }
99                 }
100         }
101
102         /**
103          * Getter for a header element or 'null' if header was not found
104          *
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
108          */
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);
112         }
113
114         /**
115          * Getter for request method. This getter might be useful for security filters
116          *
117          * @return      $requestMethod  Used request method
118          */
119         public final function getRequestMethod () {
120                 // @TODO Can't this be 'CONSOLE' ?
121                 return 'LOCAL';
122         }
123
124         /**
125          * Reads a cookie and returns it's value or null if not found
126          *
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
130          */
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);
134         }
135
136         /**
137          * Checks if the request method is GET.
138          *
139          * @return      $isGet  Whether the request method is GET
140          * @throws      UnsupportedOperationException   This method should never be called
141          */
142         public function isGetRequestMethod () {
143                 // Not support method
144                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
145         }
146
147         /**
148          * Checks if the request method is HEAD.
149          *
150          * @return      $isHead         Whether the request method is HEAD
151          * @throws      UnsupportedOperationException   This method should never be called
152          */
153         public function isHeadRequestMethod () {
154                 // Not support method
155                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
156         }
157
158         /**
159          * Checks if the request method is POST.
160          *
161          * @return      $isPost         Whether the request method is POST
162          * @throws      UnsupportedOperationException   This method should never be called
163          */
164         public function isPostRequestMethod () {
165                 // Not support method
166                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
167         }
168
169 }