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
2 changes: 2 additions & 0 deletions docs/custom_theme_builders.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ After these steps, your custom ThemeBuilder will be available and integrated wit

This method is crucial as it determines whether your builder is responsible for a specific theme. Implement logic here that uniquely identifies your theme type.

`BuilderPool` uses the first builder whose `detect()` returns `true`, so detection must be mutually exclusive. If your builder overlaps with a built-in one, exclude its themes explicitly (the TailwindCSS builder, for example, rejects themes that `HyvaThemeDetector` identifies as Hyvä).

```php
public function detect(string $themePath): bool
{
Expand Down
53 changes: 53 additions & 0 deletions src/Service/Hyva/HyvaThemeDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace OpenForgeProject\MageForge\Service\Hyva;

use Magento\Framework\Filesystem\Driver\File;

/**
* Decides whether a theme directory belongs to a Hyvä theme
*
* Shared by the Hyvä and TailwindCSS builders so that their detection is mutually exclusive
* regardless of the order they are registered in the builder pool.
*/
class HyvaThemeDetector
{
/**
* @param File $fileDriver
*/
public function __construct(
private readonly File $fileDriver,
) {
}

/**
* Check theme.xml and composer.json for a Hyvä declaration
*
* @param string $themePath
* @return bool
*/
public function isHyvaTheme(string $themePath): bool
{
$themePath = rtrim($themePath, '/');

if ($this->fileDriver->isExists($themePath . '/theme.xml')) {
$themeXmlContent = $this->fileDriver->fileGetContents($themePath . '/theme.xml');
if (stripos($themeXmlContent, 'hyva') !== false) {
return true;
}
}

if ($this->fileDriver->isExists($themePath . '/composer.json')) {
$composerContent = $this->fileDriver->fileGetContents($themePath . '/composer.json');
$composerJson = json_decode($composerContent, true);
$packageName = is_array($composerJson) ? $composerJson['name'] ?? null : null;
if (is_string($packageName) && str_contains($packageName, 'hyva')) {
return true;
}
}

return false;
}
}
23 changes: 4 additions & 19 deletions src/Service/ThemeBuilder/HyvaThemes/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Shell;
use OpenForgeProject\MageForge\Service\CacheCleaner;
use OpenForgeProject\MageForge\Service\Hyva\HyvaThemeDetector;
use OpenForgeProject\MageForge\Service\NodePackageManager;
use OpenForgeProject\MageForge\Service\StaticContentCleaner;
use OpenForgeProject\MageForge\Service\StaticContentDeployer;
Expand All @@ -28,6 +29,7 @@ class Builder implements BuilderInterface
* @param CacheCleaner $cacheCleaner
* @param SymlinkCleaner $symlinkCleaner
* @param NodePackageManager $nodePackageManager
* @param HyvaThemeDetector $hyvaThemeDetector
*/
public function __construct(
private readonly Shell $shell,
Expand All @@ -37,6 +39,7 @@ public function __construct(
private readonly CacheCleaner $cacheCleaner,
private readonly SymlinkCleaner $symlinkCleaner,
private readonly NodePackageManager $nodePackageManager,
private readonly HyvaThemeDetector $hyvaThemeDetector,
) {
}

Expand All @@ -56,25 +59,7 @@ public function detect(string $themePath): bool
return false;
}

// Check theme.xml for Hyva theme declaration
if ($this->fileDriver->isExists($themePath . '/theme.xml')) {
$themeXmlContent = $this->fileDriver->fileGetContents($themePath . '/theme.xml');
if (stripos($themeXmlContent, 'hyva') !== false) {
return true;
}
}

// Check composer.json for Hyva module dependency
if ($this->fileDriver->isExists($themePath . '/composer.json')) {
$composerContent = $this->fileDriver->fileGetContents($themePath . '/composer.json');
$composerJson = json_decode($composerContent, true);
$packageName = is_array($composerJson) ? $composerJson['name'] ?? null : null;
if (is_string($packageName) && str_contains($packageName, 'hyva')) {
return true;
}
}

return false;
return $this->hyvaThemeDetector->isHyvaTheme($themePath);
}

/**
Expand Down
26 changes: 8 additions & 18 deletions src/Service/ThemeBuilder/TailwindCSS/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Shell;
use OpenForgeProject\MageForge\Service\CacheCleaner;
use OpenForgeProject\MageForge\Service\Hyva\HyvaThemeDetector;
use OpenForgeProject\MageForge\Service\NodePackageManager;
use OpenForgeProject\MageForge\Service\StaticContentCleaner;
use OpenForgeProject\MageForge\Service\StaticContentDeployer;
Expand All @@ -28,6 +29,7 @@ class Builder implements BuilderInterface
* @param CacheCleaner $cacheCleaner
* @param SymlinkCleaner $symlinkCleaner
* @param NodePackageManager $nodePackageManager
* @param HyvaThemeDetector $hyvaThemeDetector
*/
public function __construct(
private readonly Shell $shell,
Expand All @@ -37,6 +39,7 @@ public function __construct(
private readonly CacheCleaner $cacheCleaner,
private readonly SymlinkCleaner $symlinkCleaner,
private readonly NodePackageManager $nodePackageManager,
private readonly HyvaThemeDetector $hyvaThemeDetector,
) {
}

Expand All @@ -56,25 +59,12 @@ public function detect(string $themePath): bool
return false;
}

// Check for theme.xml file in theme folder and ensure it's not a Hyva theme
if ($this->fileDriver->isExists($themePath . '/theme.xml')) {
$themeXmlContent = $this->fileDriver->fileGetContents($themePath . '/theme.xml');
if (stripos($themeXmlContent, 'hyva') === false) {
return true;
}
}

// Check for composer.json file in theme folder and ensure it's not a Hyva theme
if ($this->fileDriver->isExists($themePath . '/composer.json')) {
$composerContent = $this->fileDriver->fileGetContents($themePath . '/composer.json');
$composerJson = json_decode($composerContent, true);
$packageName = \is_array($composerJson) ? $composerJson['name'] ?? null : null;
if (\is_string($packageName) && !str_contains($packageName, 'hyva')) {
return true;
}
}
// Must be a theme at all, and must not be claimed by the Hyva builder
$hasThemeFile =
$this->fileDriver->isExists($themePath . '/theme.xml')
|| $this->fileDriver->isExists($themePath . '/composer.json');

return false;
return $hasThemeFile && !$this->hyvaThemeDetector->isHyvaTheme($themePath);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions tests/Unit/Model/TemplateEngine/Decorator/InspectorHintsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,10 @@ public function testRenderExtractsModuleNameFromRealBlockClassName(): void

public function testRenderDetectsParentBlockAndAlias(): void
{
$parentBlock = $this->getStubBuilder(AbstractBlock::class)
->disableOriginalConstructor()
->onlyMethods(['getNameInLayout'])
->getStub();
$parentBlock = $this->createStub(AbstractBlock::class);
$parentBlock->method('getNameInLayout')->willReturn('parent.block');

$block = $this->getStubBuilder(AbstractBlock::class)
->disableOriginalConstructor()
->onlyMethods(['getParentBlock', 'getNameInLayout'])
->getStub();
$block = $this->createStub(AbstractBlock::class);
$block->method('getParentBlock')->willReturn($parentBlock);
$block->method('getNameInLayout')->willReturn('child.block');

Expand Down
61 changes: 61 additions & 0 deletions tests/Unit/Service/Hyva/HyvaThemeDetectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace OpenForgeProject\MageForge\Test\Unit\Service\Hyva;

use Magento\Framework\Filesystem\Driver\File;
use OpenForgeProject\MageForge\Service\Hyva\HyvaThemeDetector;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class HyvaThemeDetectorTest extends TestCase
{
/**
* @return array<string, array{0: string|null, 1: array<string, mixed>|null, 2: bool}>
*/
public static function themeFilesProvider(): array
{
return [
'hyva in theme.xml' => ['<theme><parent>Hyva/default</parent></theme>', null, true],
'hyva in theme.xml, other composer name' => [
'<theme><parent>Hyva/default</parent></theme>',
['name' => 'vendor/custom-theme'],
true,
],
'neutral theme.xml, hyva composer name' => [
'<theme><title>Custom</title></theme>',
['name' => 'hyva-themes/theme-default'],
true,
],
'neutral theme.xml and composer name' => [
'<theme><title>Custom</title></theme>',
['name' => 'vendor/custom-theme'],
false,
],
'composer json without name' => [null, ['require' => []], false],
'no theme files' => [null, null, false],
];
}

/**
* @param string|null $themeXml
* @param array<string, mixed>|null $composerJson
* @param bool $expected
*/
#[DataProvider('themeFilesProvider')]
public function testIsHyvaTheme(?string $themeXml, ?array $composerJson, bool $expected): void
{
$fileDriver = $this->createStub(File::class);
$fileDriver->method('isExists')->willReturnMap([
['/theme/theme.xml', $themeXml !== null],
['/theme/composer.json', $composerJson !== null],
]);
$fileDriver->method('fileGetContents')->willReturnMap([
['/theme/theme.xml', null, null, (string) $themeXml],
['/theme/composer.json', null, null, json_encode($composerJson)],
]);

$this->assertSame($expected, (new HyvaThemeDetector($fileDriver))->isHyvaTheme('/theme/'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ protected function setUp(): void
*/
private function createBlock(): AbstractBlock&Stub
{
return $this->getStubBuilder(AbstractBlock::class)
return $this->getMockBuilder(AbstractBlock::class)
->disableOriginalConstructor()
->onlyMethods(['isScopePrivate', 'getCacheKey'])
->getStub();
->getMock();
}

public function testGetCacheInfoReturnsNotCacheableWhenLifetimeIsFalse(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ public function testFindDirectoryFilesUsesDirectoryRootWithoutSubPathAndSkipsMis
->method('isFile')
->willReturnCallback(static fn(string $path): bool => str_ends_with($path, '.phtml'));

$files = $this->createResolver(fileDriver: $fileDriver)->findDirectoryFiles(['/missing', '/module/templates'], '');
$files = $this->createResolver(fileDriver: $fileDriver)
->findDirectoryFiles(['/missing', '/module/templates'], '');

$this->assertSame(
['b.phtml' => '/module/templates/b.phtml', 'z.phtml' => '/module/templates/z.phtml'],
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Service/ThemeBuilder/HyvaThemes/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Shell;
use OpenForgeProject\MageForge\Service\CacheCleaner;
use OpenForgeProject\MageForge\Service\Hyva\HyvaThemeDetector;
use OpenForgeProject\MageForge\Service\NodePackageManager;
use OpenForgeProject\MageForge\Service\StaticContentCleaner;
use OpenForgeProject\MageForge\Service\StaticContentDeployer;
Expand Down Expand Up @@ -97,6 +98,7 @@ private function createBuilder(
$cacheCleaner ?? $this->cacheCleaner,
$symlinkCleaner ?? $this->symlinkCleaner,
$nodePackageManager ?? $this->nodePackageManager,
new HyvaThemeDetector($fileDriver ?? $this->fileDriver),
);
}

Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Service/ThemeBuilder/TailwindCSS/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Shell;
use OpenForgeProject\MageForge\Service\CacheCleaner;
use OpenForgeProject\MageForge\Service\Hyva\HyvaThemeDetector;
use OpenForgeProject\MageForge\Service\NodePackageManager;
use OpenForgeProject\MageForge\Service\StaticContentCleaner;
use OpenForgeProject\MageForge\Service\StaticContentDeployer;
Expand Down Expand Up @@ -162,6 +163,40 @@ public function testDetectReturnsFalseWhenNeitherFilePresent(): void
$this->assertFalse($this->builder->detect('app/design/frontend/Vendor/theme'));
}

public function testDetectReturnsFalseForHyvaComposerNameWithNeutralThemeXml(): void
{
$fileDriver = $this->createStub(File::class);
$fileDriver->method('isExists')->willReturn(true);
$fileDriver->method('fileGetContents')->willReturnMap([
['app/design/frontend/Vendor/theme/theme.xml', null, null, '<theme><title>Custom</title></theme>'],
[
'app/design/frontend/Vendor/theme/composer.json',
null,
null,
json_encode(['name' => 'hyva-themes/theme-default']),
],
]);

$this->assertFalse($this->createBuilder(fileDriver: $fileDriver)->detect('app/design/frontend/Vendor/theme'));
}

public function testDetectReturnsFalseForHyvaThemeXmlWithNeutralComposerName(): void
{
$fileDriver = $this->createStub(File::class);
$fileDriver->method('isExists')->willReturn(true);
$fileDriver->method('fileGetContents')->willReturnMap([
['app/design/frontend/Vendor/theme/theme.xml', null, null, '<theme><parent>Hyva/default</parent></theme>'],
[
'app/design/frontend/Vendor/theme/composer.json',
null,
null,
json_encode(['name' => 'vendor/custom-theme']),
],
]);

$this->assertFalse($this->createBuilder(fileDriver: $fileDriver)->detect('app/design/frontend/Vendor/theme'));
}

private function createBuilder(
?Shell $shell = null,
?File $fileDriver = null,
Expand All @@ -179,6 +214,7 @@ private function createBuilder(
$cacheCleaner ?? $this->cacheCleaner,
$symlinkCleaner ?? $this->symlinkCleaner,
$nodePackageManager ?? $this->nodePackageManager,
new HyvaThemeDetector($fileDriver ?? $this->fileDriver),
);
}

Expand Down
Loading