Skip to content
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
75 changes: 28 additions & 47 deletions setup/src/MageOS/Installer/Model/Command/ThemeConfigurer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MageOS\Installer\Model\Command;

use MageOS\Installer\Model\VO\DatabaseConfiguration;
use MageOS\Installer\Model\VO\ThemeConfiguration;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -24,12 +25,17 @@ public function __construct(
* Apply selected theme to default store view
*
* @param ThemeConfiguration $themeConfig
* @param DatabaseConfiguration $dbConfig
* @param string $baseDir
* @param OutputInterface $output
* @return bool True if successful
*/
public function apply(ThemeConfiguration $themeConfig, string $baseDir, OutputInterface $output): bool
{
public function apply(
ThemeConfiguration $themeConfig,
DatabaseConfiguration $dbConfig,
string $baseDir,
OutputInterface $output
): bool {
if (!$themeConfig->install || empty($themeConfig->theme)) {
return true; // No theme to apply
}
Expand All @@ -38,7 +44,7 @@ public function apply(ThemeConfiguration $themeConfig, string $baseDir, OutputIn
$output->write('<comment>🎨 Applying theme...</comment>');

// Get theme ID from theme table
$themeId = $this->getThemeId($themeConfig->theme, $baseDir);
$themeId = $this->getThemeId($themeConfig->theme, $dbConfig);

if ($themeId === null) {
$output->writeln(' <comment>⚠️</comment>');
Expand Down Expand Up @@ -77,56 +83,31 @@ public function apply(ThemeConfiguration $themeConfig, string $baseDir, OutputIn
}

/**
* Get theme ID from theme code
* Get theme ID by querying the database directly
*
* @param string $themeCode Theme code (e.g., 'hyva-default', 'Hyva/default')
* @param string $baseDir
* @param string $themeCode Theme code (e.g., 'hyva', 'Hyva/default')
* @param DatabaseConfiguration $dbConfig
* @return int|null Theme ID or null if not found
*/
private function getThemeId(string $themeCode, string $baseDir): ?int
private function getThemeId(string $themeCode, DatabaseConfiguration $dbConfig): ?int
{
// Try to find theme using CLI
$result = $this->processRunner->runMagentoCommand(
['theme:list'],
$baseDir,
timeout: 30
);
try {
$pdo = new \PDO(
sprintf('mysql:host=%s;dbname=%s;charset=utf8mb4', $dbConfig->host, $dbConfig->name),
$dbConfig->user,
$dbConfig->password,
[\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_TIMEOUT => 5]
);

if (!$result->isSuccess()) {
return null;
}
$stmt = $pdo->prepare(
"SELECT theme_id FROM theme WHERE area = 'frontend' AND theme_path LIKE :path LIMIT 1"
);
$stmt->execute([':path' => '%' . $themeCode . '%']);
$id = (int) $stmt->fetchColumn();

// Parse output to find theme ID
// Format: "| <id> | <area> | <theme_path> | ... |"
$lines = explode("\n", $result->output);

foreach ($lines as $line) {
// Match lines with theme data (contains pipe separators)
if (!str_contains($line, '|')) {
continue;
}

// Split by pipe and trim
$parts = array_map('trim', explode('|', $line));

if (count($parts) < 4) {
continue;
}

// Check if this is our theme
// Match on theme code or path (handles 'hyva-default' or 'Hyva/default')
$themePath = $parts[3] ?? '';

if (stripos($themePath, $themeCode) !== false ||
stripos($themePath, str_replace('-', '/', $themeCode)) !== false
) {
$themeId = (int) $parts[1];
if ($themeId > 0) {
return $themeId;
}
}
return $id > 0 ? $id : null;
} catch (\PDOException $e) {
return null;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ public function execute(InstallationContext $context, OutputInterface $output):

// Apply selected theme to store view
$theme = $context->getTheme();
if ($theme) {
$this->themeConfigurer->apply($theme, BP, $output);
$db = $context->getDatabase();
if ($theme && $db) {
$this->themeConfigurer->apply($theme, $db, BP, $output);
}

// Set indexers to schedule mode for better performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public function execute(InstallationContext $context, OutputInterface $output):

$baseDir = BP;

// Install theme
$this->themeInstaller->install($baseDir, $theme->toArray(), $output);
// Install theme (pass sensitive=true so Hyva credentials are included)
$this->themeInstaller->install($baseDir, $theme->toArray(true), $output);

return StageResult::continue();
}
Expand Down
23 changes: 18 additions & 5 deletions setup/src/MageOS/Installer/Model/VO/ThemeConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,36 @@ class ThemeConfiguration
/**
* @param bool $install
* @param string $theme
* @param string $hyvaProjectKey
* @param string $hyvaApiToken
*/
public function __construct(
public readonly bool $install,
public readonly string $theme = ''
public readonly string $theme = '',
public readonly string $hyvaProjectKey = '',
public readonly string $hyvaApiToken = ''
) {
}

/**
* Convert to array
*
* @param bool $includeSensitive Whether to include sensitive fields (none here)
* @param bool $includeSensitive Whether to include sensitive fields like API tokens
* @return array<string, mixed>
*/
public function toArray(bool $includeSensitive = false): array
{
return [
$data = [
'install' => $this->install,
'theme' => $this->theme
'theme' => $this->theme,
'hyva_project_key' => $this->hyvaProjectKey,
];

if ($includeSensitive) {
$data['hyva_api_token'] = $this->hyvaApiToken;
}

return $data;
}

/**
Expand All @@ -45,7 +56,9 @@ public static function fromArray(array $data): self
{
return new self(
$data['install'] ?? false,
$data['theme'] ?? ''
$data['theme'] ?? '',
$data['hyva_project_key'] ?? '',
$data['hyva_api_token'] ?? ''
);
}
}