Skip to content

Commit ef09123

Browse files
committed
Issue #67: Implement MariaDB/PostgreSQL connection
Signed-off-by: alexmerlin <alex.merlin.1985@gmail.com>
1 parent 47ccbfa commit ef09123

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1028
-81
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
require_once 'vendor/autoload.php';
6+
7+
const ENVIRONMENT_DEVELOPMENT = 'development';
8+
const ENVIRONMENT_PRODUCTION = 'production';
9+
10+
// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
11+
12+
/**
13+
* @param array{source: string, destination: string, environment: array<string>} $file
14+
*/
15+
function copyFile(array $file): void
16+
{
17+
if (! in_array(getEnvironment(), $file['environment'])) {
18+
echo "Skipping the copy of {$file['source']} due to environment settings." . PHP_EOL;
19+
return;
20+
}
21+
22+
if (is_readable($file['destination'])) {
23+
echo "File {$file['destination']} already exists. Skipping..." . PHP_EOL;
24+
return;
25+
}
26+
27+
if (! copy($file['source'], $file['destination'])) {
28+
echo "Cannot copy {$file['source']} file to {$file['destination']}" . PHP_EOL;
29+
} else {
30+
echo "File {$file['source']} copied successfully to {$file['destination']}." . PHP_EOL;
31+
}
32+
}
33+
34+
function getEnvironment(): string
35+
{
36+
return getenv('COMPOSER_DEV_MODE') === '1' ? ENVIRONMENT_DEVELOPMENT : ENVIRONMENT_PRODUCTION;
37+
}
38+
39+
/**
40+
* When adding files to the below array:
41+
* - `source` and `destination` paths must be relative to the project root folder
42+
* - `environment` key will indicate on what environments the file will be copied
43+
*/
44+
$files = [
45+
[
46+
'source' => 'config/autoload/local.php.dist',
47+
'destination' => 'config/autoload/local.php',
48+
'environment' => [ENVIRONMENT_DEVELOPMENT, ENVIRONMENT_PRODUCTION],
49+
],
50+
[
51+
'source' => 'config/autoload/log.local.php.dist',
52+
'destination' => 'config/autoload/log.local.php',
53+
'environment' => [ENVIRONMENT_DEVELOPMENT, ENVIRONMENT_PRODUCTION],
54+
],
55+
[
56+
'source' => 'config/autoload/messenger.local.php.dist',
57+
'destination' => 'config/autoload/messenger.local.php',
58+
'environment' => [ENVIRONMENT_DEVELOPMENT, ENVIRONMENT_PRODUCTION],
59+
],
60+
[
61+
'source' => 'config/autoload/swoole.local.php.dist',
62+
'destination' => 'config/autoload/swoole.local.php',
63+
'environment' => [ENVIRONMENT_DEVELOPMENT, ENVIRONMENT_PRODUCTION],
64+
],
65+
];
66+
67+
echo "Using environment setting: " . getEnvironment() . PHP_EOL;
68+
69+
array_walk($files, 'copyFile');

bin/doctrine

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
use Core\App\Event\TablePrefixEventListener;
7+
use Doctrine\ORM\EntityManager;
8+
use Doctrine\ORM\Events;
9+
use Doctrine\ORM\Tools\Console\ConsoleRunner;
10+
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
11+
use Dot\DataFixtures\Command\ExecuteFixturesCommand;
12+
use Dot\DataFixtures\Command\ListFixturesCommand;
13+
14+
require_once 'vendor/autoload.php';
15+
16+
$container = require 'config/container.php';
17+
18+
$entityManager = $container->get(EntityManager::class);
19+
$entityManager->getEventManager()
20+
->addEventListener(Events::loadClassMetadata, $container->get(TablePrefixEventListener::class));
21+
22+
$commands = [
23+
$container->get(ExecuteFixturesCommand::class),
24+
$container->get(ListFixturesCommand::class),
25+
];
26+
27+
ConsoleRunner::run(new SingleManagerProvider($entityManager), $commands);

composer.json

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,36 @@
4343
}
4444
},
4545
"require": {
46-
"php": "~8.2.0 || ~8.3.0 || ~8.4.0",
47-
"dotkernel/dot-cli": "^3.9",
48-
"dotkernel/dot-dependency-injection": "^1.2",
49-
"dotkernel/dot-errorhandler": "4.2.1",
50-
"laminas/laminas-component-installer": "^3.5",
51-
"laminas/laminas-config-aggregator": "^1.18",
52-
"mezzio/mezzio": "^3.20",
53-
"netglue/laminas-messenger": "^2.3.0",
54-
"symfony/redis-messenger": "^v7.2.3"
46+
"php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0",
47+
"dotkernel/dot-cache": "^4.4",
48+
"dotkernel/dot-cli": "^3.10",
49+
"dotkernel/dot-data-fixtures": "^1.5",
50+
"dotkernel/dot-dependency-injection": "^1.3",
51+
"dotkernel/dot-errorhandler": "4.4.0",
52+
"laminas/laminas-component-installer": "^3.7",
53+
"laminas/laminas-config-aggregator": "^1.19",
54+
"mezzio/mezzio": "^3.23.2",
55+
"netglue/laminas-messenger": "^2.5.0",
56+
"ramsey/uuid": "^4.9.2",
57+
"ramsey/uuid-doctrine": "^2.1",
58+
"roave/psr-container-doctrine": "^5.2.2 || ^6.1.0",
59+
"symfony/redis-messenger": "^7.4.6"
5560
},
5661
"require-dev": {
57-
"laminas/laminas-coding-standard": "^3.0",
58-
"phpstan/phpstan": "^2.0",
59-
"phpstan/phpstan-doctrine": "^2.0",
60-
"phpstan/phpstan-phpunit": "^2.0",
61-
"phpunit/phpunit": "^10.5.45",
62+
"laminas/laminas-coding-standard": "^3.1",
63+
"laminas/laminas-development-mode": "^3.15",
64+
"phpstan/phpstan": "^2.1.40",
65+
"phpstan/phpstan-doctrine": "^2.0.18",
66+
"phpstan/phpstan-phpunit": "^2.0.16",
67+
"phpunit/phpunit": "^10.5.63",
6268
"roave/security-advisories": "dev-master",
63-
"swoole/ide-helper": "~5.0.0"
69+
"swoole/ide-helper": "~5.0.3"
6470
},
6571
"autoload": {
6672
"psr-4": {
67-
"Queue\\": "src/"
73+
"Core\\App\\": "src/Core/src/App/src/",
74+
"Queue\\App\\": "src/App/src/",
75+
"Queue\\Swoole\\": "src/Swoole/src/"
6876
}
6977
},
7078
"autoload-dev": {
@@ -81,8 +89,13 @@
8189
],
8290
"cs-check": "phpcs",
8391
"cs-fix": "phpcbf",
84-
"test": "phpunit --colors=always",
85-
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
86-
"static-analysis": "phpstan analyse --memory-limit 1G"
92+
"development-disable": "laminas-development-mode disable",
93+
"development-enable": "laminas-development-mode enable",
94+
"development-status": "laminas-development-mode status",
95+
"post-update-cmd": [
96+
"php ./bin/composer-post-install-script.php"
97+
],
98+
"static-analysis": "phpstan analyse --memory-limit 1G",
99+
"test": "phpunit --colors=always"
87100
}
88101
}

config/autoload/cli.global.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
'version' => '1.0.0',
1717
'name' => 'DotKernel CLI',
1818
'commands' => [
19-
"swoole:start" => StartCommand::class,
20-
"swoole:stop" => StopCommand::class,
21-
"messenger:start" => ConsumeMessagesCommand::class,
22-
"messenger:debug" => DebugCommand::class,
23-
"processed" => GetProcessedMessagesCommand::class,
24-
"failed" => GetFailedMessagesCommand::class,
25-
"inventory" => GetQueuedMessagesCommand::class,
19+
'swoole:start' => StartCommand::class,
20+
'swoole:stop' => StopCommand::class,
21+
'messenger:start' => ConsumeMessagesCommand::class,
22+
'messenger:debug' => DebugCommand::class,
23+
'processed' => GetProcessedMessagesCommand::class,
24+
'failed' => GetFailedMessagesCommand::class,
25+
'inventory' => GetQueuedMessagesCommand::class,
2626
],
2727
],
2828
FileLockerInterface::class => [

config/autoload/dependencies.global.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22

33
declare(strict_types=1);
44

5+
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand;
6+
use Roave\PsrContainerDoctrine\Migrations\CommandFactory;
7+
58
return [
69
// Provides application-wide services.
7-
// We recommend using fully-qualified class names whenever possible as
8-
// service names.
10+
// We recommend using fully qualified class names whenever possible as service names.
911
'dependencies' => [
10-
// Use 'aliases' to alias a service name to another service. The
11-
// key is the alias name, the value is the service to which it points.
12+
// Use 'aliases' to alias a service name to another service.
13+
// The key is the alias name, the value is the service to which it points.
1214
'aliases' => [
1315
// Fully\Qualified\ClassOrInterfaceName::class => Fully\Qualified\ClassName::class,
1416
],
15-
// Use 'invokables' for constructor-less services, or services that do
16-
// not require arguments to the constructor. Map a service name to the
17-
// class name.
17+
// Use 'invokables' for constructorless services, or services that do not require arguments to the constructor.
18+
// Map a service name to the class name.
1819
'invokables' => [
1920
// Fully\Qualified\InterfaceName::class => Fully\Qualified\ClassName::class,
2021
],
2122
// Use 'factories' for services provided by callbacks/factory classes.
2223
'factories' => [
23-
// Fully\Qualified\ClassName::class => Fully\Qualified\FactoryName::class,
24+
ExecuteCommand::class => CommandFactory::class,
2425
],
2526
],
2627
];

config/autoload/local.php.dist

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,45 @@ declare(strict_types=1);
1212
$baseUrl = 'https://queue.dotkernel.net';
1313

1414
$databases = [
15-
'default' => [
16-
'host' => '',
17-
'dbname' => '',
18-
'user' => '',
19-
'password' => '',
20-
'port' => 3306,
21-
'driver' => 'pdo_mysql',
22-
'charset' => 'utf8mb4',
23-
'collate' => 'utf8mb4_general_ci',
15+
/**
16+
* You can add more database connections to this array.
17+
* Only one active connection is allowed at a time.
18+
* By default, the application uses the 'mariadb' connection.
19+
* You can switch to another connection by activating it under doctrine->connection->orm_default-->params.
20+
*/
21+
'mariadb' => [
22+
'host' => 'localhost',
23+
'dbname' => 'dotkernel',
24+
'user' => '',
25+
'password' => '',
26+
'port' => 3306,
27+
'driver' => 'pdo_mysql',
28+
'collation' => 'utf8mb4_general_ci',
29+
'table_prefix' => '',
30+
],
31+
'postgresql' => [
32+
'host' => 'localhost',
33+
'dbname' => 'dotkernel',
34+
'user' => '',
35+
'password' => '',
36+
'port' => 5432,
37+
'driver' => 'pdo_pgsql',
38+
'collation' => 'utf8mb4_general_ci',
39+
'table_prefix' => '',
2440
],
25-
// you can add more database connections into this array
2641
];
2742

2843
return [
2944
'application' => [
30-
'name' => $app['name'] ?? '',
45+
'name' => 'Dotkernel Queue',
3146
'url' => $baseUrl,
3247
],
3348
'databases' => $databases,
3449
'doctrine' => [
3550
'connection' => [
3651
'orm_default' => [
37-
'params' => $databases['default'],
52+
'params' => $databases['mariadb'],
53+
// 'params' => $databases['postgresql'],
3854
],
3955
],
4056
],

config/autoload/messenger.local.php.dist

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
88
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface as SymfonySerializer;
99

1010
return [
11-
"symfony" => [
12-
"messenger" => [
11+
'symfony' => [
12+
'messenger' => [
1313
'transports' => [
1414
'redis_transport' => [
1515
'dsn' => 'redis://127.0.0.1:6379/messages',
@@ -31,10 +31,10 @@ return [
3131
'failure_transport' => 'failed',
3232
],
3333
],
34-
"dependencies" => [
35-
"factories" => [
36-
"redis_transport" => [TransportFactory::class, 'redis_transport'],
37-
"failed" => [TransportFactory::class, 'failed'],
34+
'dependencies' => [
35+
'factories' => [
36+
'redis_transport' => [TransportFactory::class, 'redis_transport'],
37+
'failed' => [TransportFactory::class, 'failed'],
3838
SymfonySerializer::class => fn(ContainerInterface $container) => new PhpSerializer(),
3939
],
4040
],

config/cli-config.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Core\App\Doctrine\MigrationsMigratedSubscriber;
6+
use Core\App\Event\TablePrefixEventListener;
7+
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
8+
use Doctrine\Migrations\Configuration\Migration\ConfigurationArray;
9+
use Doctrine\Migrations\DependencyFactory;
10+
use Doctrine\ORM\EntityManager;
11+
use Doctrine\ORM\Events;
12+
use Dot\Log\Logger;
13+
use Psr\Container\ContainerExceptionInterface;
14+
15+
$container = require 'config/container.php';
16+
17+
try {
18+
$entityManager = $container->get(EntityManager::class);
19+
$entityManager->getEventManager()
20+
->addEventListener(Events::loadClassMetadata, $container->get(TablePrefixEventListener::class));
21+
$entityManager->getEventManager()
22+
->addEventSubscriber(new MigrationsMigratedSubscriber($container));
23+
24+
return DependencyFactory::fromEntityManager(
25+
new ConfigurationArray($container->get('config')['doctrine']['migrations']),
26+
new ExistingEntityManager($entityManager)
27+
);
28+
} catch (ContainerExceptionInterface $exception) {
29+
var_dump($exception->getMessage());exit;
30+
try {
31+
/** @var Logger $logger */
32+
$logger = $container->get('dot-log.queue-log');
33+
$logger->error($exception->getMessage());
34+
} catch (ContainerExceptionInterface $exception) {
35+
error_log($exception->getMessage());
36+
}
37+
}

config/config.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919
Netglue\PsrContainer\Messenger\ConfigProvider::class,
2020

2121
// Default App module config
22+
Core\App\ConfigProvider::class,
2223
Queue\App\ConfigProvider::class,
2324
Queue\Swoole\ConfigProvider::class,
2425

2526
// Dotkernel packages
26-
Dot\Log\ConfigProvider::class,
2727
Dot\Cli\ConfigProvider::class,
2828
Dot\DependencyInjection\ConfigProvider::class,
29+
Dot\DataFixtures\ConfigProvider::class,
2930
Dot\ErrorHandler\ConfigProvider::class,
31+
Dot\Log\ConfigProvider::class,
3032

3133
// Load application config in a pre-defined order in such a way that local settings
3234
// overwrite global settings. (Loaded as first to last):

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ includes:
44
parameters:
55
level: 5
66
paths:
7+
- bin
78
- config
89
- public
910
- src

0 commit comments

Comments
 (0)