Skip to content

Commit 57b35c7

Browse files
committed
Installation and usage instructions
1 parent e805f89 commit 57b35c7

File tree

2 files changed

+141
-138
lines changed

2 files changed

+141
-138
lines changed

README.md

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,87 @@
1-
# php-hcl-parser
2-
PHP HCL Parser
1+
# PHP HCL Parser
2+
3+
HCL is a configuration language make by HashiCorp. HCL files are used by several HashiCorp products,
4+
including Terraform.
5+
6+
This library parses HCL configuration files into PHP objects.
7+
8+
## Installation
9+
10+
You can install the PHP HCL Parser library using Composer. Just run the following command
11+
from the root of your project.
12+
13+
```
14+
composer require divineomega/php-hcl-parser
15+
```
16+
17+
## Usage
18+
19+
To parse HCL into a PHP object, create a new `HCLParser` object passing a string of HCL configuration
20+
as the first parameter of the construction, then call the `parse` method. See the example below.
21+
22+
```php
23+
$hcl = '
24+
# Specify the provider and access details
25+
provider "aws" {
26+
access_key = "${var.access_key}"
27+
secret_key = "${var.secret_key}"
28+
region = "${var.region}"
29+
}
30+
31+
# Create an instance
32+
resource "aws_instance" "example" {
33+
ami = "ami-2757f631"
34+
instance_type = "t2.micro"
35+
}
36+
37+
';
38+
39+
$configObject = (new HCLParser($hcl))->parse();
40+
```
41+
42+
The resulting object will be returned in the following format.
43+
44+
```php
45+
object(stdClass)#5 (2) {
46+
["provider"]=>
47+
array(1) {
48+
[0]=>
49+
object(stdClass)#4 (1) {
50+
["aws"]=>
51+
array(1) {
52+
[0]=>
53+
object(stdClass)#2 (3) {
54+
["access_key"]=>
55+
string(17) "${var.access_key}"
56+
["region"]=>
57+
string(13) "${var.region}"
58+
["secret_key"]=>
59+
string(17) "${var.secret_key}"
60+
}
61+
}
62+
}
63+
}
64+
["resource"]=>
65+
array(1) {
66+
[0]=>
67+
object(stdClass)#8 (1) {
68+
["aws_instance"]=>
69+
array(1) {
70+
[0]=>
71+
object(stdClass)#7 (1) {
72+
["example"]=>
73+
array(1) {
74+
[0]=>
75+
object(stdClass)#6 (2) {
76+
["ami"]=>
77+
string(12) "ami-2757f631"
78+
["instance_type"]=>
79+
string(8) "t2.micro"
80+
}
81+
}
82+
}
83+
}
84+
}
85+
}
86+
}
87+
```

test.php

Lines changed: 54 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -7,148 +7,66 @@
77
$hcl = '
88
# Specify the provider and access details
99
provider "aws" {
10-
region = "${var.aws_region}"
10+
access_key = "${var.access_key}"
11+
secret_key = "${var.secret_key}"
12+
region = "${var.region}"
1113
}
1214
13-
# Create a VPC to launch our instances into
14-
resource "aws_vpc" "default" {
15-
cidr_block = "10.0.0.0/16"
16-
}
17-
18-
# Create an internet gateway to give our subnet access to the outside world
19-
resource "aws_internet_gateway" "default" {
20-
vpc_id = "${aws_vpc.default.id}"
21-
}
22-
23-
# Grant the VPC internet access on its main route table
24-
resource "aws_route" "internet_access" {
25-
route_table_id = "${aws_vpc.default.main_route_table_id}"
26-
destination_cidr_block = "0.0.0.0/0"
27-
gateway_id = "${aws_internet_gateway.default.id}"
28-
}
29-
30-
# Create a subnet to launch our instances into
31-
resource "aws_subnet" "default" {
32-
vpc_id = "${aws_vpc.default.id}"
33-
cidr_block = "10.0.1.0/24"
34-
map_public_ip_on_launch = true
35-
}
36-
37-
# A security group for the ELB so it is accessible via the web
38-
resource "aws_security_group" "elb" {
39-
name = "terraform_example_elb"
40-
description = "Used in the terraform"
41-
vpc_id = "${aws_vpc.default.id}"
42-
43-
# HTTP access from anywhere
44-
ingress {
45-
from_port = 80
46-
to_port = 80
47-
protocol = "tcp"
48-
cidr_blocks = ["0.0.0.0/0"]
49-
}
50-
51-
# outbound internet access
52-
egress {
53-
from_port = 0
54-
to_port = 0
55-
protocol = "-1"
56-
cidr_blocks = ["0.0.0.0/0"]
57-
}
58-
}
59-
60-
# Our default security group to access
61-
# the instances over SSH and HTTP
62-
resource "aws_security_group" "default" {
63-
name = "terraform_example"
64-
description = "Used in the terraform"
65-
vpc_id = "${aws_vpc.default.id}"
66-
67-
# SSH access from anywhere
68-
ingress {
69-
from_port = 22
70-
to_port = 22
71-
protocol = "tcp"
72-
cidr_blocks = ["0.0.0.0/0"]
73-
}
74-
75-
# HTTP access from the VPC
76-
ingress {
77-
from_port = 80
78-
to_port = 80
79-
protocol = "tcp"
80-
cidr_blocks = ["10.0.0.0/16"]
81-
}
82-
83-
# outbound internet access
84-
egress {
85-
from_port = 0
86-
to_port = 0
87-
protocol = "-1"
88-
cidr_blocks = ["0.0.0.0/0"]
89-
}
15+
# Create an instance
16+
resource "aws_instance" "example" {
17+
ami = "ami-2757f631"
18+
instance_type = "t2.micro"
9019
}
9120
92-
resource "aws_elb" "web" {
93-
name = "terraform-example-elb"
94-
95-
subnets = ["${aws_subnet.default.id}"]
96-
security_groups = ["${aws_security_group.elb.id}"]
97-
instances = ["${aws_instance.web.id}"]
21+
';
9822

99-
listener {
100-
instance_port = 80
101-
instance_protocol = "http"
102-
lb_port = 80
103-
lb_protocol = "http"
23+
$configObject = (new HCLParser($hcl))->parse();
24+
25+
var_dump($configObject);
26+
27+
/*
28+
29+
object(stdClass)#5 (2) {
30+
["provider"]=>
31+
array(1) {
32+
[0]=>
33+
object(stdClass)#4 (1) {
34+
["aws"]=>
35+
array(1) {
36+
[0]=>
37+
object(stdClass)#2 (3) {
38+
["access_key"]=>
39+
string(17) "${var.access_key}"
40+
["region"]=>
41+
string(13) "${var.region}"
42+
["secret_key"]=>
43+
string(17) "${var.secret_key}"
44+
}
45+
}
46+
}
10447
}
105-
}
106-
107-
resource "aws_key_pair" "auth" {
108-
key_name = "${var.key_name}"
109-
public_key = "${file(var.public_key_path)}"
110-
}
111-
112-
resource "aws_instance" "web" {
113-
# The connection block tells our provisioner how to
114-
# communicate with the resource (instance)
115-
connection {
116-
# The default username for our AMI
117-
user = "ubuntu"
118-
119-
# The connection will use the local SSH agent for authentication.
120-
}
121-
122-
instance_type = "t2.micro"
123-
124-
# Lookup the correct AMI based on the region
125-
# we specified
126-
ami = "${lookup(var.aws_amis, var.aws_region)}"
127-
128-
# The name of our SSH keypair we created above.
129-
key_name = "${aws_key_pair.auth.id}"
130-
131-
# Our Security group to allow HTTP and SSH access
132-
vpc_security_group_ids = ["${aws_security_group.default.id}"]
133-
134-
# We\'re going to launch into the same subnet as our ELB. In a production
135-
# environment it\'s more common to have a separate private subnet for
136-
# backend instances.
137-
subnet_id = "${aws_subnet.default.id}"
138-
139-
# We run a remote provisioner on the instance after creating it.
140-
# In this case, we just install nginx and start it. By default,
141-
# this should be on port 80
142-
provisioner "remote-exec" {
143-
inline = [
144-
"sudo apt-get -y update",
145-
"sudo apt-get -y install nginx",
146-
"sudo service nginx start",
147-
]
48+
["resource"]=>
49+
array(1) {
50+
[0]=>
51+
object(stdClass)#8 (1) {
52+
["aws_instance"]=>
53+
array(1) {
54+
[0]=>
55+
object(stdClass)#7 (1) {
56+
["example"]=>
57+
array(1) {
58+
[0]=>
59+
object(stdClass)#6 (2) {
60+
["ami"]=>
61+
string(12) "ami-2757f631"
62+
["instance_type"]=>
63+
string(8) "t2.micro"
64+
}
65+
}
66+
}
67+
}
68+
}
14869
}
14970
}
150-
';
151-
152-
$object = (new HCLParser($hcl))->parse();
15371
154-
var_dump($object);
72+
*/

0 commit comments

Comments
 (0)