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

Add module_list #86

Merged
merged 4 commits into from
Nov 8, 2023
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
33 changes: 33 additions & 0 deletions src/functions/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use Drupal\Core\Extension\ModuleExtensionList;

/**
* @param string|string[] $type
*/
Expand Down Expand Up @@ -58,3 +60,34 @@ function module_invoke_all(string $hook): array
unset($args[0]);
return \Drupal::moduleHandler()->invokeAll($hook, $args);
}

/**
* @param array<string, array{filename: string}> $fixed_list
* @return string[]
*/
function module_list(
bool $refresh = false,
bool $bootstrap_refresh = false,
bool $sort = false,
?array $fixed_list = null
): array {
if ($fixed_list !== null) {
$moduleExtensionList = \Drupal::service('extension.list.module');
assert($moduleExtensionList instanceof ModuleExtensionList);
$moduleNames = array_keys($fixed_list);
$newList = array_map(
static fn (string $name) => $moduleExtensionList->get($name),
$moduleNames
);
\Drupal::moduleHandler()->setModuleList(
array_combine($moduleNames, $newList)
);
}
$list = array_keys(\Drupal::moduleHandler()->getModuleList());
$list = array_combine($list, $list);

if ($sort) {
ksort($list);
}
return $list;
}
31 changes: 31 additions & 0 deletions tests/src/Integration/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Retrofit\Drupal\Tests\Integration;

use Drupal\Core\Extension\ModuleHandlerInterface;

final class ModuleTest extends IntegrationTestCase
{
protected static $modules = ['system'];
Expand All @@ -13,4 +15,33 @@ public function testModuleExists(): void
self::assertTrue(module_exists('system'));
self::assertFalse(module_exists('user'));
}

public function testModuleList(): void
{
self::assertSame([
'sqlite' => 'sqlite',
'system' => 'system',
], module_list());
self::assertSame([
'sqlite' => 'sqlite',
'system' => 'system',
], module_list(true, true, true));

self::assertSame([
'user' => 'user',
'system' => 'system',
'sqlite' => 'sqlite',
], module_list(true, true, false, [
'user' => ['filename' => 'core/modules/user/user.module'],
'system' => ['filename' => 'core/modules/system/system.module'],
'sqlite' => ['filename' => 'core/modules/sqlite/sqlite.module'],
]));

$moduleHandler = $this->container->get('module_handler');
self::assertInstanceOf(ModuleHandlerInterface::class, $moduleHandler);
self::assertEquals(
['user', 'system', 'sqlite'],
array_keys($moduleHandler->getModuleList())
);
}
}