Start on project with the connection services and first command to list databases
This commit is contained in:
parent
5e2bc3505f
commit
2fd43ed717
4
.env
4
.env
@ -17,3 +17,7 @@
|
||||
APP_ENV=dev
|
||||
APP_SECRET=c38de86853448126a1e57bd9dbe6760e
|
||||
###< symfony/framework-bundle ###
|
||||
|
||||
DATABASE_HOSTNAME=localhost
|
||||
DATABASE_USER=user
|
||||
DATABASE_PASSWORD=password
|
117
composer.json
117
composer.json
@ -1,62 +1,63 @@
|
||||
{
|
||||
"type": "project",
|
||||
"license": "proprietary",
|
||||
"minimum-stability": "stable",
|
||||
"prefer-stable": true,
|
||||
"require": {
|
||||
"php": ">=7.2.5",
|
||||
"ext-ctype": "*",
|
||||
"ext-iconv": "*",
|
||||
"symfony/console": "5.3.*",
|
||||
"symfony/dotenv": "5.3.*",
|
||||
"symfony/flex": "^1.3.1",
|
||||
"symfony/framework-bundle": "5.3.*",
|
||||
"symfony/runtime": "5.3.*",
|
||||
"symfony/yaml": "5.3.*"
|
||||
"type": "project",
|
||||
"license": "proprietary",
|
||||
"minimum-stability": "stable",
|
||||
"prefer-stable": true,
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"ext-ctype": "*",
|
||||
"ext-iconv": "*",
|
||||
"symfony/console": "5.3.*",
|
||||
"symfony/dotenv": "5.3.*",
|
||||
"symfony/flex": "^1.3.1",
|
||||
"symfony/framework-bundle": "5.3.*",
|
||||
"symfony/runtime": "5.3.*",
|
||||
"symfony/yaml": "5.3.*",
|
||||
"ext-mysqli": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
},
|
||||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"preferred-install": {
|
||||
"*": "dist"
|
||||
},
|
||||
"require-dev": {
|
||||
},
|
||||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"preferred-install": {
|
||||
"*": "dist"
|
||||
},
|
||||
"sort-packages": true
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"App\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"replace": {
|
||||
"symfony/polyfill-ctype": "*",
|
||||
"symfony/polyfill-iconv": "*",
|
||||
"symfony/polyfill-php72": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"auto-scripts": {
|
||||
"cache:clear": "symfony-cmd",
|
||||
"assets:install %PUBLIC_DIR%": "symfony-cmd"
|
||||
},
|
||||
"post-install-cmd": [
|
||||
"@auto-scripts"
|
||||
],
|
||||
"post-update-cmd": [
|
||||
"@auto-scripts"
|
||||
]
|
||||
},
|
||||
"conflict": {
|
||||
"symfony/symfony": "*"
|
||||
},
|
||||
"extra": {
|
||||
"symfony": {
|
||||
"allow-contrib": false,
|
||||
"require": "5.3.*"
|
||||
}
|
||||
"sort-packages": true
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"App\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"replace": {
|
||||
"symfony/polyfill-ctype": "*",
|
||||
"symfony/polyfill-iconv": "*",
|
||||
"symfony/polyfill-php72": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"auto-scripts": {
|
||||
"cache:clear": "symfony-cmd",
|
||||
"assets:install %PUBLIC_DIR%": "symfony-cmd"
|
||||
},
|
||||
"post-install-cmd": [
|
||||
"@auto-scripts"
|
||||
],
|
||||
"post-update-cmd": [
|
||||
"@auto-scripts"
|
||||
]
|
||||
},
|
||||
"conflict": {
|
||||
"symfony/symfony": "*"
|
||||
},
|
||||
"extra": {
|
||||
"symfony": {
|
||||
"allow-contrib": false,
|
||||
"require": "5.3.*"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
4
composer.lock
generated
4
composer.lock
generated
@ -2684,6 +2684,8 @@
|
||||
"ext-ctype": "*",
|
||||
"ext-iconv": "*"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"platform-dev": {
|
||||
"php": "*"
|
||||
},
|
||||
"plugin-api-version": "2.1.0"
|
||||
}
|
||||
|
@ -23,3 +23,9 @@ services:
|
||||
|
||||
# add more service definitions when explicit configuration is needed
|
||||
# please note that last definitions always *replace* previous ones
|
||||
|
||||
App\Service\MysqliConnection:
|
||||
arguments:
|
||||
$hostname: '%env(string:DATABASE_HOSTNAME)%'
|
||||
$user: '%env(string:DATABASE_USER)%'
|
||||
$password: '%env(DATABASE_PASSWORD)%'
|
38
src/Console/ListDatabasesCommand.php
Normal file
38
src/Console/ListDatabasesCommand.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Service\DatabaseManager;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class ListDatabasesCommand extends Command
|
||||
{
|
||||
// the name of the command (the part after "bin/console")
|
||||
protected static $defaultName = 'app:db:list';
|
||||
|
||||
public function __construct(
|
||||
private DatabaseManager $db,
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
// ...
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$dbs = $this->db->listDatabases();
|
||||
|
||||
$output->writeln('List of databases:');
|
||||
foreach ($dbs as $db) {
|
||||
$output->writeln(sprintf(' -%s', $db));
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
28
src/Service/DatabaseManager.php
Normal file
28
src/Service/DatabaseManager.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use mysqli;
|
||||
|
||||
class DatabaseManager
|
||||
{
|
||||
private mysqli $conn;
|
||||
|
||||
public function __construct(
|
||||
private MysqliConnection $service
|
||||
)
|
||||
{
|
||||
$this->conn = $this->service->getConnection();
|
||||
}
|
||||
|
||||
public function listDatabases(): array
|
||||
{
|
||||
$dbs = [];
|
||||
$results = $this->conn->query('SHOW DATABASES');
|
||||
while ($result = $results->fetch_object()) {
|
||||
$dbs[] = $result->Database;
|
||||
}
|
||||
|
||||
return $dbs;
|
||||
}
|
||||
}
|
23
src/Service/MysqliConnection.php
Normal file
23
src/Service/MysqliConnection.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use mysqli;
|
||||
|
||||
class MysqliConnection
|
||||
{
|
||||
private mysqli $connection;
|
||||
|
||||
public function __construct($hostname, $user, $password)
|
||||
{
|
||||
$this->connection = new mysqli($hostname, $user, $password);
|
||||
if ($this->connection->connect_error) {
|
||||
throw new \Exception("Connection failed: " . $this->connection->connect_error);
|
||||
}
|
||||
}
|
||||
|
||||
public function getConnection(): mysqli
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user