|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OkayBueno\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ValidationServiceProvider |
|
10
|
|
|
* @package OkayBueno\Validation |
|
11
|
|
|
*/ |
|
12
|
|
|
class ValidationServiceProvider extends ServiceProvider |
|
13
|
|
|
{ |
|
14
|
|
|
private $configPath = '/config/validators.php'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
public function boot() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->publishes([ |
|
22
|
|
|
__DIR__.$this->configPath => config_path('validators.php'), |
|
23
|
|
|
], 'validators'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
public function register() |
|
31
|
|
|
{ |
|
32
|
|
|
// merge default config |
|
33
|
|
|
$this->mergeConfigFrom( |
|
34
|
|
|
__DIR__.$this->configPath , 'validators' |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
// Bind the validators. |
|
38
|
|
|
$this->autoBindValidators(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/*** |
|
43
|
|
|
* |
|
44
|
|
|
*/ |
|
45
|
|
|
private function autoBindValidators() |
|
46
|
|
|
{ |
|
47
|
|
|
|
|
48
|
|
|
try |
|
49
|
|
|
{ |
|
50
|
|
|
// Load config parameters needed. |
|
51
|
|
|
$validatorsBasePath = config( 'validators.validators_path' ); |
|
52
|
|
|
$baseNamespace = rtrim( config( 'validators.validator_interfaces_namespace' ), '\\' ) . '\\'; |
|
53
|
|
|
|
|
54
|
|
|
$folders = scandir( $validatorsBasePath ); |
|
55
|
|
|
|
|
56
|
|
|
// Remove the first 2 directories: "." and "..". |
|
57
|
|
|
array_shift( $folders ); |
|
58
|
|
|
array_shift( $folders ); |
|
59
|
|
|
|
|
60
|
|
|
foreach( $folders as $folder ) |
|
61
|
|
|
{ |
|
62
|
|
|
$folderPath = $validatorsBasePath.'/'.$folder; |
|
63
|
|
|
$currentImplementationNamespace = $baseNamespace.$folder.'\\src'; |
|
64
|
|
|
|
|
65
|
|
|
// Scan files within the folder. |
|
66
|
|
|
$validatorInterfacesInFolder = File::files( $folderPath ); |
|
67
|
|
|
|
|
68
|
|
|
foreach( $validatorInterfacesInFolder as $validatorInterface ) |
|
69
|
|
|
{ |
|
70
|
|
|
// For each file find the Interface and the implementation and bind them together. |
|
71
|
|
|
$interfaceName = pathinfo( $validatorInterface, PATHINFO_FILENAME ); |
|
72
|
|
|
|
|
73
|
|
|
$commonName = str_replace( 'Interface', '', $interfaceName ); |
|
74
|
|
|
$interfaceFullClassName = $baseNamespace.$interfaceName; |
|
75
|
|
|
|
|
76
|
|
|
$fullClassName = $currentImplementationNamespace.'\\'.$commonName; |
|
77
|
|
|
|
|
78
|
|
|
if ( class_exists( $fullClassName ) ) |
|
79
|
|
|
{ |
|
80
|
|
|
// Bind the class. |
|
81
|
|
|
$this->app->bind( $interfaceFullClassName, function ( $app ) use ( $fullClassName ) |
|
82
|
|
|
{ |
|
83
|
|
|
return $app->make( $fullClassName ); |
|
84
|
|
|
}); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} catch( \Exception $e ) |
|
89
|
|
|
{ |
|
90
|
|
|
// Be quiet; Silence is golden. |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|