Skip to content

Commit

Permalink
Merge pull request nWidart#1921 from alissn/FixModuleLoading
Browse files Browse the repository at this point in the history
Blazing Fast Module Loading πŸš€πŸ˜Ž
  • Loading branch information
dcblogdev authored Aug 25, 2024
2 parents b51e39c + d7f1701 commit 59cf936
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 56 deletions.
7 changes: 0 additions & 7 deletions src/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ interface RepositoryInterface
*/
public function all();

/**
* Get cached modules.
*
* @return array
*/
public function getCached();

/**
* Scan & get all available modules.
*
Expand Down
68 changes: 22 additions & 46 deletions src/FileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ abstract class FileRepository implements Countable, RepositoryInterface
*/
private $cache;

private static $modules = [];

/**
* The constructor.
*
Expand Down Expand Up @@ -140,6 +142,10 @@ abstract protected function createModule(...$args);
*/
public function scan()
{
if (! empty(self::$modules) && ! $this->app->runningUnitTests()) {
return self::$modules;
}

$paths = $this->getScanPaths();

$modules = [];
Expand All @@ -150,56 +156,24 @@ public function scan()
is_array($manifests) || $manifests = [];

foreach ($manifests as $manifest) {
$name = Json::make($manifest)->get('name');
$json = Json::make($manifest);
$name = $json->get('name');

$modules[$name] = $this->createModule($this->app, $name, dirname($manifest));
$modules[strtolower($name)] = $this->createModule($this->app, $name, dirname($manifest));
}
}

return $modules;
self::$modules = $modules;

return self::$modules;
}

/**
* Get all modules.
*/
public function all(): array
{
if (! $this->config('cache.enabled')) {
return $this->scan();
}

return $this->formatCached($this->getCached());
}

/**
* Format the cached data as array of modules.
*
* @param array $cached
* @return array
*/
protected function formatCached($cached)
{
$modules = [];

foreach ($cached as $name => $module) {
$path = $module['path'];

$modules[$name] = $this->createModule($this->app, $name, $path);
}

return $modules;
}

/**
* Get cached modules.
*
* @return array
*/
public function getCached()
{
return $this->cache->store($this->config->get('modules.cache.driver'))->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () {
return $this->toCollection()->toArray();
});
return $this->scan();
}

/**
Expand Down Expand Up @@ -232,7 +206,7 @@ public function getByStatus($status): array
*/
public function has($name): bool
{
return array_key_exists($name, $this->all());
return array_key_exists(strtolower($name), $this->all());
}

/**
Expand Down Expand Up @@ -316,12 +290,7 @@ public function boot(): void
*/
public function find(string $name)
{
foreach ($this->all() as $module) {
if ($module->getLowerName() === strtolower($name)) {
return $module;
}
}

return $this->all()[strtolower($name)] ?? null;
}

/**
Expand Down Expand Up @@ -578,4 +547,11 @@ public function setStubPath($stubPath)

return $this;
}

public function resetModules(): static
{
self::$modules = [];

return $this;
}
}
5 changes: 3 additions & 2 deletions src/Generators/ModuleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,6 @@ public function generate(): int
{
$name = $this->getName();

Event::dispatch(sprintf('modules.%s.%s', strtolower($name), ModuleEvent::CREATING));

if ($this->module->has($name)) {
if ($this->force) {
$this->module->delete($name);
Expand All @@ -289,6 +287,9 @@ public function generate(): int
return E_ERROR;
}
}

Event::dispatch(sprintf('modules.%s.%s', strtolower($name), ModuleEvent::CREATING));

$this->component->info("Creating module: [$name]");

$this->generateFolders();
Expand Down
2 changes: 1 addition & 1 deletion src/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function decodeContents()
*/
public function getAttributes()
{
return $this->decodeContents();
return $this->attributes ?? $this->decodeContents();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Traits/CanClearModulesCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public function clearCache()
if (config('modules.cache.enabled') === true) {
app('cache')->forget(config('modules.cache.key'));
}

$this->laravel['modules']->resetModules();
}
}
8 changes: 8 additions & 0 deletions tests/Commands/Actions/ModuleDeleteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public function setUp(): void
$this->activator = new FileActivator($this->app);
}

public function tearDown(): void
{
$this->artisan('module:delete', ['--all' => true, '--force' => true]);

$this->activator->reset();
parent::tearDown();
}

public function test_it_can_delete_a_module_from_disk(): void
{
$this->artisan('module:make', ['name' => ['WrongModule']]);
Expand Down

0 comments on commit 59cf936

Please sign in to comment.