{"id":1081,"date":"2014-11-25T17:58:42","date_gmt":"2014-11-25T09:58:42","guid":{"rendered":"http:\/\/blog.turn.tw\/?page_id=1081"},"modified":"2014-11-25T17:58:42","modified_gmt":"2014-11-25T09:58:42","slug":"%e5%b0%91%e6%89%93%e5%b9%be%e5%80%8b%e5%ad%97%ef%bc%9asupportfacadesfacade-php","status":"publish","type":"page","link":"https:\/\/blog.turn.tw\/?page_id=1081","title":{"rendered":"\u5c11\u6253\u5e7e\u500b\u5b57\uff1aSupport\/Facades\/Facade.php"},"content":{"rendered":"<p>\u5728Support\/Facades\u4e2d\u7684\u5927\u90e8\u4efd\u985e\u5225\uff0c\u90fd\u662f\u7e7c\u627f\u6b64Facade\u985e\u5225\u3001\u53ea\u5be6\u4f5cgetFacadeAccessor\u51fd\u5f0f\u3002<br \/>\n\u7522\u751f\u5c11\u6253\u5f88\u591a\u5b57\u7684\u6548\u679c\uff1adeveloper\u53ea\u8981\u547c\u53ebfacade\u985e\u5225\u7684\u975c\u614b\u51fd\u5f0f\uff0cLaravel\u5c31\u6703\u81ea\u52d5\u53bb\u5c0d\u61c9\u5230\u67d0\u500b\u7269\u4ef6\u7684\u67d0\u500b\u65b9\u6cd5\u3002<br \/>\n\u7576\u7136\u4e86\uff0c\u4e0d\u5c11\u5de5\u7a0b\u5e2b\u8a0e\u53ad\u9019\u7a2e\u7279\u6548\uff0c\u53c3\u898b\uff1a<a href=\"https:\/\/blog.turn.tw\/?page_id=875\" title=\"\u8b93\u4f60\u5c11\u6253\u5f88\u591a\u5b57\uff1aFacades\u3002\">\u8b93\u4f60\u5c11\u6253\u5f88\u591a\u5b57\uff1aFacades\u3002<\/a><br \/>\n\u90a3\uff0c\u9019\u7a2e\u7279\u6548\u53c8\u662f\u5982\u4f55\u505a\u5230\u7684\u5462\uff1f<\/p>\n<pre>\r\n<?php namespace Illuminate\\Support\\Facades;\r\n\r\nuse Mockery\\MockInterface;\r\n\r\nabstract class Facade {\r\n\r\n\t\/**\r\n\t * The application instance being facaded.\r\n\t *\r\n\t * @var \\Illuminate\\Foundation\\Application\r\n\t *\/\r\n\tprotected static $app;\r\n\r\n\t\/**\r\n\t * The resolved object instances.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected static $resolvedInstance;\r\n\r\n\t\/**\r\n\t * Hotswap the underlying instance behind the facade.\r\n\t *\r\n\t * @param  mixed  $instance\r\n\t * @return void\r\n\t *\/\r\n\tpublic static function swap($instance)\r\n\t{\r\n\t\tstatic::$resolvedInstance[static::getFacadeAccessor()] = $instance;\r\n\r\n\t\tstatic::$app->instance(static::getFacadeAccessor(), $instance);\r\n\t}\r\n\r\n\t\/**\r\n\t * Initiate a mock expectation on the facade.\r\n\t *\r\n\t * @param  mixed\r\n\t * @return \\Mockery\\Expectation\r\n\t *\/\r\n\tpublic static function shouldReceive()\r\n\t{\r\n\t\t$name = static::getFacadeAccessor();\r\n\r\n\t\tif (static::isMock())\r\n\t\t{\r\n\t\t\t$mock = static::$resolvedInstance[$name];\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\t$mock = static::createFreshMockInstance($name);\r\n\t\t}\r\n\r\n\t\treturn call_user_func_array(array($mock, 'shouldReceive'), func_get_args());\r\n\t}\r\n\r\n\t\/**\r\n\t * Create a fresh mock instance for the given class.\r\n\t *\r\n\t * @param  string  $name\r\n\t * @return \\Mockery\\Expectation\r\n\t *\/\r\n\tprotected static function createFreshMockInstance($name)\r\n\t{\r\n\t\tstatic::$resolvedInstance[$name] = $mock = static::createMockByName($name);\r\n\r\n\t\tif (isset(static::$app))\r\n\t\t{\r\n\t\t\tstatic::$app->instance($name, $mock);\r\n\t\t}\r\n\r\n\t\treturn $mock;\r\n\t}\r\n\r\n\t\/**\r\n\t * Create a fresh mock instance for the given class.\r\n\t *\r\n\t * @param  string  $name\r\n\t * @return \\Mockery\\Expectation\r\n\t *\/\r\n\tprotected static function createMockByName($name)\r\n\t{\r\n\t\t$class = static::getMockableClass($name);\r\n\r\n\t\treturn $class ? \\Mockery::mock($class) : \\Mockery::mock();\r\n\t}\r\n\r\n\t\/**\r\n\t * Determines whether a mock is set as the instance of the facade.\r\n\t *\r\n\t * @return bool\r\n\t *\/\r\n\tprotected static function isMock()\r\n\t{\r\n\t\t$name = static::getFacadeAccessor();\r\n\r\n\t\treturn isset(static::$resolvedInstance[$name]) && static::$resolvedInstance[$name] instanceof MockInterface;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the mockable class for the bound instance.\r\n\t *\r\n\t * @return string\r\n\t *\/\r\n\tprotected static function getMockableClass()\r\n\t{\r\n\t\tif ($root = static::getFacadeRoot()) return get_class($root);\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the root object behind the facade.\r\n\t *\r\n\t * @return mixed\r\n\t *\/\r\n\tpublic static function getFacadeRoot()\r\n\t{\r\n\t\treturn static::resolveFacadeInstance(static::getFacadeAccessor());\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the registered name of the component.\r\n\t *\r\n\t * @return string\r\n\t *\r\n\t * @throws \\RuntimeException\r\n\t *\/\r\n\tprotected static function getFacadeAccessor()\r\n\t{\r\n\t\tthrow new \\RuntimeException(\"Facade does not implement getFacadeAccessor method.\");\r\n\t}\r\n\r\n\t\/**\r\n\t * Resolve the facade root instance from the container.\r\n\t *\r\n\t * @param  string  $name\r\n\t * @return mixed\r\n\t *\/\r\n\tprotected static function resolveFacadeInstance($name)\r\n\t{\r\n\t\tif (is_object($name)) return $name;\r\n\r\n\t\tif (isset(static::$resolvedInstance[$name]))\r\n\t\t{\r\n\t\t\treturn static::$resolvedInstance[$name];\r\n\t\t}\r\n\r\n\t\treturn static::$resolvedInstance[$name] = static::$app[$name];\r\n\t}\r\n\r\n\t\/**\r\n\t * Clear a resolved facade instance.\r\n\t *\r\n\t * @param  string  $name\r\n\t * @return void\r\n\t *\/\r\n\tpublic static function clearResolvedInstance($name)\r\n\t{\r\n\t\tunset(static::$resolvedInstance[$name]);\r\n\t}\r\n\r\n\t\/**\r\n\t * Clear all of the resolved instances.\r\n\t *\r\n\t * @return void\r\n\t *\/\r\n\tpublic static function clearResolvedInstances()\r\n\t{\r\n\t\tstatic::$resolvedInstance = array();\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the application instance behind the facade.\r\n\t *\r\n\t * @return \\Illuminate\\Foundation\\Application\r\n\t *\/\r\n\tpublic static function getFacadeApplication()\r\n\t{\r\n\t\treturn static::$app;\r\n\t}\r\n\r\n\t\/**\r\n\t * Set the application instance.\r\n\t *\r\n\t * @param  \\Illuminate\\Foundation\\Application  $app\r\n\t * @return void\r\n\t *\/\r\n\tpublic static function setFacadeApplication($app)\r\n\t{\r\n\t\tstatic::$app = $app;\r\n\t}\r\n\r\n\t\/**\r\n\t * Handle dynamic, static calls to the object.\r\n\t *\r\n\t * @param  string  $method\r\n\t * @param  array   $args\r\n\t * @return mixed\r\n\t *\/\r\n\tpublic static function __callStatic($method, $args)\r\n\t{\r\n\t\t$instance = static::getFacadeRoot();\r\n\r\n\t\tswitch (count($args))\r\n\t\t{\r\n\t\t\tcase 0:\r\n\t\t\t\treturn $instance->$method();\r\n\r\n\t\t\tcase 1:\r\n\t\t\t\treturn $instance->$method($args[0]);\r\n\r\n\t\t\tcase 2:\r\n\t\t\t\treturn $instance->$method($args[0], $args[1]);\r\n\r\n\t\t\tcase 3:\r\n\t\t\t\treturn $instance->$method($args[0], $args[1], $args[2]);\r\n\r\n\t\t\tcase 4:\r\n\t\t\t\treturn $instance->$method($args[0], $args[1], $args[2], $args[3]);\r\n\r\n\t\t\tdefault:\r\n\t\t\t\treturn call_user_func_array(array($instance, $method), $args);\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>\u6700\u91cd\u8981\u7684\u51fd\u5f0f\u53ea\u662f\u9019\u6a23\u800c\u5df2\u3002\u4f7f\u7528PHP\u7684magic method\u300c__callStatic\u300d\uff0c\u5728\u985e\u5225\u547c\u53eb\u975c\u614b\u51fd\u5f0f\u800c\u627e\u4e0d\u5230\u6642\uff0c\u4f7f\u7528getFacadeRoot\u627e\u5230\u5c0d\u61c9\u7684\u7269\u4ef6\uff0c\u7136\u5f8c\u6839\u64da\u53c3\u6578\u7684\u6578\u91cf\u53bb\u547c\u53eb\u7269\u4ef6\u7684\u65b9\u6cd5\u3002<br \/>\n<green><br \/>\n\u6ce8\u610f\u9019\u908a\u7684\u8a2d\u8a08\uff1a\u53c3\u6578\u57284\u500b\u4ee5\u4e0b\u90fd\u6703\u76f4\u63a5\u547c\u53eb\u65b9\u6cd5\uff0c\u8d85\u904e\u7684\u8a71\u5247\u7528call_user_func_array\u547c\u53eb\u65b9\u6cd5\u3002<br \/>\n\u9019\u662f\u56e0\u70bacall_user_func_array\u57f7\u884c\u6642\u6709\u4e00\u9ede\u9ede\u6162\u7684\u95dc\u4fc2\u3002<br \/>\n<\/greeen><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Support\/Facades\u4e2d\u7684\u5927\u90e8\u4efd\u985e\u5225\uff0c\u90fd\u662f\u7e7c\u627f\u6b64Facade\u985e\u5225\u3001\u53ea\u5be6\u4f5cgetFacadeAcces &hellip; <a href=\"https:\/\/blog.turn.tw\/?page_id=1081\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">\u5c11\u6253\u5e7e\u500b\u5b57\uff1aSupport\/Facades\/Facade.php<\/span> <span class=\"meta-nav\">&rarr;<\/span> <\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_mi_skip_tracking":false},"_links":{"self":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1081"}],"collection":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1081"}],"version-history":[{"count":4,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1081\/revisions"}],"predecessor-version":[{"id":1085,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1081\/revisions\/1085"}],"wp:attachment":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}