3

I want to recompile container from controller when I use $this->container->compile();

public function changeAction(Request $request)
{
    //......
    echo($this->container->getParameter('mailer_user')."\n");
    /*$cmd='php ../app/console cache:clear';
    $process=new Process($cmd);
    $process->run(function ($type, $buffer) {
        if ('err' === $type) {
            echo 'ERR > '.$buffer;
        }
        else {
            echo 'OUT > '.$buffer;
        }
    });*/

    $this->container->compile();
    echo($this->container->getParameter('mailer_user')."\n");
    die();
}

I got an error : You cannot compile a dumped frozen container

I want to know if when I clear the cache from controller the container will recompile?

Tim Ogilvy
  • 1,923
  • 1
  • 24
  • 36
ghaziksibi
  • 471
  • 3
  • 12

2 Answers2

2

If you are trying to get values of parameters that have been modified during request, you can do this:

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

public function changeAction(Request $request)
{
    $originalParam = $this->container->getParameter('mailer_user');

    // Rebuild the container
    $container = new ContainerBuilder();    
    $fileLocator = new FileLocator($this->getParameter('kernel.root_dir').'/config');

    // Load the changed config file(s)
    $loader = new PhpFileLoader($container, $fileLocator);
    $loader->setResolver(new LoaderResolver([$loader]));
    $loader->load('parameters.php'); // The file that loads your parameters

    // Get the changed parameter value
    $changedParam = $container->get('mailer_user');

    // Or reset the whole container
    $this->container = $container;
}

Also, if you need to clear the cache from a controller, there is a cleaner way:

$kernel = $this->get('kernel');
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$application->setAutoExit(false);

$application->run(new \Symfony\Component\Console\Input\ArrayInput(
    ['command' => 'cache:clear']
));
chalasr
  • 12,971
  • 4
  • 40
  • 82
  • my parameters are stored in database – ghaziksibi Apr 06 '16 at 14:12
  • And so ? Do you need that I adapt the example for a `parameters.php` file ? – chalasr Apr 06 '16 at 14:14
  • I read here that when the cache is cleared the container is rebuild https://knpuniversity.com/screencast/symfony-journey-di/symfony-builds-the-container – ghaziksibi Apr 06 '16 at 14:27
  • if you can me give me en example for a parameters.php why not ^^ – ghaziksibi Apr 06 '16 at 14:28
  • Of course if you clear the cache the container is rebuilt, but I suspect that if the parameter has changed during request, even if you clear the cache, you'll get the old value (not the one of the new container build, because the container instance is still the same in your controller). – chalasr Apr 06 '16 at 14:30
  • I just try to give you exactly what you need, I'll adapt my answer to your use case, I tell you when it's done. – chalasr Apr 06 '16 at 14:32
  • thank you chalasr the seconde $this->container->getParameter('mailer_user'); it just for testing if my parameter has changed or not. i don't need it. i have trying with another controller and it work perfectly, this is what I want already – ghaziksibi Apr 06 '16 at 15:44
  • Glad to help @AhmedGhaziKsibi , don't forget to accept the answer :) – chalasr Apr 06 '16 at 15:46
  • it is done :) juste there is difference with that ? $cmd='php ../app/console cache:clear'; $process=new Process($cmd); $process->run(function ($type, $buffer) { if ('err' === $type) { echo 'ERR > '.$buffer; } else { echo 'OUT > '.$buffer; } }); – ghaziksibi Apr 06 '16 at 16:19
  • The result will be the same, only the way is different, maybe more adapted to be used from browser because you can use different kind of Input/Output and some other stuffs. I don't know why but I believed that you are using using an `exec('php app/console cache:clear')`, not the Process component which is also perfectly adapted. – chalasr Apr 06 '16 at 16:26
0

In short the answer is no, the container will not be recompiled, because it is already loaded into memory, and deleting files from disk will take no effect on current request. And on the next request cache will be warmed up and container will be compiled before you reach the controller.

Andrii Mishchenko
  • 2,626
  • 21
  • 19