Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom translation domain #17

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion config/pimcore/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ api_platform:
api_keys:
access_token:
name: 'Authorization'
type: 'header'
type: 'header'

pimcore:
translations:
domains:
- studio
96 changes: 96 additions & 0 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
2 changes: 1 addition & 1 deletion src/Service/TranslatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading