prefill.php ➔ read_from_console()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 2 and the first side effect is on line 6.

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.

Loading history...
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
    ':vendor\\\\:package_name\\\\' => function () use (&$values) {
24
        return str_replace('\\', '\\\\', $values['psr4_namespace']) . '\\\\';
25
    },
26
    ':author_name'                 => function () use (&$values) {
27
        return $values['author_name'];
28
    },
29
    ':author_username'             => function () use (&$values) {
30
        return $values['author_github_username'];
31
    },
32
    ':author_website'              => function () use (&$values) {
33
        return $values['author_website'] ?: ('https://github.com/' . $values['author_github_username']);
34
    },
35
    ':author_email'                => function () use (&$values) {
36
        return $values['author_email'] ?: ($values['author_github_username'] . '@example.com');
37
    },
38
    ':vendor'                      => function () use (&$values) {
39
        return $values['package_vendor'];
40
    },
41
    ':package_name'                => function () use (&$values) {
42
        return $values['package_name'];
43
    },
44
    ':package_description'         => function () use (&$values) {
45
        return $values['package_description'];
46
    },
47
    'League\\Skeleton'             => function () use (&$values) {
48
        return $values['psr4_namespace'];
49
    },
50
];
51
52
function read_from_console($prompt)
53
{
54
    if (function_exists('readline')) {
55
        $line = trim(readline($prompt));
56
        if (!empty($line)) {
57
            readline_add_history($line);
58
        }
59
    } else {
60
        echo $prompt;
61
        $line = trim(fgets(STDIN));
62
    }
63
    return $line;
64
}
65
66
function interpolate($text, $values)
67
{
68
    if (!preg_match_all('/\{(\w+)\}/', $text, $m)) {
69
        return $text;
70
    }
71
    foreach ($m[0] as $k => $str) {
72
        $f = $m[1][$k];
73
        $text = str_replace($str, $values[$f], $text);
74
    }
75
    return $text;
76
}
77
78
$modify = 'n';
79
do {
80
    if ($modify == 'q') {
81
        exit;
82
    }
83
84
    $values = [];
85
86
    echo "----------------------------------------------------------------------\n";
87
    echo "Please, provide the following information:\n";
88
    echo "----------------------------------------------------------------------\n";
89
    foreach ($fields as $f => $field) {
90
        $default = isset($field[COL_DEFAULT]) ? interpolate($field[COL_DEFAULT], $values): '';
91
        $prompt = sprintf(
92
            '%s%s%s: ',
93
            $field[COL_DESCRIPTION],
94
            $field[COL_HELP] ? ' (' . $field[COL_HELP] . ')': '',
95
            $field[COL_DEFAULT] !== '' ? ' [' . $default . ']': ''
96
        );
97
        $values[$f] = read_from_console($prompt);
98
        if (empty($values[$f])) {
99
            $values[$f] = $default;
100
        }
101
    }
102
    echo "\n";
103
104
    echo "----------------------------------------------------------------------\n";
105
    echo "Please, check that everything is correct:\n";
106
    echo "----------------------------------------------------------------------\n";
107
    foreach ($fields as $f => $field) {
108
        echo $field[COL_DESCRIPTION] . ": $values[$f]\n";
109
    }
110
    echo "\n";
111
} while (($modify = strtolower(read_from_console('Modify files with these values? [y/N/q] '))) != 'y');
112
echo "\n";
113
114
$files = array_merge(
115
    glob(__DIR__ . '/*.md'),
116
    glob(__DIR__ . '/*.xml.dist'),
117
    glob(__DIR__ . '/composer.json'),
118
    glob(__DIR__ . '/src/*.php'),
119
    glob(__DIR__ . '/tests/*.php')
120
);
121
foreach ($files as $f) {
122
    $contents = file_get_contents($f);
123
    foreach ($replacements as $str => $func) {
124
        $contents = str_replace($str, $func(), $contents);
125
    }
126
    file_put_contents($f, $contents);
127
}
128
129
echo "Done.\n";
130
echo "Now you should remove the file '" . basename(__FILE__) . "'.\n";
131