|
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
|
|
|
|
|
16
|
|
|
class VatCalculatorServiceProvider extends ServiceProvider |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Indicates if loading of the provider is deferred. |
|
20
|
|
|
* |
|
21
|
|
|
* @var bool |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $defer = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Bootstrap the application events. |
|
27
|
|
|
* |
|
28
|
|
|
* @return void |
|
29
|
|
|
*/ |
|
30
|
|
|
public function boot() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->publishConfig(); |
|
33
|
|
|
$this->registerValidatorExtension(); |
|
34
|
|
|
$this->registerRoutes(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Publish Teamwork configuration. |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function publishConfig() |
|
41
|
|
|
{ |
|
42
|
|
|
// Publish config files |
|
43
|
|
|
$this->publishes([ |
|
44
|
|
|
__DIR__.'/../../config/config.php' => config_path('vat_calculator.php'), |
|
45
|
|
|
__DIR__.'/../../public/js/vat_calculator.js' => public_path('js/vat_calculator.js'), |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
|
|
$this->publishes([ |
|
49
|
|
|
__DIR__.'/../../public/js/vat_calculator.js' => base_path('resources/assets/js/vat_calculator.js'), |
|
50
|
|
|
], 'vatcalculator-spark'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Register the service provider. |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
public function register() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->mergeConfig(); |
|
61
|
|
|
$this->registerVatCalculator(); |
|
62
|
|
|
$this->registerFacade(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Register the application bindings. |
|
67
|
|
|
* |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function registerVatCalculator() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->app->bind('vatcalculator', \Mpociot\VatCalculator\VatCalculator::class); |
|
73
|
|
|
|
|
74
|
|
|
$this->app->bind(\Mpociot\VatCalculator\VatCalculator::class, function ($app) { |
|
75
|
|
|
$config = $app->make('Illuminate\Contracts\Config\Repository'); |
|
76
|
|
|
|
|
77
|
|
|
return new \Mpociot\VatCalculator\VatCalculator($config); |
|
78
|
|
|
}); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Register the vault facade without the user having to add it to the app.php file. |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function registerFacade() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->app->booting(function () { |
|
89
|
|
|
$loader = \Illuminate\Foundation\AliasLoader::getInstance(); |
|
90
|
|
|
$loader->alias('VatCalculator', 'Mpociot\VatCalculator\Facades\VatCalculator'); |
|
91
|
|
|
}); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Merges user's and teamwork's configs. |
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function mergeConfig() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->mergeConfigFrom( |
|
102
|
|
|
__DIR__.'/../../config/config.php', 'vat_calculator' |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
protected function registerValidatorExtension() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->loadTranslationsFrom( |
|
109
|
|
|
__DIR__.'/../../lang', |
|
110
|
|
|
'vatnumber-validator' |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
$this->app['validator']->extend('vat_number', |
|
114
|
|
|
'Mpociot\VatCalculator\Validators\VatCalculatorValidatorExtension@validateVatNumber'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Register predefined routes, used for the |
|
119
|
|
|
* handy javascript toolkit. |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function registerRoutes() |
|
122
|
|
|
{ |
|
123
|
|
|
$config = $this->app->make('Illuminate\Contracts\Config\Repository'); |
|
124
|
|
|
if ($config->get('vat_calculator.use_routes', true)) { |
|
125
|
|
|
include __DIR__.'/../../routes.php'; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: