PHP Classes

File: src/Boot/src/Environment/AppEnvironment.php

Recommend this page to a friend!
  Packages of Wolfy-J   spiral   src/Boot/src/Environment/AppEnvironment.php   Download  
File: src/Boot/src/Environment/AppEnvironment.php
Role: Example script
Content type: text/plain
Description: Example script
Class: spiral
Modular Web application development framework
Author: By
Last change:
Date: 11 months ago
Size: 1,213 bytes
 

 

Contents

Class file image Download
<?php

declare(strict_types=1);

namespace Spiral\Boot\Environment;

use Spiral\Boot\EnvironmentInterface;
use Spiral\Boot\Injector\ProvideFrom;
use Spiral\Boot\Injector\InjectableEnumInterface;

#[ProvideFrom(method: 'detect')]
enum AppEnvironment: string implements InjectableEnumInterface
{
    case Production = 'prod';
    case Stage = 'stage';
    case Testing = 'testing';
    case Local = 'local';

    public static function detect(EnvironmentInterface $environment): self
    {
        $value = $environment->get('APP_ENV');

        // Aliases
        $value = match ($value) {
            'production' => self::Production->value,
            'test' => self::Testing->value,
            default => $value,
        };

        return \is_string($value)
            ? (self::tryFrom($value) ?? self::Local)
            : self::Local;
    }

    public function isProduction(): bool
    {
        return $this === self::Production;
    }

    public function isTesting(): bool
    {
        return $this === self::Testing;
    }

    public function isLocal(): bool
    {
        return $this === self::Local;
    }

    public function isStage(): bool
    {
        return $this === self::Stage;
    }
}