Code snippets for symfony 1.x

Navigation

Generate admin generator module cache from anywhere

Sometimes you'd like to use partial from auto-generated module, like list partial, to keep list presentation unified.

But, before using partial, you add to be sure that cache has been generated or your application will be broken.

This little method (you can place in your tools.class.php) check that cache exists, and if not, generate it.

public static function generateAdminGeneratorModuleCache($module, $configuration)
{
  if(!($configuration instanceof sfApplicationConfiguration)) {
    throw new sfConfigurationException('You must provide a valid application configuration object');
  }
  if(!file_exists(sfConfig::get('sf_app_module_dir').DIRECTORY_SEPARATOR.$module)) {
    throw new sfConfigurationException('The module specified does not exists for this application');
  }
  if(!file_exists(sfConfig::get('sf_app_module_dir').DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'generator.yml')) {
    throw new sfConfigurationException('This module does not seems to be a valid admin generator module');
  }
  if(!file_exists(sfConfig::get('sf_module_cache_dir').DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'actions'.DIRECTORY_SEPARATOR.'actions.class.php')) {
    // Init generator manager
    $generator_manager = new sfGeneratorManager($configuration);
    $config = sfYaml::load(sfConfig::get('sf_app_module_dir').DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'generator.yml');
    $config['generator']['param']['moduleName'] = $module;
    $generator_manager->generate($config['generator']['class'], $config['generator']['param']);
  }
}
 

Now you can call it from any actions with

myTools::generateAdminGeneratorModuleCache('my_module',$this->getContext()->getConfiguration());
 
by netounet on 2011-03-15, tagged cache  generator