Skip to content
This repository was archived by the owner on Sep 23, 2023. It is now read-only.

Commit f552dfe

Browse files
committed
Implement service provider and configuration
* Default configuration point to Laravel's default db connection * Service Provider registers Persistence singleton with alias * Support Lumen
1 parent 19566f6 commit f552dfe

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/AgileDataServiceProvider.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

src/agiledata.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php return [
2+
'connection' => 'default',
3+
];

0 commit comments

Comments
 (0)