Skip to content

Commit

Permalink
Merge pull request #1953 from alissn/FixManifestRegister
Browse files Browse the repository at this point in the history
Update Module manifest handling and improvement
  • Loading branch information
dcblogdev authored Sep 20, 2024
2 parents d001c04 + f5ac56b commit d275a5b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 39 deletions.
9 changes: 5 additions & 4 deletions src/Activators/FileActivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Nwidart\Modules\Activators;

use Illuminate\Cache\CacheManager;
use Illuminate\Config\Repository as Config;
use Illuminate\Container\Container;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
Expand Down Expand Up @@ -86,13 +85,15 @@ public function disable(Module $module): void
/**
* {@inheritDoc}
*/
public function hasStatus(Module $module, bool $status): bool
public function hasStatus(Module|string $module, bool $status): bool
{
if (! isset($this->modulesStatuses[$module->getName()])) {
$name = $module instanceof Module ? $module->getName() : $module;

if (! isset($this->modulesStatuses[$name])) {
return $status === false;
}

return $this->modulesStatuses[$module->getName()] === $status;
return $this->modulesStatuses[$name] === $status;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/LaravelModulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Translation\Translator;
use Nwidart\Modules\Constants\ModuleEvent;
use Nwidart\Modules\Contracts\ActivatorInterface;
use Nwidart\Modules\Contracts\RepositoryInterface;
use Nwidart\Modules\Exceptions\InvalidActivatorClass;
use Nwidart\Modules\Support\Stub;
Expand All @@ -30,7 +31,8 @@ public function boot()
fn () => new ModuleManifest(
new Filesystem(),
app(Contracts\RepositoryInterface::class)->getScanPaths(),
$this->getCachedModulePath()
$this->getCachedModulePath(),
app(ActivatorInterface::class)
)
);

Expand Down
82 changes: 48 additions & 34 deletions src/ModuleManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Exception;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Nwidart\Modules\Contracts\ActivatorInterface;

class ModuleManifest
{
Expand All @@ -12,7 +14,7 @@ class ModuleManifest
*
* @var \Illuminate\Filesystem\Filesystem
*/
public $files;
private $files;

/**
* The base path.
Expand All @@ -28,25 +30,36 @@ class ModuleManifest
*/
public $manifestPath;

/**
* The manifestData
*/
private static ?Collection $manifestData;

/**
* The loaded manifest array.
*
* @var array
*/
public $manifest;
private $manifest;

/**
* module activator class
*/
private ActivatorInterface $activator;

/**
* Create a new package manifest instance.
*
* @param \Illuminate\Support\Collection $paths
* @param Collection $paths
* @param string $manifestPath
* @return void
*/
public function __construct(Filesystem $files, $paths, $manifestPath)
public function __construct(Filesystem $files, $paths, $manifestPath, ActivatorInterface $activator)
{
$this->files = $files;
$this->paths = collect($paths);
$this->manifestPath = $manifestPath;
$this->activator = $activator;
}

/**
Expand Down Expand Up @@ -88,7 +101,7 @@ public function aliases()
public function config($key)
{
return collect($this->getManifest())->flatMap(function ($configuration) use ($key) {
return (array) ($configuration[$key] ?? []);
return (array)($configuration[$key] ?? []);
})->filter()->all();
}

Expand Down Expand Up @@ -116,19 +129,9 @@ protected function getManifest()
*
* @return void
*/
public function build()
public function build(): void
{
$providers = $this->paths
->flatMap(function ($path) {
$manifests = $this->files->glob("{$path}/module.json");
is_array($manifests) || $manifests = [];

return collect($manifests)
->map(function ($manifest) {
return $this->files->json($manifest);
});
})
->sortBy(fn ($module) => $module['priority'] ?? 0)
$providers = $this->getModulesData()
->pluck('providers')
->flatten()
->filter()
Expand All @@ -150,7 +153,7 @@ public function build()
*
* @throws \Exception
*/
protected function write(array $manifest)
protected function write(array $manifest): void
{
if (! is_writable($dirname = dirname($this->manifestPath))) {
throw new Exception("The {$dirname} directory must be present and writable.");
Expand All @@ -164,29 +167,40 @@ protected function write(array $manifest)
public function registerFiles(): void
{
//todo check this section store on module.php or not?
$this->paths
$this->getModulesData()
->each(function (array $manifest) {
if (empty($manifest['files'])) {
return;
}

foreach ($manifest['files'] as $file) {
include_once $manifest['module_directory'].DIRECTORY_SEPARATOR.$file;
}
});
}

public function getModulesData(): Collection
{
if (! empty(self::$manifestData) && ! app()->runningUnitTests()) {
return self::$manifestData;
}

self::$manifestData = $this->paths
->flatMap(function ($path) {
$manifests = $this->files->glob("{$path}/module.json");
is_array($manifests) || $manifests = [];

return collect($manifests)
->map(function ($manifest) {
$json = $this->files->json($manifest);

if (! empty($json['files'])) {
return [
'module_directory' => dirname($manifest),
...$this->files->json($manifest),
];
}
return [
'module_directory' => dirname($manifest),
...$this->files->json($manifest),
];
});
})
->filter()
->sortBy(fn ($module) => $module['priority'] ?? 0)
->each(function (array $manifest) {
foreach ($manifest['files'] as $file) {
include_once $manifest['module_directory'].DIRECTORY_SEPARATOR.$file;
}
});
->filter(fn ($module) => $this->activator->hasStatus($module['name'], true))
->sortBy(fn ($module) => $module['priority'] ?? 0);

return self::$manifestData;
}
}
2 changes: 2 additions & 0 deletions tests/LaravelModulesServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function test_it_throws_exception_if_config_is_invalid()

$this->app['config']->set('modules.activators.file', ['class' => null]);

app()->forgetInstance(ActivatorInterface::class);

$this->assertInstanceOf(ActivatorInterface::class, app(ActivatorInterface::class));
}
}

0 comments on commit d275a5b

Please sign in to comment.