Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "config"

Custom Filebased Configuration Class

This class can be used to store some simple info(number of first page posts,etc.) in a yml file. Usage is like sfConfig class.

<?php
  <?php
 
 
class mz_config 
{
  protected static $notLoaded = true;
  public static $autosave = true;
  protected static
    $config = array();
 
  /**
   * Retrieves a config parameter.
   *
   * @param string $name    A config parameter name
   * @param mixed  $default A default config parameter value
   *
   * @return mixed A config parameter value, if the config parameter exists, otherwise null
   */
  public static function get($name, $default = null)
  {
    if(self::$notLoaded)self::loadConfig();
    return isset(self::$config[$name]) ? self::$config[$name] : $default;
  }
 
  /**
   * Indicates whether or not a config parameter exists.
   *
   * @param string $name A config parameter name
   *
   * @return bool true, if the config parameter exists, otherwise false
   */
  public static function has($name)
  {
    return array_key_exists($name, self::$config);
  }
 
  /**
   * Sets a config parameter.
   *
   * If a config parameter with the name already exists the value will be overridden.
   *
   * @param string $name  A config parameter name
   * @param mixed  $value A config parameter value
   */
  public static function set($name, $value)
  {
 
    self::$config[$name] = $value;
    if(self::$autosave)self::saveConfig();
  }
 
  /**
   * Sets an array of config parameters.
   *
   * If an existing config parameter name matches any of the keys in the supplied
   * array, the associated value will be overridden.
   *
   * @param array $parameters An associative array of config parameters and their associated values
   */
  public static function add($parameters = array())
  {
    self::$config = array_merge(self::$config, $parameters);
    if(self::$autosave)self::saveConfig();
  }
 
  /**
   * Retrieves all configuration parameters.
   *
   * @return array An associative array of configuration parameters.
   */
  public static function getAll()
  {
    if(self::$notLoaded)self::loadConfig();
    return self::$config;
  }
 
  /**
   * Clears all current config parameters.
   */
  public static function clear()
  {
    self::$config = array();
    if(self::$autosave)self::saveConfig();
  }
  public static function saveConfig($filename = null)
  {
    if($filename == null)$filename = sfConfig::get('sf_app');
    $path = sfConfig::get('sf_data_dir');
    $dump_file = "{$path}/{$filename}.yml";
    $yml = sfYaml::dump(self::getAll());
    file_put_contents($dump_file,$yml);
  }
  public static function loadConfig($filename = null)
  {
    $notLoaded = false;
    if($filename == null)$filename = sfConfig::get('sf_app');
    $path = sfConfig::get('sf_data_dir');
    $dump_file = "{$path}/{$filename}.yml";
    $yml = file_get_contents($dump_file);
    $parser = new sfYamlParser();
    $data = $parser->parse($yml);
 
    self::$config = array_merge(self::$config, $data);
  }
}
 
by Mojtaba Faez on 2010-05-27, tagged config 
(1 comment)

Access control to environment and applications and custom global config file for an environment ?

With this snippet we can implement the following functionalities from the global configuration file config/config.php:

I developed a mini-library (config/config.lib.php) to include and use in the global config file config/config.php.

<?php
/**
 * IP access control to an environment
 *
 * @param string $env environment name
 * @param array $ips IPs that has access
 * @param string $clientip client IP
 */
function envCorrecte ($env,$ips,$clientip)
{
    if (SF_ENVIRONMENT==$env)
    {
        $acces=0;
        foreach($ips as $ip)
        {
            $ipclient=substr($clientip,0,strlen($ip));
            if ($ipclient==$ip)
            {
                $acces=1;
            }
        }
        if ($acces==0)
        {
            echo "Keep away! this is not a public environment.";
            exit;
        }
    }
}
/**
 * IP access control to an application of the symfony project
 *
 * @param array $apps no public applications
 * @param array $ips IPs that has access
 * @param string $clientip client IP
 */
function appCorrecte ($apps,$ips,$clientip)
{
    // solve problem with key '0' in array
        $apps_aux[0]='';
    $apps = $apps_aux + $apps;
    //
    if (array_search(SF_APP,$apps)!=false)
    {
        $acces=0;
        foreach($ips as $ip)
        {
            $ADR=$HTTP_SERVER_VARS['REMOTE_ADDR'];
            $ipclient=substr($clientip,0,strlen($ip));
            if ($ipclient==$ip)
            {
                $acces=1;
            }
        }   
        if ($acces==0)
        {
            echo "Keep away! ".SF_APP." is not a public application.";
            exit;
        }
    }
 
}
?>

Finally config/config.php file can be like this:

<?php
include("config.lib.php");
 
/**
 * IP access control to 'env' and 'int' environments
 */
$ip_dev=array("10.138.0.","192.168.");
$ip_int=array("10.138.0.","192.168.");
envCorrecte('env',$ip_env,$HTTP_SERVER_VARS['REMOTE_ADDR']);
envCorrecte('int',$ip_int,$HTTP_SERVER_VARS['REMOTE_ADDR']);
 
/**
 * IP access control to backend applications
 */
$ip_apps=array("10.138.0.","192.168.","w.x.y.z");
$apps=array('myapp_1','myapp_2','myapp_n');
appCorrecte($apps,$ip_apps,$HTTP_SERVER_VARS['REMOTE_ADDR']);
 
/**
 * Custom config.php file for each environment
 */
include("config.".SF_ENVIRONMENT.".php");
 
/**
 * Common config.php file for all environments
 */
define("example","content1");
 
?>
by Oriol Rius on 2006-08-11, tagged access  administration  backend  config  environment  security