1+ <?php
2+
3+
4+ namespace atk4 \LaravelAD ;
5+
6+
7+ use atk4 \data \Persistence ;
8+ use FoxxMD \Utilities \ArrayUtil ;
9+ use Illuminate \Support \ServiceProvider ;
10+
11+ class AgileDataServiceProvider extends ServiceProvider {
12+ public function boot ()
13+ {
14+ $ this ->publishes ([
15+ __DIR__ . '/agiledata.php ' => config_path ('agiledata.php ' ),
16+ ], 'agiledata ' );
17+ }
18+
19+ public function register ()
20+ {
21+ if ($ this ->isLumen ()) {
22+ $ this ->app ->configure ('database ' );
23+ $ this ->app ->configure ('agiledata ' );
24+ }
25+
26+ $ this ->registerPersistence ();
27+ }
28+
29+ protected function registerPersistence ()
30+ {
31+ $ this ->app ->singleton ('agiledata ' , function ($ app )
32+ {
33+ $ config = $ app ->make ('config ' );
34+ if ('default ' === $ connectionName = $ config ->get ('agiledata.connection ' ))
35+ {
36+ $ connectionName = $ config ->get ('database.default ' );
37+ }
38+ $ connectionDetails = $ config ->get ('database.connections. ' . $ connectionName );
39+ $ dsn = "{$ connectionDetails ['driver ' ]}: " ;
40+ switch ($ connectionDetails ['driver ' ])
41+ {
42+ case 'mysql ' :
43+ $ dsn .= "host= {$ connectionDetails ['host ' ]};dbname= {$ connectionDetails ['database ' ]}" ;
44+ break ;
45+ case 'sqlite ' :
46+ $ dsn .= "{$ connectionDetails ['database ' ]}" ;
47+ }
48+
49+ return Persistence::connect ($ dsn , ArrayUtil::get ($ connectionDetails ['username ' ]), ArrayUtil::get ($ connectionDetails ['password ' ]));
50+ });
51+
52+ $ this ->app ->alias ('agiledata ' , Persistence::class);
53+ }
54+
55+ /**
56+ * @return bool
57+ */
58+ protected function isLumen ()
59+ {
60+ return str_contains ($ this ->app ->version (), 'Lumen ' );
61+ }
62+ }
0 commit comments