• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

plank / laravel-mediable / 12171312057

05 Dec 2024 01:13AM UTC coverage: 94.091% (-0.6%) from 94.663%
12171312057

Pull #369

github

frasmage
test without lowest
Pull Request #369: add php 8.4 to test suite

1481 of 1574 relevant lines covered (94.09%)

77.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.6
/src/MediableServiceProvider.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Plank\Mediable;
5

6
use Illuminate\Contracts\Container\Container;
7
use Illuminate\Filesystem\FilesystemManager;
8
use Illuminate\Support\ServiceProvider;
9
use Intervention\Image\ImageManager;
10
use Intervention\Image\Interfaces\DriverInterface;
11
use Plank\Mediable\Commands\ImportMediaCommand;
12
use Plank\Mediable\Commands\PruneMediaCommand;
13
use Plank\Mediable\Commands\SyncMediaCommand;
14
use Plank\Mediable\SourceAdapters\SourceAdapterFactory;
15
use Plank\Mediable\UrlGenerators\UrlGeneratorFactory;
16

17
/**
18
 * Mediable Service Provider.
19
 *
20
 * Registers Laravel-Mediable package functionality
21
 */
22
class MediableServiceProvider extends ServiceProvider
23
{
24
    /**
25
     * Boot the service provider.
26
     *
27
     * @return void
28
     */
29
    public function boot(): void
30
    {
31
        if (!$this->app->runningInConsole()) {
753✔
32
            return;
×
33
        }
34

35
        $root = dirname(__DIR__);
753✔
36
        $this->publishes(
753✔
37
            [
753✔
38
                $root . '/config/mediable.php' => config_path('mediable.php'),
753✔
39
            ],
753✔
40
            'config'
753✔
41
        );
753✔
42

43
        $time = time();
753✔
44

45
        if (empty(glob($this->app->databasePath('migrations/*_create_mediable_tables.php')))) {
753✔
46
            $this->publishes(
753✔
47
                [
753✔
48
                    $root . '/migrations/2016_06_27_000000_create_mediable_tables.php' =>
753✔
49
                        $this->app->databasePath(
753✔
50
                            'migrations/' . date(
753✔
51
                                'Y_m_d_His',
753✔
52
                                $time
753✔
53
                            ) . '_create_mediable_tables.php'
753✔
54
                        )
753✔
55
                ],
753✔
56
                'mediable-migrations'
753✔
57
            );
753✔
58
            $time++;
753✔
59
        }
60
        if (empty(glob($this->app->databasePath('migrations/*_add_variants_to_media.php')))) {
753✔
61
            $this->publishes(
753✔
62
                [
753✔
63
                    $root . '/migrations/2020_10_12_000000_add_variants_to_media.php' =>
753✔
64
                        $this->app->databasePath(
753✔
65
                            'migrations/' . date(
753✔
66
                                'Y_m_d_His',
753✔
67
                                $time
753✔
68
                            ) . '_add_variants_to_media.php'
753✔
69
                        )
753✔
70
                ],
753✔
71
                'mediable-migrations'
753✔
72
            );
753✔
73
            $time++;
753✔
74
        }
75

76
        if (empty(glob($this->app->databasePath('migrations/*_add_alt_to_media.php')))) {
753✔
77
            $this->publishes(
753✔
78
                [
753✔
79
                    $root . '/migrations/2024_03_30_000000_add_alt_to_media.php' =>
753✔
80
                        $this->app->databasePath(
753✔
81
                            'migrations/' . date(
753✔
82
                                'Y_m_d_His',
753✔
83
                                $time
753✔
84
                            ) . '_add_alt_to_media.php'
753✔
85
                        ),
753✔
86
                ],
753✔
87
                'mediable-migrations'
753✔
88
            );
753✔
89
        }
90

91
        if (!config('mediable.ignore_migrations', false)) {
753✔
92
            $this->loadMigrationsFrom($root . '/migrations');
753✔
93
        }
94
    }
95

96
    /**
97
     * Register the service provider.
98
     *
99
     * @return void
100
     */
101
    public function register(): void
102
    {
103
        $this->mergeConfigFrom(
753✔
104
            dirname(__DIR__) . '/config/mediable.php',
753✔
105
            'mediable'
753✔
106
        );
753✔
107

108
        $this->registerSourceAdapterFactory();
753✔
109
        $this->registerImageManipulator();
753✔
110
        $this->registerUploader();
753✔
111
        $this->registerMover();
753✔
112
        $this->registerUrlGeneratorFactory();
753✔
113
        $this->registerConsoleCommands();
753✔
114
    }
115

116
    /**
117
     * Bind an instance of the Source Adapter Factory to the container.
118
     *
119
     * Attaches the default adapter types
120
     * @return void
121
     */
122
    public function registerSourceAdapterFactory(): void
123
    {
124
        $this->app->singleton('mediable.source.factory', function (Container $app) {
753✔
125
            $factory = new SourceAdapterFactory;
450✔
126

127
            $classAdapters = $app['config']->get('mediable.source_adapters.class', []);
450✔
128
            foreach ($classAdapters as $source => $adapter) {
450✔
129
                $factory->setAdapterForClass($adapter, $source);
450✔
130
            }
131

132
            $patternAdapters = $app['config']->get('mediable.source_adapters.pattern', []);
450✔
133
            foreach ($patternAdapters as $source => $adapter) {
450✔
134
                $factory->setAdapterForPattern($adapter, $source);
450✔
135
            }
136

137
            return $factory;
450✔
138
        });
753✔
139
        $this->app->alias('mediable.source.factory', SourceAdapterFactory::class);
753✔
140
    }
141

142
    /**
143
     * Bind the Media Uploader to the container.
144
     * @return void
145
     */
146
    public function registerUploader(): void
147
    {
148
        $this->app->bind('mediable.uploader', function (Container $app) {
753✔
149
            return new MediaUploader(
447✔
150
                $app['filesystem'],
447✔
151
                $app['mediable.source.factory'],
447✔
152
                $app[ImageManipulator::class],
447✔
153
                $app['config']->get('mediable')
447✔
154
            );
447✔
155
        });
753✔
156
        $this->app->alias('mediable.uploader', MediaUploader::class);
753✔
157
    }
158

159
    /**
160
     * Bind the Media Uploader to the container.
161
     * @return void
162
     */
163
    public function registerMover(): void
164
    {
165
        $this->app->bind('mediable.mover', function (Container $app) {
753✔
166
            return new MediaMover($app['filesystem']);
21✔
167
        });
753✔
168
        $this->app->alias('mediable.mover', MediaMover::class);
753✔
169
    }
170

171
    /**
172
     * Bind the Media Uploader to the container.
173
     * @return void
174
     */
175
    public function registerUrlGeneratorFactory(): void
176
    {
177
        $this->app->singleton('mediable.url.factory', function (Container $app) {
753✔
178
            $factory = new UrlGeneratorFactory;
36✔
179

180
            $config = $app['config']->get('mediable.url_generators', []);
36✔
181
            foreach ($config as $driver => $generator) {
36✔
182
                $factory->setGeneratorForFilesystemDriver($generator, $driver);
36✔
183
            }
184

185
            return $factory;
36✔
186
        });
753✔
187
        $this->app->alias('mediable.url.factory', UrlGeneratorFactory::class);
753✔
188
    }
189

190
    public function registerImageManipulator(): void
191
    {
192
        $this->app->singleton(ImageManipulator::class, function (Container $app) {
753✔
193
            return new ImageManipulator(
450✔
194
                $this->getInterventionImageManagerConfiguration($app),
450✔
195
                $app->get(FilesystemManager::class),
450✔
196
                $app->get(ImageOptimizer::class)
450✔
197
            );
450✔
198
        });
753✔
199
    }
200

201
    /**
202
     * Add package commands to artisan console.
203
     * @return void
204
     */
205
    public function registerConsoleCommands(): void
206
    {
207
        $this->commands([
753✔
208
            ImportMediaCommand::class,
753✔
209
            PruneMediaCommand::class,
753✔
210
            SyncMediaCommand::class,
753✔
211
        ]);
753✔
212
    }
213

214
    private function getInterventionImageManagerConfiguration(Container $app): ?ImageManager
215
    {
216
        $imageManager = null;
450✔
217
        if ($app->has(ImageManager::class)
450✔
218
            || (
219
                class_exists(DriverInterface::class) // intervention >= 3.0
450✔
220
                && $app->has(DriverInterface::class)
450✔
221
            )
222
        ) {
223
            // use whatever the user has bound to the container if available
224
            $imageManager = $app->get(ImageManager::class);
450✔
225
        } elseif (extension_loaded('imagick')) {
×
226
            // attempt to automatically configure for imagick
227
            if (class_exists(\Intervention\Image\Drivers\Imagick\Driver::class)) {
×
228
                // intervention/image >=3.0
229
                $imageManager = new ImageManager(
×
230
                    new \Intervention\Image\Drivers\Imagick\Driver()
×
231
                );
×
232
            } else {
233
                // intervention/image <3.0
234
                $imageManager = new ImageManager(['driver' => 'imagick']);
×
235
            }
236
        } elseif (extension_loaded('gd')) {
×
237
            // attempt to automatically configure for gd
238
            if (class_exists(\Intervention\Image\Drivers\Gd\Driver::class)) {
×
239
                // intervention/image >=3.0
240
                $imageManager = new ImageManager(
×
241
                    new \Intervention\Image\Drivers\Gd\Driver()
×
242
                );
×
243
            } else {
244
                // intervention/image <3.0
245
                $imageManager = new ImageManager(['driver' => 'gd']);
×
246
            }
247
        }
248

249
        return $imageManager;
450✔
250
    }
251
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc