okaybueno /
validation
| 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 ); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 58 | array_shift( $folders ); |
||
| 59 | |||
| 60 | foreach( $folders as $folder ) |
||
| 61 | { |
||
| 62 | $folderPath = $validatorsBasePath.'/'.$folder; |
||
| 63 | $currentInterfaceNamespace = $baseNamespace.$folder.'\\'; |
||
| 64 | $currentImplementationNamespace = $currentInterfaceNamespace.'src'; |
||
| 65 | |||
| 66 | // Scan files within the folder. |
||
| 67 | $validatorInterfacesInFolder = File::files( $folderPath ); |
||
| 68 | |||
| 69 | foreach( $validatorInterfacesInFolder as $validatorInterface ) |
||
| 70 | { |
||
| 71 | // For each file find the Interface and the implementation and bind them together. |
||
| 72 | $interfaceName = pathinfo( $validatorInterface, PATHINFO_FILENAME ); |
||
| 73 | |||
| 74 | $commonName = str_replace( 'Interface', '', $interfaceName ); |
||
| 75 | $interfaceFullClassName = $currentInterfaceNamespace.$interfaceName; |
||
| 76 | |||
| 77 | $fullClassName = $currentImplementationNamespace.'\\'.$commonName; |
||
| 78 | |||
| 79 | if ( class_exists( $fullClassName ) ) |
||
| 80 | { |
||
| 81 | // Bind the class. |
||
| 82 | $this->app->bind( $interfaceFullClassName, function ( $app ) use ( $fullClassName ) |
||
| 83 | { |
||
| 84 | return $app->make( $fullClassName ); |
||
| 85 | }); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } catch( \Exception $e ) |
||
| 90 | { |
||
| 91 | // Be quiet; Silence is golden. |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 |