Skip to content

Commit

Permalink
Fixes #115
Browse files Browse the repository at this point in the history
  • Loading branch information
filips123 authored and hassankhan committed Feb 1, 2019
1 parent a2b9931 commit 5c0ad8e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Parser/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function parseFile($filename)

/**
* {@inheritDoc}
*Loads a PHP string and gets its' contents as an array
* Loads a PHP string and gets its' contents as an array
*
* @throws ParseException If the PHP string throws an exception
* @throws UnsupportedFormatException If the PHP string does not return an array
Expand All @@ -60,7 +60,7 @@ public function parseString($config)

// Eval the string, if it throws an exception, rethrow it
try {
$data = eval($config);
$data = $this->isolate($config);
} catch (Exception $exception) {
throw new ParseException(
[
Expand Down Expand Up @@ -97,6 +97,18 @@ protected function parse($data = null, $filename = null)
return $data;
}

/**
* Runs PHP string in isolated method
*
* @param string $EGsfKPdue7ahnMTy
*
* @return array
*/
protected function isolate($EGsfKPdue7ahnMTy)
{
return eval($EGsfKPdue7ahnMTy);
}

/**
* {@inheritDoc}
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/Parser/PhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function testLoadExceptionalPhpFile()

/**
* @covers Noodlehaus\Parser\Php::parseString()
* @covers Noodlehaus\Parser\Php::isolate()
* @expectedException Noodlehaus\Exception\ParseException
* @expectedExceptionMessage PHP string threw an exception
*/
Expand All @@ -75,6 +76,7 @@ public function testLoadExceptionalPhpString()
/**
* @covers Noodlehaus\Parser\Php::parseFile()
* @covers Noodlehaus\Parser\Php::parseString()
* @covers Noodlehaus\Parser\Php::isolate()
* @covers Noodlehaus\Parser\Php::parse()
*/
public function testLoadPhpArray()
Expand All @@ -92,6 +94,7 @@ public function testLoadPhpArray()
/**
* @covers Noodlehaus\Parser\Php::parseFile()
* @covers Noodlehaus\Parser\Php::parseString()
* @covers Noodlehaus\Parser\Php::isolate()
* @covers Noodlehaus\Parser\Php::parse()
*/
public function testLoadPhpCallable()
Expand All @@ -105,4 +108,22 @@ public function testLoadPhpCallable()
$this->assertEquals('localhost', $string['host']);
$this->assertEquals('80', $string['port']);
}

/**
* @covers Noodlehaus\Parser\Php::parseFile()
* @covers Noodlehaus\Parser\Php::parseString()
* @covers Noodlehaus\Parser\Php::isolate()
* @covers Noodlehaus\Parser\Php::parse()
*/
public function testLoadPhpVariable()
{
$file = $this->php->parseFile(__DIR__ . '/../mocks/pass/config-var.php');
$string = $this->php->parseString(file_get_contents(__DIR__ . '/../mocks/pass/config-var.php'));

$this->assertEquals('localhost', $file['host']);
$this->assertEquals('80', $file['port']);

$this->assertEquals('localhost', $string['host']);
$this->assertEquals('80', $string['port']);
}
}
11 changes: 11 additions & 0 deletions tests/mocks/pass/config-var.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$config['host'] = 'localhost';
$config['port'] = 80;
$config['servers'][0] = 'host1';
$config['servers'][1] = 'host2';
$config['servers'][2] = 'host3';
$config['application']['name'] = 'configuration';
$config['application']['secret'] = 's3cr3t';

return $config;

0 comments on commit 5c0ad8e

Please sign in to comment.