|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OkayBueno\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class RepositoriesServiceProvider |
|
10
|
|
|
* @package OkayBueno\Repositories |
|
11
|
|
|
*/ |
|
12
|
|
|
class +RepositoryServiceProvider extends ServiceProvider |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
private $configPath = '/config/repositories.php'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
public function boot() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->publishes([ |
|
23
|
|
|
__DIR__.$this->configPath => config_path('repositories.php'), |
|
24
|
|
|
], 'repositories'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
public function register() |
|
32
|
|
|
{ |
|
33
|
|
|
// merge default config |
|
34
|
|
|
$this->mergeConfigFrom( |
|
35
|
|
|
__DIR__.$this->configPath , 'repositories' |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
// Bind the repositories. |
|
39
|
|
|
$this->autoBindRepositories(); |
|
40
|
|
|
|
|
41
|
|
|
// And generators. |
|
42
|
|
|
$this->registerRepositoryGenerator(); |
|
43
|
|
|
$this->registerCriteriaGenerator(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* |
|
49
|
|
|
*/ |
|
50
|
|
|
private function autoBindRepositories() |
|
51
|
|
|
{ |
|
52
|
|
|
// Load config parameters needed. |
|
53
|
|
|
$repositoriesBasePath = config( 'repositories.repositories_path' ); |
|
54
|
|
|
$baseNamespace = rtrim( config( 'repositories.repository_interfaces_namespace' ), '\\' ) . '\\'; |
|
55
|
|
|
$skipRepositories = config( 'repositories.skip' ); |
|
56
|
|
|
$implementationBindings = config( 'repositories.bindings' ); |
|
57
|
|
|
$defaultImplementation = $this->findDefaultImplementation( $implementationBindings ); |
|
58
|
|
|
|
|
59
|
|
|
if ( \File::exists( $repositoriesBasePath ) ) |
|
60
|
|
|
{ |
|
61
|
|
|
$allRepos = \File::files( $repositoriesBasePath ); |
|
62
|
|
|
|
|
63
|
|
|
foreach( $allRepos as $repo ) |
|
64
|
|
|
{ |
|
65
|
|
|
$implementation = $defaultImplementation; |
|
66
|
|
|
$interfaceName = pathinfo( $repo, PATHINFO_FILENAME ); |
|
67
|
|
|
if ( in_array( $interfaceName, $skipRepositories ) ) continue; |
|
68
|
|
|
else |
|
69
|
|
|
{ |
|
70
|
|
|
$commonName = str_replace( 'Interface', '', $interfaceName ); |
|
71
|
|
|
$interfaceFullClassName = $baseNamespace.$interfaceName; |
|
72
|
|
|
|
|
73
|
|
|
foreach( $implementationBindings as $engine => $bindRepositories ) |
|
74
|
|
|
{ |
|
75
|
|
|
if ( $bindRepositories === 'default' ) continue; |
|
76
|
|
|
else if ( in_array( $interfaceName, $bindRepositories ) ) |
|
77
|
|
|
{ |
|
78
|
|
|
$implementation = $engine; |
|
79
|
|
|
break; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$fullClassName = $baseNamespace.ucfirst( Str::camel( $implementation ) ).'\\'.$commonName; |
|
84
|
|
|
|
|
85
|
|
|
if ( class_exists( $fullClassName ) ) |
|
86
|
|
|
{ |
|
87
|
|
|
// Bind the class. |
|
88
|
|
|
$this->app->bind( $interfaceFullClassName, function ( $app ) use ( $fullClassName ) |
|
89
|
|
|
{ |
|
90
|
|
|
return $app->make( $fullClassName ); |
|
91
|
|
|
}); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $implementations |
|
101
|
|
|
* @return array|mixed|string |
|
102
|
|
|
*/ |
|
103
|
|
|
private function findDefaultImplementation( $implementations ) |
|
104
|
|
|
{ |
|
105
|
|
|
$filtered = array_filter( $implementations, function( $k ) { |
|
106
|
|
|
return $k === 'default'; |
|
107
|
|
|
}); |
|
108
|
|
|
|
|
109
|
|
|
$default = array_keys($filtered); |
|
110
|
|
|
$default = is_array( $default ) ? $default[0] : $default; |
|
111
|
|
|
|
|
112
|
|
|
return $default ? $default : 'eloquent'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* |
|
117
|
|
|
*/ |
|
118
|
|
|
private function registerRepositoryGenerator() |
|
119
|
|
|
{ |
|
120
|
|
|
$this->app->singleton('command.repository', function ($app) |
|
121
|
|
|
{ |
|
122
|
|
|
return $app['OkayBueno\Repositories\Commands\MakeRepositoryCommand']; |
|
123
|
|
|
}); |
|
124
|
|
|
|
|
125
|
|
|
$this->commands('command.repository'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* |
|
131
|
|
|
*/ |
|
132
|
|
|
private function registerCriteriaGenerator() |
|
133
|
|
|
{ |
|
134
|
|
|
$this->app->singleton('command.criteria', function ($app) |
|
135
|
|
|
{ |
|
136
|
|
|
return $app['OkayBueno\Repositories\Commands\MakeCriteriaCommand']; |
|
137
|
|
|
}); |
|
138
|
|
|
|
|
139
|
|
|
$this->commands('command.criteria'); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|