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

Laravelのコアファイルを読んでみて/Impression of reading the core code of the Laravel framework

y_sone
March 26, 2022

Laravelのコアファイルを読んでみて/Impression of reading the core code of the Laravel framework

Laravelのコアファイルを読んだ感想です。
DB接続についてコードを途中まで追ってみました。
2022年3月26日「とにかくほめる!マウントなしのLT会」にて初登壇。

y_sone

March 26, 2022
Tweet

More Decks by y_sone

Other Decks in Programming

Transcript

  1. new PDOでDB接続 // vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php protected function createPdoConnection($dsn, $username, $password, $options)

    { if (class_exists(PDOConnection::class) && ! $this->isPersistentConnection($options)) { return new PDOConnection($dsn, $username, $password, $options); } return new PDO($dsn, $username, $password, $options); }
  2. new PDOでDB接続 // vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php public function createConnection($dsn, array $config, array

    $options) { [$username, $password] = [ $config['username'] ?? null, $config['password'] ?? null, ]; try { return $this->createPdoConnection( $dsn, $username, $password, $options ); } catch (Exception $e) { // 省略 }
  3. MySQLに対応した情報をセット // vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php public function connect(array $config) { $dsn =

    $this->getDsn($config); $options = $this->getOptions($config); // We need to grab the PDO options that should be used while making the brand // new connection instance. The PDO options control various aspects of the // connection's behavior, and some might be specified by the developers. $connection = $this->createConnection($dsn, $config, $options); // 省略 return $connection; }
  4. RDMSの振り分け // vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php public function createConnector(array $config) { if (!

    isset($config['driver'])) { throw new InvalidArgumentException('A driver must be specified.'); } if ($this->container->bound($key = "db.connector.{$config['driver']}")) { return $this->container->make($key); } switch ($config['driver']) { case 'mysql': return new MySqlConnector; // 省略 } throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]."); }