MediaWiki master
StaticArrayWriter.php
Go to the documentation of this file.
1<?php
19namespace Wikimedia;
20
33 public function create( array $data, $header = 'Automatically generated' ) {
34 return self::write( $data, $header );
35 }
36
45 public static function write( array $data, $header ) {
46 $code = "<?php\n"
47 . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
48 . "return " . self::encodeArray( $data ) . ";\n";
49 return $code;
50 }
51
63 public static function writeClass( array $data, array $layout ) {
64 $code = "<?php\n"
65 . "// " . implode( "\n// ", explode( "\n", $layout['header'] ) ) . "\n"
66 . "\n"
67 . "namespace {$layout['namespace']};\n"
68 . "\n"
69 . "class {$layout['class']} {\n"
70 . "\tpublic const {$layout['const']} = " . self::encodeArray( $data, "\t\t" ) . ";\n}\n";
71 return $code;
72 }
73
81 private static function encodeArray( array $array, string $tabs = "\t" ): string {
82 $code = "[\n";
83 if ( array_is_list( $array ) ) {
84 foreach ( $array as $value ) {
85 $code .= $tabs . self::encodeValue( $value, $tabs ) . ",\n";
86 }
87 } else {
88 foreach ( $array as $key => $value ) {
89 $code .= $tabs . var_export( $key, true ) . ' => ' .
90 self::encodeValue( $value, $tabs ) . ",\n";
91 }
92 }
93 return $code . substr( $tabs, 0, -1 ) . ']';
94 }
95
103 private static function encodeValue( $value, string $tabs ): string {
104 if ( is_array( $value ) ) {
105 return self::encodeArray( $value, $tabs . "\t" );
106 }
107
108 // var_export() exports nulls as uppercase NULL which
109 // violates our own coding standards.
110 return $value === null ? 'null' : var_export( $value, true );
111 }
112}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Format a static PHP array to be written to a file.
create(array $data, $header='Automatically generated')
static write(array $data, $header)
Create a PHP file that returns the array.
static writeClass(array $data, array $layout)
Create an PHP class file with the array as a class constant.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
$header