1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mpociot\VatCalculator; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Teamwork |
7
|
|
|
* |
8
|
|
|
* @license MIT |
9
|
|
|
* @package Teamwork |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use Illuminate\Support\Facades\Validator; |
13
|
|
|
use Illuminate\Support\ServiceProvider; |
14
|
|
|
use Mpociot\VatCalculator\Facades\VatCalculator; |
|
|
|
|
15
|
|
|
use Mpociot\VatCalculator\Validators\VatCalculatorValidatorExtension; |
16
|
|
|
|
17
|
|
|
class VatCalculatorServiceProvider extends ServiceProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Indicates if loading of the provider is deferred. |
21
|
|
|
* |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $defer = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Bootstrap the application events. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function boot() |
32
|
|
|
{ |
33
|
|
|
$this->publishConfig(); |
34
|
|
|
$this->registerValidatorExtension(); |
35
|
|
|
$this->registerRoutes(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Publish Teamwork configuration. |
40
|
|
|
*/ |
41
|
|
|
protected function publishConfig() |
42
|
|
|
{ |
43
|
|
|
// Publish config files |
44
|
|
|
$this->publishes([ |
45
|
|
|
__DIR__.'/../../config/config.php' => config_path('vat_calculator.php'), |
46
|
|
|
__DIR__.'/../../public/js/vat_calculator.js' => public_path('js/vat_calculator.js'), |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
$this->publishes([ |
50
|
|
|
__DIR__.'/../../public/js/vat_calculator.js' => base_path('resources/assets/js/vat_calculator.js'), |
51
|
|
|
], 'vatcalculator-spark'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Register the service provider. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function register() |
60
|
|
|
{ |
61
|
|
|
$this->mergeConfig(); |
62
|
|
|
$this->registerVatCalculator(); |
63
|
|
|
$this->registerFacade(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Register the application bindings. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
protected function registerVatCalculator() |
72
|
|
|
{ |
73
|
|
|
$this->app->bind('vatcalculator', '\Mpociot\VatCalculator\VatCalculator'); |
74
|
|
|
|
75
|
|
|
$this->app->bind('\Mpociot\VatCalculator\VatCalculator', function ($app) { |
76
|
|
|
$config = $app->make('Illuminate\Contracts\Config\Repository'); |
77
|
|
|
|
78
|
|
|
return new \Mpociot\VatCalculator\VatCalculator($config); |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Register the vault facade without the user having to add it to the app.php file. |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function registerFacade() |
88
|
|
|
{ |
89
|
|
|
$this->app->booting(function () { |
90
|
|
|
$loader = \Illuminate\Foundation\AliasLoader::getInstance(); |
91
|
|
|
$loader->alias('VatCalculator', 'Mpociot\VatCalculator\Facades\VatCalculator'); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Merges user's and teamwork's configs. |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function mergeConfig() |
101
|
|
|
{ |
102
|
|
|
$this->mergeConfigFrom( |
103
|
|
|
__DIR__.'/../../config/config.php', 'vat_calculator' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function registerValidatorExtension() |
108
|
|
|
{ |
109
|
|
|
$this->loadTranslationsFrom( |
110
|
|
|
__DIR__.'/../../lang', |
111
|
|
|
'vatnumber-validator' |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
// Registering the validator extension with the validator factory |
115
|
|
|
$this->app['validator']->resolver( |
116
|
|
|
function ($translator, $data, $rules, $messages, $customAttributes = []) { |
117
|
|
|
return new VatCalculatorValidatorExtension( |
118
|
|
|
$translator, |
119
|
|
|
$data, |
120
|
|
|
$rules, |
121
|
|
|
$messages, |
122
|
|
|
$customAttributes |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Register predefined routes, used for the |
130
|
|
|
* handy javascript toolkit. |
131
|
|
|
*/ |
132
|
|
|
protected function registerRoutes() |
133
|
|
|
{ |
134
|
|
|
$config = $this->app->make('Illuminate\Contracts\Config\Repository'); |
135
|
|
|
if ($config->get('vat_calculator.use_routes', true)) { |
136
|
|
|
include __DIR__.'/../../routes.php'; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: