1 | <?php |
||
2 | define('COL_DESCRIPTION', 0); |
||
3 | define('COL_HELP', 1); |
||
4 | define('COL_DEFAULT', 2); |
||
5 | |||
6 | $fields = [ |
||
7 | 'author_name' => ['Your name', '', ''], |
||
8 | 'author_github_username' => ['Your GitHub username', '<username> in https://github.com/username', ''], |
||
9 | 'author_email' => ['Your email address', '', ''], |
||
10 | 'author_twitter' => ['Your twitter username', '', '@{author_github_username}'], |
||
11 | 'author_website' => ['Your website', '', 'https://github.com/{author_github_username}'], |
||
12 | |||
13 | 'package_vendor' => ['Package vendor', '<vendor> in https://github.com/vendor/package', '{author_github_username}'], |
||
14 | 'package_name' => ['Package name', '<package> in https://github.com/vendor/package', ''], |
||
15 | 'package_description' => ['Package very short description', '', ''], |
||
16 | |||
17 | 'psr4_namespace' => ['PSR-4 namespace', 'usually, Vendor\\Package', '{package_vendor}\\{package_name}'], |
||
18 | ]; |
||
19 | |||
20 | $values = []; |
||
21 | |||
22 | $replacements = [ |
||
23 | 'teabugger\\\\Enum\\\\' => function () use(&$values) { return str_replace('\\', '\\\\', $values['psr4_namespace']) . '\\\\'; }, |
||
24 | 'Maksim Kolesnik' => function () use(&$values) { return $values['author_name']; }, |
||
25 | 'teabugger' => function () use(&$values) { return $values['author_github_username']; }, |
||
26 | 'https://github.com/teabugger' => function () use(&$values) { return $values['author_website'] ?: ('https://github.com/' . $values['author_github_username']); }, |
||
27 | '[email protected]' => function () use(&$values) { return $values['author_email'] ?: ($values['author_github_username'] . '@example.com'); }, |
||
28 | 'teabugger' => function () use(&$values) { return $values['package_vendor']; }, |
||
29 | 'Enum' => function () use(&$values) { return $values['package_name']; }, |
||
30 | 'PHP ENUM implementation' => function () use(&$values) { return $values['package_description']; }, |
||
31 | 'League\\Skeleton' => function () use(&$values) { return $values['psr4_namespace']; }, |
||
32 | ]; |
||
33 | |||
34 | function read_from_console ($prompt) { |
||
35 | if ( function_exists('readline') ) { |
||
36 | $line = trim(readline($prompt)); |
||
37 | if (!empty($line)) { |
||
38 | readline_add_history($line); |
||
39 | } |
||
40 | } else { |
||
41 | echo $prompt; |
||
42 | $line = trim(fgets(STDIN)); |
||
43 | } |
||
44 | return $line; |
||
45 | } |
||
46 | |||
47 | function interpolate($text, $values) |
||
48 | { |
||
49 | if (!preg_match_all('/\{(\w+)\}/', $text, $m)) { |
||
50 | return $text; |
||
51 | } |
||
52 | foreach ($m[0] as $k => $str) { |
||
53 | $f = $m[1][$k]; |
||
54 | $text = str_replace($str, $values[$f], $text); |
||
55 | } |
||
56 | return $text; |
||
57 | } |
||
58 | |||
59 | $modify = 'n'; |
||
60 | do { |
||
61 | if ($modify == 'q') { |
||
62 | exit; |
||
63 | } |
||
64 | |||
65 | $values = []; |
||
66 | |||
67 | echo "----------------------------------------------------------------------\n"; |
||
68 | echo "Please, provide the following information:\n"; |
||
69 | echo "----------------------------------------------------------------------\n"; |
||
70 | foreach ($fields as $f => $field) { |
||
71 | $default = isset($field[COL_DEFAULT]) ? interpolate($field[COL_DEFAULT], $values): ''; |
||
72 | $prompt = sprintf( |
||
73 | '%s%s%s: ', |
||
74 | $field[COL_DESCRIPTION], |
||
75 | $field[COL_HELP] ? ' (' . $field[COL_HELP] . ')': '', |
||
76 | $field[COL_DEFAULT] !== '' ? ' [' . $default . ']': '' |
||
77 | ); |
||
78 | $values[$f] = read_from_console($prompt); |
||
79 | if (empty($values[$f])) { |
||
80 | $values[$f] = $default; |
||
81 | } |
||
82 | } |
||
83 | echo "\n"; |
||
84 | |||
85 | echo "----------------------------------------------------------------------\n"; |
||
86 | echo "Please, check that everything is correct:\n"; |
||
87 | echo "----------------------------------------------------------------------\n"; |
||
88 | foreach ($fields as $f => $field) { |
||
89 | echo $field[COL_DESCRIPTION] . ": $values[$f]\n"; |
||
90 | } |
||
91 | echo "\n"; |
||
92 | } while (($modify = strtolower(read_from_console('Modify files with these values? [y/N/q] '))) != 'y'); |
||
93 | echo "\n"; |
||
94 | |||
95 | $filterPaths = [ |
||
96 | [ |
||
97 | __DIR__ . '/*.md', |
||
98 | __DIR__ . '/*.xml.dist', |
||
99 | __DIR__ . '/composer.json', |
||
100 | __DIR__ . '/src/*.php', |
||
101 | __DIR__ . '/tests/*.php', |
||
102 | ] |
||
103 | ]; |
||
104 | $filters = []; |
||
105 | |||
106 | foreach ($filterPaths as $filterPath) { |
||
107 | $path = glob($filterPath); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
108 | if ($path !== false) { |
||
109 | $filters[] = $path; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | $filters = array_merge(...$filters); |
||
114 | |||
115 | foreach ($files as $f) { |
||
116 | $contents = file_get_contents($f); |
||
117 | foreach ($replacements as $str => $func) { |
||
118 | $contents = str_replace($str, $func(), $contents); |
||
119 | } |
||
120 | file_put_contents($f, $contents); |
||
121 | } |
||
122 | |||
123 | echo "Done.\n"; |
||
124 | echo "Now you should remove the file '" . basename(__FILE__) . "'.\n"; |
||
125 |