Issues (231)

src/helpers/Environment.php (4 issues)

1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
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 nystudio107\seomatic\Seomatic;
16
17
/**
18
 * @author    nystudio107
19
 * @package   Seomatic
20
 * @since     3.1.47
21
 */
22
class Environment
23
{
24
    // Constants
25
    // =========================================================================
26
27
    const SEOMATIC_DEV_ENV = 'local';
28
    const SEOMATIC_STAGING_ENV = 'staging';
29
    const SEOMATIC_PRODUCTION_ENV = 'live';
30
31
    const DEV_ENVIRONMENTS = [
32
        'dev',
33
        'development',
34
        'loc',
35
        'local',
36
    ];
37
38
    const STAGING_ENVIRONMENTS = [
39
        'stage',
40
        'staging',
41
        'test',
42
        'testing',
43
        'uat',
44
        'acc',
45
        'acceptance',
46
        'sandbox',
47
    ];
48
49
    const PRODUCTION_ENVIRONMENTS = [
50
        'live',
51
        'prod',
52
        'production',
53
        'pub',
54
        'public',
55
    ];
56
57
    // Static Methods
58
    // =========================================================================
59
60
    /**
61
     * Return the current environment, taking into account both the CP setting
62
     * and also the ENVIRONMENT variable setting (if applicable)
63
     *
64
     * @return string
65
     */
66
    public static function determineEnvironment(): string
67
    {
68
        // Default to whatever they have the environment set to
69
        $env = Seomatic::$settings->environment;
70
        if (Seomatic::$craft31) {
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
        }
73
        // If they've manually overridden the environment, just return it
74
        if (self::environmentOverriddenByConfig()) {
75
            return $env;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $env could return the type boolean which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
76
        }
77
        // Try to also check the `ENVIRONMENT` env var
78
        $environment = getenv('ENVIRONMENT');
79
        $environment = $environment ?: getenv('CRAFT_ENVIRONMENT');
80
        if (Seomatic::$settings->manuallySetEnvironment) {
81
            return $env;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $env could return the type boolean which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
82
        }
83
        if (!empty($environment)) {
84
            $environment = strtolower($environment);
85
            // See if the environment matches a development environment
86
            if (in_array($environment, self::DEV_ENVIRONMENTS, false)) {
87
                $env = self::SEOMATIC_DEV_ENV;
88
            }
89
            // See if the environment matches a staging environment
90
            if (in_array($environment, self::STAGING_ENVIRONMENTS, false)) {
91
                $env = self::SEOMATIC_STAGING_ENV;
92
            }
93
            // See if the environment matches a production environment
94
            if (in_array($environment, self::PRODUCTION_ENVIRONMENTS, false)) {
95
                $env = self::SEOMATIC_PRODUCTION_ENV;
96
            }
97
        }
98
        // If devMode is on, always force the environment to be 'local'
99
        if (Seomatic::$devMode) {
100
            $env = self::SEOMATIC_DEV_ENV;
101
        }
102
103
        return $env;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $env could return the type boolean which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
104
    }
105
106
    /**
107
     * Determine whether the user has overridden the `environment` setting via
108
     * a `config/seomatic.php` file
109
     *
110
     * @return bool
111
     */
112
    public static function environmentOverriddenByConfig(): bool
113
    {
114
        $result = false;
115
116
        $config = Craft::$app->getConfig()->getConfigFromFile('seomatic');
117
        if (!empty($config['environment'])) {
118
            $result = true;
119
        }
120
121
        return $result;
122
    }
123
}
124