1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\Form\Component\Button\Guesser; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Module\Module; |
4
|
|
|
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection; |
5
|
|
|
use Anomaly\Streams\Platform\Support\Str; |
6
|
|
|
use Anomaly\Streams\Platform\Ui\Button\ButtonRegistry; |
7
|
|
|
use Anomaly\Streams\Platform\Ui\Form\FormBuilder; |
8
|
|
|
use Illuminate\Contracts\Config\Repository; |
9
|
|
|
use Illuminate\Translation\Translator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class TextGuesser |
13
|
|
|
* |
14
|
|
|
* @link http://pyrocms.com/ |
15
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
16
|
|
|
* @author Ryan Thompson <[email protected]> |
17
|
|
|
* @package Anomaly\Streams\Platform\Ui\Form\Component\Button\Guesser |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
class TextGuesser |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The config repository. |
24
|
|
|
* |
25
|
|
|
* @var Repository |
26
|
|
|
*/ |
27
|
|
|
protected $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The string utility. |
31
|
|
|
* |
32
|
|
|
* @var Str |
33
|
|
|
*/ |
34
|
|
|
protected $string; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The button registry. |
38
|
|
|
* |
39
|
|
|
* @var ButtonRegistry |
40
|
|
|
*/ |
41
|
|
|
protected $buttons; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The module collection. |
45
|
|
|
* |
46
|
|
|
* @var ModuleCollection |
47
|
|
|
*/ |
48
|
|
|
protected $modules; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The translator utility. |
52
|
|
|
* |
53
|
|
|
* @var Translator |
54
|
|
|
*/ |
55
|
|
|
protected $translator; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create a new TextGuesser instance. |
59
|
|
|
* |
60
|
|
|
* @param Str $string |
61
|
|
|
* @param Repository $config |
62
|
|
|
* @param ButtonRegistry $buttons |
63
|
|
|
* @param ModuleCollection $modules |
64
|
|
|
* @param Translator $translator |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
Str $string, |
68
|
|
|
Repository $config, |
69
|
|
|
ButtonRegistry $buttons, |
70
|
|
|
ModuleCollection $modules, |
71
|
|
|
Translator $translator |
72
|
|
|
) { |
73
|
|
|
$this->config = $config; |
74
|
|
|
$this->string = $string; |
75
|
|
|
$this->buttons = $buttons; |
76
|
|
|
$this->modules = $modules; |
77
|
|
|
$this->translator = $translator; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Guess the button from the hint. |
82
|
|
|
* |
83
|
|
|
* @param FormBuilder $builder |
84
|
|
|
*/ |
85
|
|
|
public function guess(FormBuilder $builder) |
86
|
|
|
{ |
87
|
|
|
$buttons = $builder->getButtons(); |
88
|
|
|
|
89
|
|
|
$module = $this->modules->active(); |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* This will break if we can't figure |
93
|
|
|
* out what the active module is. |
94
|
|
|
*/ |
95
|
|
|
if (!$module instanceof Module) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach ($buttons as &$button) { |
|
|
|
|
100
|
|
|
|
101
|
|
|
if (isset($button['text'])) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!isset($button['button'])) { |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$text = $module->getNamespace('button.' . $button['button']); |
110
|
|
|
|
111
|
|
|
if (!isset($button['text']) && $this->translator->has($text)) { |
112
|
|
|
$button['text'] = $text; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ( |
116
|
|
|
(!isset($button['text']) || !$this->translator->has($button['text'])) |
117
|
|
|
&& $this->config->get('streams::system.lazy_translations') |
118
|
|
|
) { |
119
|
|
|
$button['text'] = $this->string->humanize($button['button']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!isset($button['text'])) { |
123
|
|
|
$button['text'] = $text; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$builder->setButtons($buttons); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.