Question

I have a strange problem with $_SESSION in my application

For different reasons that i don't explain here i had the necessity to set in the session the environment in my AppKernel.php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{

  protected $session;

  public function initializeContainer()
  {
    parent::initializeContainer();

    $this->session = $this->container->get('session');
    $this->session->set('isTestEnv', $this->getEnvironment() == 'test');
  }

In another class that i use to manage the request to some API i need now to get that param

namespace Bioversity\ServerConnectionBundle\Repository;

use Symfony\Component\HttpFoundation\Session\Session;
use Bioversity\ServerConnectionBundle\Repository\ServerResponseManager;
use Bioversity\ServerConnectionBundle\Repository\ServerResponseRequestQueryManager;

class ServerRequestManager
{

  protected $wrapper= "http://url/to/the/api.php";   

  public function __construct()
  {
    if($_SESSION['_sf2_attributes']['isTestEnv'] == 'test')
    {
        $this->wrapper= "http://url/to/the/api.test.php";
    }
  }

Everything works fine in the browser, but when i try to run the test i get a strange error

48) ServerConnectionBundle\Tests\Repository\TraitConnectionRepositoryTest::testGetTags
ErrorException: Notice: Undefined variable: _SESSION in ServerConnectionBundle/Repository/ServerRequestManager.php line 41

---------------EDIT------------

I had updated my code, now i have a services like this parameters: env: %kernel.environment%

services:
    bioversity_server_connection:
        class: Bioversity\ServerConnectionBundle\Repository\ServerRequestManager
        arguments: [%env%]

and in my class i added

class ServerRequestManager
{    
  public function __construct($env)
  {
    print_r($env);

    //if(array_key_exists('isTestEnv', $_SESSION['_sf2_attributes']))
    //{
        //if($_SESSION['_sf2_attributes']['isTestEnv'] == 'test'){
        if($env == 'test')
        {
            $this->wrapper= "http://temp.wrapper.grinfo.net/TIP/Wrapper.test.php";
            $this->setDatabaseOntology('TEST-'.$this->getDatabaseOntology());
            $this->setDatabasePGRSecure('TEST-'.$this->getDatabasePGRSecure());
            $this->setDatabaseUsers('TEST-'.$this->getDatabaseUsers());
        }
    //}
  }

I had imported the services.yml in my config.yml but I only have this error

Warning: Missing argument 1 for Bioversity\ServerConnectionBundle\Repository\ServerRequestManager::__construct(), called in /home/aczepod/Sites/Bioversity/src/Bioversity/SecurityBundle/Repository/ServerConnection.php

What is wrong now!?

Was it helpful?

Solution

Woha, what the heck are you doing. Symfony has the parameter kernel.environment which refers to the current environment! So simple inject %kernel.environment% as argument into your service.

Btw. with PHP-CLI (running tests) there is no $_SESSION at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top