Skip to content

Commit e8cb8ae

Browse files
committed
Add environment and db driver name to version
1 parent 2099962 commit e8cb8ae

File tree

10 files changed

+148
-17
lines changed

10 files changed

+148
-17
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"doctrine/doctrine-bundle": "^2.7",
2222
"doctrine/doctrine-migrations-bundle": "^3.2",
2323
"doctrine/orm": "^2.14",
24-
"ixnode/bash-version-manager": "^0.1.7",
24+
"ixnode/bash-version-manager": "^0.1.8",
2525
"ixnode/php-array-to-object": "^0.1.1",
2626
"ixnode/php-checker": "^0.1.9",
2727
"ixnode/php-container": "^0.1.8",

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data/json/schema/command/version/resource.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
"$schema": "resource.schema.json",
33
"name": "ixnode/php-api-version-bundle",
44
"description": "Provides the base API plattform functionality",
5-
"version": "0.1.9",
6-
"date": "Sunday, June 25, 2023 - 08:47:18",
7-
"license": "Copyright (c) 2022 Björn Hempel",
5+
"version": "0.1.17",
6+
"date": "Monday, August 14, 2023 - 18:53:18",
7+
"license": "Copyright (c) 2023 Björn Hempel",
88
"authors": [
99
"Björn Hempel <bjoern@hempel.li>"
1010
],
11+
"driver-name": "Sqlite - unknown version",
12+
"environment": "Test",
1113
"php-version": "8.2.7",
1214
"symfony-version": "6.3.0",
1315
"composer-version": "2.5.1",

data/json/schema/command/version/resource.schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
"type": "string"
2929
}
3030
},
31+
"driver-name": {
32+
"type": "string"
33+
},
34+
"environment": {
35+
"type": "string"
36+
},
3137
"php-version": {
3238
"type": "string",
3339
"pattern": "^\\d+\\.\\d+\\.\\d+$"

phpstan.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- bin/
5+
- config/
6+
- public/
7+
- src/
8+
- tests/

src/Command/Version/VersionCommand.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Ixnode\PhpApiVersionBundle\Command\Version;
1515

16+
use Doctrine\ORM\EntityManagerInterface;
1617
use Exception;
1718
use Ixnode\PhpApiVersionBundle\Utils\TypeCasting\TypeCastingHelper;
1819
use Ixnode\PhpApiVersionBundle\Utils\Version\Version;
@@ -29,6 +30,7 @@
2930
use Symfony\Component\Console\Input\InputInterface;
3031
use Symfony\Component\Console\Input\InputOption;
3132
use Symfony\Component\Console\Output\OutputInterface;
33+
use Symfony\Component\HttpKernel\KernelInterface;
3234

3335
/**
3436
* Class VersionCommand
@@ -78,16 +80,20 @@ class VersionCommand extends Command
7880

7981
protected const KEY_API_PLATFORM = 'api-platform-version';
8082

81-
protected Version $version;
83+
protected const KEY_DRIVER_NAME = 'driver-name';
84+
85+
protected const KEY_ENVIRONMENT = 'environment';
8286

8387
/**
8488
* VersionCommand constructor.
8589
*
8690
*/
87-
public function __construct()
91+
public function __construct(
92+
protected Version $version,
93+
protected KernelInterface $kernel,
94+
protected EntityManagerInterface $entityManager
95+
)
8896
{
89-
$this->version = new Version();
90-
9197
parent::__construct();
9298
}
9399

@@ -124,6 +130,8 @@ protected function getVersionArray(): array
124130
self::KEY_DATE => $this->version->getDate(),
125131
self::KEY_LICENSE => $this->version->getLicense(),
126132
self::KEY_AUTHORS => $this->version->getAuthors(),
133+
self::KEY_DRIVER_NAME => $this->version->getDriverName($this->entityManager),
134+
self::KEY_ENVIRONMENT => $this->version->getEnvironment($this->kernel),
127135
self::KEY_PHP => $this->version->getVersionPhp(),
128136
self::KEY_SYMFONY => $this->version->getVersionSymfony(),
129137
self::KEY_COMPOSER => $this->version->getVersionComposer(),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ixnode/php-api-version-bundle project.
5+
*
6+
* (c) Björn Hempel <https://www.hempel.li/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Ixnode\PhpApiVersionBundle\Constants\Code;
15+
16+
/**
17+
* Class Environments
18+
*
19+
* @author Björn Hempel <bjoern@hempel.li>
20+
* @version 0.1.0 (2023-08-14)
21+
* @since 0.1.0 (2023-08-14) First version.
22+
*/
23+
class Environments
24+
{
25+
final public const DEVELOPMENT = 'dev';
26+
27+
final public const DEVELOPMENT_NAME = 'Development environment';
28+
29+
final public const STAGING = 'staging';
30+
31+
final public const STAGING_NAME = 'Staging environment';
32+
33+
final public const PRODUCTION = 'prod';
34+
35+
final public const PRODUCTION_NAME = 'Production environment';
36+
37+
final public const TEST = 'test';
38+
39+
final public const TEST_NAME = 'Test';
40+
}

src/Utils/Version/Version.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
namespace Ixnode\PhpApiVersionBundle\Utils\Version;
1515

16+
use Doctrine\ORM\EntityManagerInterface;
1617
use Ixnode\BashVersionManager\Version as BashVersionManager;
18+
use Ixnode\PhpApiVersionBundle\Constants\Code\Environments;
19+
use Ixnode\PhpException\Case\CaseUnsupportedException;
1720
use Symfony\Component\HttpKernel\Kernel;
21+
use Symfony\Component\HttpKernel\KernelInterface;
1822

1923
/**
2024
* Class Version
@@ -34,4 +38,49 @@ public function getVersionSymfony(): string
3438
{
3539
return Kernel::VERSION;
3640
}
41+
42+
/**
43+
* Returns the db driver name and version.
44+
*
45+
* @throws CaseUnsupportedException
46+
*/
47+
public function getDriverName(EntityManagerInterface $entityManager): string
48+
{
49+
$connection = $entityManager->getConnection();
50+
51+
$driver = $connection->getDriver()->getDatabasePlatform();
52+
53+
$platformClassName = $driver::class;
54+
55+
return match (true) {
56+
str_contains($platformClassName, 'MariaDBPlatform') => 'MariaDB - unknown version', /* @link MariaDBPlatform */
57+
str_contains($platformClassName, 'MariaDb1027Platform') => 'MariaDB - 10.x', /* @link MariaDb1027Platform */
58+
str_contains($platformClassName, 'MySQL57Platform') => 'MySQL - 5.7', /* @link MySQL57Platform */
59+
str_contains($platformClassName, 'MySQL80Platform') => 'MySQL - 8.0', /* @link MySQL80Platform */
60+
str_contains($platformClassName, 'MySQLPlatform') => 'MySQL - unknown version', /* @link MySQLPlatform */
61+
str_contains($platformClassName, 'PostgreSQL100Platform') => 'PostgreSQL 10.0', /* @link PostgreSQL100Platform */
62+
str_contains($platformClassName, 'PostgreSQL94Platform') => 'PostgreSQL 9.4', /* @link PostgreSQL94Platform */
63+
str_contains($platformClassName, 'PostgreSQLPlatform') => 'PostgreSQL - unknown version', /* @link PostgreSQLPlatform */
64+
str_contains($platformClassName, 'SqlitePlatform') => 'Sqlite - unknown version', /* @link SqlitePlatform */
65+
default => throw new CaseUnsupportedException(sprintf('Unsupported database platform "%s".', $platformClassName)),
66+
};
67+
}
68+
69+
/**
70+
* Returns the environment name of this application.
71+
*
72+
* @param KernelInterface $kernel
73+
* @return string
74+
* @throws CaseUnsupportedException
75+
*/
76+
public function getEnvironment(KernelInterface $kernel): string
77+
{
78+
return match($kernel->getEnvironment()) {
79+
Environments::DEVELOPMENT => Environments::DEVELOPMENT_NAME,
80+
Environments::STAGING => Environments::STAGING_NAME,
81+
Environments::PRODUCTION => Environments::PRODUCTION_NAME,
82+
Environments::TEST => Environments::TEST_NAME,
83+
default => throw new CaseUnsupportedException(sprintf('Unsupported environment "%s".', $kernel->getEnvironment())),
84+
};
85+
}
3786
}

symfony.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@
8484
"config/packages/nelmio_cors.yaml"
8585
]
8686
},
87+
"phpstan/phpstan": {
88+
"version": "1.10",
89+
"recipe": {
90+
"repo": "github.com/symfony/recipes-contrib",
91+
"branch": "main",
92+
"version": "1.0",
93+
"ref": "d74d4d719d5f53856c9c13544aa22d44144b1819"
94+
},
95+
"files": [
96+
"phpstan.neon"
97+
]
98+
},
8799
"phpunit/phpunit": {
88100
"version": "9.5",
89101
"recipe": {

tests/Functional/Command/Version/VersionCommandTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Ixnode\PhpApiVersionBundle\Command\Version\VersionCommand;
1717
use Ixnode\PhpApiVersionBundle\Constants\Command\CommandSchema;
1818
use Ixnode\PhpApiVersionBundle\Tests\Functional\Command\Base\BaseFunctionalCommandTest;
19-
use Ixnode\BashVersionManager\Version;
19+
use Ixnode\PhpApiVersionBundle\Utils\Version\Version;
2020
use Ixnode\PhpContainer\File;
2121
use Ixnode\PhpContainer\Json;
2222
use Ixnode\PhpException\File\FileNotFoundException;
@@ -42,11 +42,17 @@ class VersionCommandTest extends BaseFunctionalCommandTest
4242
public function doConfig(): void
4343
{
4444
$this
45+
->setConfigUseDb()
4546
->setConfigUseParameterBag()
47+
->setConfigUseKernel()
4648
->setConfigUseCommand(
4749
VersionCommand::COMMAND_NAME,
4850
VersionCommand::class,
49-
fn () => [new Version($this->getProjectDir())]
51+
fn () => [
52+
new Version($this->getProjectDir()),
53+
self::$kernel,
54+
$this->entity->getEntityManager()
55+
]
5056
);
5157
}
5258

0 commit comments

Comments
 (0)