@@ -351,7 +351,7 @@ protected function _setConfig($config)
351
351
*/
352
352
protected function _processPost ($ data )
353
353
{
354
- $ method = $ this ->env ('REQUEST_METHOD ' );
354
+ $ method = $ this ->getEnv ('REQUEST_METHOD ' );
355
355
$ override = false ;
356
356
357
357
if (in_array ($ method , ['PUT ' , 'DELETE ' , 'PATCH ' ]) &&
@@ -506,12 +506,12 @@ protected function _normalizeNestedFiles(array $files = [])
506
506
*/
507
507
public function contentType ()
508
508
{
509
- $ type = $ this ->env ('CONTENT_TYPE ' );
509
+ $ type = $ this ->getEnv ('CONTENT_TYPE ' );
510
510
if ($ type ) {
511
511
return $ type ;
512
512
}
513
513
514
- return $ this ->env ('HTTP_CONTENT_TYPE ' );
514
+ return $ this ->getEnv ('HTTP_CONTENT_TYPE ' );
515
515
}
516
516
517
517
/**
@@ -539,12 +539,12 @@ public function session(Session $session = null)
539
539
*/
540
540
public function clientIp ()
541
541
{
542
- if ($ this ->trustProxy && $ this ->env ('HTTP_X_FORWARDED_FOR ' )) {
543
- $ ipaddr = preg_replace ('/(?:,.*)/ ' , '' , $ this ->env ('HTTP_X_FORWARDED_FOR ' ));
544
- } elseif ($ this ->trustProxy && $ this ->env ('HTTP_CLIENT_IP ' )) {
545
- $ ipaddr = $ this ->env ('HTTP_CLIENT_IP ' );
542
+ if ($ this ->trustProxy && $ this ->getEnv ('HTTP_X_FORWARDED_FOR ' )) {
543
+ $ ipaddr = preg_replace ('/(?:,.*)/ ' , '' , $ this ->getEnv ('HTTP_X_FORWARDED_FOR ' ));
544
+ } elseif ($ this ->trustProxy && $ this ->getEnv ('HTTP_CLIENT_IP ' )) {
545
+ $ ipaddr = $ this ->getEnv ('HTTP_CLIENT_IP ' );
546
546
} else {
547
- $ ipaddr = $ this ->env ('REMOTE_ADDR ' );
547
+ $ ipaddr = $ this ->getEnv ('REMOTE_ADDR ' );
548
548
}
549
549
550
550
return trim ($ ipaddr );
@@ -559,7 +559,7 @@ public function clientIp()
559
559
*/
560
560
public function referer ($ local = false )
561
561
{
562
- $ ref = $ this ->env ('HTTP_REFERER ' );
562
+ $ ref = $ this ->getEnv ('HTTP_REFERER ' );
563
563
564
564
$ base = Configure::read ('App.fullBaseUrl ' ) . $ this ->webroot ;
565
565
if (!empty ($ ref ) && !empty ($ base )) {
@@ -719,7 +719,7 @@ protected function _is($type, $args)
719
719
*/
720
720
protected function _acceptHeaderDetector ($ detect )
721
721
{
722
- $ acceptHeaders = explode (', ' , $ this ->env ('HTTP_ACCEPT ' ));
722
+ $ acceptHeaders = explode (', ' , $ this ->getEnv ('HTTP_ACCEPT ' ));
723
723
foreach ($ detect ['accept ' ] as $ header ) {
724
724
if (in_array ($ header , $ acceptHeaders )) {
725
725
return true ;
@@ -738,7 +738,7 @@ protected function _acceptHeaderDetector($detect)
738
738
protected function _headerDetector ($ detect )
739
739
{
740
740
foreach ($ detect ['header ' ] as $ header => $ value ) {
741
- $ header = $ this ->env ('http_ ' . $ header );
741
+ $ header = $ this ->getEnv ('http_ ' . $ header );
742
742
if ($ header !== null ) {
743
743
if (!is_string ($ value ) && !is_bool ($ value ) && is_callable ($ value )) {
744
744
return call_user_func ($ value , $ header );
@@ -782,15 +782,15 @@ protected function _environmentDetector($detect)
782
782
{
783
783
if (isset ($ detect ['env ' ])) {
784
784
if (isset ($ detect ['value ' ])) {
785
- return $ this ->env ($ detect ['env ' ]) == $ detect ['value ' ];
785
+ return $ this ->getEnv ($ detect ['env ' ]) == $ detect ['value ' ];
786
786
}
787
787
if (isset ($ detect ['pattern ' ])) {
788
- return (bool )preg_match ($ detect ['pattern ' ], $ this ->env ($ detect ['env ' ]));
788
+ return (bool )preg_match ($ detect ['pattern ' ], $ this ->getEnv ($ detect ['env ' ]));
789
789
}
790
790
if (isset ($ detect ['options ' ])) {
791
791
$ pattern = '/ ' . implode ('| ' , $ detect ['options ' ]) . '/i ' ;
792
792
793
- return (bool )preg_match ($ pattern , $ this ->env ($ detect ['env ' ]));
793
+ return (bool )preg_match ($ pattern , $ this ->getEnv ($ detect ['env ' ]));
794
794
}
795
795
}
796
796
@@ -966,7 +966,7 @@ public function header($name)
966
966
{
967
967
$ name = $ this ->normalizeHeaderName ($ name );
968
968
969
- return $ this ->env ($ name );
969
+ return $ this ->getEnv ($ name );
970
970
}
971
971
972
972
/**
@@ -1117,7 +1117,7 @@ public function withoutHeader($name)
1117
1117
*/
1118
1118
public function method ()
1119
1119
{
1120
- return $ this ->env ('REQUEST_METHOD ' );
1120
+ return $ this ->getEnv ('REQUEST_METHOD ' );
1121
1121
}
1122
1122
1123
1123
/**
@@ -1136,7 +1136,7 @@ public function method()
1136
1136
*/
1137
1137
public function getMethod ()
1138
1138
{
1139
- return $ this ->env ('REQUEST_METHOD ' );
1139
+ return $ this ->getEnv ('REQUEST_METHOD ' );
1140
1140
}
1141
1141
1142
1142
/**
@@ -1211,11 +1211,11 @@ public function withQueryParams(array $query)
1211
1211
*/
1212
1212
public function host ()
1213
1213
{
1214
- if ($ this ->trustProxy && $ this ->env ('HTTP_X_FORWARDED_HOST ' )) {
1215
- return $ this ->env ('HTTP_X_FORWARDED_HOST ' );
1214
+ if ($ this ->trustProxy && $ this ->getEnv ('HTTP_X_FORWARDED_HOST ' )) {
1215
+ return $ this ->getEnv ('HTTP_X_FORWARDED_HOST ' );
1216
1216
}
1217
1217
1218
- return $ this ->env ('HTTP_HOST ' );
1218
+ return $ this ->getEnv ('HTTP_HOST ' );
1219
1219
}
1220
1220
1221
1221
/**
@@ -1225,11 +1225,11 @@ public function host()
1225
1225
*/
1226
1226
public function port ()
1227
1227
{
1228
- if ($ this ->trustProxy && $ this ->env ('HTTP_X_FORWARDED_PORT ' )) {
1229
- return $ this ->env ('HTTP_X_FORWARDED_PORT ' );
1228
+ if ($ this ->trustProxy && $ this ->getEnv ('HTTP_X_FORWARDED_PORT ' )) {
1229
+ return $ this ->getEnv ('HTTP_X_FORWARDED_PORT ' );
1230
1230
}
1231
1231
1232
- return $ this ->env ('SERVER_PORT ' );
1232
+ return $ this ->getEnv ('SERVER_PORT ' );
1233
1233
}
1234
1234
1235
1235
/**
@@ -1241,11 +1241,11 @@ public function port()
1241
1241
*/
1242
1242
public function scheme ()
1243
1243
{
1244
- if ($ this ->trustProxy && $ this ->env ('HTTP_X_FORWARDED_PROTO ' )) {
1245
- return $ this ->env ('HTTP_X_FORWARDED_PROTO ' );
1244
+ if ($ this ->trustProxy && $ this ->getEnv ('HTTP_X_FORWARDED_PROTO ' )) {
1245
+ return $ this ->getEnv ('HTTP_X_FORWARDED_PROTO ' );
1246
1246
}
1247
1247
1248
- return $ this ->env ('HTTPS ' ) ? 'https ' : 'http ' ;
1248
+ return $ this ->getEnv ('HTTPS ' ) ? 'https ' : 'http ' ;
1249
1249
}
1250
1250
1251
1251
/**
@@ -1712,7 +1712,7 @@ public function getProtocolVersion()
1712
1712
}
1713
1713
1714
1714
// Lazily populate this data as it is generally not used.
1715
- preg_match ('/^HTTP\/([\d.]+)$/ ' , $ this ->env ('SERVER_PROTOCOL ' ), $ match );
1715
+ preg_match ('/^HTTP\/([\d.]+)$/ ' , $ this ->getEnv ('SERVER_PROTOCOL ' ), $ match );
1716
1716
$ protocol = '1.1 ' ;
1717
1717
if (isset ($ match [1 ])) {
1718
1718
$ protocol = $ match [1 ];
@@ -1742,10 +1742,45 @@ public function withProtocolVersion($version)
1742
1742
return $ new ;
1743
1743
}
1744
1744
1745
+ /**
1746
+ * Get a value from the request's environment data.
1747
+ * Fallback to using env() if the key is not set in the $environment property.
1748
+ *
1749
+ * @param string $key The key you want to read from.
1750
+ * @param string|null $default Default value when trying to retrieve an environment
1751
+ * variable's value that does not exist.
1752
+ * @return string|null Either the environment value, or null if the value doesn't exist.
1753
+ */
1754
+ public function getEnv ($ key , $ default = null )
1755
+ {
1756
+ $ key = strtoupper ($ key );
1757
+ if (!array_key_exists ($ key , $ this ->_environment )) {
1758
+ $ this ->_environment [$ key ] = env ($ key );
1759
+ }
1760
+
1761
+ return $ this ->_environment [$ key ] !== null ? $ this ->_environment [$ key ] : $ default ;
1762
+ }
1763
+
1764
+ /**
1765
+ * Set a value to the request's environment data.
1766
+ *
1767
+ * @param string $key The key you want to write to.
1768
+ * @param string|null $value Value to set. Default null.
1769
+ * @return $this
1770
+ */
1771
+ public function setEnv ($ key , $ value = null )
1772
+ {
1773
+ $ this ->_environment [$ key ] = $ value ;
1774
+ $ this ->clearDetectorCache ();
1775
+
1776
+ return $ this ;
1777
+ }
1778
+
1745
1779
/**
1746
1780
* Get/Set value from the request's environment data.
1747
1781
* Fallback to using env() if key not set in $environment property.
1748
1782
*
1783
+ * @deprecated 3.5.0 Use getEnv()/setEnv() instead.
1749
1784
* @param string $key The key you want to read/write from/to.
1750
1785
* @param string|null $value Value to set. Default null.
1751
1786
* @param string|null $default Default value when trying to retrieve an environment
0 commit comments