]> git.mxchange.org Git - core.git/blob - framework/main/classes/request/console/class_ConsoleRequest.php
Some updates:
[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\UnsupportedOperationException;
7 use Org\Mxchange\CoreFramework\Request\Requestable;
8
9 /**
10  * A concrete request class
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
14 <<<<<<< HEAD:framework/main/classes/request/console/class_ConsoleRequest.php
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 =======
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
18 >>>>>>> Some updates::inc/main/classes/request/console/class_ConsoleRequest.php
19  * @license             GNU GPL 3.0 or any newer version
20  * @link                http://www.shipsimu.org
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <http://www.gnu.org/licenses/>.
34  */
35 class ConsoleRequest extends BaseRequest implements Requestable {
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Creates an instance of this class and prepares it a little
48          *
49          * @return      $consoleInstance        An instance of this class
50          */
51         public static final function createConsoleRequest () {
52                 // Create an instance
53                 $consoleInstance = new ConsoleRequest();
54
55                 // Prepare the console request data for usage
56                 $consoleInstance->prepareRequestData();
57
58                 // Return the prepared instance
59                 return $consoleInstance;
60         }
61
62         /**
63          * Prepares the request data for usage
64          *
65          * @return      void
66          */
67         protected function prepareRequestData () {
68                 // 'argv' and 'argc' should be there
69                 if ((!isset($_SERVER['argv'])) || (!isset($_SERVER['argc']))) {
70                         // Maybe not right PHP mode? (needs CLI?)
71                         trigger_error(sprintf('[%s:%d]: argv/argc not set: %s', __METHOD__, __LINE__, print_r($_SERVER, true)), E_USER_ERROR);
72                 } // END - if
73
74                 // Get the "request data" from the command-line argument list
75                 $args = $_SERVER['argv'];
76
77                 // Are there less than two parameters?
78                 if ($_SERVER['argc'] < 2) {
79                         // Skip this
80                         return;
81                 } // END - if
82
83                 // Is the first element "index.php" ?
84                 if ($args[0] == 'index.php') {
85                         // Then remove it
86                         array_shift($args);
87                 } // END - if
88
89                 // Try to determine next parameters
90                 foreach ($args as $arg) {
91                         // Seperate argument key from value
92                         $argArray = explode('=', $arg);
93
94                         // Is the second one set?
95                         if (!isset($argArray[1])) {
96                                 // Add it likewise, but empty value
97                                 $this->setRequestElement($argArray[0], '');
98                         } else {
99                                 // Set a name=value pair escaped and secured
100                                 $this->setRequestElement($argArray[0], escapeshellcmd($argArray[1]));
101                         }
102                 } // END - foreach
103         }
104
105         /**
106          * Getter for a header element or 'null' if header was not found
107          *
108          * @param       $headerName             Name of the header
109          * @return      $headerValue    Value of the header or 'null' if not found
110          * @throws      UnsupportedOperationException   This method should never be called
111          */
112         public function getHeaderElement ($headerName) {
113                 // Console doesn't have any headers
114                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
115         }
116
117         /**
118          * Getter for request method. This getter might be useful for security filters
119          *
120          * @return      $requestMethod  Used request method
121          */
122         public final function getRequestMethod () {
123                 // @TODO Can't this be 'CONSOLE' ?
124                 return 'LOCAL';
125         }
126
127         /**
128          * Reads a cookie and returns it's value or null if not found
129          *
130          * @param       $cookieName             Name of cookie we shall read
131          * @return      $cookieValue    Value of cookie or null if not found
132          * @throws      UnsupportedOperationException   This method should never be called
133          */
134         public final function readCookie ($cookieName) {
135                 // There are no cookies on console
136                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
137         }
138
139         /**
140          * Checks if the request method is GET.
141          *
142          * @return      $isGet  Whether the request method is GET
143          * @throws      UnsupportedOperationException   This method should never be called
144          */
145         public function isGetRequestMethod () {
146                 // Not support method
147                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
148         }
149
150         /**
151          * Checks if the request method is HEAD.
152          *
153          * @return      $isHead         Whether the request method is HEAD
154          * @throws      UnsupportedOperationException   This method should never be called
155          */
156         public function isHeadRequestMethod () {
157                 // Not support method
158                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
159         }
160
161         /**
162          * Checks if the request method is POST.
163          *
164          * @return      $isPost         Whether the request method is POST
165          * @throws      UnsupportedOperationException   This method should never be called
166          */
167         public function isPostRequestMethod () {
168                 // Not support method
169                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
170         }
171
172 }