Development

#8399 (sfContext::switchTo() inconsistent behavior)

You must first sign up to be able to contribute.

Ticket #8399 (new defect)

Opened 3 years ago

Last modified 2 years ago

sfContext::switchTo() inconsistent behavior

Reported by: mentalstring Assigned to: fabien
Priority: minor Milestone:
Component: configuration Version: 1.4.14
Keywords: Cc:
Qualification: Unreviewed

Description

While working with switchTo() I noticed an odd behavior. For the following code:

echo sfContext::getInstance()->getConfiguration()->getApplication().'-'.
     sfProjectConfiguration::getActive()->getApplication() . PHP_EOL;

sfContext::switchTo('frontend');
echo sfContext::getInstance()->getConfiguration()->getApplication().'-'.
     sfProjectConfiguration::getActive()->getApplication() . PHP_EOL;

sfContext::switchTo('backend');
echo sfContext::getInstance()->getConfiguration()->getApplication().'-'.
     sfProjectConfiguration::getActive()->getApplication() . PHP_EOL;

The output is:

backend-backend
frontend-frontend
backend-frontend <- relevant bit 

I'm aware that there are many caveats on using sfContext::switchTo(), but I'm wondering if this is expected or if it's a bug?

Change History

10/14/11 09:58:53 changed by cyran

  • version changed from 1.3.3 to 1.4.14.

I have this same situation in symfony 1.4.14 and 1.2.12

class sfCacheManager {
    const app_name = 'system';
    static protected $env_types = array('cache', 'prod');
    static protected $is_debug;
    static protected $prev_application;
    static protected $prev_sfconfig;

    //general method for clearing cache when using sfFileCache, sfSQLiteCache and others...
    static public function clearCache($pattern) {
        self::$prev_sfconfig = sfConfig::getAll();
        $currentConfiguration = sfContext::getInstance()->getConfiguration();
        
        self::$env_types[] = $currentConfiguration->getEnvironment();
        self::$env_types = array_unique(self::$env_types);
        self::$prev_application = $currentConfiguration->getApplication();
        self::$is_debug = $currentConfiguration->isDebug();

        foreach (self::$env_types as $env) {
            $frontend_context = self::getFrontendContext($env);
            $view_cache_manager = $frontend_context->getViewCacheManager();

            if($view_cache_manager) {
                $view_cache = $view_cache_manager->getCache();
                $view_cache->removePattern(sprintf('**%s**', $pattern));
            }
        }
        self::returnToDefaultContext();
    }

    static protected function getFrontendContext($env) {
        $configuration = ProjectConfiguration::getApplicationConfiguration(self::app_name, $env, self::$is_debug);
        //initializing frontend instance with different env types
        return sfContext::createInstance($configuration, sprintf('system_%s', $env));
    }

    static protected function returnToDefaultContext() {
        //switching back to previous context
        if(sfContext::hasInstance(self::$prev_application)) {
            sfContext::switchTo(self::$prev_application);
            $sf_config_diff = array_diff(self::$prev_sfconfig, sfConfig::getAll());
            sfConfig::add($sf_config_diff);
        }
    }
}

after this i have csrf secret form frontend settings.yml not from backend settings.yml, and all forms checking csrf protection throws an error