Skip to content

Instantly share code, notes, and snippets.

@texdc
Last active September 15, 2017 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save texdc/6151978e892a00a0a24ff07d64d88025 to your computer and use it in GitHub Desktop.
Save texdc/6151978e892a00a0a24ff07d64d88025 to your computer and use it in GitHub Desktop.
A "better" ZF2 application config
<?php declare(strict_types=1);
namespace {owner}\{project}\environment;
final class ApplicationConfig
{
public const ENV_DEVELOPMENT = 'development';
public const ENV_TEST = 'test';
public const ENV_STAGING = 'staging';
public const ENV_PRODUCITON = 'production';
private const CACHE_DIR = 'data/config/';
private $modules : array;
private $devModules : array;
public function __construct(array $modules = [], array $devModules = [])
{
$this->modules = $modules;
$this->devModules = $modules + $devModules;
}
public function build(string $anEnvironment, string $cacheDir = self::CACHE_DIR) : array
{
$isProduction = in_array(strtolower($anEnvironment), [static::ENV_PRODUCTION, static::ENV_STAGING]);
return [
'modules' => ($isProduction) ? $this->modules : $this->devModules,
'module_listener_options' => [
'module_paths' => ['./module', './vendor'],
'config_glob_paths' => ['config/autoload/{{,*.}global,{,*.}local}.php'],
'config_cache_enabled' => $isProduction,
'config_cache_key' => 'app_config',
'module_map_cache_enabled' => $isProduction,
'module_map_cache_key' => 'module_map',
'cache_dir' => $cacheDir,
'check_dependencies' => $isProduction,
],
];
}
}
// define APP_ENV on your server for each environment
return (new ApplicationConfig)->build(getenv('APP_ENV'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment