1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait PrefixedContainer.php. |
14
|
|
|
* |
15
|
|
|
* Part of Overtrue\WeChat. |
16
|
|
|
* |
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
18
|
|
|
* file that was distributed with this source code. |
19
|
|
|
* |
20
|
|
|
* @author mingyoung <[email protected]> |
21
|
|
|
* @copyright 2017 |
22
|
|
|
* |
23
|
|
|
* @see https://github.com/overtrue |
24
|
|
|
* @see http://overtrue.me |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace EasyWeChat\Support\Traits; |
28
|
|
|
|
29
|
|
|
use EasyWeChat\Support\Str; |
30
|
|
|
use Pimple\Container; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Trait PrefixedContainer. |
34
|
|
|
*/ |
35
|
|
|
trait PrefixedContainer |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Container. |
39
|
|
|
* |
40
|
|
|
* @var \Pimple\Container |
41
|
|
|
*/ |
42
|
|
|
protected $container; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* ContainerAccess constructor. |
46
|
|
|
* |
47
|
|
|
* @param \Pimple\Container $container |
48
|
|
|
*/ |
49
|
|
|
public function __construct(Container $container) |
50
|
|
|
{ |
51
|
|
|
$this->container = $container; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Fetches from pimple container. |
56
|
|
|
* |
57
|
|
|
* @param string $key |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function fetch($key) |
62
|
|
|
{ |
63
|
|
|
return $this->$key; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets a parameter or an object from pimple container. |
68
|
|
|
* |
69
|
|
|
* Get the `class basename` of the current class. |
70
|
|
|
* Convert `class basename` to snake-case and concatenation with dot notation. |
71
|
|
|
* |
72
|
|
|
* E.g. Class 'EasyWechat', $key foo -> 'easy_wechat.foo' |
73
|
|
|
* |
74
|
|
|
* @param string $key The unique identifier for the parameter or object |
75
|
|
|
* |
76
|
|
|
* @return mixed The value of the parameter or an object |
77
|
|
|
* |
78
|
|
|
* @throws \InvalidArgumentException If the identifier is not defined |
79
|
|
|
*/ |
80
|
|
|
public function __get($key) |
81
|
|
|
{ |
82
|
|
|
$className = basename(str_replace('\\', '/', static::class)); |
83
|
|
|
|
84
|
|
|
$name = Str::snake($className).'.'.$key; |
85
|
|
|
|
86
|
|
|
return $this->container->offsetGet($name); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|