Lumen 中 '$app->运行()' 的实现在哪里?

Where is the implementation of '$app->run()' in Lumen?

我找不到在 Lumen 上使用的 run() 方法的实现定义在哪里。能看的那个in the bootstrapping file:

$app->run();

这个方法是在哪里定义的?

它是在 Laravel\Lumen\Concerns\RoutesRequests 上定义的。

如果你看一下 bootstrap/app.php 你会看到这个:

$app = new Laravel\Lumen\Application(
    dirname(__DIR__)
);

所以我们知道$appLaravel\Lumen\Application的一个实例。

此 class 上未定义方法 run(),但如果仔细观察,您会看到:

class Application extends Container
{
    use Concerns\RoutesRequests,
        Concerns\RegistersExceptionHandlers;

那些traits define additional behaviour for the class。具体来说,在 Laravel\Lumen\Concerns\RoutesRequests 你会发现:

/**
 * Run the application and send the response.
 *
 * @param  SymfonyRequest|null  $request
 * @return void
 */
public function run($request = null)
{
    $response = $this->dispatch($request);

    if ($response instanceof SymfonyResponse) {
        $response->send();
    } else {
        echo (string) $response;
    }

    if (count($this->middleware) > 0) {
        $this->callTerminableMiddleware($response);
    }
}