Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use env variables instead of constants #193

Merged
merged 24 commits into from
Sep 28, 2021
Merged

Use env variables instead of constants #193

merged 24 commits into from
Sep 28, 2021

Conversation

rustamwin
Copy link
Member

Q A
Is bugfix?
New feature? ✔️
Breaks BC? ✔️/❌
Fixed issues #190

@BoShurik
Copy link

What about moving Environment class to a file bootstrap.php?
Something like this:

require_once __DIR__ . '/vendor/autoload.php'

$dotenv = Dotenv::createImmutable(dirname(__DIR__, 2));
$dotenv->load();

$_ENV['YII_ENV'] = empty($_ENV['YII_ENV'])
    ? self::DEFAULT_YII_ENV
    : (string)$_ENV['YII_ENV'];
$_SERVER['YII_ENV'] = $_ENV['YII_ENV'];

$_ENV['YII_DEBUG'] = empty($_ENV['YII_DEBUG'])
    ? self::DEFAULT_YII_DEBUG
    : filter_var(
        $_ENV['YII_DEBUG'],
        FILTER_VALIDATE_BOOLEAN,
        FILTER_NULL_ON_FAILURE
    ) ?? self::DEFAULT_YII_DEBUG;
$_SERVER['YII_DEBUG'] = $_ENV['YII_DEBUG'];

if ($_ENV['YII_ENV'] === 'test') {
    $c3 = dirname(__DIR__) . '/c3.php';

    if (is_file($c3)) {
        require_once $c3;
    }
}

In that case we can get rid of an index-test.php file and Environment class that do not related to the application business logic. Also this is a good place for future preloading

@samdark
Copy link
Member

samdark commented Sep 18, 2021

Getting rid of index-test.php is a good idea.

@samdark
Copy link
Member

samdark commented Sep 19, 2021

@rustamwin would you please resolve conflicts and try @BoShurik idea?

@rustamwin
Copy link
Member Author

@rustamwin would you please resolve conflicts and try @BoShurik idea?

Yes.

environment.php Outdated Show resolved Hide resolved
psalm.xml Show resolved Hide resolved
yii Outdated Show resolved Hide resolved
public/index.php Show resolved Hide resolved
@vjik
Copy link
Member

vjik commented Sep 22, 2021

Config is created in entry points:

$config = new Config(
	dirname(__DIR__, 2),
	'/config/packages', // Configs path.
	$this->environment,
	[
		'params',
		'events',
		'events-web',
		'events-console',
	],
);

Root path, configs paths and recursive merge groups should be same in all entry points. Maybe create ConfigFactory?

rustamwin and others added 2 commits September 22, 2021 16:02
Co-authored-by: Sergei Predvoditelev <sergey.predvoditelev@gmail.com>
Co-authored-by: Sergei Predvoditelev <sergey.predvoditelev@gmail.com>
@yiiliveext
Copy link
Contributor

What about moving Environment class to a file bootstrap.php?
Something like this:

require_once __DIR__ . '/vendor/autoload.php'

$dotenv = Dotenv::createImmutable(dirname(__DIR__, 2));
$dotenv->load();

$_ENV['YII_ENV'] = empty($_ENV['YII_ENV'])
    ? self::DEFAULT_YII_ENV
    : (string)$_ENV['YII_ENV'];
$_SERVER['YII_ENV'] = $_ENV['YII_ENV'];

$_ENV['YII_DEBUG'] = empty($_ENV['YII_DEBUG'])
    ? self::DEFAULT_YII_DEBUG
    : filter_var(
        $_ENV['YII_DEBUG'],
        FILTER_VALIDATE_BOOLEAN,
        FILTER_NULL_ON_FAILURE
    ) ?? self::DEFAULT_YII_DEBUG;
$_SERVER['YII_DEBUG'] = $_ENV['YII_DEBUG'];

if ($_ENV['YII_ENV'] === 'test') {
    $c3 = dirname(__DIR__) . '/c3.php';

    if (is_file($c3)) {
        require_once $c3;
    }
}

In that case we can get rid of an index-test.php file and Environment class that do not related to the application business logic. Also this is a good place for future preloading

The name of the file is the same as config/bootstrap.php. This can be misleading.

@rustamwin rustamwin requested a review from a team September 27, 2021 17:56
@@ -0,0 +1,2 @@
YII_ENV=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about proving a default value here?

Suggested change
YII_ENV=
YII_ENV=dev

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value is '/'. See config plugin.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's usually not only config environment.

preload.php Show resolved Hide resolved
composer.json Outdated Show resolved Hide resolved
yii Show resolved Hide resolved
@@ -0,0 +1,2 @@
YII_ENV=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's usually not only config environment.

@samdark samdark merged commit 600b27c into master Sep 28, 2021
@samdark samdark deleted the env branch September 28, 2021 18:38
@samdark
Copy link
Member

samdark commented Sep 28, 2021

👍 Would you please apply that to app-api, demo and demo-api as well?

@rustamwin
Copy link
Member Author

+1 Would you please apply that to app-api, demo and demo-api as well?

Yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants