Skip to content

Commit

Permalink
allow yaml extension (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
garak committed Jun 4, 2021
1 parent 61ba06f commit c5250c8
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 68 deletions.
4 changes: 4 additions & 0 deletions config/mapping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<argument type="service" id="vich_uploader.metadata.file_locator" />
</service>

<service id="vich_uploader.metadata_driver.yml" class="Vich\UploaderBundle\Metadata\Driver\YmlDriver" public="false">
<argument type="service" id="vich_uploader.metadata.file_locator" />
</service>

<service id="vich_uploader.metadata_driver.yaml" class="Vich\UploaderBundle\Metadata\Driver\YamlDriver" public="false">
<argument type="service" id="vich_uploader.metadata.file_locator" />
</service>
Expand Down
4 changes: 2 additions & 2 deletions docs/events/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Create a listener class:
```php
<?php

namespace AppBundle\EventListener;
namespace App\EventListener;

use Vich\UploaderBundle\Event\Event;

Expand All @@ -45,7 +45,7 @@ class FooListener
Configure it in your configuration:

```yaml
# app/config/services.yml
# config/services.yaml or app/config/services.yml
services:
AppBundle\EventListener\FooListener:
tags:
Expand Down
8 changes: 4 additions & 4 deletions docs/mapping/yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ You can choose to describe your entities in YAML. This bundle supports this
format and comes with the following syntax to declare your uploadable fields:

```yaml
# config/vich_uploader/Entity.Product.yml
# config/vich_uploader/Entity.Product.yaml
Acme\DemoBundle\Entity\Product:
imageFile:
mapping: product_image # required
Expand Down Expand Up @@ -34,13 +34,13 @@ vich_uploader:

#### Naming YAML Files

The `namespace_prefix` parameter, combined with the .yml file name in `config/vich_uploader` must
The `namespace_prefix` parameter, combined with the .yaml file name in `config/vich_uploader` must
combine to form the FQCN of your entity. For example an entity of `MyApp\MyBundle\Entity\Customer`
should be configured using either of the following:

`namespace_prefix: 'MyApp\MyBundle'` and then have a config file `Entity.Customer.yml`
`namespace_prefix: 'MyApp\MyBundle'` and then have a config file `Entity.Customer.yaml`

`namespace_prefix: 'MyApp\MyBundle\Entity'` and then have a config file `Customer.yml`
`namespace_prefix: 'MyApp\MyBundle\Entity'` and then have a config file `Customer.yaml`

**N.B:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function process(ContainerBuilder $container): void

if (\class_exists(Yaml::class)) {
$drivers[] = new Reference('vich_uploader.metadata_driver.yaml');
$drivers[] = new Reference('vich_uploader.metadata_driver.yml');
}

$container
Expand Down
61 changes: 61 additions & 0 deletions src/Metadata/Driver/AbstractYamlDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Vich\UploaderBundle\Metadata\Driver;

use Metadata\ClassMetadata as JMSClassMetadata;
use Metadata\Driver\AbstractFileDriver;
use Symfony\Component\Yaml\Yaml as YmlParser;
use Vich\UploaderBundle\Metadata\ClassMetadata;

/**
* @author Kévin Gomez <contact@kevingomez.fr>
* @author Konstantin Myakshin <koc-dp@yandex.ru>
*/
abstract class AbstractYamlDriver extends AbstractFileDriver
{
protected function loadMetadataFromFile(\ReflectionClass $class, string $file): ?JMSClassMetadata
{
$config = $this->loadMappingFile($file);
$className = $this->guessClassName($file, $config, $class);
$classMetadata = new ClassMetadata($className);
$classMetadata->fileResources[] = $file;
$classMetadata->fileResources[] = $class->getFileName();

foreach ($config[$className] as $field => $mappingData) {
$fieldMetadata = [
'mapping' => $mappingData['mapping'],
'propertyName' => $field,
'fileNameProperty' => $mappingData['filename_property'] ?? null,
'size' => $mappingData['size'] ?? null,
'mimeType' => $mappingData['mime_type'] ?? null,
'originalName' => $mappingData['original_name'] ?? null,
'dimensions' => $mappingData['dimensions'] ?? null,
];

$classMetadata->fields[$field] = $fieldMetadata;
}

return $classMetadata;
}

/**
* @return mixed
*/
protected function loadMappingFile(string $file)
{
return YmlParser::parse(\file_get_contents($file));
}

protected function guessClassName(string $file, array $config, \ReflectionClass $class = null): string
{
if (null === $class) {
return \current(\array_keys($config));
}

if (!isset($config[$class->name])) {
throw new \RuntimeException(\sprintf('Expected metadata for class %s to be defined in %s.', $class->name, $file));
}

return $class->name;
}
}
59 changes: 2 additions & 57 deletions src/Metadata/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,10 @@

namespace Vich\UploaderBundle\Metadata\Driver;

use Metadata\ClassMetadata as JMSClassMetadata;
use Metadata\Driver\AbstractFileDriver;
use Symfony\Component\Yaml\Yaml as YmlParser;
use Vich\UploaderBundle\Metadata\ClassMetadata;

/**
* @author Kévin Gomez <contact@kevingomez.fr>
* @author Konstantin Myakshin <koc-dp@yandex.ru>
*/
class YamlDriver extends AbstractFileDriver
final class YamlDriver extends AbstractYamlDriver
{
protected function loadMetadataFromFile(\ReflectionClass $class, string $file): ?JMSClassMetadata
{
$config = $this->loadMappingFile($file);
$className = $this->guessClassName($file, $config, $class);
$classMetadata = new ClassMetadata($className);
$classMetadata->fileResources[] = $file;
$classMetadata->fileResources[] = $class->getFileName();

foreach ($config[$className] as $field => $mappingData) {
$fieldMetadata = [
'mapping' => $mappingData['mapping'],
'propertyName' => $field,
'fileNameProperty' => $mappingData['filename_property'] ?? null,
'size' => $mappingData['size'] ?? null,
'mimeType' => $mappingData['mime_type'] ?? null,
'originalName' => $mappingData['original_name'] ?? null,
'dimensions' => $mappingData['dimensions'] ?? null,
];

$classMetadata->fields[$field] = $fieldMetadata;
}

return $classMetadata;
}

/**
* @return mixed
*/
protected function loadMappingFile(string $file)
{
return YmlParser::parse(\file_get_contents($file));
}

protected function getExtension(): string
{
return 'yml';
}

protected function guessClassName(string $file, array $config, \ReflectionClass $class = null): string
{
if (null === $class) {
return \current(\array_keys($config));
}

if (!isset($config[$class->name])) {
throw new \RuntimeException(\sprintf('Expected metadata for class %s to be defined in %s.', $class->name, $file));
}

return $class->name;
return 'yaml';
}
}
11 changes: 11 additions & 0 deletions src/Metadata/Driver/YmlDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Vich\UploaderBundle\Metadata\Driver;

final class YmlDriver extends AbstractYamlDriver
{
protected function getExtension(): string
{
return 'yml';
}
}
4 changes: 2 additions & 2 deletions tests/Functional/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ protected function getImagesDir(KernelBrowser $client): string
return $client->getKernel()->getProjectDir().'/app/Resources/images';
}

protected function getContainer(KernelBrowser $client): ContainerInterface
protected static function getKernelContainer(KernelBrowser $client): ContainerInterface
{
return $client->getKernel()->getContainer();
}

protected function loadFixtures(KernelBrowser $client): void
{
$container = $this->getContainer($client);
$container = self::getKernelContainer($client);
$registry = $container->get('doctrine');
if ($registry instanceof ManagerRegistry) {
$om = $registry->getManager();
Expand Down
12 changes: 9 additions & 3 deletions tests/Metadata/Driver/YamlDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Metadata\Driver\DriverInterface;
use Metadata\Driver\FileLocatorInterface;
use Vich\UploaderBundle\Metadata\Driver\AbstractYamlDriver;
use Vich\UploaderBundle\Metadata\Driver\YamlDriver;

/**
Expand All @@ -21,7 +22,7 @@ public function testInconsistentYamlFile(): void
$fileLocator
->expects(self::once())
->method('findFileForClass')
->with(self::equalTo($rClass), self::equalTo('yml'))
->with(self::equalTo($rClass), self::equalTo('yaml'))
->willReturn('something not null');

$driver = new TestableYamlDriver($fileLocator);
Expand All @@ -33,7 +34,7 @@ public function testInconsistentYamlFile(): void

protected function getExtension(): string
{
return 'yml';
return 'yaml';
}

protected function getDriver(\ReflectionClass $reflectionClass, ?string $file): DriverInterface
Expand All @@ -42,7 +43,7 @@ protected function getDriver(\ReflectionClass $reflectionClass, ?string $file):
}
}

final class TestableYamlDriver extends YamlDriver
final class TestableYamlDriver extends AbstractYamlDriver
{
/**
* @var array
Expand All @@ -53,4 +54,9 @@ protected function loadMappingFile(string $file): array
{
return $this->mappingContent;
}

protected function getExtension(): string
{
return 'yaml';
}
}

0 comments on commit c5250c8

Please sign in to comment.