Skip to content

Commit e8bf819

Browse files
committed
[Mime] added the component
1 parent a99b76f commit e8bf819

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2675
-58
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"symfony/ldap": "self.version",
6161
"symfony/lock": "self.version",
6262
"symfony/messenger": "self.version",
63+
"symfony/mime": "self.version",
6364
"symfony/monolog-bridge": "self.version",
6465
"symfony/options-resolver": "self.version",
6566
"symfony/process": "self.version",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* Registers custom mime types guessers.
20+
*
21+
* @author Fabien Potencier <fabien@symfony.com>
22+
*/
23+
class AddMimeTypeGuesserPass implements CompilerPassInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function process(ContainerBuilder $container)
29+
{
30+
if ($container->has('mime_types')) {
31+
$definition = $container->findDefinition('mime_types');
32+
foreach ($container->findTaggedServiceIds('mime.mime_type_guesser', true) as $id => $attributes) {
33+
$definition->addMethodCall('registerGuesser', array(new Reference($id)));
34+
}
35+
}
36+
}
37+
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+8
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
7474
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
7575
use Symfony\Component\Messenger\Transport\TransportInterface;
76+
use Symfony\Component\Mime\MimeTypes;
77+
use Symfony\Component\Mime\MimeTypeGuesserInterface;
7678
use Symfony\Component\PropertyAccess\PropertyAccessor;
7779
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
7880
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
@@ -310,6 +312,10 @@ public function load(array $configs, ContainerBuilder $container)
310312
'Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController',
311313
));
312314

315+
if (class_exists(MimeTypes::class)) {
316+
$loader->load('mime_type.xml');
317+
}
318+
313319
$container->registerForAutoconfiguration(Command::class)
314320
->addTag('console.command');
315321
$container->registerForAutoconfiguration(ResourceCheckerInterface::class)
@@ -374,6 +380,8 @@ public function load(array $configs, ContainerBuilder $container)
374380
->addTag('messenger.message_handler');
375381
$container->registerForAutoconfiguration(TransportFactoryInterface::class)
376382
->addTag('messenger.transport_factory');
383+
$container->registerForAutoconfiguration(MimeTypeGuesserInterface::class)
384+
->addTag('mime.mime_type_guesser');
377385
$container->registerForAutoconfiguration(LoggerAwareInterface::class)
378386
->addMethodCall('setLogger', array(new Reference('logger')));
379387

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass;
16+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddMimeTypeGuesserPass;
1617
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
1718
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
1819
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
@@ -72,6 +73,11 @@ public function boot()
7273
if ($trustedHosts = $this->container->getParameter('kernel.trusted_hosts')) {
7374
Request::setTrustedHosts($trustedHosts);
7475
}
76+
77+
if ($this->container->has('mime_types')) {
78+
$mt = $this->container->get('mime_types');
79+
$mt->setDefault($mt);
80+
}
7581
}
7682

7783
public function build(ContainerBuilder $container)
@@ -118,6 +124,7 @@ public function build(ContainerBuilder $container)
118124
$container->addCompilerPass(new ResettableServicePass());
119125
$container->addCompilerPass(new TestServiceContainerWeakRefPass(), PassConfig::TYPE_BEFORE_REMOVING, -32);
120126
$container->addCompilerPass(new TestServiceContainerRealRefPass(), PassConfig::TYPE_AFTER_REMOVING);
127+
$container->addCompilerPass(new AddMimeTypeGuesserPass());
121128
$this->addCompilerPassIfExists($container, MessengerPass::class);
122129

123130
if ($container->getParameter('kernel.debug')) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<defaults public="false" />
9+
10+
<service id="mime_types" class="Symfony\Component\Mime\MimeTypes" public="true" />
11+
</services>
12+
</container>

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
</service>
4343

4444
<service id="serializer.normalizer.data_uri" class="Symfony\Component\Serializer\Normalizer\DataUriNormalizer">
45+
<argument type="service" id="mime_types" on-invalid="null" />
4546
<!-- Run before serializer.normalizer.object -->
4647
<tag name="serializer.normalizer" priority="-920" />
4748
</service>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddMimeTypeGuesserPass;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
use Symfony\Component\Mime\FileinfoMimeTypeGuesser;
20+
use Symfony\Component\Mime\MimeTypes;
21+
22+
class AddMimeTypeGuesserPassTest extends TestCase
23+
{
24+
public function testTags()
25+
{
26+
$container = new ContainerBuilder();
27+
$container->addCompilerPass(new AddMimeTypeGuesserPass());
28+
29+
$definition = new Definition(FileinfoMimeTypeGuesser::class);
30+
$definition->addArgument('/path/to/magic/file');
31+
$definition->addTag('mime.mime_type_guesser');
32+
$container->setDefinition('some_mime_type_guesser', $definition->setPublic(true));
33+
$container->register('mime_types', MimeTypes::class)->setPublic(true);
34+
$container->compile();
35+
36+
$router = $container->getDefinition('mime_types');
37+
$calls = $router->getMethodCalls();
38+
$this->assertCount(1, $calls);
39+
$this->assertEquals('registerGuesser', $calls[0][0]);
40+
$this->assertEquals(new Reference('some_mime_type_guesser'), $calls[0][1][0]);
41+
}
42+
}

src/Symfony/Bundle/FrameworkBundle/composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"symfony/contracts": "^1.0.2",
2424
"symfony/dependency-injection": "^4.2",
2525
"symfony/event-dispatcher": "^4.1",
26-
"symfony/http-foundation": "^4.1.2",
26+
"symfony/http-foundation": "^4.3",
2727
"symfony/http-kernel": "^4.2",
2828
"symfony/polyfill-mbstring": "~1.0",
2929
"symfony/filesystem": "~3.4|~4.0",
@@ -43,10 +43,11 @@
4343
"symfony/form": "^4.2",
4444
"symfony/expression-language": "~3.4|~4.0",
4545
"symfony/messenger": "^4.2",
46+
"symfony/mime": "^4.3",
4647
"symfony/process": "~3.4|~4.0",
4748
"symfony/security-core": "~3.4|~4.0",
4849
"symfony/security-csrf": "~3.4|~4.0",
49-
"symfony/serializer": "^4.2",
50+
"symfony/serializer": "^4.3",
5051
"symfony/stopwatch": "~3.4|~4.0",
5152
"symfony/translation": "~4.2",
5253
"symfony/templating": "~3.4|~4.0",

src/Symfony/Component/HttpFoundation/File/File.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
use Symfony\Component\HttpFoundation\File\Exception\FileException;
1515
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
16-
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
17-
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
16+
use Symfony\Component\Mime\MimeTypes;
1817

1918
/**
2019
* A file in the file system.
@@ -50,12 +49,12 @@ public function __construct(string $path, bool $checkPath = true)
5049
*
5150
* @return string|null The guessed extension or null if it cannot be guessed
5251
*
53-
* @see ExtensionGuesser
52+
* @see MimeTypes
5453
* @see getMimeType()
5554
*/
5655
public function guessExtension()
5756
{
58-
return ExtensionGuesser::getInstance()->guess($this->getMimeType());
57+
return MimeTypes::getDefault()->getExtensions($this->getMimeType())[0] ?? null;
5958
}
6059

6160
/**
@@ -67,11 +66,11 @@ public function guessExtension()
6766
*
6867
* @return string|null The guessed mime type (e.g. "application/pdf")
6968
*
70-
* @see MimeTypeGuesser
69+
* @see MimeTypes
7170
*/
7271
public function getMimeType()
7372
{
74-
return MimeTypeGuesser::getInstance()->guess($this->getPathname());
73+
return MimeTypes::getDefault()->guessMimeType($this->getPathname());
7574
}
7675

7776
/**

src/Symfony/Component/HttpFoundation/File/MimeType/ExtensionGuesser.php

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace Symfony\Component\HttpFoundation\File\MimeType;
1313

14+
use Symfony\Component\Mime\MimeTypes;
15+
16+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.', ExtensionGuesser::class, MimeTypes::class), E_USER_DEPRECATED);
17+
1418
/**
1519
* A singleton mime type to file extension guesser.
1620
*
@@ -22,6 +26,8 @@
2226
* $guesser->register(new MyCustomExtensionGuesser());
2327
*
2428
* The last registered guesser is preferred over previously registered ones.
29+
*
30+
* @deprecated since Symfony 4.3, use {@link MimeTypes} instead
2531
*/
2632
class ExtensionGuesser implements ExtensionGuesserInterface
2733
{

src/Symfony/Component/HttpFoundation/File/MimeType/ExtensionGuesserInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
namespace Symfony\Component\HttpFoundation\File\MimeType;
1313

14+
use Symfony\Component\Mime\MimeTypes;
15+
1416
/**
1517
* Guesses the file extension corresponding to a given mime type.
18+
*
19+
* @deprecated since Symfony 4.3, use {@link MimeTypes} instead
1620
*/
1721
interface ExtensionGuesserInterface
1822
{

src/Symfony/Component/HttpFoundation/File/MimeType/FileBinaryMimeTypeGuesser.php

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
1515
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
16+
use Symfony\Component\Mime\FileBinaryMimeTypeGuesser as NewFileBinaryMimeTypeGuesser;
17+
18+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.', FileBinaryMimeTypeGuesser::class, NewFileBinaryMimeTypeGuesser::class), E_USER_DEPRECATED);
1619

1720
/**
1821
* Guesses the mime type with the binary "file" (only available on *nix).

src/Symfony/Component/HttpFoundation/File/MimeType/FileinfoMimeTypeGuesser.php

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
1515
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
16+
use Symfony\Component\Mime\FileinfoMimeTypeGuesser as NewFileinfoMimeTypeGuesser;
17+
18+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.', FileinfoMimeTypeGuesser::class, NewFileinfoMimeTypeGuesser::class), E_USER_DEPRECATED);
1619

1720
/**
1821
* Guesses the mime type using the PECL extension FileInfo.

src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeExtensionGuesser.php

+6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@
1111

1212
namespace Symfony\Component\HttpFoundation\File\MimeType;
1313

14+
use Symfony\Component\Mime\MimeTypes;
15+
16+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.', MimeTypeExtensionGuesser::class, MimeTypes::class), E_USER_DEPRECATED);
17+
1418
/**
1519
* Provides a best-guess mapping of mime type to file extension.
20+
*
21+
* @deprecated since Symfony 4.3, use {@link MimeTypes} instead
1622
*/
1723
class MimeTypeExtensionGuesser implements ExtensionGuesserInterface
1824
{

src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
1515
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
16+
use Symfony\Component\Mime\MimeTypes;
17+
18+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.', MimeTypeGuesser::class, MimeTypes::class), E_USER_DEPRECATED);
1619

1720
/**
1821
* A singleton mime type guesser.

src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesserInterface.php

+3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313

1414
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
1515
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
16+
use Symfony\Component\Mime\MimeTypes;
1617

1718
/**
1819
* Guesses the mime type of a file.
1920
*
2021
* @author Bernhard Schussek <bschussek@gmail.com>
22+
*
23+
* @deprecated since Symfony 4.3, use {@link MimeTypes} instead
2124
*/
2225
interface MimeTypeGuesserInterface
2326
{

src/Symfony/Component/HttpFoundation/File/UploadedFile.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Symfony\Component\HttpFoundation\File\Exception\NoFileException;
2121
use Symfony\Component\HttpFoundation\File\Exception\NoTmpDirFileException;
2222
use Symfony\Component\HttpFoundation\File\Exception\PartialFileException;
23-
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
23+
use Symfony\Component\Mime\MimeTypes;
2424

2525
/**
2626
* A file uploaded through a form.
@@ -140,7 +140,7 @@ public function getClientMimeType()
140140
*/
141141
public function guessClientExtension()
142142
{
143-
return ExtensionGuesser::getInstance()->guess($this->getClientMimeType());
143+
return MimeTypes::getDefault()->getExtensions($this->getClientMimeType())[0] ?? null;
144144
}
145145

146146
/**

0 commit comments

Comments
 (0)