sqlite.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php namespace Laravel\Database\Connectors; use PDO;
  2. class SQLite extends Connector {
  3. /**
  4. * Establish a PDO database connection.
  5. *
  6. * @param array $config
  7. * @return PDO
  8. */
  9. public function connect($config)
  10. {
  11. $options = $this->options($config);
  12. // SQLite provides supported for "in-memory" databases, which exist only for
  13. // lifetime of the request. Any given in-memory database may only have one
  14. // PDO connection open to it at a time. These are mainly for tests.
  15. if ($config['database'] == ':memory:')
  16. {
  17. return new PDO('sqlite::memory:', null, null, $options);
  18. }
  19. // We'll allow the "database" configuration option to be a fully qualified
  20. // path to the database so we'll check if that is the case first. If it
  21. // isn't a fully qualified path we will use the storage directory.
  22. if (file_exists($config['database']))
  23. {
  24. $path = $config['database'];
  25. }
  26. // The database option does not appear to be a fully qualified path so we
  27. // will just assume it is a relative path from the storage directory
  28. // which is typically used to store all SQLite databases.
  29. else
  30. {
  31. $path = path('storage').'database'.DS.$config['database'].'.sqlite';
  32. }
  33. return new PDO('sqlite:'.$path, null, null, $options);
  34. }
  35. }