Skip to content

Commit 0be2757

Browse files
authored
Merge pull request #3 from Binternet/master
Documentation and Multiple OS Support
2 parents afdcc47 + 4ac7a51 commit 0be2757

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/HCLParser.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,41 @@
22

33
namespace DivineOmega\HCLParser;
44

5+
/**
6+
* Class HCLParser
7+
* @package DivineOmega\HCLParser
8+
*/
59
class HCLParser
610
{
11+
/**
12+
* @var
13+
*/
714
private $hcl;
815

16+
/**
17+
* HCLParser constructor.
18+
* @param $hcl
19+
*/
920
public function __construct($hcl)
1021
{
1122
$this->hcl = $hcl;
1223
}
1324

25+
/**
26+
* @return string
27+
*/
1428
private function getJSONString()
1529
{
16-
$command = __DIR__.'/../bin/json2hcl_v0.0.6_linux_amd64 --reverse <<\'EOF\''.PHP_EOL.$this->hcl.PHP_EOL.'EOF';
30+
$command = __DIR__.'/../bin/' . Installer::getBinaryFilename() . ' --reverse <<\'EOF\''.PHP_EOL.$this->hcl.PHP_EOL.'EOF';
1731

1832
exec($command, $lines);
1933

2034
return implode(PHP_EOL, $lines);
2135
}
2236

37+
/**
38+
* @return mixed
39+
*/
2340
public function parse()
2441
{
2542
return json_decode($this->getJSONString());

src/Installer.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,63 @@
22

33
namespace DivineOmega\HCLParser;
44

5+
/**
6+
* Class Installer
7+
* @package DivineOmega\HCLParser
8+
*/
59
abstract class Installer
610
{
11+
/**
12+
* json2hcl Version we want to use
13+
* @see https://github.com/kvz/json2hcl/releases
14+
*/
15+
const JSON2HCL_VERSION = '0.0.6';
16+
17+
/**
18+
* Returns the correct binary filename according to the Operating System and Architecture
19+
*/
20+
public static function getBinaryFilename()
21+
{
22+
# Defaults
23+
$osString = 'linux';
24+
$architecture = 'amd64';
25+
26+
# Switch architecture if needed
27+
if (2147483647 == PHP_INT_MAX) {
28+
$architecture = '386';
29+
}
30+
31+
# Switch Operating System if needed
32+
switch (TRUE) {
33+
case stristr(PHP_OS, 'DAR'):
34+
$osString = 'darwin';
35+
break;
36+
case stristr(PHP_OS, 'WIN'):
37+
$osString = 'windows';
38+
break;
39+
}
40+
41+
return sprintf('json2hcl_v%s_%s_%s',self::JSON2HCL_VERSION, $osString, $architecture);
42+
}
43+
744
public static function installBinaries()
845
{
9-
$binaryUrls = ['https://github.com/kvz/json2hcl/releases/download/v0.0.6/json2hcl_v0.0.6_linux_amd64'];
46+
$binaryUrls = [
47+
sprintf('https://github.com/kvz/json2hcl/releases/download/v%s/%s', self::JSON2HCL_VERSION, self::getBinaryFilename() )
48+
];
1049

1150
foreach ($binaryUrls as $binaryUrl) {
12-
$destination = __DIR__.'/../bin/'.basename($binaryUrl);
51+
$destination = __DIR__ . '/../bin/' . basename($binaryUrl);
1352

53+
# Skip if file exists
1454
if (file_exists($destination)) {
1555
continue;
1656
}
1757

58+
# Download
1859
file_put_contents($destination, file_get_contents($binaryUrl));
60+
61+
# Make executable
1962
chmod($destination, 0755);
2063
}
2164
}

0 commit comments

Comments
 (0)