1: <?php namespace Illuminate\Support\Facades;
2:
3: use Mockery\MockInterface;
4:
5: abstract class Facade {
6:
7: 8: 9: 10: 11:
12: protected static $app;
13:
14: 15: 16: 17: 18:
19: protected static $resolvedInstance;
20:
21: 22: 23: 24: 25: 26:
27: public static function swap($instance)
28: {
29: static::$resolvedInstance[static::getFacadeAccessor()] = $instance;
30:
31: static::$app->instance(static::getFacadeAccessor(), $instance);
32: }
33:
34: 35: 36: 37: 38: 39:
40: public static function shouldReceive()
41: {
42: $name = static::getFacadeAccessor();
43:
44: if (static::isMock())
45: {
46: $mock = static::$resolvedInstance[$name];
47: }
48: else
49: {
50: static::$resolvedInstance[$name] = $mock = \Mockery::mock(static::getMockableClass($name));
51:
52: static::$app->instance($name, $mock);
53: }
54:
55: return call_user_func_array(array($mock, 'shouldReceive'), func_get_args());
56: }
57:
58: 59: 60: 61: 62:
63: protected static function isMock()
64: {
65: $name = static::getFacadeAccessor();
66:
67: return isset(static::$resolvedInstance[$name]) and static::$resolvedInstance[$name] instanceof MockInterface;
68: }
69:
70: 71: 72: 73: 74:
75: protected static function getMockableClass()
76: {
77: return get_class(static::getFacadeRoot());
78: }
79:
80: 81: 82: 83: 84:
85: public static function getFacadeRoot()
86: {
87: return static::resolveFacadeInstance(static::getFacadeAccessor());
88: }
89:
90: 91: 92: 93: 94:
95: protected static function getFacadeAccessor()
96: {
97: throw new \RuntimeException("Facade does not implement getFacadeAccessor method.");
98: }
99:
100: 101: 102: 103: 104: 105:
106: protected static function resolveFacadeInstance($name)
107: {
108: if (is_object($name)) return $name;
109:
110: if (isset(static::$resolvedInstance[$name]))
111: {
112: return static::$resolvedInstance[$name];
113: }
114:
115: return static::$resolvedInstance[$name] = static::$app[$name];
116: }
117:
118: 119: 120: 121: 122: 123:
124: public static function clearResolvedInstance($name)
125: {
126: unset(static::$resolvedInstance[$name]);
127: }
128:
129: 130: 131: 132: 133:
134: public static function clearResolvedInstances()
135: {
136: static::$resolvedInstance = array();
137: }
138:
139: 140: 141: 142: 143:
144: public static function getFacadeApplication()
145: {
146: return static::$app;
147: }
148:
149: 150: 151: 152: 153: 154:
155: public static function setFacadeApplication($app)
156: {
157: static::$app = $app;
158: }
159:
160: 161: 162: 163: 164: 165: 166:
167: public static function __callStatic($method, $args)
168: {
169: $instance = static::resolveFacadeInstance(static::getFacadeAccessor());
170:
171: switch (count($args))
172: {
173: case 0:
174: return $instance->$method();
175:
176: case 1:
177: return $instance->$method($args[0]);
178:
179: case 2:
180: return $instance->$method($args[0], $args[1]);
181:
182: case 3:
183: return $instance->$method($args[0], $args[1], $args[2]);
184:
185: case 4:
186: return $instance->$method($args[0], $args[1], $args[2], $args[3]);
187:
188: default:
189: return call_user_func_array(array($instance, $method), $args);
190: }
191: }
192:
193: }