Skip to content

Commit

Permalink
Rename module from Yireo_HyvaCheckoutUtils to Yireo_HyvaThemeAutoRegi…
Browse files Browse the repository at this point in the history
…stration
  • Loading branch information
jissereitsma committed Oct 3, 2024
1 parent 26ea38e commit b15194e
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 54 deletions.
6 changes: 3 additions & 3 deletions .module.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
EXTENSION_VENDOR="Yireo"
EXTENSION_NAME="Yireo_HyvaCheckoutUtils"
COMPOSER_NAME="yireo/magento2-hyva-checkout-utils"
PHP_VERSIONS=("8.1", "8.2")
EXTENSION_NAME="Yireo_HyvaThemeAutoRegistration"
COMPOSER_NAME="yireo/magento2-hyva-theme-auto-registration"
PHP_VERSIONS=("8.1", "8.2", "8.3")
28 changes: 5 additions & 23 deletions Observer/RegisterModuleForHyvaConfig.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php
declare(strict_types=1);

namespace Yireo\HyvaCheckoutUtils\Observer;
namespace Yireo\HyvaThemeAutoRegistration\Observer;

use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Driver\File as FileDriver;
use Magento\Framework\Module\ModuleList;
use Yireo\HyvaCheckoutUtils\Utils\HyvaFiles;
use Yireo\HyvaThemeAutoRegistration\Utils\HyvaFiles;

class RegisterModuleForHyvaConfig implements ObserverInterface
{
Expand All @@ -19,7 +18,6 @@ class RegisterModuleForHyvaConfig implements ObserverInterface
*/
public function __construct(
private ComponentRegistrar $componentRegistrar,
private FileDriver $fileDriver,
private ModuleList $moduleList,
private HyvaFiles $hyvaFiles,
private array $moduleNames = []
Expand All @@ -31,8 +29,9 @@ public function __construct(
* @return void
* @throws FileSystemException
*/
public function execute(Observer $event)
public function execute(Observer $observer)
{
$event = $observer->getEvent();
$config = $event->getData('config');
$extensions = $config->hasData('extensions') ? $config->getData('extensions') : [];

Expand All @@ -47,7 +46,7 @@ public function execute(Observer $event)
continue;
}

if (false === $this->hasHyvaFiles($moduleName)) {
if (false === $this->hyvaFiles->hasHyvaFiles($moduleName)) {
continue;
}

Expand All @@ -62,21 +61,4 @@ private function allowModuleName(string $moduleName): bool
{
return (bool) preg_match('/^(Yireo|YireoTraining)_/', $moduleName);
}

/**
* @param string $moduleName
* @return bool
* @throws FileSystemException
*/
private function hasHyvaFiles(string $moduleName): bool
{
$path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
foreach ($this->hyvaFiles->getFiles() as $file) {
if ($this->fileDriver->isExists($path . '/' . $file)) {
return true;
}
}

return false;
}
}
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Yireo HyvaCheckoutUtils
# Yireo HyvaThemeAutoRegistration
**Magento 2 module to make it easier to register a custom `tailwind.config.js` file of your own module in the global Hyvä Themes Tailwind configuration**

### Background
Expand All @@ -14,15 +14,12 @@ Next, add the following DI configuration to your module its `etc/di.xml` file (a
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Yireo\HyvaCheckoutUtils\Observer\RegisterModuleForHyvaConfig">
<type name="Yireo\HyvaThemeAutoRegistration\Observer\RegisterModuleForHyvaConfig">
<arguments>
<argument name="moduleNames" xsi:type="array">
<item name="Foo_Bar" xsi:type="object">Foo_Bar</item>
<item name="Foo_Bar" xsi:type="string">Foo_Bar</item>
</argument>
</arguments>
</type>
</config>
```

### Todo
Rename this module from `Yireo_HyvaCheckoutUtils` to something like `Yireo_HyvaThemesAutoRegistration` because this has zero to do with the checkout.
26 changes: 26 additions & 0 deletions Test/Integration/ModuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Yireo\HyvaThemeAutoRegistration\Test\Integration;

use PHPUnit\Framework\TestCase;
use Yireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsEnabled;
use Yireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsRegistered;
use Yireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsRegisteredForReal;

class ModuleTest extends TestCase
{
use AssertModuleIsEnabled;
use AssertModuleIsRegistered;
use AssertModuleIsRegisteredForReal;

public function testModuleIsEnabled()
{
$moduleName = 'Yireo_HyvaThemeAutoRegistration';

$this->assertModuleIsRegistered($moduleName);
$this->assertModuleIsRegisteredForReal($moduleName);
$this->assertModuleIsEnabled($moduleName);
}
}
64 changes: 64 additions & 0 deletions Test/Integration/Observer/RegisterModuleForHyvaConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Yireo\HyvaThemeAutoRegistration\Test\Integration\Observer;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;
use Magento\Framework\Event\ConfigInterface as EventManagerConfig;
use Magento\Framework\Event\Observer;
use Magento\TestFramework\Fixture\AppArea;
use Magento\TestFramework\Fixture\ComponentsDir;
use PHPUnit\Framework\TestCase;
use Yireo\HyvaThemeAutoRegistration\Observer\RegisterModuleForHyvaConfig;

class RegisterModuleForHyvaConfigTest extends TestCase
{
#[AppArea('frontend')]
public function testIfObserverIsRegistered()
{
// @todo: Copy this to integration test helper
$eventManagerConfig = ObjectManager::getInstance()->get(EventManagerConfig::class);
$observers = $eventManagerConfig->getObservers('hyva_config_generate_before');

$foundObserver = false;
$debugInfo = [];
foreach($observers as $observer) {
$debugInfo[] = $observer;
if ($observer['instance'] === RegisterModuleForHyvaConfig::class) {
$foundObserver = true;
}
}

$this->assertTrue($foundObserver, var_export($debugInfo, true));
}

#[ComponentsDir('./_files')]
public function testIfObserverExecutionIncludesConstructorBasedModules()
{
// @todo: Enable the relevant module

$observer = $this->getObserver();
$target = ObjectManager::getInstance()->get(RegisterModuleForHyvaConfig::class);
$target->execute($observer);

$this->assertNotEmpty($observer->getEvent()->getConfig()['extensions']);
$this->assertObserverEventContainsModule($observer, 'Yireo_Foobar1');
}

private function getObserver(): Observer
{
$config = new DataObject(['extensions' => []]);
$event = new DataObject(['config' => $config]);
$observer = ObjectManager::getInstance()->create(Observer::class);
$observer->setEvent($event);
return $observer;
}

private function assertObserverEventContainsModule(Observer $observer, string $moduleName)
{
$extensions = $observer->getEvent()->getConfig()['extensions'];
$this->assertContains($moduleName, $extensions);
}
}
5 changes: 5 additions & 0 deletions Test/Integration/_files/Foo_Bar1/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Foo_Bar1"/>
</config>
6 changes: 6 additions & 0 deletions Test/Integration/_files/Foo_Bar1/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Foo_Bar1', __DIR__);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('dummy');
5 changes: 5 additions & 0 deletions Test/Integration/_files/Yireo_Foobar1/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Yireo_Foobar1"/>
</config>
6 changes: 6 additions & 0 deletions Test/Integration/_files/Yireo_Foobar1/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Yireo_Foobar1', __DIR__);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('dummy');
26 changes: 25 additions & 1 deletion Utils/HyvaFiles.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
<?php
declare(strict_types=1);

namespace Yireo\HyvaCheckoutUtils\Utils;
namespace Yireo\HyvaThemeAutoRegistration\Utils;

use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Driver\File as FileDriver;

class HyvaFiles
{
public function __construct(
private ComponentRegistrar $componentRegistrar,
private FileDriver $fileDriver,
) {
}
public function getFiles(): array
{
return [
'view/frontend/tailwind/tailwind.config.js',
'view/frontend/tailwind/tailwind-source.css'
];
}

/**
* @param string $moduleName
* @return bool
* @throws FileSystemException
*/
public function hasHyvaFiles(string $moduleName): bool
{
$path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
foreach ($this->getFiles() as $file) {
if ($this->fileDriver->isExists($path . '/' . $file)) {
return true;
}
}

return false;
}
}
15 changes: 0 additions & 15 deletions Utils/RegisterHyvaModule.php

This file was deleted.

6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yireo/magento2-hyva-checkout-utils",
"version": "1.0.1",
"name": "yireo/magento2-hyva-theme-auto-registration",
"version": "1.0.2",
"description": "N/A",
"type": "magento2-module",
"require": {
Expand All @@ -14,7 +14,7 @@
"registration.php"
],
"psr-4": {
"Yireo\\HyvaCheckoutUtils\\": ""
"Yireo\\HyvaThemeAutoRegistration\\": ""
}
}
}
2 changes: 1 addition & 1 deletion etc/frontend/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"
>
<event name="hyva_config_generate_before">
<observer name="Yireo_HyvaCheckoutUtils" instance="Yireo\HyvaCheckoutUtils\Observer\RegisterModuleForHyvaConfig"/>
<observer name="Yireo_HyvaThemeAutoRegistration" instance="Yireo\HyvaThemeAutoRegistration\Observer\RegisterModuleForHyvaConfig"/>
</event>
</config>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Yireo_HyvaCheckoutUtils"/>
<module name="Yireo_HyvaThemeAutoRegistration"/>
</config>
3 changes: 2 additions & 1 deletion registration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Yireo_HyvaCheckoutUtils', __DIR__);
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Yireo_HyvaThemeAutoRegistration', __DIR__);

0 comments on commit b15194e

Please sign in to comment.