Skip to content

Commit 8e30cab

Browse files
author
Jordan Hall
committed
Initial commit
0 parents commit 8e30cab

File tree

6 files changed

+249
-0
lines changed

6 files changed

+249
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
composer.lock
3+
/bin/*
4+
!/bin/.gitkeep

bin/.gitkeep

Whitespace-only changes.

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "divineomega/php-hcl-parser",
3+
"description": "PHP HCL Parser",
4+
"type": "library",
5+
"license": "LGPL-3.0-only",
6+
"authors": [
7+
{
8+
"name": "Jordan Hall",
9+
"email": "jordan@hall05.co.uk"
10+
}
11+
],
12+
"require": {},
13+
"autoload": {
14+
"psr-4": {
15+
"DivineOmega\\HCLParser\\": "./src/"
16+
}
17+
},
18+
"scripts": {
19+
"post-autoload-dump": [
20+
"DivineOmega\\HCLParser\\Installer::installBinaries"
21+
]
22+
}
23+
}

src/HCLParser.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace DivineOmega\HCLParser;
4+
5+
class HCLParser
6+
{
7+
private $hcl;
8+
9+
public function __construct($hcl)
10+
{
11+
$this->hcl = $hcl;
12+
}
13+
14+
public function parse()
15+
{
16+
$working = $this->hcl;
17+
18+
// Remove single line comments
19+
$working = preg_replace('/#.*/', '', $working);
20+
21+
// Replace = with :
22+
$working = str_replace('=', ':', $working);
23+
24+
// Surround with { }
25+
$working = '{'.$working.'}';
26+
27+
// Surround keys with " "
28+
preg_match_all('/(?:\s*)(\w+)\s+:/', $working, $matches, PREG_OFFSET_CAPTURE);
29+
30+
for ($i=count($matches[1])-1; $i >= 0; $i--) {
31+
$match = $matches[1][$i];
32+
$matchText = $match[0];
33+
$startPos = $match[1];
34+
$working = substr_replace($working, '"'.$matchText.'"', $startPos, strlen($matchText));
35+
}
36+
37+
// Add : between " and {
38+
$working = preg_replace('/"\s*{/', '": {', $working);
39+
40+
// Add , between "} and "
41+
$working = preg_replace('/"\s*}\s*"/', '\"}, '.PHP_EOL.PHP_EOL.'"', $working);
42+
43+
// Add , between " and "
44+
$working = preg_replace('/"\s*"/', '",'.PHP_EOL.'"', $working);
45+
46+
// Removing trailing comma before ]
47+
$working = preg_replace('/,\s*]/', PHP_EOL.']', $working);
48+
49+
var_dump($working); die;
50+
51+
return json_decode($working);
52+
}
53+
}

src/Installer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace DivineOmega\HCLParser;
4+
5+
abstract class Installer
6+
{
7+
public static function installBinaries()
8+
{
9+
$binaryUrls = ['https://github.com/kvz/json2hcl/releases/download/v0.0.6/json2hcl_v0.0.6_linux_amd64'];
10+
11+
foreach($binaryUrls as $binaryUrl) {
12+
file_put_contents(__DIR__.'/../bin/'.basename($binaryUrl), file_get_contents($binaryUrl));
13+
}
14+
}
15+
}

test.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
use DivineOmega\HCLParser\HCLParser;
4+
5+
require __DIR__.'/vendor/autoload.php';
6+
7+
$hcl = '
8+
# Specify the provider and access details
9+
provider "aws" {
10+
region = "${var.aws_region}"
11+
}
12+
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+
}
90+
}
91+
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}"]
98+
99+
listener {
100+
instance_port = 80
101+
instance_protocol = "http"
102+
lb_port = 80
103+
lb_protocol = "http"
104+
}
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+
]
148+
}
149+
}
150+
';
151+
152+
$object = (new HCLParser($hcl))->parse();
153+
154+
var_dump($object);

0 commit comments

Comments
 (0)