diff --git a/config/pimcore/config.yaml b/config/pimcore/config.yaml index 8b2aedd4a..7065d2c51 100644 --- a/config/pimcore/config.yaml +++ b/config/pimcore/config.yaml @@ -17,4 +17,9 @@ api_platform: api_keys: access_token: name: 'Authorization' - type: 'header' \ No newline at end of file + type: 'header' + +pimcore: + translations: + domains: + - studio diff --git a/src/Installer.php b/src/Installer.php index 41b9a11b4..422808377 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -16,11 +16,107 @@ namespace Pimcore\Bundle\StudioApiBundle; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Schema\Comparator; +use Doctrine\DBAL\Schema\Schema; +use Doctrine\DBAL\Schema\SchemaException; +use Pimcore\Bundle\StudioApiBundle\Service\TranslatorService; +use Pimcore\Extension\Bundle\Installer\Exception\InstallationException; use Pimcore\Extension\Bundle\Installer\SettingsStoreAwareInstaller; +use Symfony\Component\HttpKernel\Bundle\BundleInterface; /** * @internal */ final class Installer extends SettingsStoreAwareInstaller { + public function __construct( + private readonly Connection $db, + BundleInterface $bundle, + ) { + parent::__construct($bundle); + } + + /** + * @throws SchemaException|Exception + */ + public function install(): void + { + $schema = $this->db->createSchemaManager()->introspectSchema(); + + $this->createTranslationTable($schema); + $this->executeDiffSql($schema); + + parent::install(); + } + + private function createTranslationTable(Schema $schema): void + { + $translationsDomainTableName = 'translations_' . TranslatorService::DOMAIN; + if (!$schema->hasTable($translationsDomainTableName)) { + $translationDomainTable = $schema->createTable($translationsDomainTableName); + + $translationDomainTable->addColumn('key', 'string', [ + 'notnull' => true, + 'length' => 190, + ]); + + $translationDomainTable->addColumn('type', 'string', [ + 'notnull' => true, + 'length' => 15, + ]); + + $translationDomainTable->addColumn('language', 'string', [ + 'notnull' => true, + 'length' => 10, + ]); + + $translationDomainTable->addColumn('text', 'text'); + + $translationDomainTable->addColumn('creationDate', 'integer', [ + 'unsigned' => true, + 'length' => 11, + ]); + + $translationDomainTable->addColumn('modificationDate', 'integer', [ + 'unsigned' => true, + 'length' => 11, + ]); + + $translationDomainTable->addColumn('userOwner', 'integer', [ + 'unsigned' => true, + 'length' => 11, + ]); + + $translationDomainTable->addColumn('userModification', 'integer', [ + 'unsigned' => true, + 'length' => 11, + ]); + + $translationDomainTable->setPrimaryKey(['key', 'language'], 'pk_translation'); + $translationDomainTable->addIndex(['language'], 'idx_language'); + } + } + + /** + * @throws Exception + */ + private function executeDiffSql(Schema $newSchema): void + { + $currentSchema = $this->db->createSchemaManager()->introspectSchema(); + $schemaComparator = new Comparator($this->db->getDatabasePlatform()); + $schemaDiff = $schemaComparator->compareSchemas($currentSchema, $newSchema); + $dbPlatform = $this->db->getDatabasePlatform(); + if (!$dbPlatform instanceof AbstractPlatform) { + throw new InstallationException('Could not get database platform.'); + } + + $sqlStatements = $dbPlatform->getAlterSchemaSQL($schemaDiff); + + if (!empty($sqlStatements)) { + $this->db->executeStatement(implode(';', $sqlStatements)); + } + } } diff --git a/src/Service/TranslatorService.php b/src/Service/TranslatorService.php index 79133ba6b..7d18e41dc 100644 --- a/src/Service/TranslatorService.php +++ b/src/Service/TranslatorService.php @@ -28,7 +28,7 @@ final class TranslatorService implements TranslatorServiceInterface { private TranslatorBagInterface $translator; - private const DOMAIN = 'admin'; + public const DOMAIN = 'studio'; public function __construct( TranslatorInterface $translator