Skip to content

Commit 5e7c87f

Browse files
authored
Merge pull request #152 from NottingHack/repository-service-provider
RepositoryServiceProvider: dynamic loading of out repos
2 parents abce855 + 36f913e commit 5e7c87f

File tree

2 files changed

+59
-49
lines changed

2 files changed

+59
-49
lines changed

app/Providers/RepositoryServiceProvider.php

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,10 @@
22

33
namespace App\Providers;
44

5-
use HMS\Entities\Link;
6-
use HMS\Entities\Meta;
7-
use HMS\Entities\Role;
8-
use HMS\Entities\User;
9-
use HMS\Entities\Invite;
10-
use HMS\Entities\Profile;
11-
use HMS\Entities\Banking\Account;
12-
use HMS\Repositories\LinkRepository;
13-
use HMS\Repositories\MetaRepository;
14-
use HMS\Repositories\RoleRepository;
15-
use HMS\Repositories\UserRepository;
16-
use HMS\Repositories\InviteRepository;
17-
use HMS\Repositories\ProfileRepository;
185
use Illuminate\Support\ServiceProvider;
196
use HMS\Repositories\PermissionRepository;
20-
use HMS\Repositories\Banking\AccountRepository;
217
use LaravelDoctrine\ACL\Permissions\Permission;
22-
use HMS\Repositories\Doctrine\DoctrineLinkRepository;
23-
use HMS\Repositories\Doctrine\DoctrineMetaRepository;
24-
use HMS\Repositories\Doctrine\DoctrineRoleRepository;
25-
use HMS\Repositories\Doctrine\DoctrineUserRepository;
26-
use HMS\Repositories\Doctrine\DoctrineInviteRepository;
27-
use HMS\Repositories\Doctrine\DoctrineProfileRepository;
288
use HMS\Repositories\Doctrine\DoctrinePermissionRepository;
29-
use HMS\Repositories\Banking\Doctrine\DoctrineAccountRepository;
309

3110
class RepositoryServiceProvider extends ServiceProvider
3211
{
@@ -47,36 +26,23 @@ public function boot()
4726
*/
4827
public function register()
4928
{
50-
$this->app->singleton(LinkRepository::class, function ($app) {
51-
return new DoctrineLinkRepository($app['em'], $app['em']->getClassMetaData(Link::class));
52-
});
53-
54-
$this->app->singleton(MetaRepository::class, function ($app) {
55-
return new DoctrineMetaRepository($app['em'], $app['em']->getClassMetaData(Meta::class));
56-
});
57-
58-
$this->app->singleton(InviteRepository::class, function ($app) {
59-
return new DoctrineInviteRepository($app['em'], $app['em']->getClassMetaData(Invite::class));
60-
});
61-
62-
$this->app->singleton(RoleRepository::class, function ($app) {
63-
return new DoctrineRoleRepository($app['em'], $app['em']->getClassMetaData(Role::class));
64-
});
65-
66-
$this->app->singleton(UserRepository::class, function ($app) {
67-
return new DoctrineUserRepository($app['em'], $app['em']->getClassMetaData(User::class));
68-
});
69-
70-
$this->app->singleton(ProfileRepository::class, function ($app) {
71-
return new DoctrineProfileRepository($app['em'], $app['em']->getClassMetaData(Profile::class));
72-
});
73-
29+
foreach (config('repositories.repositories') as $repository) {
30+
$entity = config('repositories.entity_namespace') . '\\' . $repository;
31+
$interface = config('repositories.repositoriy_namespace') . '\\' . $repository . 'Repository';
32+
$implmentation = config('repositories.repositoriy_namespace') . '\\' .
33+
(strpos($repository, '\\') ? explode('\\', $repository)[0] . '\\' : '') .
34+
'Doctrine\\Doctrine' .
35+
(strpos($repository, '\\') ? explode('\\', $repository)[1] : $repository) .
36+
'Repository';
37+
38+
$this->app->singleton($interface, function ($app) use ($implmentation, $entity) {
39+
return new $implmentation($app['em'], $app['em']->getClassMetaData($entity));
40+
});
41+
}
42+
43+
// Special case so do this one by hand
7444
$this->app->singleton(PermissionRepository::class, function ($app) {
7545
return new DoctrinePermissionRepository($app['em'], $app['em']->getClassMetaData(Permission::class));
7646
});
77-
78-
$this->app->singleton(AccountRepository::class, function ($app) {
79-
return new DoctrineAccountRepository($app['em'], $app['em']->getClassMetaData(Account::class));
80-
});
8147
}
8248
}

config/repositories.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Repository Interface Namespace
7+
|--------------------------------------------------------------------------
8+
|
9+
| This base namespace prefix will be used with each of the repositories listed
10+
| below to generate the full interface and implementation class names.
11+
|
12+
*/
13+
'repositoriy_namespace' => 'HMS\Repositories',
14+
15+
/*
16+
|--------------------------------------------------------------------------
17+
| Entity Namespace
18+
|--------------------------------------------------------------------------
19+
|
20+
| This base prefix will be used with each repositories listed below to
21+
| generate the Entity full class name.
22+
|
23+
*/
24+
'entity_namespace' => 'HMS\Entities',
25+
26+
/*
27+
|--------------------------------------------------------------------------
28+
| Autoloaded Repositories
29+
|--------------------------------------------------------------------------
30+
|
31+
| The repositories listed here will be automatically loaded on the
32+
| request to your application.
33+
|
34+
*/
35+
'repositories' => [
36+
'Link',
37+
'Meta',
38+
'Role',
39+
'User',
40+
'Invite',
41+
'Profile',
42+
'Banking\Account',
43+
],
44+
];

0 commit comments

Comments
 (0)