Issues (257)

src/helpers/Environment.php (4 issues)

1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\helpers;
13
14
use Craft;
15
use craft\helpers\App;
16
use nystudio107\seomatic\Seomatic;
17
18
/**
19
 * @author    nystudio107
20
 * @package   Seomatic
21
 * @since     3.1.47
22
 */
23
class Environment
24
{
25
    // Constants
26
    // =========================================================================
27
28
    public const SEOMATIC_DEV_ENV = 'local';
29
    public const SEOMATIC_STAGING_ENV = 'staging';
30
    public const SEOMATIC_PRODUCTION_ENV = 'live';
31
32
    public const DEV_ENVIRONMENTS = [
33
        'dev',
34
        'development',
35
        'loc',
36
        'local',
37
    ];
38
39
    public const STAGING_ENVIRONMENTS = [
40
        'stage',
41
        'staging',
42
        'test',
43
        'testing',
44
        'uat',
45
        'acc',
46
        'acceptance',
47
        'sandbox',
48
    ];
49
50
    public const PRODUCTION_ENVIRONMENTS = [
51
        'live',
52
        'prod',
53
        'production',
54
        'pub',
55
        'public',
56
    ];
57
58
    // Static Methods
59
    // =========================================================================
60
61
    /**
62
     * Return the current environment, taking into account both the CP setting
63
     * and also the ENVIRONMENT variable setting (if applicable)
64
     *
65
     * @return string
66
     */
67
    public static function determineEnvironment(): string
68
    {
69
        // Default to whatever they have the environment set to
70
        $env = Seomatic::$settings->environment;
71
        $env = Craft::parseEnv($env);
0 ignored issues
show
Deprecated Code introduced by
The function Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
        $env = /** @scrutinizer ignore-deprecated */ Craft::parseEnv($env);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
72
        // If they've manually overridden the environment, just return it
73
        if (self::environmentOverriddenByConfig()) {
74
            return $env;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $env could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
75
        }
76
        if (Seomatic::$settings->manuallySetEnvironment) {
77
            return $env;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $env could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
78
        }
79
        // Try to also check the `ENVIRONMENT` env var
80
        $environment = App::env('ENVIRONMENT');
81
        $environment = $environment ?: App::env('CRAFT_ENVIRONMENT');
82
        if (!empty($environment)) {
83
            $environment = strtolower($environment);
84
            // See if the environment matches a development environment
85
            if (in_array($environment, self::DEV_ENVIRONMENTS, false)) {
86
                $env = self::SEOMATIC_DEV_ENV;
87
            }
88
            // See if the environment matches a staging environment
89
            if (in_array($environment, self::STAGING_ENVIRONMENTS, false)) {
90
                $env = self::SEOMATIC_STAGING_ENV;
91
            }
92
            // See if the environment matches a production environment
93
            if (in_array($environment, self::PRODUCTION_ENVIRONMENTS, false)) {
94
                $env = self::SEOMATIC_PRODUCTION_ENV;
95
            }
96
        }
97
        // If devMode is on, always force the environment to be 'local'
98
        if (Seomatic::$devMode) {
99
            $env = self::SEOMATIC_DEV_ENV;
100
        }
101
102
        return $env;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $env could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
103
    }
104
105
    /**
106
     * Determine whether the user has overridden the `environment` setting via
107
     * a `config/seomatic.php` file
108
     *
109
     * @return bool
110
     */
111
    public static function environmentOverriddenByConfig(): bool
112
    {
113
        $result = false;
114
115
        $config = Craft::$app->getConfig()->getConfigFromFile('seomatic');
116
        if (!empty($config['environment'])) {
117
            $result = true;
118
        }
119
120
        return $result;
121
    }
122
}
123