-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWireModuleMediaServiceProvider.php
More file actions
133 lines (119 loc) · 5.85 KB
/
Copy pathWireModuleMediaServiceProvider.php
File metadata and controls
133 lines (119 loc) · 5.85 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
namespace NyonCode\WireModuleMedia;
use Illuminate\Support\Facades\Route;
use Livewire\Livewire;
use NyonCode\LaravelPackageToolkit\Commands\InstallCommand;
use NyonCode\LaravelPackageToolkit\Packager;
use NyonCode\LaravelPackageToolkit\PackageServiceProvider;
use NyonCode\WireCore\Core\Plugin\PluginManager;
use NyonCode\WireCore\Foundation\Assets\Bundle;
use NyonCode\WireCore\Foundation\Setup\SetupRegistry;
use NyonCode\WireCore\Foundation\View\PageChrome;
use NyonCode\WireModuleMedia\Console\MakeThumbnailsCommand;
use NyonCode\WireModuleMedia\Console\SyncUsageCommand;
use NyonCode\WireModuleMedia\Contracts\MakesThumbnails;
use NyonCode\WireModuleMedia\Http\Controllers\MediaController;
use NyonCode\WireModuleMedia\Install\LinkPublicDisk;
use NyonCode\WireModuleMedia\Livewire\MediaPicker;
use NyonCode\WireModuleMedia\Support\GdThumbnailer;
/** A module that arrives as a package; see wire-module-users for the shape. */
class WireModuleMediaServiceProvider extends PackageServiceProvider
{
/** The editor's controller. This package's first and only JS (ADR 0024). */
public const ASSETS_PATH = __DIR__.'/../dist';
/**
* @throws \Exception
*/
public function configure(Packager $packager): void
{
$packager
->name('WireModuleMedia')
->hasShortName('wire-module-media')
->registeredPackage(function (): void {
// GD by default, because most PHP builds have it and a fresh
// installation should get thumbnails without installing
// anything. `bind`, not `singleton`: an application that would
// rather use Imagick, Intervention or a resizing CDN replaces
// this in one line and nothing else here changes.
$this->app->bind(MakesThumbnails::class, GdThumbnailer::class);
$this->app->resolving(PluginManager::class, function (PluginManager $manager): void {
if (! $manager->has('media')) {
$manager->register(new MediaModule);
}
});
// Without the symlink every uploaded file is a 404 and nothing errors.
SetupRegistry::instance()->register(LinkPublicDisk::class);
})
->hasViews()
->hasCommands([MakeThumbnailsCommand::class, SyncUsageCommand::class])
->bootedPackage(function (): void {
$this->registerRoutes();
// The picker, addressable by name so a Blade file can mount it
// without importing a class from a package it does not require.
Livewire::component('wire-media-picker', MediaPicker::class);
// And rendered once per page by whatever draws the chrome. This
// is the whole reason PageChrome exists: the shell sits above
// this package and cannot name its views, and this package
// cannot reach into the shell's layout.
$this->app->make(PageChrome::class)->add('wire-module-media::picker-modal');
Bundle::serve('wire-module-media', self::ASSETS_PATH);
})
->hasAssets('dist', entries: [
Bundle::make('wire-media-editor.js'),
])
->hasAssetFallback(Bundle::servedByRoute('wire-module-media'))
->hasConfig()
->hasMigrations()
->hasTranslations()
->hasInstallCommand(function (InstallCommand $command): void {
$command
->publishConfig()
->publishMigrations()
->afterInstallation(function (InstallCommand $installer): void {
$installer->comment(' ✅ The media library is registered as the `media` module');
$installer->comment(' • Run: php artisan migrate');
// The default disk is `public`, which is a symlink an
// application makes once and forgets — and until it does,
// every preview is a broken image.
if ((string) config('wire-module-media.disk', 'public') === 'public') {
$installer->comment(' • Run: php artisan storage:link (the public disk needs it)');
}
});
})
->hasAbout();
}
/**
* The route that streams a file the disk will not hand out itself.
*
* Registered here rather than in a routes file, because whether it exists at
* all and what middleware it sits behind are the application's configuration
* — and a routes file cannot be conditional on that without reading config
* at a point where it may not be loaded yet.
*
* Skipped when the application has cached its routes, which is Laravel's own
* rule: a cached route file is the whole route table, and adding to it at
* boot would mean two different tables depending on whether a cache exists.
*/
protected function registerRoutes(): void
{
if (! config('wire-module-media.route.enabled', true) || $this->app->routesAreCached()) {
return;
}
Route::middleware((array) config('wire-module-media.route.middleware', ['web', 'auth']))
->prefix((string) config('wire-module-media.route.prefix', 'wire-media'))
->group(function (): void {
Route::get('{media}', [MediaController::class, 'show'])->name('wire-media.show');
Route::get('{media}/download', [MediaController::class, 'download'])->name('wire-media.download');
});
}
/**
* @return array<string, string>
*/
public function aboutData(): array
{
return [
'Media disk' => (string) config('wire-module-media.disk', 'public'),
];
}
}