@@ -80,6 +80,13 @@ class Application extends Container
8080 */
8181 protected $ environmentFile = '.env ' ;
8282
83+ /**
84+ * The environment name.
85+ *
86+ * @var string|null
87+ */
88+ protected $ environment ;
89+
8390 /**
8491 * Indicates if the application is running in the console.
8592 */
@@ -450,16 +457,6 @@ public function runningInConsole(): bool
450457 return $ this ->isRunningInConsole ;
451458 }
452459
453- /**
454- * Determines if the application is running UNIT TEST
455- *
456- * @return bool
457- */
458- public function isRunningUnitTests (): bool
459- {
460- return strpos ($ _SERVER ['argv ' ][0 ] ?? '' , 'phpunit ' ) !== false ;
461- }
462-
463460 /**
464461 * Checks if the application has been bootstrapped.
465462 *
@@ -637,6 +634,97 @@ public function getProvider(string $provider): ?ServiceProvider
637634 return null ;
638635 }
639636
637+ /**
638+ * Determine if the application is in the given environment.
639+ *
640+ * @param string|array $environments
641+ * @return bool
642+ */
643+ public function environment (...$ environments ): bool
644+ {
645+ if (count ($ environments ) === 1 && is_array ($ environments [0 ])) {
646+ $ environments = $ environments [0 ];
647+ }
648+
649+ $ current = $ this ['config ' ]->get ('app.env ' , 'production ' );
650+
651+ foreach ($ environments as $ environment ) {
652+ if ($ environment === $ current || strtolower ($ environment ) === strtolower ($ current )) {
653+ return true ;
654+ }
655+ }
656+
657+ return false ;
658+ }
659+
660+ /**
661+ * Get or check the current application environment.
662+ *
663+ * @param string|null $environment
664+ * @return string|bool
665+ */
666+ public function environmentIs ($ environment = null )
667+ {
668+ if (is_null ($ environment )) {
669+ return $ this ['config ' ]->get ('app.env ' , 'production ' );
670+ }
671+
672+ return $ this ->environment ($ environment );
673+ }
674+
675+ /**
676+ * Determine if the application is running in production.
677+ *
678+ * @return bool
679+ */
680+ public function isProduction (): bool
681+ {
682+ return $ this ->environment ('production ' );
683+ }
684+
685+ /**
686+ * Determine if the application is running in development.
687+ *
688+ * @return bool
689+ */
690+ public function isDevelopment (): bool
691+ {
692+ return $ this ->environment ('development ' , 'local ' );
693+ }
694+
695+ /**
696+ * Determines if the application is running UNIT TEST
697+ *
698+ * @return bool
699+ */
700+ public function isRunningUnitTests (): bool
701+ {
702+ return strpos ($ _SERVER ['argv ' ][0 ] ?? '' , 'phpunit ' ) !== false ;
703+ }
704+
705+ /**
706+ * Get the environment file the application is using.
707+ *
708+ * @return string
709+ */
710+ public function environmentFile (): string
711+ {
712+ return $ this ->environmentFile ;
713+ }
714+
715+ /**
716+ * Set the environment file to be loaded during bootstrapping.
717+ *
718+ * @param string $file
719+ * @return $this
720+ */
721+ public function loadEnvironmentFrom ($ file )
722+ {
723+ $ this ->environmentFile = $ file ;
724+
725+ return $ this ;
726+ }
727+
640728 /**
641729 * Dispatches the application request.
642730 *
0 commit comments