switchTo() public static method

Sets the current context to something else
public static switchTo ( string $name )
$name string The name of the context to switch to
 protected function createContextInstance($application = 'frontend', $enviroment = 'dev', $debug = true)
 {
     $configuration = ProjectConfiguration::getApplicationConfiguration($application, $enviroment, $debug);
     sfContext::createInstance($configuration);
     sfContext::switchTo($application);
     $this->context = sfContext::getInstance();
 }
 protected function createContextInstance($application = 'backend', $enviroment = 'dev', $debug = true)
 {
     $configuration = ProjectConfiguration::getApplicationConfiguration($application, $enviroment, $debug);
     sfContext::createInstance($configuration);
     sfContext::switchTo($application);
     $this->context = sfContext::getInstance();
     //include(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_config_dir_name').'/nc_flavor.yml'));
 }
 /**
  * build new sfContext
  * 
  * @return void
  */
 public function setupContext()
 {
     if (!$this instanceof sfPhpunitContextInitilizerInterface) {
         throw new Exception('You should implement `sfPhpunitContextInitilizerInterface` before initialazing context');
     }
     $app = $this->getApplication();
     $env = $this->getEnvironment();
     $name = $app . '-' . $env;
     if (!sfContext::hasInstance($name)) {
         sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration($app, $env, true), $name);
     }
     sfContext::switchTo($name);
 }
Beispiel #4
0
 public function _initialize()
 {
     if (!file_exists('config/ProjectConfiguration.class.php')) {
         throw new \Codeception\Exception\Module('Symfony1', 'config/ProjectConfiguration.class.php not found. This file is required for running symfony1');
     }
     require_once 'config/ProjectConfiguration.class.php';
     $conf = \ProjectConfiguration::getApplicationConfiguration($this->config['app'], 'test', true);
     \sfContext::createInstance($conf, 'default');
     \sfContext::switchTo('default');
     // chdir(\sfConfig::get('sf_web_dir'));
     $this->browser = new \sfBrowser();
     $this->browser->get('/');
     \sfForm::disableCSRFProtection();
 }
 /**
  * setUp method for PHPUnit
  *
  */
 protected function setUp()
 {
     // Here we have to initialize the according context for the test case.
     // As this initialization is quite expensive, the script tries to
     // to do this as rare as possible.
     $app = $this->getApplication();
     if (!sfContext::hasInstance($app)) {
         $configuration = ProjectConfiguration::getApplicationConfiguration($app, $this->getEnvironment(), $this->isDebug());
         sfContext::createInstance($configuration, $app);
         // We have to create a configuration first before the symfony lib is defined.
         // this is the only but ugly chance for including the lime lib correctly
         // without creating a project configuration instance somewhere before
         require_once $configuration->getSymfonyLibDir() . '/vendor/lime/lime.php';
     }
     // do we have to switch the context?
     if ($app != sfContext::getInstance()->getConfiguration()->getApplication()) {
         sfContext::switchTo($app);
     }
     // autoloading ready, continue
     $this->testBrowser = new sfTestFunctional(new sfBrowser(), $this->getTest());
     $this->_start();
 }
Beispiel #6
0
 public function executeRemove($request)
 {
     $this->patterns = array();
     if ($request->isMethod('post')) {
         sfContext::switchTo('frontend');
         $frontendContext = sfContext::getInstance();
         if ($page = $request->getParameter('page')) {
             $routing = $frontendContext->getRouting();
             if ($route = $routing->findRoute($page)) {
                 $params = $route['parameters'];
                 $module = $params['module'];
                 $action = $params['action'];
                 unset($params['module']);
                 unset($params['action']);
                 unset($params['sf_culture']);
                 $this->patterns = LsCache::generateCachePatterns($module, $action, $params);
                 LsCache::clearCachePatterns($this->patterns);
             }
         }
         if ($entityId = $request->getParameter('entity_id')) {
             $this->patterns = array_merge($this->patterns, LsCache::clearEntityCacheById($entityId));
         }
         if ($relationshipId = $request->getParameter('relationship_id')) {
             $this->patterns = array_merge($this->patterns, LsCache::clearRelationshipCacheById($relationshipId));
         }
         if ($listId = $request->getParameter('list_id')) {
             $this->patterns = array_merge($this->patterns, LsCache::clearListCacheById($listId));
         }
         if ($userName = $request->getParameter('username')) {
             $this->patterns = array_merge($this->patterns, LsCache::clearUserCacheByName($userName));
         }
         if ($groupName = $request->getParameter('groupname')) {
             $this->patterns = array_merge($this->patterns, LsCache::clearGroupCacheByName($groupName));
         }
         sfContext::switchTo('backend');
     }
 }
Beispiel #7
0
$frontend_context_prod = sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false));
$i18n_context = sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration('i18n', 'test', true));
$cache_context = sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration('cache', 'test', true));
// ::getInstance()
$t->diag('::getInstance()');
$t->isa_ok($frontend_context, 'sfContext', '::createInstance() takes an application configuration and returns application context instance');
$t->isa_ok(sfContext::getInstance('frontend'), 'sfContext', '::createInstance() creates application name context instance');
$context = sfContext::getInstance('frontend');
$context1 = sfContext::getInstance('i18n');
$context2 = sfContext::getInstance('cache');
$t->is(sfContext::getInstance('i18n'), $context1, '::getInstance() returns the named context if it already exists');
// ::switchTo();
$t->diag('::switchTo()');
sfContext::switchTo('i18n');
$t->is(sfContext::getInstance(), $context1, '::switchTo() changes the default context instance returned by ::getInstance()');
sfContext::switchTo('cache');
$t->is(sfContext::getInstance(), $context2, '::switchTo() changes the default context instance returned by ::getInstance()');
// ->get() ->set() ->has()
$t->diag('->get() ->set() ->has()');
$t->is($context1->has('object'), false, '->has() returns false if no object of the given name exist');
$object = new stdClass();
$context1->set('object', $object, '->set() stores an object in the current context instance');
$t->is($context1->has('object'), true, '->has() returns true if an object is stored for the given name');
$t->is($context1->get('object'), $object, '->get() returns the object associated with the given name');
try {
    $context1->get('object1');
    $t->fail('->get() throws an sfException if no object is stored for the given name');
} catch (sfException $e) {
    $t->pass('->get() throws an sfException if no object is stored for the given name');
}
$context['foo'] = $frontend_context;
Beispiel #8
0
 /**
  *
  */
 private function removeNavCaches()
 {
     $currentApp = sfContext::getInstance()->getConfiguration()->getApplication();
     $cacheApp = 'pc_frontend';
     if (!sfContext::hasInstance($cacheApp)) {
         sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration($cacheApp, $this->getContext()->getConfiguration()->getEnvironment(), $this->getContext()->getConfiguration()->isDebug()));
     }
     sfContext::switchTo($cacheApp);
     if ($cache = sfContext::getInstance($cacheApp)->getViewCacheManager()) {
         $cache->remove('@sf_cache_partial?module=default&action=_globalNav&sf_cache_key=*');
         $cache->remove('@sf_cache_partial?module=default&action=_localNav&sf_cache_key=*');
     }
     sfContext::switchTo($currentApp);
 }
 public function purgeCacheUri($cacheUri, $targetApplication = null, $hosts = '*')
 {
     $currentApplication = sfConfig::get('sf_app');
     if ($switchRequired = !is_null($targetApplication) && $targetApplication !== $currentApplication) {
         try {
             sfContext::switchTo($targetApplication);
         } catch (Exception $e) {
             $this->logError(sprintf('Impossible to invalidate template cache from "%s" application context "%s" (%s: %s)', $targetApplication, get_class($e), $e->getMessage()));
             return;
         }
         $viewCacheManager = sfContext::getInstance()->getViewCacheManager();
     } else {
         $viewCacheManager = $this->viewCacheManager;
     }
     if (!$viewCacheManager instanceof sfViewCacheManager) {
         return;
     }
     $error = null;
     try {
         $viewCacheManager->remove($cacheUri, $hosts);
     } catch (Exception $e) {
         if (sfConfig::get('sf_logging_enabled')) {
             $error = sprintf('Problem during cache invalidation process for uri "%s": %s', $cacheUri, $e->getMessage());
         }
     }
     if ($switchRequired) {
         sfContext::switchTo($currentApplication);
     }
     if (!is_null($error)) {
         $this->logError($error);
     }
 }
Beispiel #10
0
function _app_url_for_internal_uri($application, $internal_uri, $absolute = false)
{
    // stores current states
    $current_application = sfContext::getInstance()->getConfiguration()->getApplication();
    $current_environment = sfContext::getInstance()->getConfiguration()->getEnvironment();
    $current_is_debug = sfContext::getInstance()->getConfiguration()->isDebug();
    $current_config = sfConfig::getAll();
    // computes a url
    if (sfContext::hasInstance($application)) {
        $context = sfContext::getInstance($application);
        sfContext::switchTo($application);
    } else {
        $config = ProjectConfiguration::getApplicationConfiguration($application, $current_environment, $current_is_debug);
        $context = sfContext::createInstance($config, $application);
    }
    $is_strip_script_name = (bool) sfConfig::get('sf_no_script_name');
    $result_url = $context->getController()->genUrl($internal_uri, $absolute);
    // restores the previous states
    sfContext::switchTo($current_application);
    sfConfig::add($current_config);
    // replaces a script name
    $before_script_name = basename(sfContext::getInstance()->getRequest()->getScriptName());
    $after_script_name = _create_script_name($application, $current_environment);
    if ($is_strip_script_name) {
        $before_script_name = '/' . $before_script_name;
        $after_script_name = '';
    }
    return str_replace($before_script_name, $after_script_name, $result_url);
}
<?php

$app = 'frontend';
include dirname(__FILE__) . '/../bootstrap/functional.php';
new sfDatabaseManager($configuration);
$task = new sfDoctrineBuildTask($configuration->getEventDispatcher(), new sfFormatter());
$task->run(array(), array('sql', 'db', 'and-load', 'no-confirmation', 'application' => $app));
$conn = Doctrine::getConnectionByTableName('Article');
$conn->beginTransaction();
$browser = new sfTestFunctional(new sfBrowser());
$browser->get('/en/articles')->with('request')->begin()->isParameter('module', 'content')->isParameter('action', 'index')->isParameter('sf_culture', 'en')->end()->with('response')->begin()->checkElement('h1', '/Articles/')->checkElement('ul li', 2)->checkElement('ul li', '/My first article/')->checkElement('ul li', '/My second article/', array('position' => 1))->end()->with('view_cache')->begin()->isCached(true, false)->end()->click('My first article')->with('response')->begin()->checkElement('h1', '/My first article/')->checkElement('ul#comments li', 2)->end()->with('view_cache')->begin()->isCached(true, false)->end();
$browser->test()->info('Updating first article with php code - cache invalidation is disabled on frontend');
$firstArticle = Doctrine::getTable('Article')->doSelectForSlug(array('slug' => 'my-first-article'));
$firstArticle->title = 'My first article, cache invalidation is disabled';
$firstArticle->save($conn);
$browser->get('/en/articles')->with('response')->begin()->checkElement('h1', '/Articles/')->checkElement('ul li', 2)->checkElement('ul li', '/My first article/')->checkElement('ul li:contains("cache invalidation is disabled")', false)->end()->with('view_cache')->begin()->isCached(true, false)->end();
$browser->test()->info('Updating first article with php code - switch to backend');
sfContext::switchTo('backend');
$firstArticle = Doctrine::getTable('Article')->doSelectForSlug(array('slug' => 'my-first-article'));
$firstArticle->title = 'My first article, modified';
$firstArticle->save($conn);
sfContext::switchTo('frontend');
$browser->get('/en/articles')->with('response')->begin()->checkElement('h1', '/Articles/')->checkElement('ul li', 2)->checkElement('ul li', '/My first article, modified/')->checkElement('ul li', '/My second article/', array('position' => 1))->end()->with('view_cache')->begin()->isCached(true, false)->end()->click('My first article, modified')->with('response')->begin()->checkElement('h1', '/My first article, modified/')->checkElement('ul#comments li', 2)->end()->with('view_cache')->begin()->isCached(true, false)->end();
$browser->test()->info('Updating first article from another app (with caching system disabled) - switch to backend');
sfContext::switchTo('backend');
$configuration->loadHelpers('Partial');
$backendBrowser = new sfTestFunctional(new sfBrowser());
$backendBrowser->get('/article/1/edit')->with('request')->begin()->isParameter('module', 'article')->isParameter('action', 'edit')->end()->setField('article[en][title]', 'My first article, edited')->click('Save')->followRedirect()->with('response')->begin()->checkElement('div.notice', 1)->end();
$browser->get('/en/article/' . $firstArticle->slug . '/view')->with('response')->begin()->checkElement('h1', '/My first article, edited/')->checkElement('ul#comments li', 2)->end()->with('view_cache')->begin()->isCached(true, false)->end();
$conn->rollback();
 protected function setUp()
 {
     $app = $this->getApplication();
     if (!sfContext::hasInstance($app)) {
         $configuration = ProjectConfiguration::getApplicationConfiguration($app, $this->getEnvironment(), $this->isDebug());
         sfContext::createInstance($configuration, $app);
         require_once $configuration->getSymfonyLibDir() . '/vendor/lime/lime.php';
     }
     if ($app != sfContext::getInstance()->getConfiguration()->getApplication()) {
         sfContext::switchTo($app);
     }
     $this->testBrowser = new sfTestFunctional(new sfBrowser(), $this->getTest());
     $this->_start();
 }
 protected function createContextInstance($application, $enviroment)
 {
     $configuration = ProjectConfiguration::getApplicationConfiguration($application, $enviroment, 'false');
     sfContext::createInstance($configuration);
     sfContext::switchTo($application);
 }
Beispiel #14
0
 /**
  * Génération brute de l'url cross app, passer par la fonction genUrl de préference (gestion du cache)
  *
  * @static
  * @throws Exception
  * @param string $app
  * @param string $url
  * @param bool $absolute
  * @param string $env
  * @param string $initialInstanceName for test purpose
  * @return mixed
  */
 public static function buildUrl($app, $url, $absolute = false, $env = null, $initialInstanceName = null)
 {
     $initialApp = sfConfig::get('sf_app');
     if ($initialInstanceName == null) {
         $initialInstanceName = $initialApp;
     }
     $initialScriptName = $_SERVER['SCRIPT_NAME'];
     $initialFrontController = basename($initialScriptName);
     $initialConfig = sfConfig::getAll();
     $debug = sfConfig::get('sf_debug');
     //environnement par défaut
     if ($env == null) {
         $env = $initialConfig['sf_environment'];
     }
     //création du contexte
     if (!sfContext::hasInstance($app)) {
         sfConfig::set('sf_factory_storage', 'sfNoStorage');
         // la config de base est restaurée à la fin de la fonction
         sfConfig::set('sf_use_database', false);
         $configuration = ProjectConfiguration::getApplicationConfiguration($app, $env, $debug);
         $context = sfContext::createInstance($configuration, $app);
         unset($configuration);
     } else {
         $context = sfContext::getInstance($app);
     }
     //détermination du front controller
     $finalFrontController = $app;
     if ($env != 'prod') {
         $finalFrontController .= '_' . $env;
     }
     $finalFrontController .= '.php';
     $crossUrl = $context->getController()->genUrl($url, $absolute);
     unset($context);
     //vérrification de l'existence du front controller
     if (!file_exists(sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . $finalFrontController)) {
         throw new Exception('Le front controller ' . $finalFrontController . ' est introuvable.');
     }
     $crossUrl = str_replace($initialFrontController, $finalFrontController, $crossUrl);
     //retour au context initial
     if ($app !== $initialInstanceName) {
         sfContext::switchTo($initialInstanceName);
         sfConfig::clear();
         sfConfig::add($initialConfig);
     }
     return $crossUrl;
 }
 /** Initialize the application context.
  *
  * @return void
  */
 private function _initContext()
 {
     if (empty($this->_application)) {
         $this->_application = self::getDefaultApplicationName();
     }
     if (!sfContext::hasInstance()) {
         sfContext::createInstance($this->getApplicationConfiguration());
     }
     sfContext::switchTo($this->_application);
 }