|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2015 RhubarbPHP |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace Rhubarb\Crown; |
| 20 | + |
| 21 | +require_once __DIR__."/Settings.php"; |
| 22 | + |
| 23 | +use \Gcd\Core\Request; |
| 24 | +use Gcd\Core\UrlHandlers\UrlHandler; |
| 25 | + |
| 26 | +/** |
| 27 | + * A class providing the rest of the platform some contextual information |
| 28 | + * |
| 29 | + * @property bool $UnitTesting |
| 30 | + * @property bool $IsAjaxRequest |
| 31 | + * @property bool $IsCliInvocation |
| 32 | + * @property bool $Live True to indicate this is a live production server |
| 33 | + * @property bool $DeveloperMode True to enable developer only functionality |
| 34 | + * @property bool $SimulateNonCli True to pretend that the request is not a CLI request (even if it is - used by unit testing) |
| 35 | + * @property mixed $SimulatedRequestBody For unit testing - simulates the request body instead of using php://input |
| 36 | + * @property UrlHandler $UrlHandler The URL handler currently generating the response |
| 37 | + * |
| 38 | + * @author acuthbert |
| 39 | + * @copyright GCD Technologies 2012 |
| 40 | + */ |
| 41 | +class Context extends Settings |
| 42 | +{ |
| 43 | + protected function InitialiseDefaultValues() |
| 44 | + { |
| 45 | + global $unitTesting; |
| 46 | + |
| 47 | + parent::InitialiseDefaultValues(); |
| 48 | + |
| 49 | + // $unitTesting is set in phpunit-bootstrap.php |
| 50 | + $this->UnitTesting = ( isset( $unitTesting ) && $unitTesting ) ? true : false; |
| 51 | + $this->DeveloperMode = false; |
| 52 | + $this->Live = false; |
| 53 | + } |
| 54 | + |
| 55 | + public function GetIsAjaxRequest() |
| 56 | + { |
| 57 | + if ( isset( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) == 'xmlhttprequest') |
| 58 | + { |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Check if the script was invoked via PHP's CLI |
| 67 | + * |
| 68 | + * @return bool |
| 69 | + */ |
| 70 | + public function GetIsCliInvocation() |
| 71 | + { |
| 72 | + if ( $this->SimulateNonCli ) |
| 73 | + { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + return 'cli' === php_sapi_name(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * A static accessor for the Request property |
| 82 | + * |
| 83 | + * @return \Gcd\Core\Request\Request The current Request |
| 84 | + */ |
| 85 | + public static function CurrentRequest() |
| 86 | + { |
| 87 | + $contextInstance = new static(); |
| 88 | + |
| 89 | + return $contextInstance->Request; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Lazily initialise and then return the current Request. |
| 94 | + * |
| 95 | + * @return \Gcd\Core\Request\Request |
| 96 | + */ |
| 97 | + public function GetRequest() |
| 98 | + { |
| 99 | + if ( !isset( $this->modelData[ 'Request'] ) ) |
| 100 | + { |
| 101 | + if ( $this->IsCliInvocation ) |
| 102 | + { |
| 103 | + $request = new Request\CliRequest(); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + $contentType = ( isset( $_SERVER[ "CONTENT_TYPE"] ) ) ? strtolower( $_SERVER[ "CONTENT_TYPE" ] ) : ""; |
| 108 | + |
| 109 | + switch( $contentType ) |
| 110 | + { |
| 111 | + case "application/json": |
| 112 | + $request = new Request\JsonRequest(); |
| 113 | + break; |
| 114 | + default: |
| 115 | + $request = new Request\WebRequest(); |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + $this->modelData[ 'Request' ] = $request; |
| 121 | + } |
| 122 | + |
| 123 | + return $this->modelData[ 'Request' ]; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Returns the body of the request. |
| 128 | + * |
| 129 | + * This is not automatically passed to the request as this might be an expensive operation |
| 130 | + * that may never actually get used (e.g. a File upload) |
| 131 | + * |
| 132 | + * @return bool|mixed|string |
| 133 | + */ |
| 134 | + public function GetRequestBody() |
| 135 | + { |
| 136 | + if ( $this->UnitTesting ) |
| 137 | + { |
| 138 | + return $this->SimulatedRequestBody; |
| 139 | + } |
| 140 | + |
| 141 | + $requestBody = file_get_contents( "php://input" ); |
| 142 | + |
| 143 | + return $requestBody; |
| 144 | + } |
| 145 | +} |
0 commit comments