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