Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to override ZF2 application.config.php directives locally?

I'm working on a ZF2 project and my public/index.php file is like follows:

<?php
chdir(dirname(__DIR__));
require 'init_autoloader.php';
Zend\Mvc\Application::init(require 'config/application.config.php')->run();

Application initialize process starts with using application.config.php and I know that ZF2 provides nice way to override module configurations locally via filenames like modulename.local.php but not for application.config.php file.

For example, in application.config.php, I have a module_listener_options key as follows:

return array(
    'modules' => array(
    // ...
    ),

    'module_listener_options' => array(
        'module_paths' => array(
         // ...
         ),

    'config_glob_paths' => array(
        'config/autoload/{,*.}{global,local}.php',
     ),

     'config_cache_enabled' => TRUE,
     'config_cache_key' => 'configuration_cache',
     'cache_dir' => __DIR__ . '/../data/cache'
// ...
)

So I want to disable config caching locally while working on a development environment but I want to turn it on in a production environment without need a post-deployment tricks (like writing custom git-hook / bash script etc..).

Also, I have a APPLICATION_ENVIRONMENT $_ENV variable on all servers (dev, prod, test) I don't know what is the best approach to achieve this in ZF2.

I found the Stephen Rees-Carter's article, yes workarounds this problem but i want to learn is there any other / more elegant solutions which doesn't depends on composer.

like image 612
edigu Avatar asked Feb 17 '23 17:02

edigu


2 Answers

You could just test your environment variable in your app config and set caching accordingly, eg.,

<?php
// application.config.php
$env = getenv('APPLICATION_ENVIRONMENT');
$configCacheEnabled = ($env == 'production');

return array(
    //..

    'config_cache_enabled' => $configCacheEnabled,

    //..
);
like image 182
Crisp Avatar answered Apr 06 '23 01:04

Crisp


Here's an example to include modules only on your development setup, with a local file override. You can easily just remove the environment variable check if you wished to just override with the presence of the local config file.

application.config.php

$config = array(
    'modules' => array(
        'Application',
        'ZfcBase',
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

if(getenv('APPLICATION_ENV') == 'development' && is_readable('config/autoload/application.config.local.php')){
    $localAppConfig = require 'config/autoload/application.config.local.php';
    $config = array_merge_recursive($config,$localAppConfig);
} 

return $config;

config/application.config.local.php

return array(
    'modules' => array(
        'ZendDeveloperTools',
        'ZFTool'
    ),
    /**
     * Add any overrides to the new local config
     */
);

You can then just add overrides to your local file, which can be different for staging and production environments.

like image 36
Andrew Avatar answered Apr 06 '23 02:04

Andrew