1313
1414namespace Ixnode \PhpApiVersionBundle \Utils \Version ;
1515
16+ use Doctrine \ORM \EntityManagerInterface ;
1617use Ixnode \BashVersionManager \Version as BashVersionManager ;
18+ use Ixnode \PhpApiVersionBundle \Constants \Code \Environments ;
19+ use Ixnode \PhpException \Case \CaseUnsupportedException ;
1720use 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}
0 commit comments