Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Revert "Revert "Merge branch 'superdweebie-rand-bugfix'""
Browse files Browse the repository at this point in the history
This reverts commit b0ae4689135d62555cf9bbe55cdd7dddcd2d8f05 in order to allow
forward-porting fixes to develop.

Conflicts:
	library/Zend/ModuleManager/ModuleEvent.php
  • Loading branch information
weierophinney committed Aug 19, 2013
165 parents 44bad20 + 47ff527 + 33b5707 + 0f253b5 + 644d22a + f3cba00 + e470732 + ae8cd95 + a4f21b6 + f36ff90 + 5344247 + 5cdfa08 + 4925e55 + fb107e5 + 98634df + 8d23ab7 + 19e17a6 + 98ac67f + 0a2d51c + 86db540 + efbb513 + 24c143c + 307427e + 4c7a822 + 25d3bd7 + 40bfe7f + 70ca2ff + 76fd684 + 82cff31 + 186a39c + 39a35fe + b0c8193 + 1a7e426 + 09ea754 + 8317963 + 950705c + bd78289 + 25f3e05 + 1c0577b + 6bcfccb + 0b2a1a9 + 7974490 + f3ba45e + 4357e80 + 7d43d02 + 421a0f4 + e7aa329 + 6d05bfe + f27c5e2 + 6cb3b21 + 1ecb5ed + b66b0e2 + 0b91e40 + 6bda391 + b932fa5 + a431b75 + 9ce83ec + a35dff6 + 60e4965 + 0f071e9 + 3fe17b4 + 196ca18 + 17cbc64 + 8f4a51f + 88ec12d + 31ab35e + 59c83c4 + d50da4c + 01af50b + 6a46af5 + 4308adc + 1c3d629 + 18a268d + 1987408 + abc72db + 175f7ab + 8a85704 + 7706019 + cc5d38c + fbaa5aa + 0555415 + 20ae04b + 0680687 + e65301c + 424e30a + d36a7f1 + 64bb794 + c74649b + b14bb6b + 4e73e4e + 0ee93d0 + e887bfd + f66ad20 + 66c5ff2 + f5b2841 + 717175b + 52c5e49 + 8f39d69 + 2003fce + 1ccb3fd + 315a9ac + 2b82c0f + 1565409 + fd7399e + cb27129 + 62ca6c9 + 7b73995 + 18f93e0 + 72fe59b + 9f5b116 + 28a04a3 + b56ee4d + 9287e61 + 1a420ec + 6a43cc1 + ad11b96 + cf95523 + 25ef603 + 894f762 + c00c005 + 69fb7fc + 57cb4a0 + e25b09c + 17a7c5c + c526cd4 + 518ca6f + 57fa76c + 853c28f + 520cebc + dc5f609 + ccf1c7e + 251b79e + 78fbb55 + fcc04e7 + 9bf2948 + 9561532 + b614ad3 + 954a3b1 + dacc257 + 07a262d + 59c02c3 + 7064caa + 6f75739 + 747b7ed + eb24d30 + 8ff8298 + 97c61d8 + da4af46 + 11c9caf + cc11df8 + cd54577 + 298afbb + 24c5cf6 + fa546ac + 5e4d7a6 + cddbfdf + 7e73be1 + ecda841 + dc32703 + 9bb6302 + fee2498 + f827502 + 4b8d9e1 + eff3fbb + 240c93a commit 14d7077
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,34 @@ 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 = false)
{
$pathinfo = pathinfo($filename);
$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);

if (!isset($pathinfo['extension'])) {
throw new Exception\RuntimeException(sprintf(
Expand All @@ -77,14 +98,14 @@ public static function fromFile($filename, $returnConfigObject = false)
$extension = strtolower($pathinfo['extension']);

if ($extension === 'php') {
if (!is_file($filename) || !is_readable($filename)) {
if (!is_file($filepath) || !is_readable($filepath)) {
throw new Exception\RuntimeException(sprintf(
"File '%s' doesn't exist or not readable",
$filename
));
}

$config = include $filename;
$config = include $filepath;
} elseif (isset(static::$extensions[$extension])) {
$reader = static::$extensions[$extension];
if (!$reader instanceof Reader\ReaderInterface) {
Expand All @@ -93,7 +114,7 @@ public static function fromFile($filename, $returnConfigObject = false)
}

/** @var Reader\ReaderInterface $reader */
$config = $reader->fromFile($filename);
$config = $reader->fromFile($filepath);
} else {
throw new Exception\RuntimeException(sprintf(
'Unsupported config file extension: .%s',
Expand All @@ -109,14 +130,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 = false)
{
$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;
Expand Down
139 changes: 139 additions & 0 deletions src/Reader/JavaProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Config\Reader;

use Zend\Config\Reader\ReaderInterface;
use Zend\Config\Exception;

/**
* Java-style properties config reader.
*/
class JavaProperties implements ReaderInterface
{
/**
* Directory of the Java-style properties file
*
* @var string
*/
protected $directory;

/**
* fromFile(): defined by Reader interface.
*
* @see ReaderInterface::fromFile()
* @param string $filename
* @return array
* @throws Exception\RuntimeException if the file cannot be read
*/
public function fromFile($filename)
{
if (!is_file($filename) || !is_readable($filename)) {
throw new Exception\RuntimeException(sprintf(
"File '%s' doesn't exist or not readable",
$filename
));
}

$this->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;
}
}
23 changes: 23 additions & 0 deletions test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class FactoryTest extends \PHPUnit_Framework_TestCase
{
protected $tmpFiles = array();
protected $originalIncludePath;

protected function getTestAssetFileName($ext)
{
Expand All @@ -26,8 +27,16 @@ 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)) {
Expand Down Expand Up @@ -102,6 +111,20 @@ 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 (
Expand Down
79 changes: 79 additions & 0 deletions test/Reader/JavaPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Config\Reader;

use Zend\Config\Reader\JavaProperties;

/**
* @group Zend_Config
*/
class JavaPropertiesTest extends AbstractReaderTestCase
{
public function setUp()
{
$this->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);
}
}
3 changes: 3 additions & 0 deletions test/Reader/TestAssets/JavaProperties/include-base.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#comment
!comment
@include:include-target.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
single.line:test
multiple:line \
test

0 comments on commit 14d7077

Please sign in to comment.