@@ -154,25 +154,22 @@ public function __construct(array $environmentVariables = null, string $envPrefi
154154 $ this ->environmentVariables = $ environmentVariables ?? getenv ();
155155 $ this ->envPrefix = $ envPrefix ;
156156
157- if ($ this ->isValidPlatform ()) {
158- if ($ this ->inRuntime ()) {
159- if ($ routes = $ this ->getValue ('ROUTES ' )) {
160- $ this ->routesDef = $ this ->decode ($ routes );
161- }
162- if ($ relationships = $ this ->getValue ('RELATIONSHIPS ' )) {
163- $ this ->relationshipsDef = $ this ->decode ($ relationships );
164- }
165-
166- $ this ->registerFormatter ('pdo_mysql ' , [$ this , 'pdoMySQLFormatter ' ]);
167- $ this ->registerFormatter ('pdo_pgsql ' , [$ this , 'pdoPostgreSQLFormatter ' ]);
168- }
169- if ($ variables = $ this ->getValue ('VARIABLES ' )) {
170- $ this ->variablesDef = $ this ->decode ($ variables );
171- }
172- if ($ application = $ this ->getValue ('APPLICATION ' )) {
173- $ this ->applicationDef = $ this ->decode ($ application );
174- }
157+ if ($ routes = $ this ->getValue ('ROUTES ' )) {
158+ $ this ->routesDef = $ this ->decode ($ routes );
159+ }
160+ if ($ relationships = $ this ->getValue ('RELATIONSHIPS ' )) {
161+ $ this ->relationshipsDef = $ this ->decode ($ relationships );
162+ }
163+
164+ if ($ variables = $ this ->getValue ('VARIABLES ' )) {
165+ $ this ->variablesDef = $ this ->decode ($ variables );
175166 }
167+ if ($ application = $ this ->getValue ('APPLICATION ' )) {
168+ $ this ->applicationDef = $ this ->decode ($ application );
169+ }
170+
171+ $ this ->registerFormatter ('pdo_mysql ' , [$ this , 'pdoMySQLFormatter ' ]);
172+ $ this ->registerFormatter ('pdo_pgsql ' , [$ this , 'pdoPostgreSQLFormatter ' ]);
176173 }
177174
178175 /**
@@ -227,12 +224,14 @@ public function inRuntime() : bool
227224 */
228225 public function credentials (string $ relationship , int $ index = 0 ) : array
229226 {
230- if (!$ this ->isValidPlatform ()) {
231- throw new NotValidPlatformException ('You are not running on Platform.sh, so relationships are not available. ' );
232- }
233227
234- if ($ this ->inBuild ()) {
235- throw new BuildTimeVariableAccessException ('Relationships are not available during the build phase. ' );
228+ if (empty ($ this ->relationshipsDef )) {
229+ if ($ this ->inBuild ()) {
230+ throw new BuildTimeVariableAccessException ('Relationships are not available during the build phase. ' );
231+ }
232+ throw new NotValidPlatformException ('No relationships are defined. Are you sure you are on Platform.sh? '
233+ . ' If you \'re running on your local system you may need to create a tunnel '
234+ . ' to access your environment services. See https://docs.platform.sh/gettingstarted/local/tethered.html ' );
236235 }
237236
238237 if (empty ($ this ->relationshipsDef [$ relationship ])) {
@@ -261,10 +260,6 @@ public function credentials(string $relationship, int $index = 0) : array
261260 */
262261 public function variable (string $ name , $ default = null )
263262 {
264- if (!$ this ->isValidPlatform ()) {
265- return $ default ;
266- }
267-
268263 return $ this ->variablesDef [$ name ] ?? $ default ;
269264 }
270265
@@ -279,8 +274,8 @@ public function variable(string $name, $default = null)
279274 */
280275 public function variables () : array
281276 {
282- if (! $ this ->isValidPlatform ( )) {
283- throw new NotValidPlatformException ('You are not running on Platform.sh, so the variables array is not available. ' );
277+ if (empty ( $ this ->variablesDef )) {
278+ throw new NotValidPlatformException ('No variables are defined. Are you sure you are running on Platform.sh? ' );
284279 }
285280
286281 return $ this ->variablesDef ;
@@ -296,13 +291,12 @@ public function variables() : array
296291 */
297292 public function routes () : array
298293 {
299- if (!$ this ->isValidPlatform ()) {
300- throw new NotValidPlatformException ('You are not running on Platform.sh, so routes are not available. ' );
301- }
302-
303294 if ($ this ->inBuild ()) {
304295 throw new BuildTimeVariableAccessException ('Routes are not available during the build phase. ' );
305296 }
297+ if (empty ($ this ->variablesDef )) {
298+ throw new NotValidPlatformException ('No routes are defined. Are you sure you are running on Platform.sh? ' );
299+ }
306300
307301 return $ this ->routesDef ;
308302 }
@@ -344,8 +338,8 @@ public function getRoute(string $id) : array
344338 */
345339 public function application () : array
346340 {
347- if (! $ this ->isValidPlatform ( )) {
348- throw new NotValidPlatformException ('You are not running on Platform.sh, so the application definition is not available. ' );
341+ if (empty ( $ this ->applicationDef )) {
342+ throw new NotValidPlatformException ('No application definition is available. Are you sure you are running on Platform.sh? ' );
349343 }
350344
351345 return $ this ->applicationDef ;
@@ -490,10 +484,6 @@ protected function decode($variable)
490484 */
491485 public function __get ($ property )
492486 {
493- if (!$ this ->isValidPlatform ()) {
494- throw new NotValidPlatformException (sprintf ('You are not running on Platform.sh, so the %s variable are not available. ' , $ property ));
495- }
496-
497487 $ isBuildVar = in_array ($ property , array_keys ($ this ->directVariables ));
498488 $ isRuntimeVar = in_array ($ property , array_keys ($ this ->directVariablesRuntime ));
499489 // For now, all unprefixed variables are also runtime variables. If that ever changes this
@@ -505,13 +495,25 @@ public function __get($property)
505495 }
506496
507497 if ($ isBuildVar ) {
508- return $ this ->getValue ($ this ->directVariables [$ property ]);
498+ $ value = $ this ->getValue ($ this ->directVariables [$ property ]);
499+ if (is_null ($ value )) {
500+ throw new NotValidPlatformException (sprintf ('The %s variable is not defined. Are you sure you \'re running on Platform.sh? ' , $ property ));
501+ }
502+ return $ value ;
509503 }
510504 if ($ isUnprefixedVar ) {
511- return $ this ->environmentVariables [$ this ->unPrefixedVariablesRuntime [$ property ]] ?? null ;
505+ $ value = $ this ->environmentVariables [$ this ->unPrefixedVariablesRuntime [$ property ]] ?? null ;
506+ if (is_null ($ value )) {
507+ throw new NotValidPlatformException (sprintf ('The %s variable is not defined. Are you sure you \'re running on Platform.sh? ' , $ property ));
508+ }
509+ return $ value ;
512510 }
513511 if ($ isRuntimeVar ) {
514- return $ this ->getValue ($ this ->directVariablesRuntime [$ property ]);
512+ $ value = $ this ->getValue ($ this ->directVariablesRuntime [$ property ]);
513+ if (is_null ($ value )) {
514+ throw new NotValidPlatformException (sprintf ('The %s variable is not defined. Are you sure you \'re running on Platform.sh? ' , $ property ));
515+ }
516+ return $ value ;
515517 }
516518
517519 throw new \InvalidArgumentException (sprintf ('No such variable defined: %s ' , $ property ));
@@ -528,10 +530,6 @@ public function __get($property)
528530 */
529531 public function __isset ($ property )
530532 {
531- if (!$ this ->isValidPlatform ()) {
532- return false ;
533- }
534-
535533 $ isBuildVar = in_array ($ property , array_keys ($ this ->directVariables ));
536534 $ isRuntimeVar = in_array ($ property , array_keys ($ this ->directVariablesRuntime ));
537535 // For now, all unprefixed variables are also runtime variables. If that ever changes this
0 commit comments