Skip to content

Commit

Permalink
add custom MimeTypes class
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Feb 4, 2020
1 parent 4fac507 commit 8f7a178
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 16 deletions.
9 changes: 5 additions & 4 deletions src/Facades/MimeTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
namespace Astrotomic\LaravelMime\Facades;

use Illuminate\Support\Facades\Facade;
use Symfony\Component\Mime\MimeTypesInterface;
use Astrotomic\LaravelMime\MimeTypes as AstrotomicMimeTypes;

/**
* @method static bool isGuesserSupported()
* @method static null|string guessMimeType(string $path)
* @method static string[] getExtensions(string $mimeType)
* @method static string[] getMimeTypes(string $ext)
* @method static string[] getExtensions(string|string[] $mimeTypes)
* @method static string[] getMimeTypes(string|string[] $extensions)
*
* @see \Symfony\Component\Mime\MimeTypeGuesserInterface
* @see \Symfony\Component\Mime\MimeTypesInterface
* @see \Symfony\Component\Mime\MimeTypes
* @see \Astrotomic\LaravelMime\MimeTypes
*/
class MimeTypes extends Facade
{
protected static function getFacadeAccessor(): string
{
return MimeTypesInterface::class;
return AstrotomicMimeTypes::class;
}
}
68 changes: 68 additions & 0 deletions src/MimeTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Astrotomic\LaravelMime;

use BadMethodCallException;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Symfony\Component\Mime\MimeTypes as SymfonyMimeTypes;

/** @mixin SymfonyMimeTypes */
class MimeTypes
{
/** @var SymfonyMimeTypes */
protected $mimeTypes;

public function __construct(SymfonyMimeTypes $mimeTypes)
{
$this->mimeTypes = $mimeTypes;
}

/**
* @param string|string[] $mimeTypes
*
* @return array
*/
public function getExtensions($mimeTypes): array
{
return Collection::make(Arr::wrap($mimeTypes))
->map(function (string $mimeType): array {
return $this->mimeTypes->getExtensions($mimeType);
})
->collapse()
->unique()
->values()
->all();
}

/**
* @param string|string[] $extensions
*
* @return array
*/
public function getMimeTypes($extensions): array
{
return Collection::make(Arr::wrap($extensions))
->map(function (string $extension): array {
return $this->mimeTypes->getMimeTypes($extension);
})
->collapse()
->unique()
->values()
->all();
}

public function __call(string $method, array $arguments)
{
if (method_exists($this->mimeTypes, $method)) {
return call_user_func_array([$this->mimeTypes, $method], $arguments);
}

throw new BadMethodCallException(sprintf(
'Method "%s" does not exist on class "%s" and could not be found on "%s".',
$method,
static::class,
get_class($this->mimeTypes)
));
}
}
15 changes: 9 additions & 6 deletions src/MimeTypesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Mime\MimeTypes as SymfonyMimeTypes;
use Symfony\Component\Mime\MimeTypesInterface;

class MimeTypesServiceProvider extends ServiceProvider implements DeferrableProvider
{
public function register(): void
{
$this->app->singleton('laravel-mime',MimeTypes::class);
$this->app->alias('laravel-mime', MimeTypesInterface::class);
$this->app->alias('laravel-mime', MimeTypeGuesserInterface::class);
$this->app->singleton(SymfonyMimeTypes::class,SymfonyMimeTypes::class);
$this->app->alias(SymfonyMimeTypes::class, MimeTypesInterface::class);
$this->app->alias(SymfonyMimeTypes::class, MimeTypeGuesserInterface::class);

$this->app->singleton(MimeTypes::class,MimeTypes::class);
}

public function provides(): array
{
return [
'laravel-mime',
MimeTypes::class,
SymfonyMimeTypes::class,
MimeTypesInterface::class,
MimeTypeGuesserInterface::class,

MimeTypes::class,
];
}
}
64 changes: 58 additions & 6 deletions tests/MimeTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Astrotomic\LaravelMime\Tests;

use Astrotomic\LaravelMime\MimeTypes;
use Astrotomic\LaravelMime\MimeTypesServiceProvider;
use Illuminate\Support\Arr;
use Orchestra\Testbench\TestCase;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Mime\MimeTypes as SymfonyMimeTypes;
use Symfony\Component\Mime\MimeTypesInterface;
use Astrotomic\LaravelMime\Facades\MimeTypes as MimeTypesFacade;

class MimeTypesTest extends TestCase
{
Expand All @@ -19,20 +22,69 @@ protected function getPackageProviders($app)

/**
* @test
* @dataProvider serviceBindings
* @dataProvider symfonyServiceBindings
*/
public function it_can_instantiate_service(string $service): void
public function it_can_instantiate_symfony_bindings(string $service): void
{
$this->assertInstanceOf(SymfonyMimeTypes::class, $this->app->make($service));
}

/**
* @test
* @dataProvider laravelServiceBindings
*/
public function it_can_instantiate_laravel_bindings(string $service): void
{
$this->assertInstanceOf(MimeTypes::class, $this->app->make($service));
}

public function serviceBindings(): array
/** @test */
public function it_can_convert_multiple_mimetypes_into_extensions(): void
{
$extensions = MimeTypesFacade::getExtensions([
'image/png',
'image/jpeg',
'image/jpeg2000',
'image/jpeg2',
]);

$this->assertEquals(
Arr::sort(['png', 'jpeg', 'jpg', 'jpe', 'jp2', 'jpg2']),
Arr::sort($extensions)
);
}

/** @test */
public function it_can_convert_multiple_extensions_into_mimetypes(): void
{
$extensions = MimeTypesFacade::getMimeTypes([
'png',
'jpeg',
'jpg',
'jpe',
'jp2',
'jpg2',
]);

$this->assertEquals(
Arr::sort(['image/png', 'image/jpeg', 'image/pjpeg', 'image/jp2', 'image/jpeg2000', 'image/jpeg2000-image', 'image/x-jpeg2000-image']),
Arr::sort($extensions)
);
}

public function symfonyServiceBindings(): array
{
return [
['laravel-mime'],
[MimeTypes::class],
[SymfonyMimeTypes::class],
[MimeTypesInterface::class],
[MimeTypeGuesserInterface::class],
];
}

public function laravelServiceBindings(): array
{
return [
[MimeTypes::class],
];
}
}

0 comments on commit 8f7a178

Please sign in to comment.