关于 Facades 里 @method 的地方,有点不明白的地方

laravel和很多第三方包,类库里都有类似Facade的类顶部都有漂亮的@method标签,不知道这是怎么生成的。PHPStorm提示能点击,但是点了暂时没能到达指定方法。

/**
 * @method static mixed guard(string|null $name = null)
 * @method static void shouldUse(string $name);
 * @method static bool check()
 * @method static bool guest()
 * @method static \Illuminate\Contracts\Auth\Authenticatable|null user()
 * @method static int|null id()
 * @method static bool validate(array $credentials = [])
 * @method static void setUser(\Illuminate\Contracts\Auth\Authenticatable $user)
 * @method static bool attempt(array $credentials = [], bool $remember = false)
 * @method static bool once(array $credentials = [])
 * @method static void login(\Illuminate\Contracts\Auth\Authenticatable $user, bool $remember = false)
 * @method static \Illuminate\Contracts\Auth\Authenticatable loginUsingId(mixed $id, bool $remember = false)
 * @method static bool onceUsingId(mixed $id)
 * @method static bool viaRemember()
 * @method static void logout()
 *
 * @see \Illuminate\Auth\AuthManager
 * @see \Illuminate\Contracts\Auth\Factory
 * @see \Illuminate\Contracts\Auth\Guard
 * @see \Illuminate\Contracts\Auth\StatefulGuard
 */
class Auth extends Facade
{
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
leo
最佳答案

这里的注释只是起到一个 IDE 代码提示的作用,对本身功能没有任何影响。

至于怎么找真正定义的地方,参考以下流程:

  1. https://github.com/laravel/framework/blob/... 可知 Auth::xxx 等价于 app('auth')->xxx,所以需要找到定义 auth 的地方;
  2. 根据对 Laravel 的尿性很容易推断出在这个类里 Illuminate\Auth\AuthServiceProvider,扫一眼代码可以看到 https://github.com/laravel/framework/blob/...
  3. 直接看闭包的最后一行,返回的是 AuthManager 对象,你要找的方法定义都在里面。
4年前 评论
讨论数量: 3
leo

这里的注释只是起到一个 IDE 代码提示的作用,对本身功能没有任何影响。

至于怎么找真正定义的地方,参考以下流程:

  1. https://github.com/laravel/framework/blob/... 可知 Auth::xxx 等价于 app('auth')->xxx,所以需要找到定义 auth 的地方;
  2. 根据对 Laravel 的尿性很容易推断出在这个类里 Illuminate\Auth\AuthServiceProvider,扫一眼代码可以看到 https://github.com/laravel/framework/blob/...
  3. 直接看闭包的最后一行,返回的是 AuthManager 对象,你要找的方法定义都在里面。
4年前 评论
leo

这里的注释只是起到一个 IDE 代码提示的作用,对本身功能没有任何影响。

至于怎么找真正定义的地方,参考以下流程:

  1. https://github.com/laravel/framework/blob/... 可知 Auth::xxx 等价于 app('auth')->xxx,所以需要找到定义 auth 的地方;
  2. 根据对 Laravel 的尿性很容易推断出在这个类里 Illuminate\Auth\AuthServiceProvider,扫一眼代码可以看到 https://github.com/laravel/framework/blob/...
  3. 直接看闭包的最后一行,返回的是 AuthManager 对象,你要找的方法定义都在里面。
4年前 评论

我稍微研究了一下,根据调用栈推测应该是根据方法名调用父类callStatic,再调用本类call,再调用到真实类型中的方法,比如

Router::middleware( 最后调用到RouteRegistrar的__call,再调用到attribute方法。

不知道能不能帮助大家

3年前 评论
kinra丶 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!