Skip to content

Commit

Permalink
Restructured app to keep config/env-dist.php in created project and h…
Browse files Browse the repository at this point in the history
…ave config/env.php created from config/env-dist.php.

config/env.php will no longer be version controlled.
  • Loading branch information
rotexdegba committed Oct 3, 2017
1 parent 8f2b8e0 commit 879844b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore-dist
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ logs/*

!public/js/*/vendor

/config/app-settings.php
/config/app-settings.php
/config/env.php
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ of this template application framework.
| |-- app-settings-dist.php
| |-- dependencies.php
| |-- env.php
| |-- env-dist.php
| |-- ini-settings.php
| `-- routes-and-middlewares.php
|
Expand Down
4 changes: 4 additions & 0 deletions documentation/QUICKSTART.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@

* **`config/env.php`:** Edit it to define your application's environment. It should return one of **S3MVC_APP_ENV_DEV**, **S3MVC_APP_ENV_PRODUCTION**, **S3MVC_APP_ENV_STAGING** or **S3MVC_APP_ENV_TESTING** relevant to the environment you are installing your web-application.

* This file should not be committed to version control (it has already been added (by default) to the `.gitignore` file for your application if you are using **git** for version control). Instead, it should be created by making a copy of **`config/env-dist.php`** and then configured uniquely for each environment your application is to be deployed to.

* **`config/env-dist.php`:** A template file for creating **`config/env.php`** in new environments your application will be deployed to. It should be version controlled.

* **`config/ini-settings.php`:** Modify ini settings via **`ini_set(..)`** here. Remember to update **`date.timezone`** in this file to match your timezone (see http://php.net/manual/en/timezones.php).

* **`config/routes-and-middlewares.php`:** Add additional routes and middlewares (see https://www.slimframework.com/docs/concepts/middleware.html for more information on middlewares) for your application here (if needed). You can decide to define all the routes for your application here (in this case set the **S3MVC_APP_USE_MVC_ROUTES** constant in **`public/index.php`** to false). A default **`/`** route is defined in this file and will be active if **S3MVC_APP_USE_MVC_ROUTES** has a value of **`false`**.
Expand Down
79 changes: 68 additions & 11 deletions public/index-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* This function detects which environment your web-app is running in
* (i.e. one of Production, Development, Staging or Testing).
*
* NOTE: Make sure you edit /public/env.php to return one of S3MVC_APP_ENV_DEV,
* NOTE: Make sure you edit ../config/env.php to return one of S3MVC_APP_ENV_DEV,
* S3MVC_APP_ENV_PRODUCTION, S3MVC_APP_ENV_STAGING or S3MVC_APP_ENV_TESTING
* relevant to the environment you are installing your web-app.
*
Expand All @@ -58,9 +58,71 @@ function s3MVC_GetCurrentAppEnvironment() {

if( !$current_env ) {

$root_dir = dirname(dirname(__FILE__)). DIRECTORY_SEPARATOR;
$current_env = include $root_dir.'config'. DIRECTORY_SEPARATOR.'env.php';
}
$root_dir = S3MVC_APP_ROOT_PATH. DIRECTORY_SEPARATOR;
$env_file = $root_dir.'config'. DIRECTORY_SEPARATOR.'env.php';

if( !file_exists($env_file) ) {

$env_file_path_abs = "{$root_dir}config". DIRECTORY_SEPARATOR.'env.php';
$env_dist_file_path_abs = "{$root_dir}config". DIRECTORY_SEPARATOR.'env-dist.php';

// We are not in a dev environment.
// Make error message to be sent to the client less detailed
$env_file_missing_error_page_content = <<<END
<p>
Please check server log file for details.
<br>Goodbye!!!
</p>
END;

$env_file_missing_error_page = <<<END
<html>
<head>
<title>SlimPHP 3 Skeleton MVC App Error</title>
<style>
body{
margin:0;
padding:30px;
font:14px/1.5 Helvetica,Arial,Verdana,sans-serif;
}
h1{
margin:0;
font-size:48px;
font-weight:normal;
line-height:48px;
}
</style>
</head>
<body>
<h1>SlimPHP 3 Skeleton MVC App Environment Configuration Error</h1>
$env_file_missing_error_page_content
</body>
</html>
END;
echo $env_file_missing_error_page;

$current_uri = \Slim\Http\Request::createFromEnvironment(new \Slim\Http\Environment(s3MVC_GetSuperGlobal('server')))->getUri()->__toString();

// Write full message to log via error_log(...)
// http://php.net/manual/en/function.error-log.php
$log_message = "ERROR: [$current_uri] `$env_file_path_abs` not found."
. " Please copy `$env_dist_file_path_abs` to `$env_file_path_abs` and"
. " configure `$env_file_path_abs` for your application's current environment.";

// error_log ( $log_message , 0 ) means message is sent to PHP's system logger,
// using the Operating System's system logging mechanism or a file, depending
// on what the error_log configuration directive is set to.
if( @error_log ( $log_message , 0 ) === false ) {

// last attempt to log
error_log ( $log_message , 4 ); // message is sent directly to the SAPI logging handler.
}
exit;
} // if( !file_exists($env_file) )

$current_env = include $env_file;

} // if( !$current_env )

return $current_env;
}
Expand All @@ -84,10 +146,7 @@ function s3MVC_PrependAction2ActionMethodName($action_method_name) {
$app_settings_file_path = "{$s3mvc_root_dir}config". DIRECTORY_SEPARATOR.'app-settings.php';

if( !file_exists($app_settings_file_path) ) {

$app_settings_file_path_rel = '.'.DIRECTORY_SEPARATOR."config". DIRECTORY_SEPARATOR.'app-settings.php';
$app_settings_dist_file_path_rel = '.'.DIRECTORY_SEPARATOR."config". DIRECTORY_SEPARATOR.'app-settings-dist.php';


$app_settings_file_path_abs = "{$s3mvc_root_dir}config". DIRECTORY_SEPARATOR.'app-settings.php';
$app_settings_dist_file_path_abs = "{$s3mvc_root_dir}config". DIRECTORY_SEPARATOR.'app-settings-dist.php';

Expand Down Expand Up @@ -133,7 +192,7 @@ function s3MVC_PrependAction2ActionMethodName($action_method_name) {
</style>
</head>
<body>
<h1>SlimPHP 3 Skeleton MVC App Configuration Error</h1>
<h1>SlimPHP 3 Skeleton MVC App Settings Configuration Error</h1>
$app_settings_file_missing_error_page_content
</body>
</html>
Expand All @@ -156,8 +215,6 @@ function s3MVC_PrependAction2ActionMethodName($action_method_name) {
// last attempt to log
error_log ( $log_message , 4 ); // message is sent directly to the SAPI logging handler.
}


exit;
}

Expand Down
8 changes: 4 additions & 4 deletions src/s3mvc-tools/post-composer-create-project.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public static function exec(){
}
sleep(1);

static::printInfo( "Moving `{$config_src_folder}env-dist.php` to `{$config_src_folder}env.php` ...." );
static::printInfo( "Copying `{$config_src_folder}env-dist.php` to `{$config_src_folder}env.php` ...." );

if( @rename("{$config_src_folder}env-dist.php", "{$config_src_folder}env.php") ) {
if( @copy("{$config_src_folder}env-dist.php", "{$config_src_folder}env.php") ) {

static::printInfo( "Successfully Moved!".PHP_EOL );
static::printInfo( "Successfully Copied!".PHP_EOL );

} else {

static::printError( "Could not move `{$config_src_folder}env-dist.php` to `{$config_src_folder}env.php`!".PHP_EOL );
static::printError( "Could not copy `{$config_src_folder}env-dist.php` to `{$config_src_folder}env.php`!".PHP_EOL );
}
sleep(1);

Expand Down

0 comments on commit 879844b

Please sign in to comment.