diff --git a/src/Factory.php b/src/Factory.php index 87705c4..d00a6ab 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -59,34 +59,13 @@ class Factory * * @param string $filename * @param bool $returnConfigObject - * @param bool $useIncludePath * @return array|Config * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ - public static function fromFile($filename, $returnConfigObject = false, $useIncludePath = false) + public static function fromFile($filename, $returnConfigObject = false) { - $filepath = $filename; - if (!file_exists($filename)) { - if (!$useIncludePath) { - throw new Exception\RuntimeException(sprintf( - 'Filename "%s" cannot be found relative to the working directory', - $filename - )); - } - - $fromIncludePath = stream_resolve_include_path($filename); - if (!$fromIncludePath) { - throw new Exception\RuntimeException(sprintf( - 'Filename "%s" cannot be found relative to the working directory or the include_path ("%s")', - $filename, - get_include_path() - )); - } - $filepath = $fromIncludePath; - } - - $pathinfo = pathinfo($filepath); + $pathinfo = pathinfo($filename); if (!isset($pathinfo['extension'])) { throw new Exception\RuntimeException(sprintf( @@ -98,14 +77,14 @@ public static function fromFile($filename, $returnConfigObject = false, $useIncl $extension = strtolower($pathinfo['extension']); if ($extension === 'php') { - if (!is_file($filepath) || !is_readable($filepath)) { + if (!is_file($filename) || !is_readable($filename)) { throw new Exception\RuntimeException(sprintf( "File '%s' doesn't exist or not readable", $filename )); } - $config = include $filepath; + $config = include $filename; } elseif (isset(static::$extensions[$extension])) { $reader = static::$extensions[$extension]; if (!$reader instanceof Reader\ReaderInterface) { @@ -114,7 +93,7 @@ public static function fromFile($filename, $returnConfigObject = false, $useIncl } /** @var Reader\ReaderInterface $reader */ - $config = $reader->fromFile($filepath); + $config = $reader->fromFile($filename); } else { throw new Exception\RuntimeException(sprintf( 'Unsupported config file extension: .%s', @@ -130,15 +109,14 @@ public static function fromFile($filename, $returnConfigObject = false, $useIncl * * @param array $files * @param bool $returnConfigObject - * @param bool $useIncludePath * @return array|Config */ - public static function fromFiles(array $files, $returnConfigObject = false, $useIncludePath = false) + public static function fromFiles(array $files, $returnConfigObject = false) { $config = array(); foreach ($files as $file) { - $config = ArrayUtils::merge($config, static::fromFile($file, false, $useIncludePath)); + $config = ArrayUtils::merge($config, static::fromFile($file)); } return ($returnConfigObject) ? new Config($config) : $config; diff --git a/src/Reader/JavaProperties.php b/src/Reader/JavaProperties.php deleted file mode 100644 index 7c6e8bf..0000000 --- a/src/Reader/JavaProperties.php +++ /dev/null @@ -1,139 +0,0 @@ -directory = dirname($filename); - - $config = $this->parse(file_get_contents($filename)); - - return $this->process($config); - } - - /** - * fromString(): defined by Reader interface. - * - * @see ReaderInterface::fromString() - * @param string $string - * @return array - * @throws Exception\RuntimeException if an @include key is found - */ - public function fromString($string) - { - if (empty($string)) { - return array(); - } - - $this->directory = null; - - $config = $this->parse($string); - - return $this->process($config); - } - - /** - * Process the array for @include - * - * @param array $data - * @return array - * @throws Exception\RuntimeException if an @include key is found - */ - protected function process(array $data) - { - foreach ($data as $key => $value) { - if (trim($key) === '@include') { - if ($this->directory === null) { - throw new Exception\RuntimeException('Cannot process @include statement for a string'); - } - $reader = clone $this; - unset($data[$key]); - $data = array_replace_recursive($data, $reader->fromFile($this->directory . '/' . $value)); - } - } - return $data; - } - - /** - * Parse Java-style properties string - * - * @todo Support use of the equals sign "key=value" as key-value delimiter - * @todo Ignore whitespace that precedes text past the first line of multiline values - * - * @param string $string - * @return array - */ - protected function parse($string) - { - $result = array(); - $lines = explode("\n", $string); - $key = ""; - $isWaitingOtherLine = false; - foreach ($lines as $i => $line) { - // Ignore empty lines and commented lines - if (empty($line) - || (!$isWaitingOtherLine && strpos($line, "#") === 0) - || (!$isWaitingOtherLine && strpos($line, "!") === 0)) { - continue; - } - - // Add a new key-value pair or append value to a previous pair - if (!$isWaitingOtherLine) { - $key = substr($line, 0, strpos($line, ':')); - $value = substr($line, strpos($line, ':') + 1, strlen($line)); - } else { - $value .= $line; - } - - // Check if ends with single '\' (indicating another line is expected) - if (strrpos($value, "\\") === strlen($value) - strlen("\\")) { - $value = substr($value, 0, strlen($value) - 1); - $isWaitingOtherLine = true; - } else { - $isWaitingOtherLine = false; - } - - $result[$key] = stripslashes($value); - unset($lines[$i]); - } - - return $result; - } -} diff --git a/test/FactoryTest.php b/test/FactoryTest.php index a66935f..fa03b44 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -17,7 +17,6 @@ class FactoryTest extends \PHPUnit_Framework_TestCase { protected $tmpFiles = array(); - protected $originalIncludePath; protected function getTestAssetFileName($ext) { @@ -27,16 +26,8 @@ protected function getTestAssetFileName($ext) return $this->tmpfiles[$ext]; } - public function setUp() - { - $this->originalIncludePath = get_include_path(); - set_include_path(__DIR__ . '/TestAssets'); - } - public function tearDown() { - set_include_path($this->originalIncludePath); - foreach ($this->tmpFiles as $file) { if (file_exists($file)) { if (!is_writable($file)) { @@ -111,20 +102,6 @@ public function testFromIniAndXmlAndPhpFiles() $this->assertEquals('baz', $config['last']['bar']); } - public function testFromIniAndXmlAndPhpFilesFromIncludePath() - { - $files = array ( - 'Ini/include-base.ini', - 'Xml/include-base2.xml', - 'Php/include-base3.php', - ); - $config = Factory::fromFiles($files, false, true); - - $this->assertEquals('bar', $config['base']['foo']); - $this->assertEquals('baz', $config['test']['bar']); - $this->assertEquals('baz', $config['last']['bar']); - } - public function testReturnsConfigObjectIfRequestedAndArrayOtherwise() { $files = array ( diff --git a/test/Reader/JavaPropertiesTest.php b/test/Reader/JavaPropertiesTest.php deleted file mode 100644 index f585ef7..0000000 --- a/test/Reader/JavaPropertiesTest.php +++ /dev/null @@ -1,79 +0,0 @@ -reader = new JavaProperties(); - } - - /** - * getTestAssetPath(): defined by AbstractReaderTestCase. - * - * @see AbstractReaderTestCase::getTestAssetPath() - * @return string - */ - protected function getTestAssetPath($name) - { - return __DIR__ . '/TestAssets/JavaProperties/' . $name . '.properties'; - } - - public function testFromFile() - { - $arrayJavaProperties = $this->reader->fromFile($this->getTestAssetPath('include-target')); - - $this->assertNotEmpty($arrayJavaProperties); - $this->assertEquals($arrayJavaProperties['single.line'], 'test'); - $this->assertEquals($arrayJavaProperties['multiple'], 'line test'); - } - - public function testIncludeAsElement() - { - $arrayJavaProperties = $this->reader->fromFile($this->getTestAssetPath('include-base')); - - $this->assertNotEmpty($arrayJavaProperties); - $this->assertEquals($arrayJavaProperties['single.line'], 'test'); - $this->assertEquals($arrayJavaProperties['multiple'], 'line test'); - } - - public function testFromString() - { - $JavaProperties = <<<'ASSET' -#comment -!comment -single.line:test -multiple:line \ -test -ASSET; - - $arrayJavaProperties = $this->reader->fromString($JavaProperties); - - $this->assertNotEmpty($arrayJavaProperties); - $this->assertEquals($arrayJavaProperties['single.line'], 'test'); - $this->assertEquals($arrayJavaProperties['multiple'], 'line test'); - } - - public function testInvalidIncludeInString() - { - $JavaProperties = '@include:fail.properties'; - - $expectedErrorMessage = 'Cannot process @include statement for a string'; - - $this->setExpectedException('Zend\Config\Exception\RuntimeException', $expectedErrorMessage); - $arrayJavaPropterties = $this->reader->fromString($JavaProperties); - } -} diff --git a/test/Reader/TestAssets/JavaProperties/include-base.properties b/test/Reader/TestAssets/JavaProperties/include-base.properties deleted file mode 100644 index a2cdfbf..0000000 --- a/test/Reader/TestAssets/JavaProperties/include-base.properties +++ /dev/null @@ -1,3 +0,0 @@ -#comment -!comment -@include:include-target.properties diff --git a/test/Reader/TestAssets/JavaProperties/include-target.properties b/test/Reader/TestAssets/JavaProperties/include-target.properties deleted file mode 100644 index e081355..0000000 --- a/test/Reader/TestAssets/JavaProperties/include-target.properties +++ /dev/null @@ -1,3 +0,0 @@ -single.line:test -multiple:line \ -test