1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ballen\Linguist; |
4
|
|
|
|
5
|
|
|
use Ballen\Linguist\Configuration; |
6
|
|
|
use Ballen\Linguist\Transformers\PlaintextTransformer; |
7
|
|
|
use Ballen\Linguist\Transformers\HtmlTransformer; |
8
|
|
|
use Ballen\Linguist\Transformers\MarkdownTansformer; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Linguist |
12
|
|
|
* |
13
|
|
|
* Linguist is a PHP library for parsing strings, it can extract and manipulate |
14
|
|
|
* prefixed words in content ideal for working with @mentions, #topics and |
15
|
|
|
* even custom action tags! |
16
|
|
|
* |
17
|
|
|
* @author Bobby Allen <[email protected]> |
18
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html |
19
|
|
|
* @link https://github.com/allebb/linguist |
20
|
|
|
* @link http://www.bobbyallen.me |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
class Parser |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Runtime configuration object storeage. |
28
|
|
|
* @var \Ballen\Linguist\Entities\Configuration |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
private $configuration; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The original message text. |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $message; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class constructor |
40
|
|
|
* @param string $string The string to parse. |
41
|
|
|
* @param \Ballen\Linguist\Configuration $configuration Optional custom tag/url configuration |
42
|
|
|
* @throws InvalidArgumentException |
43
|
|
|
*/ |
44
|
22 |
|
public function __construct($string, $configuration = null) |
45
|
|
|
{ |
46
|
22 |
|
if (is_null($configuration)) { |
47
|
20 |
|
$configuration = new Configuration; |
48
|
20 |
|
$configuration->loadDefault(); |
49
|
|
|
} |
50
|
22 |
|
$this->loadConfiguration($configuration); |
51
|
22 |
|
$this->message = $string; |
52
|
22 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets a custom tag/url configuration. |
56
|
|
|
* @param \Ballen\Linguist\Configuration $configuration |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
6 |
|
public function setConfiguration(Configuration $configuration) |
60
|
|
|
{ |
61
|
6 |
|
$this->loadConfiguration($configuration); |
62
|
6 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return all or a single array of a certain type of tag |
66
|
|
|
* @param string $type The tag name to return |
67
|
|
|
* @return array |
68
|
|
|
* @throws InvalidArgumentException |
69
|
|
|
*/ |
70
|
12 |
|
public function tags($type = null) |
71
|
|
|
{ |
72
|
12 |
|
if ($type === null) { |
73
|
8 |
|
return $this->gatherTags(); |
74
|
|
|
} |
75
|
8 |
|
if (isset($this->gatherTags()[$type])) { |
76
|
6 |
|
return $this->gatherTags()[$type]; |
77
|
|
|
} |
78
|
2 |
|
throw new \InvalidArgumentException(sprintf('The tag "%s" has no results.', $type)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return the configuration for a specific tag. |
83
|
|
|
* @param string $name The tag name/type. |
84
|
|
|
* @return array |
85
|
|
|
* @throws InvalidArgumentException |
86
|
|
|
*/ |
87
|
4 |
|
public function tag($name) |
88
|
|
|
{ |
89
|
4 |
|
if (isset($this->configuration->get()[$name])) { |
90
|
2 |
|
return $this->configuration->get()[$name]; |
91
|
|
|
} |
92
|
2 |
|
throw new \InvalidArgumentException(sprintf('The tag "%s" is not registered!', $name)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Generates HTML output by adding HTML links to the tags. |
97
|
|
|
* @return HtmlTransformer |
98
|
|
|
*/ |
99
|
4 |
|
public function html() |
100
|
|
|
{ |
101
|
4 |
|
return new HtmlTransformer($this->plain($this->message), $this->configuration); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Generate Markdown output by adding links to the tags. |
106
|
|
|
* @return MarkdownTansformer |
107
|
|
|
*/ |
108
|
2 |
|
public function markdown() |
109
|
|
|
{ |
110
|
2 |
|
return new MarkdownTansformer($this->plain($this->message), $this->configuration); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return the plan text version of the message removing all HTML formatting. |
115
|
|
|
* @return PlaintextTransformer |
116
|
|
|
*/ |
117
|
18 |
|
public function plain() |
118
|
|
|
{ |
119
|
18 |
|
return new PlaintextTransformer($this->message, $this->configuration); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Finds, returns and categorises all tags found in the message. |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
12 |
|
private function gatherTags() |
127
|
|
|
{ |
128
|
12 |
|
$tag_configuration = $this->configuration->get(); |
129
|
12 |
|
foreach (array_keys($tag_configuration) as $tagtype) { |
130
|
12 |
|
preg_match_all('/\s+' . $tag_configuration[$tagtype]['prefix'] . '(\w+)/', $this->plain($this->message), $matches); |
|
|
|
|
131
|
12 |
|
$tags[$tagtype] = $matches[1]; |
132
|
|
|
} |
133
|
12 |
|
return $tags; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Loads the configuration into the Parser object. |
138
|
|
|
* @param Configuration $configuration |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
22 |
|
private function loadConfiguration(Configuration $configuration) |
142
|
|
|
{ |
143
|
22 |
|
$this->configuration = $configuration; |
|
|
|
|
144
|
22 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Magic method calls to enable users to call $this->mentions etc. |
148
|
|
|
* @param string $name |
149
|
|
|
* @param array $arguments |
150
|
|
|
* @return array |
151
|
|
|
* @throws RuntimeException |
152
|
|
|
*/ |
153
|
4 |
|
public function __call($name, $arguments = []) |
154
|
|
|
{ |
155
|
4 |
|
$tags = array_keys($this->tags()); |
156
|
4 |
|
if (!in_array($name, $tags)) { |
157
|
2 |
|
throw new \RuntimeException('Invalid tag type(s) requested.'); |
158
|
|
|
} |
159
|
2 |
|
return $this->tags($name); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths