Renamed Registry -> GenericRegistry to make it clear that this registry does
[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  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15  * @license             GNU GPL 3.0 or any newer version
16  * @link                http://www.shipsimu.org
17  *
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.
22  *
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.
27  *
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/>.
30  */
31 class ConsoleRequest extends BaseRequest implements Requestable {
32         /**
33          * Protected constructor
34          *
35          * @return      void
36          */
37         protected function __construct () {
38                 // Call parent constructor
39                 parent::__construct(__CLASS__);
40         }
41
42         /**
43          * Creates an instance of this class and prepares it a little
44          *
45          * @return      $consoleInstance        An instance of this class
46          */
47         public static final function createConsoleRequest () {
48                 // Create an instance
49                 $consoleInstance = new ConsoleRequest();
50
51                 // Prepare the console request data for usage
52                 $consoleInstance->prepareRequestData();
53
54                 // Return the prepared instance
55                 return $consoleInstance;
56         }
57
58         /**
59          * Prepares the request data for usage
60          *
61          * @return      void
62          */
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);
68                 } // END - if
69
70                 // Get the "request data" from the command-line argument list
71                 $args = $_SERVER['argv'];
72
73                 // Are there less than two parameters?
74                 if ($_SERVER['argc'] < 2) {
75                         // Skip this
76                         return;
77                 } // END - if
78
79                 // Is the first element "index.php" ?
80                 if ($args[0] == 'index.php') {
81                         // Then remove it
82                         array_shift($args);
83                 } // END - if
84
85                 // Try to determine next parameters
86                 foreach ($args as $arg) {
87                         // Seperate argument key from value
88                         $argArray = explode('=', $arg);
89
90                         // Is the second one set?
91                         if (!isset($argArray[1])) {
92                                 // Add it likewise, but empty value
93                                 $this->setRequestElement($argArray[0], '');
94                         } else {
95                                 // Set a name=value pair escaped and secured
96                                 $this->setRequestElement($argArray[0], escapeshellcmd($argArray[1]));
97                         }
98                 } // END - foreach
99         }
100
101         /**
102          * Getter for a header element or 'null' if header was not found
103          *
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
107          */
108         public function getHeaderElement ($headerName) {
109                 // Console doesn't have any headers
110                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
111         }
112
113         /**
114          * Getter for request method. This getter might be useful for security filters
115          *
116          * @return      $requestMethod  Used request method
117          */
118         public final function getRequestMethod () {
119                 // @TODO Can't this be 'CONSOLE' ?
120                 return 'LOCAL';
121         }
122
123         /**
124          * Reads a cookie and returns it's value or null if not found
125          *
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
129          */
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);
133         }
134
135         /**
136          * Checks if the request method is GET.
137          *
138          * @return      $isGet  Whether the request method is GET
139          * @throws      UnsupportedOperationException   This method should never be called
140          */
141         public function isGetRequestMethod () {
142                 // Not support method
143                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
144         }
145
146         /**
147          * Checks if the request method is HEAD.
148          *
149          * @return      $isHead         Whether the request method is HEAD
150          * @throws      UnsupportedOperationException   This method should never be called
151          */
152         public function isHeadRequestMethod () {
153                 // Not support method
154                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
155         }
156
157         /**
158          * Checks if the request method is POST.
159          *
160          * @return      $isPost         Whether the request method is POST
161          * @throws      UnsupportedOperationException   This method should never be called
162          */
163         public function isPostRequestMethod () {
164                 // Not support method
165                 throw new UnsupportedOperationException(array($this, __FUNCTION__, $executorInstance), self::EXCEPTION_UNSPPORTED_OPERATION);
166         }
167
168 }