LoginSignup
1
2

More than 5 years have passed since last update.

FuelPHPで実装したAPIを呼んだ時の基本的な流れ

Posted at

自分用のメモ

Controller のテストを書くにあたり、インスタンスがいつ作られるのか知りたかったので、そこまでを調査

public/index.php

routerequest が呼ばれる

index.php
try
{
    // Boot the app...
    require APPPATH.'bootstrap.php';

    // 次: ここ
    // ... and execute the main request
    $response = $routerequest();
}

Request::forge(\$route, false)->execute(array($e))->response() が呼ばれる

index.php
$routerequest = function($route = null, $e = false)
{
    中略
    if ($route instanceof Closure)
    {
        中略
    }
    elseif ($route)
    {
        // 次: ここ
        $response = Request::forge($route, false)->execute(array($e))->response();
    }
    中略

    return $response;
};

core/classes/request.php

new static が呼ばれる

request.php
public static function forge($uri = null, $options = true, $method = null)
{
    中略
    if ( ! empty($options['driver']))
    {
        中略
    }
    // 次: ここ
    $request = new static($uri, isset($options['route']) ? $options['route'] : true, $method);
    if (static::$active)
    {
        中略
    }

    // fire any request created events
    \Event::instance()->has_events('request_created') and \Event::instance()->trigger('request_created', '', 'none');

    return $request;
}

Router::process が呼ばれる

request.php

public function __construct($uri, $route = true, $method = null)
{
    中略
    // check if a module was requested
    if (count($this->uri->get_segments()) and $module_path = \Module::exists($this->uri->get_segment(1)))
    {
        中略
    }

    // 次: ここ
    $this->route = \Router::process($this, $route);

    中略
}

core/classes/router.php

router.php
public static function process(\Request $request, $route = true)
{
    $match = false;

    if ($route)
    {
        中略
    }

    if ( ! $match)
    {
        中略
    }

    if ($match->callable !== null)
    {
        return $match;
    }
    // 次: ここだけど面倒なので以降は端折ります
    return static::parse_match($match);
}

public/index.php

前述の Request::forge が Request オブジェクトを返すので、execute を実行

index.php
$routerequest = function($route = null, $e = false)
{
    中略
    {
        // 次: ここの execute
        $response = Request::forge($route, false)->execute(array($e))->response();
    }
    中略

    return $response;
};

core/classes/request.php

request.php
public function execute($method_params = null)
{
    中略
    try
    {
        if ($this->route->callable !== null)
        {
            中略
        }
        else
        {
            中略
            // Load the controller using reflection <- ここでクラスをロード
            $class = new \ReflectionClass($class);

            if ($class->isAbstract())
            {
                throw new \HttpNotFoundException();
            }

            // Create a new instance of the controller <- ここでインスタンスを作成
            $this->controller_instance = $class->newInstance($this);
            中略
        }

        中略
    }
    中略

    return $this;
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2