|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Sergey Bondarenko, <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Drupal\TqExtension\Utils; |
|
6
|
|
|
|
|
7
|
|
|
// Helpers. |
|
8
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
9
|
|
|
|
|
10
|
|
|
trait Interaction |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string[] |
|
14
|
|
|
*/ |
|
15
|
|
|
private static $tags = []; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ConsoleOutput |
|
18
|
|
|
*/ |
|
19
|
|
|
private static $consoleOutput; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param array $strings |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function debug(array $strings) |
|
25
|
|
|
{ |
|
26
|
|
|
if (self::hasTag('debug')) { |
|
27
|
|
|
array_unshift($strings, '<question>DEBUG:</question>'); |
|
28
|
|
|
call_user_func_array(['self', 'consoleOutput'], array_merge( |
|
29
|
|
|
['comment', 4, $strings], |
|
30
|
|
|
array_slice(func_get_args(), 1)) |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $type |
|
37
|
|
|
* Could be "comment", "info", "question" or "error". |
|
38
|
|
|
* @param int $indent |
|
39
|
|
|
* Number of spaces. |
|
40
|
|
|
* @param array $strings |
|
41
|
|
|
* Paragraphs. |
|
42
|
|
|
* @param string ... |
|
43
|
|
|
* Any replacement argument for "sprintf()". |
|
44
|
|
|
* |
|
45
|
|
|
* @link http://symfony.com/doc/current/components/console/introduction.html#coloring-the-output |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function consoleOutput($type, $indent, array $strings) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!isset(self::$consoleOutput)) { |
|
50
|
|
|
self::$consoleOutput = new ConsoleOutput; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$indent = implode(' ', array_fill_keys(range(0, $indent), '')); |
|
54
|
|
|
$arguments = func_get_args(); |
|
55
|
|
|
// Remove the "indent" and "strings" parameters from an array with arguments. |
|
56
|
|
|
unset($arguments[1], $arguments[2]); |
|
57
|
|
|
|
|
58
|
|
|
// Replace the "type" argument by message that will be printed. |
|
59
|
|
|
$arguments[0] = "$indent<$type>" . implode(PHP_EOL . "</$type>$indent<$type>", $strings) . "</$type>"; |
|
60
|
|
|
|
|
61
|
|
|
self::$consoleOutput->writeln(call_user_func_array('sprintf', $arguments)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $tag |
|
66
|
|
|
* The name of tag. |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
* Indicates the state of tag existence in a feature and/or scenario. |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function hasTag($tag) |
|
72
|
|
|
{ |
|
73
|
|
|
return isset(self::$tags[$tag]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $tag |
|
78
|
|
|
* The name of tag. |
|
79
|
|
|
* @param string $default |
|
80
|
|
|
* Default value, if tag does not exist or empty. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
* Tag value or an empty string. |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function getTag($tag, $default = '') |
|
86
|
|
|
{ |
|
87
|
|
|
return empty(self::$tags[$tag]) ? $default : self::$tags[$tag]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string[] $tags |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function collectTags(array $tags) |
|
94
|
|
|
{ |
|
95
|
|
|
foreach ($tags as $tag) { |
|
96
|
|
|
$values = explode(':', $tag); |
|
97
|
|
|
$value = ''; |
|
98
|
|
|
|
|
99
|
|
|
if (count($values) > 1) { |
|
100
|
|
|
list($tag, $value) = $values; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
self::$tags[strtolower($tag)] = $value; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|