1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
11
|
|
|
* THE SOFTWARE. |
12
|
|
|
* |
13
|
|
|
* This software consists of voluntary contributions made by many individuals |
14
|
|
|
* and is licensed under the MIT license. |
15
|
|
|
* |
16
|
|
|
* Copyright (c) 2015-2020 Yuuki Takezawa |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Ytake\LaravelAspect; |
21
|
|
|
|
22
|
|
|
use Illuminate\Contracts\Container\Container; |
23
|
|
|
use Ray\Aop\BindInterface; |
24
|
|
|
use Ray\Aop\WeavedInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ContainerInterceptor |
28
|
|
|
*/ |
29
|
|
|
final class ContainerInterceptor |
30
|
|
|
{ |
31
|
|
|
/** @var Container|\Illuminate\Container\Container */ |
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
/** @var AnnotateClass */ |
35
|
|
|
private $annotateClass; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Container $container |
39
|
|
|
* @param AnnotateClass $annotateClass |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Container $container, AnnotateClass $annotateClass) |
42
|
|
|
{ |
43
|
|
|
$this->container = $container; |
44
|
|
|
$this->annotateClass = $annotateClass; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $abstract |
49
|
|
|
* @param BindInterface $bind |
50
|
|
|
* @param string $className |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function intercept(string $abstract, BindInterface $bind, string $className): bool |
55
|
|
|
{ |
56
|
|
|
if ($abstract === $className) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (isset($this->container->contextual[$abstract])) { |
|
|
|
|
61
|
|
|
$this->resolveContextualBindings($abstract, $className); |
62
|
|
|
} |
63
|
|
|
$this->container->bind($abstract, function (Container $app, array $params = []) use ($bind, $className) { |
64
|
|
|
/** @var WeavedInterface $instance */ |
65
|
|
|
$instance = $app->make($className, $params); |
66
|
|
|
$instance->bindings = $bind->getBindings(); |
|
|
|
|
67
|
|
|
$method = $this->annotateClass->getPostConstructMethod($instance); |
68
|
|
|
if (!empty($method)) { |
69
|
|
|
$instance->bindings = $bind->getBindings(); |
|
|
|
|
70
|
|
|
$instance->$method(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $instance; |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $class |
81
|
|
|
* @param string $compiledClass |
82
|
|
|
*/ |
83
|
|
|
private function resolveContextualBindings(string $class, string $compiledClass): void |
84
|
|
|
{ |
85
|
|
|
foreach ($this->container->contextual[$class] as $abstract => $concrete) { |
|
|
|
|
86
|
|
|
$this->container->when($compiledClass) |
87
|
|
|
->needs($abstract) |
88
|
|
|
->give($concrete); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: