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

Add support for Dotenv v3, v4, and v5 #25

Merged
merged 1 commit into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public function activate( Composer $composer, IOInterface $io ) {
$this->composer = $composer;
$this->io = $io;

if ( file_exists( getcwd() . DIRECTORY_SEPARATOR . '.env' ) ) {
$dotenv = Dotenv::createImmutable( getcwd() );
$dotenv->load();
$path = getcwd();
if ( file_exists( $path . DIRECTORY_SEPARATOR . '.env' ) ) {
$this->loadDotenv( $path );
}
}

Expand All @@ -76,6 +76,34 @@ public function uninstall( Composer $composer, IOInterface $io ) {
// no need to uninstall anything
}

/**
* Activate vlucas/phpdotenv, if available.
*
* @param string $path
*/
protected function loadDotenv( $path ) {
// Dotenv V5
if ( method_exists( 'Dotenv\\Dotenv', 'createUnsafeImmutable' ) ) {
$dotenv = Dotenv::createUnsafeImmutable( $path );
$dotenv->safeLoad();
return;
}

// Dotenv V4
if ( method_exists( 'Dotenv\\Dotenv', 'createImmutable' ) ) {
$dotenv = Dotenv::createImmutable( $path );
$dotenv->safeLoad();
return;
}

// Dotenv V3
if ( method_exists( 'Dotenv\\Dotenv', 'create' ) ) {
$dotenv = Dotenv::create( $path );
$dotenv->safeLoad();
return;
}
}

/**
* Set subscribed events.
*
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "composer-plugin",
"license": "MIT",
"require": {
"vlucas/phpdotenv": "^4.1.0",
"vlucas/phpdotenv": "^3.0 || ^4.0 || ^5.0",
"composer-plugin-api": "^1.0 || ^2.0"
},
"authors": [
Expand Down