1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* Topic Preview |
5
|
|
|
* |
6
|
|
|
* @copyright (c) 2016 Matt Friedman |
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace vse\topicpreview\core\trim; |
12
|
|
|
|
13
|
|
|
class manager |
14
|
|
|
{ |
15
|
|
|
/** @var array Array of tools from the service collection */ |
16
|
|
|
protected $tools = array(); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor |
20
|
|
|
* |
21
|
|
|
* @param array $tools Array of tools from the service collection |
22
|
|
|
*/ |
23
|
43 |
|
public function __construct($tools) |
24
|
|
|
{ |
25
|
43 |
|
$this->tools = $tools; |
26
|
43 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get all available tools |
30
|
|
|
* |
31
|
|
|
* @return array Array of available tools |
32
|
|
|
*/ |
33
|
38 |
|
public function get_tools() |
34
|
|
|
{ |
35
|
38 |
|
$tools = array(); |
36
|
|
|
|
37
|
|
|
/** @var tools\tool_interface $tool */ |
38
|
38 |
|
foreach ($this->tools as $tool) |
39
|
|
|
{ |
40
|
38 |
|
if ($tool->is_available()) |
41
|
38 |
|
{ |
42
|
38 |
|
$tools[$tool->get_name()] = $tool; |
43
|
38 |
|
} |
44
|
38 |
|
} |
45
|
|
|
|
46
|
38 |
|
$tools = $this->order_tools($tools); |
47
|
|
|
|
48
|
38 |
|
return $tools; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get tool by name |
53
|
|
|
* |
54
|
|
|
* @param string $name Name of a trim tool service |
55
|
|
|
* |
56
|
|
|
* @return null|tools\tool_interface A trim tool object |
57
|
|
|
*/ |
58
|
5 |
|
public function get_tool($name) |
59
|
|
|
{ |
60
|
|
|
/** @var tools\tool_interface $tool */ |
61
|
5 |
|
foreach ($this->tools as $tool) |
62
|
|
|
{ |
63
|
5 |
|
if ($tool->get_name() === $name) |
64
|
5 |
|
{ |
65
|
1 |
|
return $tool; |
66
|
|
|
} |
67
|
4 |
|
} |
68
|
|
|
|
69
|
4 |
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Configure tools in the proper order and remove |
74
|
|
|
* any conflicting tools |
75
|
|
|
* |
76
|
|
|
* @param $tools array Array of available tools |
77
|
|
|
* |
78
|
|
|
* @return array Array of available tools |
79
|
|
|
*/ |
80
|
38 |
|
protected function order_tools(array $tools) |
81
|
|
|
{ |
82
|
38 |
|
if (isset($tools['bbcodes'])) |
83
|
38 |
|
{ |
84
|
13 |
|
unset($tools['bbcodes_legacy']); |
85
|
13 |
|
} |
86
|
|
|
|
87
|
38 |
|
ksort($tools, SORT_STRING); |
88
|
|
|
|
89
|
38 |
|
return $tools; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|