1
|
|
|
<?php namespace Anomaly\ConfigurationModule\Configuration\Form; |
2
|
|
|
|
3
|
|
|
use Anomaly\ConfigurationModule\Configuration\Contract\ConfigurationRepositoryInterface; |
4
|
|
|
use Illuminate\Config\Repository; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ConfigurationFormFields |
9
|
|
|
* |
10
|
|
|
* @link http://pyrocms.com/ |
11
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
12
|
|
|
* @author Ryan Thompson <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class ConfigurationFormFields |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The config repository. |
19
|
|
|
* |
20
|
|
|
* @var Repository |
21
|
|
|
*/ |
22
|
|
|
protected $config; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new ConfigurationFormFields instance. |
26
|
|
|
* |
27
|
|
|
* @param Repository $config |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Repository $config) |
30
|
|
|
{ |
31
|
|
|
$this->config = $config; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return the form fields. |
36
|
|
|
* |
37
|
|
|
* @param ConfigurationFormBuilder $builder |
38
|
|
|
*/ |
39
|
|
|
public function handle(ConfigurationFormBuilder $builder, ConfigurationRepositoryInterface $configuration) |
40
|
|
|
{ |
41
|
|
|
$scope = $builder->getScope(); |
42
|
|
|
$namespace = $builder->getFormEntry() . '::'; |
43
|
|
|
|
44
|
|
|
/* |
45
|
|
|
* Get the fields from the config system. Sections are |
46
|
|
|
* optionally defined the same way. |
47
|
|
|
*/ |
48
|
|
|
if (!$fields = $this->config->get($namespace . 'configuration/configuration')) { |
49
|
|
|
$fields = $fields = $this->config->get($namespace . 'configuration', []); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($sections = $this->config->get($namespace . 'configuration/sections')) { |
53
|
|
|
$builder->setSections($sections); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
* Finish each field. |
58
|
|
|
*/ |
59
|
|
|
foreach ($fields as $slug => &$field) { |
60
|
|
|
|
61
|
|
|
/* |
62
|
|
|
* Force an array. This is done later |
63
|
|
|
* too in normalization but we need it now |
64
|
|
|
* because we are normalizing / guessing our |
65
|
|
|
* own parameters somewhat. |
66
|
|
|
*/ |
67
|
|
|
if (is_string($field)) { |
68
|
|
|
$field = [ |
69
|
|
|
'type' => $field, |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Make sure we have a config property. |
74
|
|
|
$field['config'] = array_get($field, 'config', []); |
75
|
|
|
|
76
|
|
|
// Default the label. |
77
|
|
View Code Duplication |
if (trans()->has( |
|
|
|
|
78
|
|
|
$label = array_get( |
79
|
|
|
$field, |
80
|
|
|
'label', |
81
|
|
|
$namespace . 'configuration.' . $slug . '.label' |
82
|
|
|
) |
83
|
|
|
) |
84
|
|
|
) { |
85
|
|
|
$field['label'] = $label; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Default the label. |
89
|
|
|
$field['label'] = array_get( |
90
|
|
|
$field, |
91
|
|
|
'label', |
92
|
|
|
$namespace . 'configuration.' . $slug . '.name' |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
// Default the warning. |
96
|
|
View Code Duplication |
if (trans()->has( |
|
|
|
|
97
|
|
|
$warning = array_get( |
98
|
|
|
$field, |
99
|
|
|
'warning', |
100
|
|
|
$namespace . 'configuration.' . $slug . '.warning' |
101
|
|
|
) |
102
|
|
|
) |
103
|
|
|
) { |
104
|
|
|
$field['warning'] = $warning; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Default the placeholder. |
108
|
|
|
$field['config']['placeholder'] = array_get( |
109
|
|
|
$field, |
110
|
|
|
'placeholder', |
111
|
|
|
$namespace . 'configuration.' . $slug . '.placeholder' |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
// Default the instructions. |
115
|
|
View Code Duplication |
if (trans()->has( |
|
|
|
|
116
|
|
|
$instructions = array_get( |
117
|
|
|
$field, |
118
|
|
|
'instructions', |
119
|
|
|
$namespace . 'configuration.' . $slug . '.instructions' |
120
|
|
|
) |
121
|
|
|
) |
122
|
|
|
) { |
123
|
|
|
$field['instructions'] = $instructions; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Get the value defaulting to the default value. |
127
|
|
|
if ($applied = $configuration->get($namespace . $slug, $scope)) { |
128
|
|
|
$field['value'] = $applied->getValue(); |
129
|
|
|
} else { |
130
|
|
|
$field['value'] = array_get($field['config'], 'default_value'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$builder->setFields($fields); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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.