Skip to content

Too many global variables #41

Closed
tom-- opened this Issue Jan 7, 2013 · 5 comments

3 participants

@tom--
tom-- commented Jan 7, 2013

By the time an action runs the following globals have been defined:

'frontendConfigDir' => '/sites/spin-be/frontend/config'
'root' => '/sites/spin-be/frontend/config/../..'
'paramsLocalFile' => '/sites/spin-be/frontend/config/params-local.php'
'paramsLocalFileArray' => array(...)
'paramsEnvFile' => '/sites/spin-be/frontend/config/params-env.php'
'paramsEnvFileArray' => array(...)
'paramsCommonFile' => '/sites/spin-be/frontend/config/../../common/config/params.php'
'commonConfigDir' => '/sites/spin-be/common/config'
'commonParamsLocalFile' => '/sites/spin-be/common/config/params-local.php'
'commonParamsLocal' => array(...)
'commonEnvParamsFile' => '/sites/spin-be/common/config/params-env.php'
'commonEnvParams' => array(...)
'paramsCommonArray' => array(...)
'params' => array(...)
'mainLocalFile' => '/sites/spin-be/frontend/config/main-local.php'
'mainLocalConfiguration' => array(...)
'mainEnvFile' => '/sites/spin-be/frontend/config/main-env.php'
'mainEnvConfiguration' => array(...)
'config' => array(...)
'app' => WebApplication(...)

Only the last of them seems reasonable (though I won't use it).

The others can be avoided by using an IEFE where needed, for example:

<?php
return call_user_func(function () {
    $commonConfigDir = dirname(__FILE__);

    // get local parameters in
    $commonParamsLocalFile = $commonConfigDir . DIRECTORY_SEPARATOR . 'params-local.php';
    $commonParamsLocal = file_exists($commonParamsLocalFile) ? require ($commonParamsLocalFile) : array();

    // if exists, include it, otherwise set as an empty array
    $commonEnvParamsFile = $commonConfigDir . DIRECTORY_SEPARATOR . 'params-env.php';
    $commonEnvParams = file_exists($commonEnvParamsFile) ? require($commonEnvParamsFile) : array();

    return CMap::mergeArray(
        array(
            // cache settings -if APC is not loaded, then use CDbCache
            'cache.core' => extension_loaded('apc') ?
                array(
                    'class' => 'CApcCache',
                ) :
                array(
                    'class' => 'CDbCache',
                    'connectionID' => 'db',
                    'autoCreateCacheTable' => true,
                    'cacheTableName' => 'cache',
                ),
            'cache.content' => array(
                'class' => 'CDbCache',
                'connectionID' => 'db',
                'autoCreateCacheTable' => true,
                'cacheTableName' => 'cache',
            ),
            // url rules needed by CUrlManager
            'url.rules' => array(
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),
            'php.exePath' => '/usr/bin/php'
        ),
        CMap::mergeArray($commonEnvParams, $commonParamsLocal)
    );
});
@hijarian
Clevertech member

@tom--, a lot more simple way is just to wrap the variable declarations in unnamed block, like this:

<?php
{
    $globalConfig = array(<...>);
    $localConfig = require(<...>);
    return CMap::mergeArray($globalConfig, $localConfig);
}

This way we don't need rocket science like anonymous functions. It'll definitely be in YiiBoilerplate someday.

@tom--
tom-- commented Jan 22, 2013

good gracious! i didn't even know php had such a feature. nice to know. and better performance too, i imagine.

@tom--
tom-- commented Jan 22, 2013

@hijarian is that feature of PHP documented? I haven't been able to find it.

@hijarian
Clevertech member

Sorry, @tom--, forget it, it doesn't work. 👎 I mixed up PHP and C++ in my mind. Block scoping doesn't work that way in PHP. So, for now you have the sole working solution.

@rinatio
rinatio commented Sep 6, 2013

I cleaned up config files a bit, there are no so much globals anymore

@rinatio rinatio closed this Sep 6, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.