Use StaticArrayWriter in LCStoreStaticArray
[lhc/web/wiklou.git] / includes / cache / localisation / LCStoreStaticArray.php
index 1e20082..3b6da73 100644 (file)
@@ -20,6 +20,8 @@
  * @file
  */
 
+use Wikimedia\StaticArrayWriter;
+
 /**
  * @since 1.26
  */
@@ -47,6 +49,13 @@ class LCStoreStaticArray implements LCStore {
        }
 
        public function startWrite( $code ) {
+               if ( !file_exists( $this->directory ) ) {
+                       if ( !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
+                               throw new MWException( "Unable to create the localisation store " .
+                                       "directory \"{$this->directory}\"" );
+                       }
+               }
+
                $this->currentLang = $code;
                $this->fname = $this->directory . '/' . $code . '.l10n.php';
                $this->data[$code] = [];
@@ -77,9 +86,7 @@ class LCStoreStaticArray implements LCStore {
                }
                if ( is_array( $value ) ) {
                        // [A]rray
-                       return [ 'a', array_map( function ( $v ) {
-                               return LCStoreStaticArray::encode( $v );
-                       }, $value ) ];
+                       return [ 'a', array_map( 'LCStoreStaticArray::encode', $value ) ];
                }
 
                throw new RuntimeException( 'Cannot encode ' . var_export( $value, true ) );
@@ -97,28 +104,25 @@ class LCStoreStaticArray implements LCStore {
                $data = $encoded[1];
 
                switch ( $type ) {
-               case 'v':
-                       return $data;
-               case 's':
-                       return unserialize( $data );
-               case 'a':
-                       return array_map( function ( $v ) {
-                               return LCStoreStaticArray::decode( $v );
-                       }, $data );
-               default:
-                       throw new RuntimeException(
-                               'Unable to decode ' . var_export( $encoded, true ) );
+                       case 'v':
+                               return $data;
+                       case 's':
+                               return unserialize( $data );
+                       case 'a':
+                               return array_map( 'LCStoreStaticArray::decode', $data );
+                       default:
+                               throw new RuntimeException(
+                                       'Unable to decode ' . var_export( $encoded, true ) );
                }
        }
 
        public function finishWrite() {
-               file_put_contents(
-                       $this->fname,
-                       "<?php\n" .
-                       "// Generated by LCStoreStaticArray.php -- do not edit!\n" .
-                       "return " .
-                       var_export( $this->data[$this->currentLang], true ) . ';'
+               $writer = new StaticArrayWriter();
+               $out = $writer->create(
+                       $this->data[$this->currentLang],
+                       'Generated by LCStoreStaticArray.php -- do not edit!'
                );
+               file_put_contents( $this->fname, $out );
                $this->currentLang = null;
                $this->fname = null;
        }