Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ちょっと深掘りLaravelライフサイクル

 ちょっと深掘りLaravelライフサイクル

7/8(sat) Laravelもくもく会@表参道のLT発表資料

k-kurikuri

July 08, 2017
Tweet

More Decks by k-kurikuri

Other Decks in Programming

Transcript

  1. <?php
 
 require __DIR__.'/../bootstrap/autoload.php';
 
 $app = require_once __DIR__.'/../bootstrap/app.php';
 


    $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
 
 $response = $kernel->handle(
 $request = Illuminate\Http\Request::capture()
 );
 
 $response->send();
 
 $kernel->terminate($request, $response);
  2. Kernel • ϒϥοΫϘοΫεʹΞϓϦέʔγϣϯશମΛૢ࡞͢ΔΫϥε • Http͔ConsoleϦΫΤετʹΑͬͯੜ੒͢ΔKernelΫϥε͕มΘΔ • handleϝιουͰΞϓϦέʔγϣϯͷ͋Ε΍͜Ε΍Λૢ࡞ • Env, Config,

    Handler, Facade, Service ProviderͷॳظԽॲཧ • PipelineΫϥεΛ࢖༻͠MiddlewareͷϋϯυϦϯάɾRouterͱ ControllerσΟεύονΛߦ͏
  3. <?php
 
 namespace Illuminate\Foundation\Http;
 
 use Exception;
 use Throwable;
 use

    Illuminate\Routing\Router;
 use Illuminate\Pipeline\Pipeline;
 use Illuminate\Support\Facades\Facade;
 use Illuminate\Contracts\Foundation\Application;
 use Illuminate\Contracts\Http\Kernel as KernelContract; use Symfony\Component\Debug\Exception\FatalThrowableError
 
 class Kernel implements KernelContract
 {
 protected $app;
 
 protected $router;
 
 protected $bootstrappers = [
 'Illuminate\Foundation\Bootstrap\DetectEnvironment',
 'Illuminate\Foundation\Bootstrap\LoadConfiguration',
 'Illuminate\Foundation\Bootstrap\ConfigureLogging',
 'Illuminate\Foundation\Bootstrap\HandleExceptions',
 'Illuminate\Foundation\Bootstrap\RegisterFacades',
 'Illuminate\Foundation\Bootstrap\RegisterProviders',
 'Illuminate\Foundation\Bootstrap\BootProviders',
 ]; // লུ
 }
  4. Application (Container) • αʔϏείϯςφͱͯ͠Πϯελϯεͷ؅ཧΛߦ͏ • Kernelͱ͸ผʹLog, Event, RoutingͷServiceProviderΛ ؅ཧ͍ͯ͠Δ •

    Laravelόʔδϣϯɺ.envϑΝΠϧ໊ΛϓϩύςΟʹ࣋ͭ • Kernel͔ΒґཔΛड͚ServiceProviderͷregisterΛݺͼ ग़͢
  5. <?php
 
 namespace Illuminate\Foundation;
 
 use Closure;
 use RuntimeException;
 use

    Illuminate\Support\Arr;
 use Illuminate\Support\Str;
 use Illuminate\Http\Request;
 use Illuminate\Container\Container;
 use Illuminate\Filesystem\Filesystem;
 use Illuminate\Log\LogServiceProvider;
 use Illuminate\Support\ServiceProvider;
 use Illuminate\Events\EventServiceProvider;
 use Illuminate\Routing\RoutingServiceProvider;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 use Illuminate\Contracts\Http\Kernel as HttpKernelContract;
 use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
 use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Illuminate\Contracts\Foundation\Application as ApplicationContract;
 
 class Application extends Container implements ApplicationContract, HttpKernelInterface
 {
 /**
 * The Laravel framework version.
 *
 * @var string
 */
 const VERSION = '5.4.21';
 
 
 /**
 * The environment file to load during bootstrapping.
 *
 * @var string
 */
 protected $environmentFile = ‘.env'; // লུ
 }
  6. Service Provider • αʔϏε(֤ػೳ)ʹಛԽͨ͠ॳظԽΫϥε • ओʹregister, bootϝιουͰߏ੒͞ΕΔ • register͸ίϯςφʹొ࿥͢ΔͨΊͷϝιου •

    boot͸registerͷޙʹݺ͹ΕΔͨΊશͯͷ ServiceProviderʹΞΫηεͰ͖ΔɻArtisan vendor:publishͱ͔͸͜͜Ͱߦ͏
  7. 
 /**
 * Send the given request through the middleware

    / router.
 *
 * @param \Illuminate\Http\Request $request
 * @return \Illuminate\Http\Response
 */
 protected function sendRequestThroughRouter($request)
 {
 $this->app->instance('request', $request);
 
 Facade::clearResolvedInstance('request');
 
 $this->bootstrap();
 
 return (new Pipeline($this->app))
 ->send($request)
 ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
 ->then($this->dispatchToRouter());
 }
  8. /**
 * Run the route action and return the response.


    *
 * @return mixed
 *
 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
 */
 protected function runController()
 {
 return (new ControllerDispatcher($this->container)) ->dispatch(
 $this, $this->getController(), $this->getControllerMethod()
 );
 }
  9. <?php
 
 namespace Illuminate\Http;
 
 use ArrayObject;
 use JsonSerializable;
 use

    Illuminate\Contracts\Support\Jsonable;
 use Illuminate\Contracts\Support\Renderable;
 use Symfony\Component\HttpFoundation\Response as BaseResponse;
 
 class Response extends BaseResponse
 {
 use ResponseTrait;
 
 public function setContent($content)
 {
 $this->original = $content;
 
 if ($this->shouldBeJson($content)) {
 $this->header('Content-Type', 'application/json');
 
 $content = $this->morphToJson($content);
 }
 elseif ($content instanceof Renderable) {
 $content = $content->render();
 }
 
 return parent::setContent($content);
 }