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

plank / laravel-mediable / 8619248403

09 Apr 2024 04:37PM UTC coverage: 94.908%. Remained the same
8619248403

push

github

frasmage
fix typo in driver class name

0 of 2 new or added lines in 1 file covered. (0.0%)

1491 of 1571 relevant lines covered (94.91%)

155.05 hits per line

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

92.0
/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()) {
1,506✔
32
            return;
×
33
        }
34

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

43
        $time = time();
1,506✔
44

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

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

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

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

108
        $this->registerSourceAdapterFactory();
1,506✔
109
        $this->registerImageManipulator();
1,506✔
110
        $this->registerUploader();
1,506✔
111
        $this->registerMover();
1,506✔
112
        $this->registerUrlGeneratorFactory();
1,506✔
113
        $this->registerConsoleCommands();
1,506✔
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) {
1,506✔
125
            $factory = new SourceAdapterFactory;
900✔
126

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

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

137
            return $factory;
900✔
138
        });
1,506✔
139
        $this->app->alias('mediable.source.factory', SourceAdapterFactory::class);
1,506✔
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) {
1,506✔
149
            return new MediaUploader(
894✔
150
                $app['filesystem'],
894✔
151
                $app['mediable.source.factory'],
894✔
152
                $app[ImageManipulator::class],
894✔
153
                $app['config']->get('mediable')
894✔
154
            );
894✔
155
        });
1,506✔
156
        $this->app->alias('mediable.uploader', MediaUploader::class);
1,506✔
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) {
1,506✔
166
            return new MediaMover($app['filesystem']);
42✔
167
        });
1,506✔
168
        $this->app->alias('mediable.mover', MediaMover::class);
1,506✔
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) {
1,506✔
178
            $factory = new UrlGeneratorFactory;
72✔
179

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

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

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

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

214
    private function getInterventionImageManagerConfiguration(Container $app): ?ImageManager
215
    {
216
        $imageManager = null;
900✔
217
        if ($app->has(ImageManager::class)
900✔
218
            || (
219
                class_exists(DriverInterface::class) // intervention >= 3.0
900✔
220
                && $app->has(DriverInterface::class)
900✔
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')) {
450✔
226
            // attempt to automatically configure for imagick
227
            if (class_exists(\Intervention\Image\Drivers\Imagick\Driver::class)) {
450✔
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']);
450✔
235
            }
236
        } elseif (extension_loaded('gd')) {
×
237
            // attempt to automatically configure for gd
NEW
238
            if (class_exists(\Intervention\Image\Drivers\Gd\Driver::class)) {
×
239
                // intervention/image >=3.0
240
                $imageManager = new ImageManager(
×
NEW
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;
900✔
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