Symfony "silent" clear cache

While I was preparing a little more complex CGI task for symfony I found one limitation. You can just enable or disable log messages for all currently running tasks.

Basically, if you run a task you can set whether you want to see log messages or not by setting -q option. But this setting is used also when you call another task inside a task.

Silent "sfCacheClearTask"

So, if you create your own task that logs only important messages you want to see and you don't want to be confused by tens of other file system outputs from other tasks you probably have to (or this is the only way I know) extend the original sfCacheClearTask:

 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
class silentClearCacheTask extends sfCacheClearTask {
    protected function configure() {
        parent::configure();
        $this->aliases    = array(); // remove the 'cc' alias
        $this->namespace  = 'og3';
        $this->name       = 'cc';
        $this->briefDescription    = 'Silent clear cache task';
        $this->detailedDescription = 'See [symfony help cc|INFO].';

        // turn on "silent" mode
        $this->filesystem = new sfFilesystem(null, $this->formatter);
    }
}

The whole magic is on this line:

 16 
$this->filesystem = new sfFilesystem(null, $this->formatter);

The sfFilesystem class constructor takes as the first argument sfEventDispatcher. That's the class which is responsible for printing log messages to the console. If you set it to null whole task won't log any file system messages. The only output comes from the sfCacheClearTask itself when it says what is it going to do next:

>> cache     Clearing cache type "all" for "frontend" app and "prod" env
>> cache     Clearing cache type "all" for "backend" app and "prod" env
>> cache     Clearing cache type "all" for "backend" app and "dev" env

Unfortunately, this is not the 100% solution. If you want to use the same 'trick' with generate:app it'll seem like it doesn't work. In fact it does work but only for the generate:app. If you have a look into the source code you'll find out that at the end of execute method it calls task sfProjectPermissionsTask:

$fixPerms = new sfProjectPermissionsTask($this->dispatcher, $this->formatter);

If you try to set $this->dispatcher = null in the sfGenerateAppTask constructor, you'll disable completely all log messages and that's not what you were probably looking for...

blog comments powered by Disqus