-
Notifications
You must be signed in to change notification settings - Fork 554
/
HandlesStubs.php
62 lines (52 loc) · 1.59 KB
/
HandlesStubs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace A17\Twill\Commands\Traits;
trait HandlesStubs
{
/**
* @param array $variables
* @param string $stub
* @param array|null $delimiters
* @return string
*/
public function replaceVariables($variables, $stub, $delimiters = null)
{
$delimiters = $delimiters ?: ['{{', '}}'];
foreach ($variables as $key => $value) {
$key = "{$delimiters[0]}{$key}{$delimiters[1]}";
$stub = str_replace($key, $value, $stub);
}
return $stub;
}
/**
* @param array $variables
* @param string $stub
* @param array|null $delimiters
* @return string
*/
public function replaceConditionals($conditionals, $stub, $delimiters = null)
{
$delimiters = $delimiters ?: ['{{', '}}'];
foreach ($conditionals as $key => $value) {
$start = "{$delimiters[0]}{$key}{$delimiters[1]}";
$end = "{$delimiters[0]}\/{$key}{$delimiters[1]}";
if ((bool)$value) {
// replace delimiters only
$stub = preg_replace("/$start/", '', $stub);
$stub = preg_replace("/$end/", '', $stub);
} else {
// replace delimiters and everything between
$anything = '[\s\S]+?';
$stub = preg_replace("/{$start}{$anything}{$end}/", '', $stub);
}
}
return $stub;
}
/**
* @param string $stub
* @return string
*/
public function removeEmptyLinesWithOnlySpaces($stub)
{
return preg_replace('#^ +\n#m', '', $stub);
}
}