1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the FacadeBundle |
||
5 | * |
||
6 | * (c) Indra Gunawan <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Indragunawan\FacadeBundle; |
||
13 | |||
14 | use Psr\Container\ContainerInterface; |
||
15 | |||
16 | /** |
||
17 | * @author Indra Gunawan <[email protected]> |
||
18 | */ |
||
19 | abstract class AbstractFacade |
||
20 | { |
||
21 | protected static $container; |
||
22 | |||
23 | /** |
||
24 | * Facade service container. |
||
25 | */ |
||
26 | 3 | public static function setFacadeContainer(ContainerInterface $container) |
|
27 | { |
||
28 | 3 | static::$container = $container; |
|
29 | 3 | } |
|
30 | |||
31 | /** |
||
32 | * Get the registered id of the service. |
||
33 | * |
||
34 | * @return string |
||
35 | */ |
||
36 | abstract protected static function getFacadeAccessor(); |
||
37 | |||
38 | /** |
||
39 | * Handle dynamic calls to the service. |
||
40 | * |
||
41 | * @param string $method |
||
42 | * @param array $arguments |
||
43 | * |
||
44 | * @return mixed |
||
45 | * |
||
46 | * @throws \RuntimeException |
||
47 | */ |
||
48 | 2 | public static function __callStatic($method, $arguments) |
|
49 | { |
||
50 | 2 | $class = \get_called_class(); |
|
51 | |||
52 | 2 | if (!static::$container->has($class)) { |
|
53 | 1 | throw new \RuntimeException(sprintf('"%s" facade has not been register.', $class)); |
|
54 | } |
||
55 | |||
56 | 1 | $service = static::$container->get($class); |
|
57 | |||
58 | 1 | return $service->$method(...$arguments); |
|
59 | } |
||
60 | } |
||
61 |