1 | <?php |
||
14 | class ContextGenerator |
||
15 | { |
||
16 | /** |
||
17 | * Labels and flags that may be used when defining keywords. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | public static $LABELS_FLAGS = array( |
||
22 | '(R)' => 2, // reserved |
||
23 | '(D)' => 8, // data type |
||
24 | '(K)' => 16, // keyword |
||
25 | '(F)' => 32, // function name |
||
26 | ); |
||
27 | |||
28 | /** |
||
29 | * Documentation links for each context. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | public static $LINKS = array( |
||
34 | 'MySql50000' => 'https://dev.mysql.com/doc/refman/5.0/en/keywords.html', |
||
35 | 'MySql50100' => 'https://dev.mysql.com/doc/refman/5.1/en/keywords.html', |
||
36 | 'MySql50500' => 'https://dev.mysql.com/doc/refman/5.5/en/keywords.html', |
||
37 | 'MySql50600' => 'https://dev.mysql.com/doc/refman/5.6/en/keywords.html', |
||
38 | 'MySql50700' => 'https://dev.mysql.com/doc/refman/5.7/en/keywords.html', |
||
39 | 'MySql80000' => 'https://dev.mysql.com/doc/refman/8.0/en/keywords.html', |
||
40 | 'MariaDb100000' => 'https://mariadb.com/kb/en/the-mariadb-library/reserved-words/', |
||
41 | 'MariaDb100100' => 'https://mariadb.com/kb/en/the-mariadb-library/reserved-words/', |
||
42 | 'MariaDb100200' => 'https://mariadb.com/kb/en/the-mariadb-library/reserved-words/', |
||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * The template of a context. |
||
47 | * |
||
48 | * Parameters: |
||
49 | * 1 - name |
||
50 | * 2 - class |
||
51 | * 3 - link |
||
52 | * 4 - keywords array |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | const TEMPLATE = |
||
57 | '<?php' . "\n" . |
||
58 | '' . "\n" . |
||
59 | '/**' . "\n" . |
||
60 | ' * Context for %1$s.' . "\n" . |
||
61 | ' *' . "\n" . |
||
62 | ' * This file was auto-generated.' . "\n" . |
||
63 | ' *' . "\n" . |
||
64 | ' * @see %3$s' . "\n" . |
||
65 | ' */' . "\n" . |
||
66 | '' . "\n" . |
||
67 | 'namespace PhpMyAdmin\\SqlParser\\Contexts;' . "\n" . |
||
68 | '' . "\n" . |
||
69 | 'use PhpMyAdmin\\SqlParser\\Context;' . "\n" . |
||
70 | '' . "\n" . |
||
71 | '/**' . "\n" . |
||
72 | ' * Context for %1$s.' . "\n" . |
||
73 | ' *' . "\n" . |
||
74 | ' * @category Contexts' . "\n" . |
||
75 | ' *' . "\n" . |
||
76 | ' * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+' . "\n" . |
||
77 | ' */' . "\n" . |
||
78 | 'class %2$s extends Context' . "\n" . |
||
79 | '{' . "\n" . |
||
80 | ' /**' . "\n" . |
||
81 | ' * List of keywords.' . "\n" . |
||
82 | ' *' . "\n" . |
||
83 | ' * The value associated to each keyword represents its flags.' . "\n" . |
||
84 | ' *' . "\n" . |
||
85 | ' * @see Token::FLAG_KEYWORD_*' . "\n" . |
||
86 | ' *' . "\n" . |
||
87 | ' * @var array' . "\n" . |
||
88 | ' */' . "\n" . |
||
89 | ' public static $KEYWORDS = array(' . "\n" . |
||
90 | '%4$s' . |
||
91 | ' );' . "\n" . |
||
92 | '}' . "\n"; |
||
93 | |||
94 | /** |
||
95 | * Sorts an array of words. |
||
96 | * |
||
97 | * @param array $arr |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | public static function sortWords(array &$arr) |
||
113 | |||
114 | /** |
||
115 | * Reads a list of words and sorts it by type, length and keyword. |
||
116 | * |
||
117 | * @param string[] $files |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | public static function readWords(array $files) |
||
175 | |||
176 | /** |
||
177 | * Prints an array of a words in PHP format. |
||
178 | * |
||
179 | * @param array $words the list of words to be formatted |
||
180 | * @param int $spaces the number of spaces that starts every line |
||
181 | * @param int $line the length of a line |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | public static function printWords($words, $spaces = 8, $line = 80) |
||
220 | |||
221 | /** |
||
222 | * Generates a context's class. |
||
223 | * |
||
224 | * @param array $options the options that are used in generating this context |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public static function generate($options) |
||
242 | |||
243 | /** |
||
244 | * Formats context name. |
||
245 | * |
||
246 | * @param string $name Name to format. |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | public static function formatName($name) |
||
282 | |||
283 | /** |
||
284 | * Builds a test. |
||
285 | * |
||
286 | * Reads the input file, generates the data and writes it back. |
||
287 | * |
||
288 | * @param string $input the input file |
||
289 | * @param string $output the output directory |
||
290 | */ |
||
291 | public static function build($input, $output) |
||
348 | |||
349 | /** |
||
350 | * Generates recursively all tests preserving the directory structure. |
||
351 | * |
||
352 | * @param string $input the input directory |
||
353 | * @param string $output the output directory |
||
354 | */ |
||
355 | public static function buildAll($input, $output) |
||
370 | } |
||
371 | |||
397 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.