Development

Changeset 3703

You must first sign up to be able to contribute.

Changeset 3703

Show
Ignore:
Timestamp:
04/03/07 08:23:18 (2 years ago)
Author:
fabien
Message:

added a way to register custom config handlers (closes #1624)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • doc/trunk/book/19-Mastering-Symfony-s-Configuration-Files.txt

    r3568 r3703  
    514514>The `class` must either be autoloaded (that's the case here) or defined in the file whose path is written in a `file` parameter under the `param` key. 
    515515 
     516As with many other symfony configuration files, you can also register a configuration handler directly in your PHP code: 
     517 
     518    sfConfigCache::getInstance()->registerConfigHandler('config/map.yml', myMapConfigHandler, array()); 
     519 
    516520When you need the code based on the `map.yml` file and generated by the `myMapConfigHandler` handler in your application, call the following line: 
    517521 
  • trunk/lib/config/sfConfigCache.class.php

    r3525 r3703  
    2424{ 
    2525  protected 
    26     $handlers = array(); 
     26    $handlers = array(), 
     27    $userHandlers = array(); 
    2728 
    2829  protected static 
     
    5960      // we need to load the handlers first 
    6061      $this->loadConfigHandlers(); 
     62    } 
     63 
     64    if (count($this->userHandlers) != 0) 
     65    { 
     66      // we load user defined handlers 
     67      $this->mergeUserConfigHandlers(); 
    6168    } 
    6269 
     
    319326    { 
    320327      // module directory doesn't exist or isn't readable 
    321       $error = sprintf('Module directory "%s" does not exist or is not readable', 
    322                        sfConfig::get('sf_app_module_dir')); 
     328      $error = sprintf('Module directory "%s" does not exist or is not readable', sfConfig::get('sf_app_module_dir')); 
    323329      throw new sfConfigurationException($error); 
    324330    } 
     
    340346    $fileCache->set(basename($cache), '', $data); 
    341347  } 
     348 
     349  /** 
     350   * Registers a configuration handler. 
     351   * 
     352   * @param string The handler to use when parsing a configuration file 
     353   * @param class  A configuration handler class 
     354   * @param string An array of options for the handler class initialization 
     355   */ 
     356  public function registerConfigHandler($handler, $class, $params = array()) 
     357  { 
     358    $this->userHandlers[$handler] = new $class(); 
     359    $this->userHandlers[$handler]->initialize($params); 
     360  } 
     361 
     362  /** 
     363   * Merges configuration handlers from the config_handlers.yml   
     364   * and the ones defined with registerConfigHandler() 
     365   * 
     366   */ 
     367  protected function mergeUserConfigHandlers() 
     368  { 
     369    // user defined configuration handlers 
     370    $this->handlers = array_merge($this->handlers, $this->userHandlers); 
     371 
     372    $this->userHandlers = array(); 
     373  } 
    342374}