SQLiteConnector.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Illuminate\Database\Connectors;
  3. use InvalidArgumentException;
  4. class SQLiteConnector extends Connector implements ConnectorInterface
  5. {
  6. /**
  7. * Establish a database connection.
  8. *
  9. * @param array $config
  10. * @return \PDO
  11. *
  12. * @throws \InvalidArgumentException
  13. */
  14. public function connect(array $config)
  15. {
  16. $options = $this->getOptions($config);
  17. // SQLite supports "in-memory" databases that only last as long as the owning
  18. // connection does. These are useful for tests or for short lifetime store
  19. // querying. In-memory databases may only have a single open connection.
  20. if ($config['database'] == ':memory:') {
  21. return $this->createConnection('sqlite::memory:', $config, $options);
  22. }
  23. $path = realpath($config['database']);
  24. // Here we'll verify that the SQLite database exists before going any further
  25. // as the developer probably wants to know if the database exists and this
  26. // SQLite driver will not throw any exception if it does not by default.
  27. if ($path === false) {
  28. throw new InvalidArgumentException("Database (${config['database']}) does not exist.");
  29. }
  30. return $this->createConnection("sqlite:{$path}", $config, $options);
  31. }
  32. }