Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from devuri/0.4.1
Browse files Browse the repository at this point in the history
0.4.1
  • Loading branch information
devuri authored Mar 6, 2021
2 parents 6ae5833 + 5f1a50f commit 745326a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 53 deletions.
48 changes: 40 additions & 8 deletions sample-wp-config.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
<?php

// Safely load /vendor/autoload.php or exit.
if ( file_exists( dirname( __FILE__ ) . '/vendor/autoload.php' ) ) {
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
} else {
exit("Cant find the autoload file (/vendor/autoload.php)");
}

use DevUri\Config\Setup;

/**
* 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
*/
require_once dirname( __FILE__ ) . '/vendor/autoload.php';

use DevUri\Config\Setup;
// To override setup constant defined in config() method define them before.
Setup::init(__DIR__)->config('development');
// after setup we can define other constant in the normal way or using env function
// or simply use "Setup::get( 'UPLOAD_DIR' )"
// // Custom Uploads Directory.
// define('UPLOADS', Setup::get( 'UPLOAD_DIR' ) );

// or
// define('UPLOADS', env( 'UPLOAD_DIR' ) );
// remember to include with use "use function Env\env;" when using the env function.

// Both "Setup::get( 'UPLOAD_DIR' )" and "env( 'UPLOAD_DIR' )"
// will grab the val from .env file.

// Can also do.
// Setup::init(__DIR__)->config();
// this will setup in development mode.

/**
* WordPress Database Table prefix.
Expand All @@ -17,12 +53,8 @@
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = "wp_";

// setup config.
Setup::init(__DIR__, 'production' );

// Can also do, this will setup in development mode.
// Setup::init(__DIR__);
// we can move this to .env as well like so.
// $table_prefix = env('DB_PREFIX');

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

Expand Down
8 changes: 6 additions & 2 deletions src/ConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ public function required( $name ): void {
}
}

public static function get( $name ): void {
Config::get($name);
public static function get( $name ){
try {
Config::get($name);
} catch ( \Exception $e ) {
return $e->getMessage();
}
}

public static function apply(): void {
Expand Down
30 changes: 3 additions & 27 deletions src/EnvConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class EnvConfig implements ConfigInterface
/**
* Defines Version
*/
const VERSION = '0.3.1';
const VERSION = '0.4.1';

/**
* Constructer.
Expand All @@ -46,12 +46,10 @@ abstract class EnvConfig implements ConfigInterface
* @param array $args additional args.
* @link https://github.com/WordPress/WordPress/blob/master/wp-includes/default-constants.php
*/
public function __construct( $path, $setup = null ) {
public function __construct( $path ) {

// define directory path.
$this->path = $path;

// check whether a .env file exists.
if ( ! file_exists( $this->path . '/.env') ) {
exit(" env file was not found" );
}
Expand All @@ -65,30 +63,8 @@ public function __construct( $path, $setup = null ) {
exit( $e->getMessage() );
}

// self::init( __DIR__, 'production' )
if ( ! is_array( $setup ) ) {
$setup = array( 'environment' => $setup );
}

// defualt setup.
$default = array(
'default' => true,
'environment' => null,
'symfony' => false,
);
$setup = array_merge( $default, $setup );

// Get the values from $_ENV, instead getenv().
Env::$options = Env::USE_ENV_ARRAY;

$this->is_required();

// run default setup using env vars.
$this->config( $setup );

// print_r( ABSPATH );
// print_r( @get_defined_constants() );

}

/**
Expand All @@ -101,7 +77,7 @@ public function __construct( $path, $setup = null ) {
abstract function config( $setup ): void;

// required vars.
private function is_required() {
protected function is_required() {

try {

Expand Down
50 changes: 34 additions & 16 deletions src/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,43 @@
class Setup extends EnvConfig
{

/**
* Singleton
*
* @return object
*/
public static function init( $path ): self {

if ( ! isset( self::$instance ) ) {
self::$instance = new self( $path );
}
return self::$instance;
}

/**
* Runs config setup with default setting.
*
* @param array $setup
* @return
*/
public function config( $setup ): void {
public function config( $setup = null ): void {

// check required vars.
$this->is_required();

// self::init( __DIR__ )->config('production')
if ( ! is_array( $setup ) ) {
$setup = array( 'environment' => $setup );
}

// defualt setup.
$default = array(
'default' => true,
'environment' => null,
'symfony' => false,
);
$setup = array_merge( $default, $setup );

if ( $setup['default'] ) {
$this->environment( $setup['environment'] )
->debug( $setup['environment'] )
Expand Down Expand Up @@ -50,30 +80,18 @@ protected static function const( $key ){
return $constant[$key];
}

/**
* Singleton
*
* @return object
*/
public static function init( $path, $setup = null ): self {

if ( ! isset( self::$instance ) ) {
self::$instance = new self( $path, $setup );
}
return self::$instance;
}

/**
* Debug Settings
*
* @return void
*/
public function debug( $environment ): self {

// check debug settings.
$this->is_debug( $environment );

// debugger
/**
* Debugger setup based on environment.
*/
if ( defined('WP_DEBUG') && false === WP_DEBUG ) :

// Disable Plugin and Theme Editor.
Expand Down

0 comments on commit 745326a

Please sign in to comment.