From d043ecad4b4e3368b1e280e51cf907c58d99423a Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 1 Jul 2013 15:21:55 -0500 Subject: [PATCH 1/2] [zendframework/zf2#4766] Config makes include_path usage opt-in only - You must pass the `$useIncludePath` to either `fromFile()` or `fromFiles()` for it to be used. Constants were created with boolean values for those using IDEs. --- src/Factory.php | 23 ++++++++++++++++++++--- test/FactoryTest.php | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index b530c65..960fdde 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -13,6 +13,14 @@ class Factory { + /**@+ + * Constants defining rules for whether or not to use the include_path; + * used with fromFile() and fromFiles(). + */ + const NO_USE_INCLUDE_PATH = false; + const USE_INCLUDE_PATH = true; + /**@-*/ + /** * Plugin manager for loading readers * @@ -59,14 +67,22 @@ 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) + public static function fromFile($filename, $returnConfigObject = false, $useIncludePath = self::NO_USE_INCLUDE_PATH) { $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( @@ -122,14 +138,15 @@ public static function fromFile($filename, $returnConfigObject = false) * * @param array $files * @param bool $returnConfigObject + * @param bool $useIncludePath * @return array|Config */ - public static function fromFiles(array $files, $returnConfigObject = false) + public static function fromFiles(array $files, $returnConfigObject = false, $useIncludePath = self::NO_USE_INCLUDE_PATH) { $config = array(); foreach ($files as $file) { - $config = ArrayUtils::merge($config, static::fromFile($file)); + $config = ArrayUtils::merge($config, static::fromFile($file, false, $useIncludePath)); } return ($returnConfigObject) ? new Config($config) : $config; diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 6549a0e..4b182c9 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -122,7 +122,7 @@ public function testFromIniAndXmlAndPhpFilesFromIncludePath() 'Xml/include-base2.xml', 'Php/include-base3.php', ); - $config = Factory::fromFiles($files, Factory::USE_INCLUDE_PATH); + $config = Factory::fromFiles($files, false, Factory::USE_INCLUDE_PATH); $this->assertEquals('bar', $config['base']['foo']); $this->assertEquals('baz', $config['test']['bar']); From 720d46f96e70baefcd4daaad7a1260b741676ae8 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 2 Jul 2013 09:09:01 -0500 Subject: [PATCH 2/2] Remove constants - Remove constants, per @DASPRiD --- src/Factory.php | 12 ++---------- test/FactoryTest.php | 2 +- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index 960fdde..71a7bd1 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -13,14 +13,6 @@ class Factory { - /**@+ - * Constants defining rules for whether or not to use the include_path; - * used with fromFile() and fromFiles(). - */ - const NO_USE_INCLUDE_PATH = false; - const USE_INCLUDE_PATH = true; - /**@-*/ - /** * Plugin manager for loading readers * @@ -72,7 +64,7 @@ class Factory * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ - public static function fromFile($filename, $returnConfigObject = false, $useIncludePath = self::NO_USE_INCLUDE_PATH) + public static function fromFile($filename, $returnConfigObject = false, $useIncludePath = false) { $filepath = $filename; if (!file_exists($filename)) { @@ -141,7 +133,7 @@ public static function fromFile($filename, $returnConfigObject = false, $useIncl * @param bool $useIncludePath * @return array|Config */ - public static function fromFiles(array $files, $returnConfigObject = false, $useIncludePath = self::NO_USE_INCLUDE_PATH) + public static function fromFiles(array $files, $returnConfigObject = false, $useIncludePath = false) { $config = array(); diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 4b182c9..ea1af66 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -122,7 +122,7 @@ public function testFromIniAndXmlAndPhpFilesFromIncludePath() 'Xml/include-base2.xml', 'Php/include-base3.php', ); - $config = Factory::fromFiles($files, false, Factory::USE_INCLUDE_PATH); + $config = Factory::fromFiles($files, false, true); $this->assertEquals('bar', $config['base']['foo']); $this->assertEquals('baz', $config['test']['bar']);