1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the mucts.com. |
4
|
|
|
* |
5
|
|
|
* This source file is subject to the MIT license that is bundled |
6
|
|
|
* with this source code in the file LICENSE. |
7
|
|
|
* |
8
|
|
|
* @version 1.0 |
9
|
|
|
* @author herry<[email protected]> |
10
|
|
|
* @copyright © 2020 MuCTS.com All Rights Reserved. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace MuCTS\Sobot\Contracts; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
use MuCTS\Sobot\Exceptions\NotFoundException; |
17
|
|
|
use MuCTS\Sobot\Exceptions\ParamException; |
18
|
|
|
|
19
|
|
|
abstract class Container |
20
|
|
|
{ |
21
|
|
|
/** @var array */ |
22
|
|
|
protected $config; |
23
|
|
|
/** @var Cache|null */ |
24
|
|
|
protected $cache; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Container constructor. |
28
|
|
|
* @param array $config |
29
|
|
|
* @param string|Cache|null $cache |
30
|
|
|
* @throws ParamException |
31
|
|
|
*/ |
32
|
|
|
public function __construct(array $config, $cache = null) |
33
|
|
|
{ |
34
|
|
|
$this->config = $config; |
35
|
|
|
if (is_string($cache) && class_exists($cache)) { |
36
|
|
|
$cache = new $cache; |
37
|
|
|
} |
38
|
|
|
if (!is_null($cache)) { |
39
|
|
|
if (is_object($cache) && in_array(Cache::class, class_implements($cache))) { |
40
|
|
|
$this->cache = $cache; |
41
|
|
|
} else { |
42
|
|
|
throw new ParamException(sprintf('Cache Not Implements %s', Cache::class)); |
43
|
|
|
} |
44
|
|
|
} else { |
45
|
|
|
$this->cache = null; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $name |
51
|
|
|
* @param $arguments |
52
|
|
|
* @return mixed |
53
|
|
|
* @throws NotFoundException |
54
|
|
|
*/ |
55
|
|
|
public function __call($name, $arguments) |
56
|
|
|
{ |
57
|
|
|
$name = underline_to_hump($name); |
58
|
|
|
$class = get_class($this) . '\\' . $name; |
59
|
|
|
if (class_exists($class)) { |
60
|
|
|
return new $class($this->config, $this->cache); |
61
|
|
|
} |
62
|
|
|
$class = get_namespace($this) . '\\' . $name; |
63
|
|
|
if (class_exists($class)) { |
64
|
|
|
return new $class($this->config, $this->cache); |
65
|
|
|
} |
66
|
|
|
$class = get_namespace($this) . '\\' . $name . '\\Client'; |
67
|
|
|
if (class_exists($class)) { |
68
|
|
|
return new $class($this->config, $this->cache); |
69
|
|
|
} |
70
|
|
|
$class = get_namespace($this) . '\\' . $name . '\\' . $name; |
71
|
|
|
if (class_exists($class)) { |
72
|
|
|
return new $class($this->config, $this->cache); |
73
|
|
|
} |
74
|
|
|
throw new NotFoundException(sprintf('Class [%s] not found', $class)); |
75
|
|
|
} |
76
|
|
|
} |