@@ -11,43 +11,17 @@ public function __construct($hcl)
1111 $ this ->hcl = $ hcl ;
1212 }
1313
14- public function parse ()
14+ private function getJSONString ()
1515 {
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- }
16+ $ command = __DIR__ .'/../bin/json2hcl_v0.0.6_linux_amd64 --reverse << \'EOF \'' .PHP_EOL .$ this ->hcl .PHP_EOL .'EOF ' ;
3617
37- // Add : between " and {
38- $ working = preg_replace ('/"\s*{/ ' , '": { ' , $ working );
18+ exec ($ command , $ lines );
3919
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;
20+ return implode (PHP_EOL , $ lines );
21+ }
5022
51- return json_decode ($ working );
23+ public function parse ()
24+ {
25+ return json_decode ($ this ->getJSONString ());
5226 }
5327}
0 commit comments