-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFacade.php
70 lines (60 loc) · 1.66 KB
/
Facade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Darya\Service;
use RuntimeException;
use Darya\Service\Contracts\Container as ContainerInterface;
/**
* Darya's service facade implementation. Very similar to Laravel's approach.
*
* @author Chris Andrew <[email protected]>
*/
abstract class Facade
{
/**
* The service container to use for service facades.
*
* @var ContainerInterface
*/
protected static $serviceContainer;
/**
* Set the service container to use for all facades.
*
* @param ContainerInterface $container
*/
public static function setServiceContainer(ContainerInterface $container)
{
static::$serviceContainer = $container;
}
/**
* Return the service interface or alias to resolve from the container.
*
* All facades must override this method.
*
* @return string
*/
public static function getServiceName()
{
return 'Darya\Service\Contracts\Container';
}
/**
* Magic method that redirects static calls to the facade's related service.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
$service = static::getServiceName();
if (!static::$serviceContainer) {
throw new RuntimeException('Tried to use a facade without setting a service container');
}
$instance = static::$serviceContainer->get($service);
if (!is_object($instance)) {
throw new RuntimeException('Facade resolved non-object from the service container');
}
if (!method_exists($instance, $method)) {
throw new RuntimeException('Call to non-existent method "' . $method . '" on facade instance');
}
return static::$serviceContainer->call(array($instance, $method), $parameters);
}
}