大多数的symfony配置文件使用YAML格式。YAML 官方网站称 YAML 是“一种所有编程语言可用的友好的数据序列化标准”。 YAML 是一种轻量化的数据描述语言。类似于 PHP, YAML一样具有轻量化的语法与数据类型,如字符串、布尔型、浮点型或整数型等。但不同于PHP的是,它区分数组(序列)和散列表(哈希表) 。 尽管 YAML 格式能够描述更为复杂的嵌套的数据结构,本节仅描述你在 symfony 中使用 YAML 作为配置文件所需要了解的 YAML 最小功能集。 基础类型YAML 基础类型语法与PHP中的相似。 字符串A string in YAML'A singled-quoted string in YAML'"A double-quoted string in YAML\n" 当一个字符串前后包含空格时,必须使用引号标注的格式。
当一个字符串包含换行符,您可以通过使用 \/ /| |\/| | / / | | | |__
或者,字符串可以通过 This is a very long sentence that spans several lines in the YAML but which will be rendered as a string without carriage returns. 数字 12 # 整数 014 # 八进制整数
0xC #十六进制整数
13.4 #浮点数 1.2e+34 #指数 .inf空值 #无穷大 在YAML中,空值可以用 在YAML中,布尔值用 日期 YAML 使用 ISO-8601 标准表示日期: 2001-12-14t21:59:43.10-05:00#简单日期示例2002-12-14集合 YAML文件很少仅用来描述一个简单的基础类型。大多数时候,它被用来描述一个集合。一个集合可以是一个序列(数组)或散列表(哈希表)。序列和散 列表都会被转换成为 PHP 的数组。
使用一个跟着空格的“ - Perl - Python 这相当于下面的PHP代码: array('PHP', 'Perl', 'Python');使用一个跟着空格的冒号标记每一个键/值对来声明一个散列表: PHP: 5.2MySQL: 5.1 Apache: 2.2.20 这相当于下面的PHP代码: array('PHP' => 5.2, 'MySQL' => 5.1, 'Apache' => '2.2.20');
一个散列项的冒号和值之间必须至少有一个或者更多个空格,而具体的空格数目并不重要: PHP: 5.2MySQL: 5.1 Apache: 2.2.20 YAML使用一个或多个空格的缩进来描述嵌套集合: "symfony 1.0":PHP: 5.0 Propel: 1.2 "symfony 1.2": PHP: 5.2 Propel: 1.3 这段 YAML 代码相当于下面的PHP代码: array('symfony 1.0' => array( 'PHP' => 5.0, 'Propel' => 1.2, ), 'symfony 1.2' => array( 'PHP' => 5.2, 'Propel' => 1.3, ), ); 在YAML文件中使用缩进时必须牢记的一件事情是: 必须使用一个或多个空格进行缩进,而不能使用制表符 (Tab键) 。 你可以像这样混合使用嵌套的序列和散列表: 'Chapter 1':- Introduction - Event Types 'Chapter 2': - Introduction - Helpers YAML也可以使用流风格的集合,使用明确的标示符而不是缩进进行集合范围的界定。 一个序列可以写成方括号( 一个散列表可以写成大括号( 你也可以混合两种风格,以实现更好的可读性: 'Chapter 1': [Introduction, Event Types]'Chapter 2': [Introduction, Helpers]"symfony 1.0": { PHP: 5.0, Propel: 1.2 } "symfony 1.2": { PHP: 5.2, Propel: 1.3 }注释 YAML 中添加注释的方式是通过井字符(#): #行末评论"symfony 1.0": { PHP: 5.0, Propel: 1.2 } # Comment at the end of a line "symfony 1.2": { PHP: 5.2, Propel: 1.3 } 动态 YAML 文件 在 symfony 中,一个 YAML 文件可以包含 PHP 代码,这部分代码将在 YAML 文件解析前被执行: 1.0:version: <?php echo file_get_contents('1.0/VERSION')."\n" ?> 1.1: version: "<?php echo file_get_contents('1.1/VERSION') ?>" 注意保持文件的正确缩进格式。当在 YAML 中嵌入 PHP 代码时,应记住以下几点:
下面的例子使用了本章中提到的 YAML 语法: "symfony 1.0":end_of_maintainance: 2010-01-01 is_stable: true release_manager: "Gregoire Hubert" description: > This stable version is the right choice for projects that need to be maintained for a long period of time. latest_beta: ~ latest_minor: 1.0.20 supported_orms: [Propel] archives: { source: [zip, tgz], sandbox: [zip, tgz] } "symfony 1.2": end_of_maintainance: 2008-11-01 is_stable: true release_manager: 'Fabian Lange' description: > This stable version is the right choice if you start a new project today. latest_beta: null latest_minor: 1.2.5 supported_orms: - Propel - Doctrine archives: source: - zip - tgz sandbox: - zip - tgz |
|