webCOMAND

io_comand_env\env

A framework to define and access development, staging and production environments within a package.  This makes it possible to develop and test package code on a production system, but within a development or staging environment, before it is ready for release to production.

The env class works by adjusting where the Package Router looks for package files based on the environment determined by one of the following options (first match wins):

  1. Specified by the env option passed to the constructor.
    e.g. $env = new \io_comand_env\env(['env'=>'staging']);
  2. Specified by the env query parameter in the request URL.
    e.g. https://account.webcomand.com/com_package_example/?env=staging
  3. Set for the user session.
    e.g. $user_session->set_user_setting(\io_comand_env\env::USER_ENVIRONMENT, 'staging');
  4. If none of the options above match, the default environment will be used, which is production, unless overridden with the default option passed to the constructor.

The determined environment will be used to update the user session automatically, which will be picked up automatically for subsequent requests, if not overridden again with another option above.

The webCOMAND auto-loader will look for package files in the usual package folder, but within a subfolder based on the determined environment (e.g. "dev" or "staging").

If package files are managed as File content in webCOMAND, File Settings can be added to write files to a dev, staging and production folders upon store, save and approve respectively.

Constants

The following constants are available.

  • ENV_NAMESPACE = 'io_comand_env'
  • USER_ENVIRONMENT = 'io.comand.env'
  • RUN_ENVIRONMENT_PRIVILEGE - 'ENVIRONMENT_RUN'

Example

The following index.php file in the root folder of a package can be used to route requests to the appropriate environment if an env query parameter is specified in the URL (e.g. ?env=staging).

<?php
/**
 * use the webCOMAND Framework to set the package environment based on
 * the active user and their selected environment or the env query
 * parameter (ie. ?env=staging).  If env=staging, the COMAND auto-
 * loader will find packages in the packages/staging subfolder if it
 * exists, otherwise fallback to the main packages folder(s).
 */
$framework = new \com_webcomand::framework();
$env = new \io_comand_env\env(['framework'=>$framework]);

echo("Environment = $env");
// subsequent packages will auto-load from the appropriate environment

cMVC Example

If the index.php file in the root folder of a package utilizes the cMVC Framework, the cMVC router must be configured to look for files in the appropriate environment subfolder.

The package router will look in the specified environment, and then fallback to the production environment if the file is not within the specified environment.  However, the cMVC router will only look in the specified environment.  It will not fallback to production.
<?php
namespace com_example_package;

$framework = new \com_webcomand\framework();
$env = new \io_comand_env\env([
    'framework' => $framework,
    'add_package_paths' => TRUE,
    'verify_user' => FALSE
]);

$mvc_path = __DIR__ . ($env != '' ? DIRECTORY_SEPARATOR . $env : '');

\io_comand_mvc\router::route([
	'namespace' => __NAMESPACE__,
    'namespace_path' => $mvc_path, // adjust path to controllers
    'base_dir' => $mvc_path, // adjust path to views and files
    'files' => 'files', // change from 'public', so package router won't find production public files first
    'vars' => [
        'framework' => $framework,
        'env' => $env
    ]
]);