Skip to content

Commit 2baa7fb

Browse files
committed
refactor: Tidy up repository creation code
1 parent 5ec0006 commit 2baa7fb

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/Concise.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ final class Concise
1818
*/
1919
private Application $app;
2020

21+
/**
22+
* @var \Illuminate\Database\DatabaseManager
23+
*/
24+
private DatabaseManager $databases;
25+
2126
/**
2227
* @var \Articulate\Concise\IdentityMap
2328
*/
@@ -42,10 +47,11 @@ final class Concise
4247
*/
4348
private array $repositories = [];
4449

45-
public function __construct(Application $app)
50+
public function __construct(Application $app, DatabaseManager $databases)
4651
{
4752
$this->app = $app;
4853
$this->identities = new IdentityMap();
54+
$this->databases = $databases;
4955
}
5056

5157
/**
@@ -231,10 +237,17 @@ public function repository(string $class): Repository
231237

232238
$repositoryClass = $mapper->repository();
233239

234-
return $this->repositories[$class] = new $repositoryClass(
235-
$this,
236-
$mapper,
237-
$this->app->make(DatabaseManager::class)->connection($mapper->connection())
238-
);
240+
/**
241+
* This has to be here because Application::make() expects a 'string',
242+
* not a 'class-string', which is...well, yeah, you know exactly what it
243+
* is.
244+
*
245+
* @phpstan-ignore argument.type
246+
*/
247+
return $this->repositories[$class] = $this->app->make($repositoryClass, [
248+
'concise' => $this,
249+
'mapper' => $mapper,
250+
'connection' => $this->databases->connection($mapper->connection()),
251+
]);
239252
}
240253
}

src/ConciseServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Articulate\Concise;
55

6+
use Illuminate\Database\DatabaseManager;
67
use Illuminate\Support\ServiceProvider;
78

89
class ConciseServiceProvider extends ServiceProvider
@@ -11,7 +12,10 @@ public function register(): void
1112
{
1213
// Register the Concise class
1314
$this->app->singleton(Concise::class, function () {
14-
return new Concise($this->app);
15+
return new Concise(
16+
$this->app,
17+
$this->app->make(DatabaseManager::class),
18+
);
1519
});
1620

1721
// Register the mappers' repositories

0 commit comments

Comments
 (0)