Skip to content

Commit

Permalink
Add php dotenv support for config file
Browse files Browse the repository at this point in the history
  • Loading branch information
edpittol committed Apr 8, 2017
1 parent df84851 commit 7e32738
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Database
DB_NAME=aztecwpdevenv
DB_USER=root
DB_PASSWORD=
DB_HOST=localhost
DB_CHARSET=utf8
DB_COLLATE=
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Project files
/public/
/tmp/
!/public/wp-config.php
.salts

# Eclipse Project
.settings/
Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
}
],
"require" : {
"php-di/php-di" : "^5.4"
"php-di/php-di" : "^5.4",
"vlucas/phpdotenv" : "^2.4",
"aaemnnosttv/wp-cli-dotenv-command" : "^1.0"
},
"require-dev" : {
"wp-cli/wp-cli" : "^1.1",
Expand All @@ -16,6 +18,9 @@
"autoload" : {
"psr-4" : {
"AztecWpDevEnv\\" : "src/"
}
},
"files" : [
"src/WP_CLI/bootstrap.php"
]
}
}
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
</rule>

<!-- Sniff the theme files -->
<file>./theme/src/</file>
<file>./src/</file>
</ruleset>
88 changes: 88 additions & 0 deletions public/wp-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the
* installation. You don't have to use the web site, you can
* copy this file to "wp-config.php" and fill in the values.
*
* This file contains the following configurations:
*
* * MySQL settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://codex.wordpress.org/Editing_wp-config.php
*
* @package WordPress
*/

$root_dir = __DIR__ . '/../';

require_once $root_dir . '/vendor/autoload.php';

// Load main env file
$dotenv = new Dotenv\Dotenv( $root_dir );
$dotenv->load();

// Load auto-generated salts file
$dotenv = new Dotenv\Dotenv( $root_dir, '.salts' );
$dotenv->load();

// ** MySQL settings ** //
/** The name of the database for WordPress */
define( 'DB_NAME', getenv( 'DB_NAME' ) );

/** MySQL database username */
define( 'DB_USER', getenv( 'DB_USER' ));

/** MySQL database password */
define( 'DB_PASSWORD', getenv( 'DB_PASSWORD' ) );

/** MySQL hostname */
define( 'DB_HOST', getenv( 'DB_HOST' ) );

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', getenv( 'DB_CHARSET' ) );

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', getenv( 'DB_COLLATE' ) );

/**
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* @since 2.6.0
*/
define( 'AUTH_KEY', getenv( 'AUTH_KEY' ) );
define( 'SECURE_AUTH_KEY', getenv( 'SECURE_AUTH_KEY' ) );
define( 'LOGGED_IN_KEY', getenv( 'LOGGED_IN_KEY' ) );
define( 'NONCE_KEY', getenv( 'NONCE_KEY' ) );
define( 'AUTH_SALT', getenv( 'AUTH_SALT' ) );
define( 'SECURE_AUTH_SALT', getenv( 'SECURE_AUTH_SALT' ) );
define( 'LOGGED_IN_SALT', getenv( 'LOGGED_IN_SALT' ) );
define( 'NONCE_SALT', getenv( 'NONCE_SALT' ) );

/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';




/* That's all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) )
define( 'ABSPATH', dirname( __FILE__ ) . '/' );

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
29 changes: 29 additions & 0 deletions src/WP_CLI/Dotenv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Dotenv WP CLI command hooks
*
* @package AztecWpDevEnv
*/

namespace AztecWpDevEnv\WP_CLI;

/**
* Add new functionalities to the WP CLI dot env command
*/
class Dotenv {

/**
* Ensure that the salts file exist
*
* If the file salts doesn't exist, the salts aren't generated
*/
public function ensure_salts_file() {
$salts_file = __DIR__ . '/../../.salts';

if ( ! file_exists( $salts_file ) ) {
if ( ! touch( $salts_file ) ) {
\WP_CLI::error( 'Cannot create the salts file.' );
}
}
}
}
13 changes: 13 additions & 0 deletions src/WP_CLI/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use AztecWpDevEnv\WP_CLI\Dotenv as Aztec_WP_CLI_Dotenv;

require_once __DIR__ . '/../../vendor/autoload.php';

if ( ! class_exists( WP_CLI::class ) ) {
require_once __DIR__ . '/../../vendor/wp-cli/wp-cli/php/class-wp-cli.php';
}

$dot_env = new Aztec_WP_CLI_Dotenv();

\WP_CLI::add_hook( 'before_invoke:dotenv salts generate', array( $dot_env, 'ensure_salts_file' ) );

0 comments on commit 7e32738

Please sign in to comment.