diff --git a/api.php b/api.php index 8b6ac7e..c8c7ca0 100644 --- a/api.php +++ b/api.php @@ -28,11 +28,9 @@ $di_container = $di_builder->build(); $settings = $di_container->get(\SBRL\TomlConfig::class); - // 4: Database -// TODO: Setup the database here - +// Done via a static factory method & PHP-DI // 5: Action diff --git a/di_config.php b/di_config.php index 755b505..06cfdc5 100644 --- a/di_config.php +++ b/di_config.php @@ -3,6 +3,8 @@ use Psr\Container\ContainerInterface; use SBRL\TomlConfig; +use AirQuality\Database; + use AirQuality\Repositories\IDeviceRepository; use AirQuality\Repositories\MariaDBDeviceRepository; use AirQuality\Repositories\IMeasurementDataRepository; @@ -23,8 +25,5 @@ return [ IDeviceRepository::class => DI\autowire(MariaDBDeviceRepository::class), IMeasurementDataRepository::class => DI\autowire(MariaDBMeasurementDataRepository::class), - IMeasurementTypeRepository::class => DI\autowire(MariaDBMeasurementTypeRepository::class), - - - \SBRL\SessionManager::class => DI\factory([ \SBRL\SessionManager::class, "get_instance" ]) + IMeasurementTypeRepository::class => DI\autowire(MariaDBMeasurementTypeRepository::class) ]; diff --git a/logic/Database.php b/logic/Database.php index 93bc271..e1932c1 100644 --- a/logic/Database.php +++ b/logic/Database.php @@ -2,6 +2,9 @@ namespace AirQuality; +use \SBRL\TomlConfig; +use Psr\Container\ContainerInterface; + /** * Holds the main database connection. */ @@ -20,11 +23,15 @@ class Database \PDO::ATTR_AUTOCOMMIT => false ]; - function __construct(\SBRL\TomlConfig $in_settings) { + function __construct(TomlConfig $in_settings) { + error_log("Created database instance"); $this->settings = $in_settings; + + $this->connect(); // Connect automagically } public function connect() { + error_log($this->get_connection_string()); $this->connection = new \PDO( $this->get_connection_string(), $this->settings->get("database.username"), @@ -44,4 +51,5 @@ class Database private function get_connection_string() { return "{$this->settings->get("database.type")}:host={$this->settings->get("database.host")};dbname={$this->settings->get("database.name")};charset=utf8mb4"; } + } diff --git a/settings.default.toml b/settings.default.toml index 64ebf1c..1e9cacc 100644 --- a/settings.default.toml +++ b/settings.default.toml @@ -8,8 +8,8 @@ type = "mysql" # MariaDB. MySQL servers might work too, but no promises. -# The host that the database is running on. Both IP addresses & domain names are fine :-) -host = "localhost" +# The host that the database is running on. Both IP addresses & domain names are fine I think :-) +host = "127.0.0.1" # The database to use. name = "aq_db"