divineomega / php-hcl-parser

PHP HCL 解析器

v1.2.0 2018-07-04 17:56 UTC

This package is auto-updated.

Last update: 2024-09-06 10:13:35 UTC


README

Build Status Coverage Status StyleCI

HCL 是 HashiCorp 创建的一种配置语言。HCL 文件被 HashiCorp 的多个产品使用,包括 Terraform。

此库将 HCL 配置文件解析为 PHP 对象。

安装

您可以使用 Composer 安装 PHP HCL 解析器库。只需从您项目的根目录运行以下命令。

composer require divineomega/php-hcl-parser

使用方法

要将 HCL 解析为 PHP 对象,创建一个新的 HCLParser 对象,将其 HCL(作为字符串)传递给它,然后调用 parse 方法。请参阅下面的示例。

$hcl = file_get_contents('example.tf');
$configObject = (new HCLParser($hcl))->parse();

结果对象将类似于以下内容。

object(stdClass)#5 (2) {
  ["provider"]=>
  array(1) {
    [0]=>
    object(stdClass)#4 (1) {
      ["aws"]=>
      array(1) {
        [0]=>
        object(stdClass)#2 (3) {
          ["access_key"]=>
          string(17) "${var.access_key}"
          ["region"]=>
          string(13) "${var.region}"
          ["secret_key"]=>
          string(17) "${var.secret_key}"
        }
      }
    }
  }
  ["resource"]=>
  array(1) {
    [0]=>
    object(stdClass)#8 (1) {
      ["aws_instance"]=>
      array(1) {
        [0]=>
        object(stdClass)#7 (1) {
          ["example"]=>
          array(1) {
            [0]=>
            object(stdClass)#6 (2) {
              ["ami"]=>
              string(12) "ami-2757f631"
              ["instance_type"]=>
              string(8) "t2.micro"
            }
          }
        }
      }
    }
  }
}