1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Repository Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Repository Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Repository\Providers; |
17
|
|
|
|
18
|
|
|
use Illuminate\Support\ServiceProvider; |
19
|
|
|
use Rinvex\Repository\Listeners\RepositoryEventListener; |
20
|
|
|
|
21
|
|
|
class RepositoryServiceProvider extends ServiceProvider |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The repository alias pattern. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $repositoryAliasPattern = '{{class}}Contract'; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function register() |
34
|
|
|
{ |
35
|
|
|
// Merge config |
36
|
|
|
$this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'rinvex.repository'); |
37
|
|
|
|
38
|
|
|
// Register the event listener |
39
|
|
|
$this->app->bind('rinvex.repository.listener', RepositoryEventListener::class); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function boot() |
46
|
|
|
{ |
47
|
|
|
// Publish Resources |
48
|
|
|
$this->publishResources(); |
49
|
|
|
|
50
|
|
|
// Subscribe the registered event listener |
51
|
|
|
$this->app['events']->subscribe('rinvex.repository.listener'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Publish package resources. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
protected function publishResources() |
60
|
|
|
{ |
61
|
|
|
// Publish config |
62
|
|
|
$this->publishes([ |
63
|
|
|
realpath(__DIR__.'/../../config/config.php') => config_path('rinvex.repository.php'), |
64
|
|
|
], 'config'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Register an IoC binding whether it's already been registered or not. |
69
|
|
|
* |
70
|
|
|
* @param string $abstract |
71
|
|
|
* @param \Closure|string|null $concrete |
72
|
|
|
* @param bool $shared |
73
|
|
|
* @param string|null $alias |
74
|
|
|
* @param bool $force |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
protected function bindRepository($abstract, $concrete = null, $shared = true, $alias = null, $force = false) |
79
|
|
|
{ |
80
|
|
|
if (! $this->app->bound($abstract) || $force) { |
81
|
|
|
$concrete = $concrete ?: $abstract; |
82
|
|
|
$this->app->bind($abstract, $concrete, $shared); |
83
|
|
|
$this->app->alias($abstract, $this->prepareRepositoryAlias($alias, $concrete)); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Prepare the repository alias. |
89
|
|
|
* |
90
|
|
|
* @param string|null $alias |
91
|
|
|
* @param mixed $concrete |
92
|
|
|
* |
93
|
|
|
* @return string |
|
|
|
|
94
|
|
|
*/ |
95
|
|
|
protected function prepareRepositoryAlias($alias, $concrete) |
96
|
|
|
{ |
97
|
|
|
if (! $alias && ! $concrete instanceof \Closure) { |
|
|
|
|
98
|
|
|
$concrete = str_replace('Repositories', 'Contracts', $concrete); |
99
|
|
|
$alias = str_replace('{{class}}', $concrete, $this->repositoryAliasPattern); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $alias; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.