Thanks to visit codestin.com
Credit goes to github.com

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

Commit 4a506c1

Browse files
committed
Merge branch 'feature/config-writer-manager' into develop
Close #2704
2 parents 9253e52 + 044fba5 commit 4a506c1

5 files changed

Lines changed: 280 additions & 2 deletions

File tree

library/Zend/Config/Factory.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class Factory
2525
*/
2626
public static $readers = null;
2727

28+
/**
29+
* Plugin manager for loading writers
30+
*
31+
* @var null|WriterPluginManager
32+
*/
33+
public static $writers = null;
34+
2835
/**
2936
* Registered config file extensions.
3037
* key is extension, value is reader instance or plugin name
@@ -38,6 +45,19 @@ class Factory
3845
'yaml' => 'yaml',
3946
);
4047

48+
/**
49+
* Register config file extensions for writing
50+
* key is extension, value is writer instance or plugin name
51+
*
52+
* @var array
53+
*/
54+
protected static $writerExtensions = array(
55+
'php' => 'php',
56+
'ini' => 'ini',
57+
'json' => 'json',
58+
'xml' => 'xml',
59+
'yaml' => 'yaml',
60+
);
4161

4262
/**
4363
* Read a config from a file.
@@ -107,10 +127,67 @@ public static function fromFiles(array $files, $returnConfigObject = false)
107127
return ($returnConfigObject) ? new Config($config) : $config;
108128
}
109129

130+
/**
131+
* Writes a config to a file
132+
*
133+
* @param string $filename
134+
* @param array|Config $config
135+
* @return boolean TRUE on success | FALSE on failure
136+
* @throws Exception\RuntimeException
137+
* @throws Exception\InvalidArgumentException
138+
*/
139+
public static function toFile($filename, $config)
140+
{
141+
if (
142+
(is_object($config) && !($config instanceOf Config)) ||
143+
(!is_object($config) && !is_array($config))
144+
) {
145+
throw new Exception\InvalidArgumentException(
146+
__METHOD__." \$config should be an array or instance of Zend\\Config\\Config"
147+
);
148+
}
149+
150+
$extension = substr(strrchr($filename, '.'), 1);
151+
$directory = dirname($filename);
152+
153+
if (!is_dir($directory)) {
154+
throw new Exception\RuntimeException(
155+
"Directory '{$directory}' does not exists!"
156+
);
157+
}
158+
159+
if (!is_writable($directory)) {
160+
throw new Exception\RuntimeException(
161+
"Cannot write in directory '{$directory}'"
162+
);
163+
}
164+
165+
if(!isset(self::$writerExtensions[$extension])) {
166+
throw new Exception\RuntimeException(
167+
"Unsupported config file extension: '.{$extension}' for writing."
168+
);
169+
}
170+
171+
$writer = self::$writerExtensions[$extension];
172+
if (($writer instanceOf Writer\AbstractWriter) === false) {
173+
$writer = self::getWriterPluginManager()->get($writer);
174+
self::$writerExtensions[$extension] = $writer;
175+
}
176+
177+
if (is_object($config)) {
178+
$config = $config->toArray();
179+
}
180+
181+
$content = $writer->processConfig($config);
182+
183+
return (bool) (file_put_contents($filename, $content) !== false);
184+
}
185+
110186
/**
111187
* Set reader plugin manager
112188
*
113189
* @param ReaderPluginManager $readers
190+
* @return void
114191
*/
115192
public static function setReaderPluginManager(ReaderPluginManager $readers)
116193
{
@@ -130,12 +207,38 @@ public static function getReaderPluginManager()
130207
return static::$readers;
131208
}
132209

210+
/**
211+
* Set writer plugin manager
212+
*
213+
* @param WriterPluginManager $writers
214+
* @return void
215+
*/
216+
public static function setWriterPluginManager(WriterPluginManager $writers)
217+
{
218+
self::$writers = $writers;
219+
}
220+
221+
/**
222+
* Get the writer plugin manager
223+
*
224+
* @return WriterPluginManager
225+
*/
226+
public static function getWriterPluginManager()
227+
{
228+
if (static::$writers === null) {
229+
static::$writers = new WriterPluginManager();
230+
}
231+
232+
return static::$writers;
233+
}
234+
133235
/**
134236
* Set config reader for file extension
135237
*
136238
* @param string $extension
137239
* @param string|Reader\ReaderInterface $reader
138240
* @throws Exception\InvalidArgumentException
241+
* @return void
139242
*/
140243
public static function registerReader($extension, $reader)
141244
{
@@ -152,4 +255,28 @@ public static function registerReader($extension, $reader)
152255

153256
self::$extensions[$extension] = $reader;
154257
}
258+
259+
/**
260+
* Set config writer for file extension
261+
*
262+
* @param string $extension
263+
* @param string|Writer\AbstractWriter $writer
264+
* @throw Exception\InvalidArgumentException
265+
* @return void
266+
*/
267+
public static function registerWriter($extension, $writer)
268+
{
269+
$extension = strtolower($extension);
270+
271+
if (!is_string($writer) && !$writer instanceof Writer\AbstractWriter) {
272+
throw new Exception\InvalidArgumentException(sprintf(
273+
'Writer should be plugin name, class name or ' .
274+
'instance of %s\Writer\AbstractWriter; received "%s"',
275+
__NAMESPACE__,
276+
(is_object($writer) ? get_class($writer) : gettype($writer))
277+
));
278+
}
279+
280+
self::$writerExtensions[$extension] = $writer;
281+
}
155282
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Zend\Config;
3+
4+
use Zend\ServiceManager\AbstractPluginManager;
5+
6+
class WriterPluginManager extends AbstractPluginManager
7+
{
8+
protected $invokableClasses = array(
9+
'php' => 'Zend\Config\Writer\PhpArray',
10+
'ini' => 'Zend\Config\Writer\Ini',
11+
'json' => 'Zend\Config\Writer\Json',
12+
'yaml' => 'Zend\Config\Writer\Yaml',
13+
'xml' => 'Zend\Config\Writer\Xml',
14+
);
15+
16+
public function validatePlugin($plugin)
17+
{
18+
if ($plugin instanceOf Writer\AbstractWriter) {
19+
return;
20+
}
21+
22+
$type = is_object($plugin) ? get_class($plugin) : gettype($plugin);
23+
24+
throw new Exception\InvalidArgumentException(
25+
"Plugin of type {$type} is invalid. Plugin must extend ".
26+
__NAMESPACE__.'\Writer\AbstractWriter'
27+
);
28+
}
29+
}

library/Zend/Db/Sql/AbstractSql.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ abstract class AbstractSql
2727
*/
2828
protected $processInfo = array('paramPrefix' => '', 'subselectCount' => 0);
2929

30+
/**
31+
* @var array
32+
*/
33+
protected $instanceParameterIndex = array();
34+
3035
protected function processExpression(ExpressionInterface $expression, PlatformInterface $platform, Adapter $adapter = null, $namedParameterPrefix = null)
3136
{
3237
// static counter for the number of times this method was invoked across the PHP runtime
@@ -42,7 +47,12 @@ protected function processExpression(ExpressionInterface $expression, PlatformIn
4247

4348
// initialize variables
4449
$parts = $expression->getExpressionData();
45-
$expressionParamIndex = 1;
50+
51+
if(!isset($this->instanceParameterIndex[$namedParameterPrefix])) {
52+
$this->instanceParameterIndex[$namedParameterPrefix] = 1;
53+
}
54+
55+
$expressionParamIndex = &$this->instanceParameterIndex[$namedParameterPrefix];
4656

4757
foreach ($parts as $part) {
4858

tests/ZendTest/Config/FactoryTest.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@
2020
*/
2121
class FactoryTest extends \PHPUnit_Framework_TestCase
2222
{
23+
protected $tmpFiles = array();
24+
25+
protected function getTestAssetFileName($ext)
26+
{
27+
if (empty($this->tmpfiles[$ext])) {
28+
$this->tmpfiles[$ext] = tempnam(sys_get_temp_dir(), 'zend-config-writer').'.'.$ext;
29+
}
30+
return $this->tmpfiles[$ext];
31+
}
32+
33+
public function tearDown()
34+
{
35+
foreach($this->tmpFiles as $file) {
36+
if (file_exists($file)) {
37+
if (!is_writable($file)) {
38+
chmod($file, 0777);
39+
}
40+
@unlink($file);
41+
}
42+
}
43+
}
44+
2345
public function testFromIni()
2446
{
2547
$config = Factory::fromFile(__DIR__ . '/TestAssets/Ini/include-base.ini');
@@ -125,7 +147,7 @@ public function testFactoryCanRegisterCustomReaderInstance()
125147
$this->assertEquals($configObject['one'], 1);
126148
}
127149

128-
public function testFactoryCanRegisterCustomReaderPlugn()
150+
public function testFactoryCanRegisterCustomReaderPlugin()
129151
{
130152
$dummyReader = new Reader\TestAssets\DummyReader();
131153
Factory::getReaderPluginManager()->setService('DummyReader', $dummyReader);
@@ -138,5 +160,73 @@ public function testFactoryCanRegisterCustomReaderPlugn()
138160
$this->assertEquals($configObject['one'], 1);
139161
}
140162

163+
public function testFactoryToFileInvalidFileExtension()
164+
{
165+
$this->setExpectedException('RuntimeException');
166+
$result = Factory::toFile(__DIR__.'/TestAssets/bad.ext', array());
167+
}
168+
169+
public function testFactoryToFileNoDirInHere()
170+
{
171+
$this->setExpectedException('RuntimeException');
172+
$result = Factory::toFile(__DIR__.'/TestAssets/NoDirInHere/nonExisiting/dummy.php', array());
173+
}
174+
175+
public function testFactoryWriteToFile()
176+
{
177+
$config = array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo'));
178+
179+
$file = $this->getTestAssetFileName('php');
180+
$result = Factory::toFile($file, $config);
181+
182+
// build string line by line as we are trailing-whitespace sensitive.
183+
$expected = "<?php\n";
184+
$expected .= "return array (\n";
185+
$expected .= " 'test' => 'foo',\n";
186+
$expected .= " 'bar' => \n";
187+
$expected .= " array (\n";
188+
$expected .= " 0 => 'baz',\n";
189+
$expected .= " 1 => 'foo',\n";
190+
$expected .= " ),\n";
191+
$expected .= ");\n";
192+
193+
$this->assertEquals(true, $result);
194+
$this->assertEquals($expected, file_get_contents($file));
195+
}
196+
197+
public function testFactoryToFileWrongConfig()
198+
{
199+
$this->setExpectedException('InvalidArgumentException');
200+
$result = Factory::toFile('test.ini', 'Im wrong');
201+
}
202+
203+
public function testFactoryRegisterInvalidWriter()
204+
{
205+
$this->setExpectedException('InvalidArgumentException');
206+
Factory::registerWriter('dum', new Reader\TestAssets\DummyReader());
207+
}
208+
209+
public function testFactoryCanRegisterCustomWriterInstance()
210+
{
211+
Factory::registerWriter('dum', new Writer\TestAssets\DummyWriter());
212+
213+
$file = $this->getTestAssetFileName('dum');
141214

215+
$res = Factory::toFile($file, array('one' => 1));
216+
217+
$this->assertEquals($res, true);
218+
}
219+
220+
public function testFactoryCanRegisterCustomWriterPlugin()
221+
{
222+
$dummyWriter = new Writer\TestAssets\DummyWriter();
223+
Factory::getWriterPluginManager()->setService('DummyWriter', $dummyWriter);
224+
225+
Factory::registerWriter('dum', 'DummyWriter');
226+
227+
$file = $this->getTestAssetFileName('dum');
228+
229+
$res = Factory::toFile($file, array('one' => 1));
230+
$this->assertEquals($res, true);
231+
}
142232
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
* @package Zend_Config
9+
*/
10+
11+
namespace ZendTest\Config\Writer\TestAssets;
12+
13+
use Zend\Config\Writer\AbstractWriter;
14+
use Zend\Config\Exception;
15+
16+
class DummyWriter extends AbstractWriter
17+
{
18+
public function processConfig(array $config)
19+
{
20+
return serialize($config);
21+
}
22+
}

0 commit comments

Comments
 (0)