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

Commit

Permalink
Merge branch 'develop' of git://github.com/zendframework/zf2 into hot…
Browse files Browse the repository at this point in the history
…fix/cache-empty-namespace
  • Loading branch information
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 13 deletions.
141 changes: 134 additions & 7 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class Factory
*/
public static $readers = null;

/**
* Plugin manager for loading writers
*
* @var null|WriterPluginManager
*/
public static $writers = null;

/**
* Registered config file extensions.
* key is extension, value is reader instance or plugin name
Expand All @@ -38,6 +45,19 @@ class Factory
'yaml' => 'yaml',
);

/**
* Register config file extensions for writing
* key is extension, value is writer instance or plugin name
*
* @var array
*/
protected static $writerExtensions = array(
'php' => 'php',
'ini' => 'ini',
'json' => 'json',
'xml' => 'xml',
'yaml' => 'yaml',
);

/**
* Read a config from a file.
Expand Down Expand Up @@ -70,11 +90,11 @@ public static function fromFile($filename, $returnConfigObject = false)
}

$config = include $filename;
} elseif (isset(self::$extensions[$extension])) {
$reader = self::$extensions[$extension];
} elseif (isset(static::$extensions[$extension])) {
$reader = static::$extensions[$extension];
if (!$reader instanceof Reader\ReaderInterface) {
$reader = self::getReaderPluginManager()->get($reader);
self::$extensions[$extension] = $reader;
$reader = static::getReaderPluginManager()->get($reader);
static::$extensions[$extension] = $reader;
}

/** @var Reader\ReaderInterface $reader */
Expand All @@ -101,20 +121,77 @@ public static function fromFiles(array $files, $returnConfigObject = false)
$config = array();

foreach ($files as $file) {
$config = ArrayUtils::merge($config, self::fromFile($file));
$config = ArrayUtils::merge($config, static::fromFile($file));
}

return ($returnConfigObject) ? new Config($config) : $config;
}

/**
* Writes a config to a file
*
* @param string $filename
* @param array|Config $config
* @return boolean TRUE on success | FALSE on failure
* @throws Exception\RuntimeException
* @throws Exception\InvalidArgumentException
*/
public static function toFile($filename, $config)
{
if (
(is_object($config) && !($config instanceOf Config)) ||
(!is_object($config) && !is_array($config))
) {
throw new Exception\InvalidArgumentException(
__METHOD__." \$config should be an array or instance of Zend\\Config\\Config"
);
}

$extension = substr(strrchr($filename, '.'), 1);
$directory = dirname($filename);

if (!is_dir($directory)) {
throw new Exception\RuntimeException(
"Directory '{$directory}' does not exists!"
);
}

if (!is_writable($directory)) {
throw new Exception\RuntimeException(
"Cannot write in directory '{$directory}'"
);
}

if(!isset(self::$writerExtensions[$extension])) {
throw new Exception\RuntimeException(
"Unsupported config file extension: '.{$extension}' for writing."
);
}

$writer = self::$writerExtensions[$extension];
if (($writer instanceOf Writer\AbstractWriter) === false) {
$writer = self::getWriterPluginManager()->get($writer);
self::$writerExtensions[$extension] = $writer;
}

if (is_object($config)) {
$config = $config->toArray();
}

$content = $writer->processConfig($config);

return (bool) (file_put_contents($filename, $content) !== false);
}

/**
* Set reader plugin manager
*
* @param ReaderPluginManager $readers
* @return void
*/
public static function setReaderPluginManager(ReaderPluginManager $readers)
{
self::$readers = $readers;
static::$readers = $readers;
}

/**
Expand All @@ -130,12 +207,38 @@ public static function getReaderPluginManager()
return static::$readers;
}

/**
* Set writer plugin manager
*
* @param WriterPluginManager $writers
* @return void
*/
public static function setWriterPluginManager(WriterPluginManager $writers)
{
self::$writers = $writers;
}

/**
* Get the writer plugin manager
*
* @return WriterPluginManager
*/
public static function getWriterPluginManager()
{
if (static::$writers === null) {
static::$writers = new WriterPluginManager();
}

return static::$writers;
}

/**
* Set config reader for file extension
*
* @param string $extension
* @param string|Reader\ReaderInterface $reader
* @throws Exception\InvalidArgumentException
* @return void
*/
public static function registerReader($extension, $reader)
{
Expand All @@ -150,6 +253,30 @@ public static function registerReader($extension, $reader)
));
}

self::$extensions[$extension] = $reader;
static::$extensions[$extension] = $reader;
}

/**
* Set config writer for file extension
*
* @param string $extension
* @param string|Writer\AbstractWriter $writer
* @throw Exception\InvalidArgumentException
* @return void
*/
public static function registerWriter($extension, $writer)
{
$extension = strtolower($extension);

if (!is_string($writer) && !$writer instanceof Writer\AbstractWriter) {
throw new Exception\InvalidArgumentException(sprintf(
'Writer should be plugin name, class name or ' .
'instance of %s\Writer\AbstractWriter; received "%s"',
__NAMESPACE__,
(is_object($writer) ? get_class($writer) : gettype($writer))
));
}

self::$writerExtensions[$extension] = $writer;
}
}
4 changes: 2 additions & 2 deletions src/Reader/Ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function fromFile($filename)
$this->directory = dirname($filename);

set_error_handler(
function($error, $message = '', $file = '', $line = 0) use ($filename) {
function ($error, $message = '', $file = '', $line = 0) use ($filename) {
throw new Exception\RuntimeException(sprintf(
'Error reading INI file "%s": %s',
$filename, $message
Expand Down Expand Up @@ -105,7 +105,7 @@ public function fromString($string)
$this->directory = null;

set_error_handler(
function($error, $message = '', $file = '', $line = 0) {
function ($error, $message = '', $file = '', $line = 0) {
throw new Exception\RuntimeException(sprintf(
'Error reading INI string: %s',
$message
Expand Down
4 changes: 2 additions & 2 deletions src/Reader/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function fromFile($filename)
$this->directory = dirname($filename);

set_error_handler(
function($error, $message = '', $file = '', $line = 0) use ($filename) {
function ($error, $message = '', $file = '', $line = 0) use ($filename) {
throw new Exception\RuntimeException(sprintf(
'Error reading XML file "%s": %s',
$filename, $message
Expand Down Expand Up @@ -104,7 +104,7 @@ public function fromString($string)
$this->directory = null;

set_error_handler(
function($error, $message = '', $file = '', $line = 0) {
function ($error, $message = '', $file = '', $line = 0) {
throw new Exception\RuntimeException(sprintf(
'Error reading XML string: %s',
$message
Expand Down
2 changes: 1 addition & 1 deletion src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function toFile($filename, $config, $exclusiveLock = true)
}

set_error_handler(
function($error, $message = '', $file = '', $line = 0) use ($filename) {
function ($error, $message = '', $file = '', $line = 0) use ($filename) {
throw new Exception\RuntimeException(sprintf(
'Error writing to "%s": %s',
$filename, $message
Expand Down
29 changes: 29 additions & 0 deletions src/WriterPluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Zend\Config;

use Zend\ServiceManager\AbstractPluginManager;

class WriterPluginManager extends AbstractPluginManager
{
protected $invokableClasses = array(
'php' => 'Zend\Config\Writer\PhpArray',
'ini' => 'Zend\Config\Writer\Ini',
'json' => 'Zend\Config\Writer\Json',
'yaml' => 'Zend\Config\Writer\Yaml',
'xml' => 'Zend\Config\Writer\Xml',
);

public function validatePlugin($plugin)
{
if ($plugin instanceOf Writer\AbstractWriter) {
return;
}

$type = is_object($plugin) ? get_class($plugin) : gettype($plugin);

throw new Exception\InvalidArgumentException(
"Plugin of type {$type} is invalid. Plugin must extend ".
__NAMESPACE__.'\Writer\AbstractWriter'
);
}
}
92 changes: 91 additions & 1 deletion test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@
*/
class FactoryTest extends \PHPUnit_Framework_TestCase
{
protected $tmpFiles = array();

protected function getTestAssetFileName($ext)
{
if (empty($this->tmpfiles[$ext])) {
$this->tmpfiles[$ext] = tempnam(sys_get_temp_dir(), 'zend-config-writer').'.'.$ext;
}
return $this->tmpfiles[$ext];
}

public function tearDown()
{
foreach($this->tmpFiles as $file) {
if (file_exists($file)) {
if (!is_writable($file)) {
chmod($file, 0777);
}
@unlink($file);
}
}
}

public function testFromIni()
{
$config = Factory::fromFile(__DIR__ . '/TestAssets/Ini/include-base.ini');
Expand Down Expand Up @@ -125,7 +147,7 @@ public function testFactoryCanRegisterCustomReaderInstance()
$this->assertEquals($configObject['one'], 1);
}

public function testFactoryCanRegisterCustomReaderPlugn()
public function testFactoryCanRegisterCustomReaderPlugin()
{
$dummyReader = new Reader\TestAssets\DummyReader();
Factory::getReaderPluginManager()->setService('DummyReader', $dummyReader);
Expand All @@ -138,5 +160,73 @@ public function testFactoryCanRegisterCustomReaderPlugn()
$this->assertEquals($configObject['one'], 1);
}

public function testFactoryToFileInvalidFileExtension()
{
$this->setExpectedException('RuntimeException');
$result = Factory::toFile(__DIR__.'/TestAssets/bad.ext', array());
}

public function testFactoryToFileNoDirInHere()
{
$this->setExpectedException('RuntimeException');
$result = Factory::toFile(__DIR__.'/TestAssets/NoDirInHere/nonExisiting/dummy.php', array());
}

public function testFactoryWriteToFile()
{
$config = array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo'));

$file = $this->getTestAssetFileName('php');
$result = Factory::toFile($file, $config);

// build string line by line as we are trailing-whitespace sensitive.
$expected = "<?php\n";
$expected .= "return array (\n";
$expected .= " 'test' => 'foo',\n";
$expected .= " 'bar' => \n";
$expected .= " array (\n";
$expected .= " 0 => 'baz',\n";
$expected .= " 1 => 'foo',\n";
$expected .= " ),\n";
$expected .= ");\n";

$this->assertEquals(true, $result);
$this->assertEquals($expected, file_get_contents($file));
}

public function testFactoryToFileWrongConfig()
{
$this->setExpectedException('InvalidArgumentException');
$result = Factory::toFile('test.ini', 'Im wrong');
}

public function testFactoryRegisterInvalidWriter()
{
$this->setExpectedException('InvalidArgumentException');
Factory::registerWriter('dum', new Reader\TestAssets\DummyReader());
}

public function testFactoryCanRegisterCustomWriterInstance()
{
Factory::registerWriter('dum', new Writer\TestAssets\DummyWriter());

$file = $this->getTestAssetFileName('dum');

$res = Factory::toFile($file, array('one' => 1));

$this->assertEquals($res, true);
}

public function testFactoryCanRegisterCustomWriterPlugin()
{
$dummyWriter = new Writer\TestAssets\DummyWriter();
Factory::getWriterPluginManager()->setService('DummyWriter', $dummyWriter);

Factory::registerWriter('dum', 'DummyWriter');

$file = $this->getTestAssetFileName('dum');

$res = Factory::toFile($file, array('one' => 1));
$this->assertEquals($res, true);
}
}
Loading

0 comments on commit eb24d30

Please sign in to comment.