|
1
|
|
|
<?php namespace Distilleries\FormBuilder; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Html\HtmlBuilder; |
|
4
|
|
|
use Illuminate\Html\FormBuilder as LaravelForm; |
|
5
|
|
|
use Kris\LaravelFormBuilder\FormHelper; |
|
6
|
|
|
use Illuminate\Foundation\AliasLoader; |
|
7
|
|
|
|
|
8
|
|
|
class FormBuilderServiceProvider extends \Kris\LaravelFormBuilder\FormBuilderServiceProvider { |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Register the service provider. |
|
13
|
|
|
* |
|
14
|
|
|
* @return void |
|
15
|
|
|
*/ |
|
16
|
164 |
|
public function register() |
|
17
|
|
|
{ |
|
18
|
164 |
|
$this->commands('Kris\LaravelFormBuilder\Console\FormMakeCommand'); |
|
19
|
|
|
|
|
20
|
164 |
|
$this->registerHtmlIfNeeded(); |
|
21
|
164 |
|
$this->registerFormIfHeeded(); |
|
22
|
|
|
|
|
23
|
164 |
|
$this->mergeConfigFrom( |
|
24
|
164 |
|
__DIR__ . '/../../config/config.php', |
|
25
|
|
|
'laravel-form-builder' |
|
26
|
164 |
|
); |
|
27
|
|
|
|
|
28
|
164 |
|
$this->registerFormHelper(); |
|
29
|
|
|
|
|
30
|
|
|
$this->app->singleton('laravel-form-builder', function ($app) { |
|
31
|
|
|
|
|
32
|
64 |
|
return new \Kris\LaravelFormBuilder\FormBuilder($app, $app['laravel-form-helper']); |
|
33
|
164 |
|
}); |
|
34
|
|
|
|
|
35
|
164 |
|
$this->commands('Distilleries\FormBuilder\Console\FormMakeCommand'); |
|
36
|
|
|
|
|
37
|
164 |
|
$this->alias(); |
|
38
|
164 |
|
} |
|
39
|
|
|
|
|
40
|
164 |
|
protected function registerFormHelper() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->app->singleton('laravel-form-helper', function($app) |
|
43
|
|
|
{ |
|
44
|
|
|
|
|
45
|
144 |
|
$configuration = $app['config']->get('form-builder'); |
|
46
|
|
|
|
|
47
|
144 |
|
return new FormHelper($app['view'], $app['request'], $configuration); |
|
48
|
164 |
|
}); |
|
49
|
|
|
|
|
50
|
164 |
|
$this->app->alias('laravel-form-helper', 'Kris\LaravelFormBuilder\FormHelper'); |
|
51
|
164 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
164 |
|
public function boot() |
|
55
|
|
|
{ |
|
56
|
|
|
|
|
57
|
164 |
|
parent::boot(); |
|
58
|
|
|
|
|
59
|
164 |
|
$this->loadViewsFrom(__DIR__.'/../../views', 'form-builder'); |
|
60
|
164 |
|
$this->loadTranslationsFrom(__DIR__.'/../../lang', 'form-builder'); |
|
61
|
|
|
|
|
62
|
164 |
|
$this->publishes([ |
|
63
|
164 |
|
__DIR__.'/../../config/config.php' => config_path('form-builder.php') |
|
64
|
164 |
|
]); |
|
65
|
164 |
|
$this->publishes([ |
|
66
|
164 |
|
__DIR__.'/../../views' => base_path('resources/views/vendor/form-builder'), |
|
67
|
164 |
|
], 'views'); |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
164 |
|
$this->mergeConfigFrom( |
|
71
|
164 |
|
__DIR__.'/../../config/config.php', |
|
72
|
|
|
'form-builder' |
|
73
|
164 |
|
); |
|
74
|
164 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add Laravel Form to container if not already set |
|
79
|
|
|
*/ |
|
80
|
164 |
|
private function registerFormIfHeeded() |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
164 |
|
if (!$this->app->offsetExists('form')) { |
|
83
|
|
|
|
|
84
|
|
|
$this->app->singleton('form', function($app) { |
|
85
|
|
|
|
|
86
|
84 |
|
$form = new LaravelForm($app['html'], $app['url'], $app['session.store']->getToken()); |
|
87
|
|
|
|
|
88
|
84 |
|
return $form->setSessionStore($app['session.store']); |
|
89
|
164 |
|
}); |
|
90
|
|
|
|
|
91
|
164 |
|
if (! $this->aliasExists('Form')) { |
|
92
|
|
|
|
|
93
|
4 |
|
AliasLoader::getInstance()->alias( |
|
94
|
4 |
|
'Form', |
|
95
|
164 |
|
'Illuminate\Html\FormFacade' |
|
96
|
4 |
|
); |
|
97
|
4 |
|
} |
|
98
|
164 |
|
} |
|
99
|
164 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Add Laravel Html to container if not already set |
|
105
|
|
|
*/ |
|
106
|
164 |
|
private function registerHtmlIfNeeded() |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
164 |
|
if (!$this->app->offsetExists('html')) { |
|
109
|
|
|
|
|
110
|
164 |
|
$this->app->singleton('html', function($app) { |
|
111
|
84 |
|
return new HtmlBuilder($app['url']); |
|
112
|
164 |
|
}); |
|
113
|
|
|
|
|
114
|
164 |
|
if (! $this->aliasExists('Html')) { |
|
115
|
|
|
|
|
116
|
4 |
|
AliasLoader::getInstance()->alias( |
|
117
|
4 |
|
'Html', |
|
118
|
|
|
'Illuminate\Html\HtmlFacade' |
|
119
|
4 |
|
); |
|
120
|
4 |
|
} |
|
121
|
164 |
|
} |
|
122
|
164 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
164 |
|
public function alias() { |
|
126
|
|
|
|
|
127
|
164 |
|
AliasLoader::getInstance()->alias( |
|
128
|
164 |
|
'FormBuilder', |
|
129
|
|
|
'Distilleries\FormBuilder\Facades\FormBuilder' |
|
130
|
164 |
|
); |
|
131
|
164 |
|
AliasLoader::getInstance()->alias( |
|
132
|
164 |
|
'Request', |
|
133
|
|
|
'Illuminate\Support\Facades\Request' |
|
134
|
164 |
|
); |
|
135
|
164 |
|
AliasLoader::getInstance()->alias( |
|
136
|
164 |
|
'Route', |
|
137
|
|
|
'Illuminate\Support\Facades\Route' |
|
138
|
164 |
|
); |
|
139
|
164 |
|
AliasLoader::getInstance()->alias( |
|
140
|
164 |
|
'File', |
|
141
|
|
|
'Illuminate\Support\Facades\File' |
|
142
|
164 |
|
); |
|
143
|
164 |
|
AliasLoader::getInstance()->alias( |
|
144
|
164 |
|
'Redirect', |
|
145
|
|
|
'Illuminate\Support\Facades\Redirect' |
|
146
|
164 |
|
); |
|
147
|
164 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Check if an alias already exists in the IOC |
|
152
|
|
|
* @param $alias |
|
153
|
|
|
* @return bool |
|
154
|
|
|
*/ |
|
155
|
164 |
|
private function aliasExists($alias) |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
164 |
|
return array_key_exists($alias, AliasLoader::getInstance()->getAliases()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
} |
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.