@@ -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}
0 commit comments