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\Traits; |
17
|
|
|
|
18
|
|
|
trait Bindable |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The repository alias pattern. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $repositoryAliasPattern = '{{class}}Contract'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Register an IoC binding whether it's already been registered or not. |
29
|
|
|
* |
30
|
|
|
* @param string $abstract |
31
|
|
|
* @param \Closure|string|null $concrete |
32
|
|
|
* @param bool $shared |
33
|
|
|
* @param string|null $alias |
34
|
|
|
* @param bool $force |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
protected function bindRepository($abstract, $concrete = null, $shared = true, $alias = null, $force = false) |
39
|
|
|
{ |
40
|
|
|
if (! $this->app->bound($abstract) || $force) { |
41
|
|
|
$concrete = $concrete ?: $abstract; |
42
|
|
|
$this->app->bind($abstract, $concrete, $shared); |
|
|
|
|
43
|
|
|
$this->app->alias($abstract, $this->prepareRepositoryAlias($alias, $concrete)); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Prepare the repository alias. |
49
|
|
|
* |
50
|
|
|
* @param string|null $alias |
51
|
|
|
* @param mixed $concrete |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
protected function prepareRepositoryAlias($alias, $concrete) |
56
|
|
|
{ |
57
|
|
|
if (! $alias && ! $concrete instanceof \Closure) { |
|
|
|
|
58
|
|
|
$concrete = str_replace('Repositories', 'Contracts', $concrete); |
59
|
|
|
$alias = str_replace('{{class}}', $concrete, $this->repositoryAliasPattern); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $alias; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: