-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
96 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters