Repository navigation
Expand file tree
/
Copy pathWireModuleAuditServiceProvider.php
More file actions
83 lines (74 loc) · 3.33 KB
/
Copy pathWireModuleAuditServiceProvider.php
File metadata and controls
83 lines (74 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace NyonCode\WireModuleAudit;
use NyonCode\LaravelPackageToolkit\Commands\InstallCommand;
use NyonCode\LaravelPackageToolkit\Packager;
use NyonCode\LaravelPackageToolkit\PackageServiceProvider;
use NyonCode\WireCore\Core\Plugin\PluginManager;
use NyonCode\WireCore\Foundation\Setup\SetupRegistry;
use NyonCode\WireModuleAudit\Install\RecordAuditTrail;
use NyonCode\WireModuleAudit\Support\AuditLog;
/**
* A module that arrives as a package — the same shape wire-module-users has.
*
* `resolving`, in the register phase: the module has to be in the manager's list
* before it boots and before the core provider spreads declarations into the
* registries. Later is refused, because a module that arrives then looks
* installed and does nothing.
*/
class WireModuleAuditServiceProvider extends PackageServiceProvider
{
/**
* @throws \Exception
*/
public function configure(Packager $packager): void
{
$packager
->name('WireModuleAudit')
->hasShortName('wire-module-audit')
->registeredPackage(function (): void {
$this->app->resolving(PluginManager::class, function (PluginManager $manager): void {
if (! $manager->has('audit')) {
$manager->register(new AuditModule);
}
});
// Recording lives in another package's config, so this module can be
// installed, work perfectly, and show an empty table for ever.
SetupRegistry::instance()->register(RecordAuditTrail::class);
})
->hasConfig()
->hasTranslations()
->hasInstallCommand(function (InstallCommand $command): void {
$command
->publishConfig()
->afterInstallation(function (InstallCommand $installer): void {
$installer->comment(' ✅ The audit log is registered as the `audit` module');
// Two things this package does not own, and both of them
// decide whether the screen has anything on it. Said at
// install time because the alternative is an empty log
// that reads as a broken package.
// The table is core's migration, and core publishes it on
// demand — so an installation can have the screen and no
// table at all.
if (! AuditLog::available()) {
$installer->comment(' ⚠️ No `audit_logs` table — run: php artisan vendor:publish --tag=wire-core::migrations && php artisan migrate');
}
// And the recording itself is core's switch.
if (! AuditLog::recording()) {
$installer->comment(' ⚠️ wire-core.audit.enabled is off — nothing is being recorded yet');
}
});
})
->hasAbout();
}
/**
* @return array<string, string>
*/
public function aboutData(): array
{
return [
'Recording' => AuditLog::recording() ? 'on' : 'off',
'Log table' => AuditLog::available() ? 'ready' : 'missing',
];
}
}