Wprowadzenie do Zend Framework

     Nauka Zend Framework


     Przewodnik po Zend Framework

  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Translation 21.3% Update 2011-11-16 - Revision 24356 - Version ZF 1.11.x

    Rozdział 20. Zend_Config_Writer

    20.1. Zend_Config_Writer

    Zend_Config_Writer gives you the ability to write config files out of Zend_Config objects. It works with an adapter-less system and thus is very easy to use. By default Zend_Config_Writer ships with four adapters, which are all file-based. You instantiate a writer with specific options, which can be filename and config. Then you call the write() method of the writer and the config file is created. You can also give $filename and $config directly to the write() method. Currently the following writers are shipped with Zend_Config_Writer:

    • Zend_Config_Writer_Array

    • Zend_Config_Writer_Ini

    • Zend_Config_Writer_Json

    • Zend_Config_Writer_Xml

    • Zend_Config_Writer_Yaml

    When modifying or creating a Zend_Config object, there are some things to know. To create or modify a value, you simply say set the parameter of the Zend_Config object via the parameter accessor (->). To create a section in the root or to create a branch, you just create a new array ("$config->branch = array();"). To define which section extends another one, you call the setExtend() method on the root Zend_Config object.

    Przykład 20.1. Using Zend_Config_Writer

    This example illustrates the basic use of Zend_Config_Writer_Xml to create a new config file:

    // Create the config object
    $config = new Zend_Config(array(), true);
    $config->production = array();
    $config->staging    = array();


    $config->production->db = array();
    $config->production->db->hostname 'localhost';
    $config->production->db->username 'production';

    $config->staging->db = array();
    $config->staging->db->username 'staging';

    // Write the config file in one of the following ways:
    // a)
    $writer = new Zend_Config_Writer_Xml(array('config'   => $config,
    'filename' => 'config.xml'));

    // b)
    $writer = new Zend_Config_Writer_Xml();

    // c)
    $writer = new Zend_Config_Writer_Xml();

    This will create an XML config file with the sections production and staging, where staging extends production.

    Przykład 20.2. Modifying an Existing Config

    This example demonstrates how to edit an existing config file.

    // Load all sections from an existing config file, while skipping the extends.
    $config = new Zend_Config_Ini('config.ini',
    'skipExtends'        => true,
    'allowModifications' => true));

    // Modify a value
    $config->production->hostname 'foobar';

    // Write the config file
    $writer = new Zend_Config_Writer_Ini(array('config'   => $config,
    'filename' => 'config.ini'));

    [Notatka] Loading a Config File

    When loading an existing config file for modifications it is very important to load all sections and to skip the extends, so that no values are merged. This is done by giving the skipExtends as option to the constructor.

    For all the File-Based writers (INI, JSON, XML, YAML, and PHP Array) internally the render() is used to build the configuration string. This method can be used independently to access the string-representation of the configuration data.

    20.1.1. Notes specific to the INI writer

    • The INI writer has two modes for rendering with regard to sections. By default the top-level configuration is always written into section names. By calling $writer->setRenderWithoutSections(); all options are written into the global namespace of the INI file and no sections are applied.

    • Zend_Config_Writer_Ini has an additional option parameter nestSeparator, which defines with which character the single nodes are separated. The default is a single dot, which is accepted by Zend_Config_Ini by default.

    20.1.2. Notes specific to the YAML writer

    The YAML writer lets you optionally specify an alternate YAML encoder to use. By default, one is shipped with the framework that is suitable for most configuration tasks. If you find it insufficient, or wish to use more advanced YAML, you may provide an alternate encoder callback.

    The method for doing so is to use the Zend_Config_Writer_Yaml::setYamlEncoder() method, passing it a valid callback.

    // Use the Symfony Yaml Component:
    $writer = new Zend_Config_Writer_Yaml($filename);

    The above uses the Symfony Components' sfYaml component in order to encode the configuration to YAML.

    digg delicious meneame google twitter technorati facebook