» YAML is a recursive acronym meaning "YAML Ain't Markup Language", and is intended as a "human friendly data serialization standard for all programming languages." It is often used for application configuration.
Zend_Config_Yaml is a lightweight
Zend_Config extension. It includes a parser capable of
recognizing most common YAML syntax used for purposes of configuration, and allows
specifying other parsers should you want more complex syntax (e.g., ext/syck, spyc,
The following is a YAML version of a standard application configuration.
production: phpSettings: display_startup_errors: false display_errors: false includePaths: library: APPLICATION_PATH/../library bootstrap: path: APPLICATION_PATH/Bootstrap.php class: "Bootstrap" appnamespace: "Application" resources: frontController: controllerDirectory: APPLICATION_PATH/controllers moduleDirectory: APPLICATION_PATH/modules params: displayExceptions: false modules: db: adapter: "pdo_sqlite" params: dbname: APPLICATION_PATH/../data/db/application.db layout: layoutPath: APPLICATION_PATH/layouts/scripts/ staging: _extends: production testing: _extends: production phpSettings: display_startup_errors: true display_errors: true development: _extends: production phpSettings: display_startup_errors: true display_errors: true resources: frontController: params: displayExceptions: true
To utilize it, you simply instantiate
it to the location of this file and indicating the section of the file to load. By
default, constant names found in values will be substituted with their appropriate
$config = new Zend_Config_Yaml( APPLICATION_PATH . '/configs/application.yaml', APPLICATION_ENV );
Once instantiated, you use it as you would any other configuration object.
$db = Zend_Db::factory($config->resources->db);
The following options may be passed as keys to the third, $options argument of the constructor.
The default behavior of
Zend_Config is to mark the
object as immutable once loaded. Passing this flag with a boolean
true will enable modifications to the object.
By default, any time a section extends another,
Zend_Config will merge the section with the section
it extends. Speciying a boolean true value to this
option will disable this feature, giving you only the configuration defined
explicitly in that section.
Zend_Config_Yaml will replace constant
names found in values with the defined constant value. You map pass a
boolean true to this option to disable this
Zend_Config_Yaml uses a built in decoder,
Zend_Config_Yaml::decode(), to parse and process
YAML files. You may specify an alternate callback to use in place of the
built-in one using this option.
Constructor. $yaml should refer to a valid filesystem location containing a YAML configuration file. $section, if specified, indicates a specific section of the configuration file to use. $options is discussed in the options section.
Parses a YAML string into a PHP array.
This static function may be used to globally override the default settings for how constants found in YAML strings are handled. By default, constant names are replaced with the appropriate constant values; passing a boolean true value to this method will override that behavior. (You can override it per-instance via the ignore_constants option as well.)
This static method gives you the current setting for the ignore_constants flag.
Beispiel #1 Using Zend_Config_Yaml with sfYaml
As noted in the options
Zend_Config_Yaml allows you to specify an
alternate YAML parser at instantiation.
» sfYaml is a » Symfony component that
implements a complete YAML parser in PHP, and includes a number of additional
features including the ability to parse PHP expressions embedded in the YAML. In
this example, we use the sfYaml::load() method as our YAML
decoder callback. (Note: this assumes that the
sfYaml class is either already loaded or available via
$config = new Zend_Config_Yaml( APPLICATION_PATH . '/configs/application.yaml', APPLICATION_ENV, array('yaml_decoder' => array('sfYaml', 'load')) );