Skip to content

Commit 80323ba

Browse files
authored
Merge pull request #2022 from alissn/UpdateManifest
Remove Cached Module Providers Manifest to Improve Dynamic Module Detection
2 parents 1bec27f + c3da51a commit 80323ba

File tree

6 files changed

+19
-163
lines changed

6 files changed

+19
-163
lines changed

src/Commands/ModuleClearCompiledCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Illuminate\Console\Command;
66
use Nwidart\Modules\ModuleManifest;
77

8+
/**
9+
* @deprecated This command is deprecated and will be removed in the next major version.
10+
*/
811
class ModuleClearCompiledCommand extends Command
912
{
1013
/**
@@ -27,6 +30,6 @@ public function handle(ModuleManifest $manifest): void
2730
@unlink($manifest->manifestPath);
2831
}
2932

30-
$this->components->info('Compiled module files removed successfully.');
33+
$this->components->warn('You may stop calling the `module:clear-compiled` command.');
3134
}
3235
}

src/Commands/ModuleDiscoverCommand.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Illuminate\Console\Command;
66
use Nwidart\Modules\ModuleManifest;
77

8+
/**
9+
* @deprecated This command is deprecated and will be removed in the next major version.
10+
*/
811
class ModuleDiscoverCommand extends Command
912
{
1013
/**
@@ -23,14 +26,6 @@ class ModuleDiscoverCommand extends Command
2326

2427
public function handle(ModuleManifest $manifest): void
2528
{
26-
$this->components->info('Discovering modules');
27-
28-
$manifest->build();
29-
30-
collect($manifest->providersArray())
31-
->map(fn ($provider) => preg_match('/Modules\\\\(.*?)\\\\/', $provider, $matches) ? $matches[1] : null)
32-
->unique()
33-
->each(fn ($description) => $this->components->task($description))
34-
->whenNotEmpty(fn () => $this->newLine());
29+
$this->components->warn('You may stop calling the `module:discover` command.');
3530
}
3631
}

src/ModuleManifest.php

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Nwidart\Modules;
44

5-
use Exception;
65
use Illuminate\Filesystem\Filesystem;
76
use Illuminate\Support\Collection;
87
use Nwidart\Modules\Contracts\ActivatorInterface;
@@ -50,91 +49,28 @@ public function __construct(Filesystem $files, array $paths, string $manifestPat
5049
$this->activator = $activator;
5150
}
5251

53-
/**
54-
* Get all of the service provider class names for all packages.
55-
*/
56-
public function providers(): array
57-
{
58-
return $this->config('providers');
59-
}
60-
61-
/**
62-
* Get all of the service provider class names for all packages.
63-
*/
64-
public function providersArray(): array
65-
{
66-
return $this->getManifest()['providers'] ?? [];
67-
}
68-
69-
/**
70-
* Get all of the aliases for all packages.
71-
*/
72-
public function aliases(): array
73-
{
74-
return $this->config('aliases');
75-
}
76-
77-
/**
78-
* Get all of the values for all packages for the given configuration name.
79-
*/
80-
public function config(string $key): array
81-
{
82-
return collect($this->getManifest())->flatMap(function ($configuration) use ($key) {
83-
return (array) ($configuration[$key] ?? []);
84-
})->filter()->all();
85-
}
86-
8752
/**
8853
* Get the current package manifest.
8954
*/
90-
protected function getManifest(): array
55+
public function getProviders(): array
9156
{
9257
if (! is_null($this->manifest)) {
9358
return $this->manifest;
9459
}
9560

96-
if (! is_file($this->manifestPath)) {
97-
$this->build();
98-
}
99-
100-
return $this->manifest = is_file($this->manifestPath) ?
101-
$this->files->getRequire($this->manifestPath) : [];
61+
return $this->manifest = $this->build();
10262
}
10363

10464
/**
10565
* Build the manifest and write it to disk.
10666
*/
107-
public function build(): void
67+
public function build(): array
10868
{
109-
$providers = $this->getModulesData()
69+
return $this->getModulesData()
11070
->pluck('providers')
11171
->flatten()
11272
->filter()
11373
->toArray();
114-
115-
$this->write(
116-
[
117-
'providers' => $providers,
118-
'eager' => $providers,
119-
'deferred' => [],
120-
]
121-
);
122-
}
123-
124-
/**
125-
* Write the given manifest array to disk.
126-
*
127-
* @throws \Exception
128-
*/
129-
protected function write(array $manifest): void
130-
{
131-
if (! is_writable($dirname = dirname($this->manifestPath))) {
132-
throw new Exception("The {$dirname} directory must be present and writable.");
133-
}
134-
$this->files->replace(
135-
$this->manifestPath,
136-
'<?php return '.var_export($manifest, true).';'
137-
);
13874
}
13975

14076
public function registerFiles(): void

src/ModulesServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function registerModules()
3131
$manifest = app()->make(ModuleManifest::class);
3232

3333
(new ProviderRepository($this->app, new Filesystem, $this->getCachedModulePath()))
34-
->load($manifest->providersArray());
34+
->load($manifest->getProviders());
3535

3636
$manifest->registerFiles();
3737

tests/Commands/Actions/ClearCompiledCommandTest.php

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Nwidart\Modules\ModuleManifest;
88
use Nwidart\Modules\Tests\BaseTestCase;
99

10+
/**
11+
* @deprecated This Test File is deprecated and will be removed in the next major version.
12+
*/
1013
class ClearCompiledCommandTest extends BaseTestCase
1114
{
1215
private RepositoryInterface $repository;
@@ -37,46 +40,4 @@ public function test_manifest_file_clear_when_call_command()
3740
$this->assertFileDoesNotExist($this->manifestPath);
3841
$this->assertSame(0, $code);
3942
}
40-
41-
public function test_manifest_file_clear_when_create_module()
42-
{
43-
$this->assertFileExists($this->manifestPath);
44-
45-
$this->createModule('Foo');
46-
47-
$this->assertFileDoesNotExist($this->manifestPath);
48-
}
49-
50-
public function test_manifest_file_clear_when_delete_module()
51-
{
52-
$this->assertFileExists($this->manifestPath);
53-
54-
$this->createModule('Foo');
55-
56-
$this->artisan('module:delete', ['module' => 'Foo', '--force' => true]);
57-
58-
$this->assertFileDoesNotExist($this->manifestPath);
59-
}
60-
61-
public function test_manifest_file_clear_when_disable_module()
62-
{
63-
$this->assertFileExists($this->manifestPath);
64-
65-
$this->createModule('Foo');
66-
67-
$this->artisan('module:disable', ['module' => 'Foo']);
68-
69-
$this->assertFileDoesNotExist($this->manifestPath);
70-
}
71-
72-
public function test_manifest_file_clear_when_enable_module()
73-
{
74-
$this->assertFileExists($this->manifestPath);
75-
76-
$this->createModule('Foo');
77-
78-
$this->artisan('module:enable', ['module' => 'Foo']);
79-
80-
$this->assertFileDoesNotExist($this->manifestPath);
81-
}
8243
}

tests/Commands/Actions/ModuleDiscoverCommandTest.php

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Nwidart\Modules\ModuleManifest;
88
use Nwidart\Modules\Tests\BaseTestCase;
99

10+
/**
11+
* @deprecated This Test File is deprecated and will be removed in the next major version.
12+
*/
1013
class ModuleDiscoverCommandTest extends BaseTestCase
1114
{
1215
private RepositoryInterface $repository;
@@ -37,46 +40,4 @@ public function test_run_command_without_error()
3740

3841
$this->assertSame(0, $code);
3942
}
40-
41-
public function test_manifest_file_contain_new_module_provider()
42-
{
43-
$this->createModule('Foo');
44-
45-
$path = base_path('modules/Foo').'/module.json';
46-
$provider = json_decode($this->finder->get($path))->providers[0];
47-
48-
$code = $this->artisan('module:discover');
49-
$this->assertSame(0, $code);
50-
51-
$manifest = require $this->manifestPath;
52-
53-
$this->assertContains($provider, $manifest['providers'], 'provider not found in manifest file');
54-
$this->assertContains($provider, $manifest['eager'], 'provider not found in manifest file');
55-
}
56-
57-
public function test_manifest_file_contain_multi_module_provider()
58-
{
59-
$modules = [
60-
'Foo',
61-
'Bar',
62-
'Baz',
63-
];
64-
65-
foreach ($modules as $module) {
66-
$this->createModule($module);
67-
}
68-
69-
$code = $this->artisan('module:discover');
70-
$this->assertSame(0, $code);
71-
72-
$manifest = require $this->manifestPath;
73-
74-
foreach ($modules as $module) {
75-
$path = module_path($module).'/module.json';
76-
$provider = json_decode($this->finder->get($path))->providers[0];
77-
78-
$this->assertContains($provider, $manifest['providers'], 'provider not found in manifest file');
79-
$this->assertContains($provider, $manifest['eager'], 'provider not found in manifest file');
80-
}
81-
}
8243
}

0 commit comments

Comments
 (0)