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

Commit

Permalink
Show file tree
Hide file tree
Showing 27 changed files with 278 additions and 141 deletions.
6 changes: 1 addition & 5 deletions src/Config.php
Expand Up @@ -5,7 +5,6 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config;
Expand All @@ -21,9 +20,6 @@
*
* Implements Countable, Iterator and ArrayAccess
* to facilitate easy access to the data.
*
* @category Zend
* @package Zend_Config
*/
class Config implements Countable, Iterator, ArrayAccess
{
Expand Down Expand Up @@ -343,7 +339,7 @@ public function offsetUnset($offset)
* @param Config $merge
* @return Config
*/
public function merge(self $merge)
public function merge(Config $merge)
{
/** @var Config $value */
foreach ($merge as $key => $value) {
Expand Down
6 changes: 0 additions & 6 deletions src/Exception/ExceptionInterface.php
Expand Up @@ -5,15 +5,9 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config\Exception;

/**
* @category Zend
* @package Zend_Config
* @subpackage Exception
*/
interface ExceptionInterface
{}
6 changes: 0 additions & 6 deletions src/Exception/InvalidArgumentException.php
Expand Up @@ -5,15 +5,9 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config\Exception;

/**
* @category Zend
* @package Zend_Config
* @subpackage Exception
*/
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{}
6 changes: 0 additions & 6 deletions src/Exception/RuntimeException.php
Expand Up @@ -5,15 +5,9 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config\Exception;

/**
* @category Zend
* @package Zend_Config
* @subpackage Exception
*/
class RuntimeException extends \RuntimeException implements ExceptionInterface
{}
132 changes: 127 additions & 5 deletions src/Factory.php
Expand Up @@ -5,17 +5,12 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config;

use Zend\Stdlib\ArrayUtils;

/**
* @category Zend
* @package Zend_Config
*/
class Factory
{
/**
Expand All @@ -25,6 +20,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 +40,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 @@ -107,10 +122,67 @@ public static function fromFiles(array $files, $returnConfigObject = false)
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)
{
Expand All @@ -130,12 +202,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 @@ -152,4 +250,28 @@ public static function registerReader($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;
}
}
6 changes: 0 additions & 6 deletions src/Processor/Constant.php
Expand Up @@ -5,16 +5,10 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config\Processor;

/**
* @category Zend
* @package Zend_Config
* @subpackage Processor
*/
class Constant extends Token implements ProcessorInterface
{
/**
Expand Down
6 changes: 0 additions & 6 deletions src/Processor/Filter.php
Expand Up @@ -5,7 +5,6 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config\Processor;
Expand All @@ -14,11 +13,6 @@
use Zend\Config\Exception;
use Zend\Filter\FilterInterface as ZendFilter;

/**
* @category Zend
* @package Zend_Config
* @subpackage Processor
*/
class Filter implements ProcessorInterface
{
/**
Expand Down
6 changes: 0 additions & 6 deletions src/Processor/ProcessorInterface.php
Expand Up @@ -5,18 +5,12 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config\Processor;

use Zend\Config\Config;

/**
* @category Zend
* @package Zend_Config
* @subpackage Processor
*/
interface ProcessorInterface
{
/**
Expand Down
6 changes: 0 additions & 6 deletions src/Processor/Queue.php
Expand Up @@ -5,7 +5,6 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config\Processor;
Expand All @@ -14,11 +13,6 @@
use Zend\Config\Exception;
use Zend\Stdlib\PriorityQueue;

/**
* @category Zend
* @package Zend_Config
* @subpackage Processor
*/
class Queue extends PriorityQueue implements ProcessorInterface
{
/**
Expand Down
6 changes: 0 additions & 6 deletions src/Processor/Token.php
Expand Up @@ -5,7 +5,6 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config\Processor;
Expand All @@ -14,11 +13,6 @@
use Zend\Config\Config;
use Zend\Config\Exception;

/**
* @category Zend
* @package Zend_Config
* @subpackage Processor
*/
class Token implements ProcessorInterface
{
/**
Expand Down
7 changes: 0 additions & 7 deletions src/Processor/Translator.php
Expand Up @@ -5,7 +5,6 @@
* @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
* @package Zend_Config
*/

namespace Zend\Config\Processor;
Expand All @@ -14,11 +13,6 @@
use Zend\Config\Exception;
use Zend\I18n\Translator\Translator as ZendTranslator;

/**
* @category Zend
* @package Zend_Config
* @subpackage Processor
*/
class Translator implements ProcessorInterface
{
/**
Expand Down Expand Up @@ -142,5 +136,4 @@ public function processValue($value)
{
return $this->translator->translate($value, $this->textDomain, $this->locale);
}

}

0 comments on commit 7e73be1

Please sign in to comment.