11<?php
2- defined ('COREPATH ' ) or exit ('No direct script access allowed ' );
32
43/**
5- * CodeIgniter
6- *
7- * An open source application development framework for PHP
8- *
9- * This content is released under the MIT License (MIT)
10- *
11- * Copyright (c) 2014-2019 British Columbia Institute of Technology
12- * Copyright (c) 2019-2020 CodeIgniter Foundation
13- *
14- * Permission is hereby granted, free of charge, to any person obtaining a copy
15- * of this software and associated documentation files (the "Software"), to deal
16- * in the Software without restriction, including without limitation the rights
17- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18- * copies of the Software, and to permit persons to whom the Software is
19- * furnished to do so, subject to the following conditions:
4+ * This file is part of WebbyPHP Framework.
205 *
21- * The above copyright notice and this permission notice shall be included in
22- * all copies or substantial portions of the Software.
6+ * (c) Kwame Oteng Appiah-Nti <[email protected] > 237 *
24- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30- * THE SOFTWARE.
31- *
32- * @package CodeIgniter
33- * @author CodeIgniter Dev Team
34- * @copyright 2019-2020 CodeIgniter Foundation
35- * @license https://opensource.org/licenses/MIT MIT License
36- * @link https://codeigniter.com
37- * @since Version 4.0.0
38- * @used-for Version 3.1.11
39- * @filesource
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
4010 */
4111
12+ defined ('COREPATH ' ) or exit ('No direct script access allowed ' );
13+
4214/**
4315 * Environment-specific configuration
4416 */
@@ -78,13 +50,11 @@ public function load(): bool
7850 {
7951 $ vars = $ this ->parse ();
8052
81- if ($ vars === null )
82- {
53+ if ($ vars === null ) {
8354 return false ;
8455 }
8556
86- foreach ($ vars as $ name => $ value )
87- {
57+ foreach ($ vars as $ name => $ value ) {
8858 $ this ->setVariable ($ name , $ value );
8959 }
9060
@@ -101,32 +71,27 @@ public function load(): bool
10171 public function parse (): ?array
10272 {
10373 // We don't want to enforce the presence of a .env file, they should be optional.
104- if (! is_file ($ this ->path ))
105- {
74+ if (! is_file ($ this ->path )) {
10675 return null ;
10776 }
10877
10978 // Ensure the file is readable
110- if (! is_readable ($ this ->path ))
111- {
79+ if (! is_readable ($ this ->path )) {
11280 throw new \InvalidArgumentException ("The .env file is not readable: {$ this ->path }" );
11381 }
11482
11583 $ vars = [];
11684
11785 $ lines = file ($ this ->path , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
11886
119- foreach ($ lines as $ line )
120- {
87+ foreach ($ lines as $ line ) {
12188 // Is it a comment?
122- if (strpos (trim ($ line ), '# ' ) === 0 )
123- {
89+ if (strpos (trim ($ line ), '# ' ) === 0 ) {
12490 continue ;
12591 }
12692
12793 // If there is an equal sign, then we know we are assigning a variable.
128- if (strpos ($ line , '= ' ) !== false )
129- {
94+ if (strpos ($ line , '= ' ) !== false ) {
13095 list ($ name , $ value ) = $ this ->normaliseVariable ($ line );
13196 $ vars [$ name ] = $ value ;
13297 }
@@ -147,16 +112,13 @@ public function parse(): ?array
147112 */
148113 protected function setVariable (string $ name , string $ value = '' )
149114 {
150- if (! getenv ($ name , true ))
151- {
115+ if (! getenv ($ name , true )) {
152116 putenv ("$ name= $ value " );
153117 }
154- if (empty ($ _ENV [$ name ]))
155- {
118+ if (empty ($ _ENV [$ name ])) {
156119 $ _ENV [$ name ] = $ value ;
157120 }
158- if (empty ($ _SERVER [$ name ]))
159- {
121+ if (empty ($ _SERVER [$ name ])) {
160122 $ _SERVER [$ name ] = $ value ;
161123 }
162124 }
@@ -175,8 +137,7 @@ protected function setVariable(string $name, string $value = '')
175137 public function normaliseVariable (string $ name , string $ value = '' ): array
176138 {
177139 // Split our compound string into it's parts.
178- if (strpos ($ name , '= ' ) !== false )
179- {
140+ if (strpos ($ name , '= ' ) !== false ) {
180141 list ($ name , $ value ) = explode ('= ' , $ name , 2 );
181142 }
182143
@@ -212,18 +173,16 @@ public function normaliseVariable(string $name, string $value = ''): array
212173 */
213174 protected function sanitizeValue (string $ value ): string
214175 {
215- if (! $ value )
216- {
176+ if (! $ value ) {
217177 return $ value ;
218178 }
219179
220180 // Does it begin with a quote?
221- if (strpbrk ($ value [0 ], '" \'' ) !== false )
222- {
181+ if (strpbrk ($ value [0 ], '" \'' ) !== false ) {
223182 // value starts with a quote
224183 $ quote = $ value [0 ];
225184 $ regexPattern = sprintf (
226- '/^
185+ '/^
227186 %1$s # match a quote at the start of the value
228187 ( # capturing sub-pattern used
229188 (?: # we do not need to capture this
@@ -234,21 +193,19 @@ protected function sanitizeValue(string $value): string
234193 ) # end of the capturing sub-pattern
235194 %1$s # and the closing quote
236195 .*$ # and discard any string after the closing quote
237- /mx ' , $ quote
196+ /mx ' ,
197+ $ quote
238198 );
239199 $ value = preg_replace ($ regexPattern , '$1 ' , $ value );
240200 $ value = str_replace ("\\$ quote " , $ quote , $ value );
241201 $ value = str_replace ('\\\\' , '\\' , $ value );
242- }
243- else
244- {
202+ } else {
245203 $ parts = explode (' # ' , $ value , 2 );
246204
247205 $ value = trim ($ parts [0 ]);
248206
249207 // Unquoted values cannot contain whitespace
250- if (preg_match ('/\s+/ ' , $ value ) > 0 )
251- {
208+ if (preg_match ('/\s+/ ' , $ value ) > 0 ) {
252209 throw new \InvalidArgumentException ('.env values containing spaces must be surrounded by quotes. ' );
253210 }
254211 }
@@ -273,17 +230,15 @@ protected function sanitizeValue(string $value): string
273230 */
274231 protected function resolveNestedVariables (string $ value ): string
275232 {
276- if (strpos ($ value , '$ ' ) !== false )
277- {
233+ if (strpos ($ value , '$ ' ) !== false ) {
278234 $ loader = $ this ;
279235
280236 $ value = preg_replace_callback (
281237 '/\${([a-zA-Z0-9_]+)}/ ' ,
282238 function ($ matchedPatterns ) use ($ loader ) {
283239 $ nestedVariable = $ loader ->getVariable ($ matchedPatterns [1 ]);
284240
285- if (is_null ($ nestedVariable ))
286- {
241+ if (is_null ($ nestedVariable )) {
287242 return $ matchedPatterns [0 ];
288243 }
289244
@@ -328,8 +283,7 @@ public function prepareVariable(string $value): string
328283 */
329284 protected function getVariable (string $ name )
330285 {
331- switch (true )
332- {
286+ switch (true ) {
333287 case array_key_exists ($ name , $ _ENV ):
334288 return $ _ENV [$ name ];
335289 case array_key_exists ($ name , $ _SERVER ):
0 commit comments