Skip to content

Commit

Permalink
Only add the YamlDriver to the chain when symfony/yaml is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Aug 1, 2023
1 parent d436996 commit 7befd1d
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Builder/DefaultDriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Metadata\Driver\DriverChain;
use Metadata\Driver\DriverInterface;
use Metadata\Driver\FileLocator;
use Symfony\Component\Yaml\Yaml;

final class DefaultDriverFactory implements DriverFactoryInterface
{
Expand Down Expand Up @@ -56,19 +57,27 @@ public function enableEnumSupport(bool $enableEnumSupport = true): void

public function createDriver(array $metadataDirs, Reader $annotationReader): DriverInterface
{
$driver = new DriverChain([
new AnnotationOrAttributeDriver($this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator, $annotationReader),
]);
/*
* Build the sorted list of metadata drivers based on the environment. The final order should be:
*
* - YAML Driver
* - XML Driver
* - Annotations/Attributes Driver
* - Null (Fallback) Driver
*/
$metadataDrivers = [new AnnotationOrAttributeDriver($this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator, $annotationReader)];

if (!empty($metadataDirs)) {
$fileLocator = new FileLocator($metadataDirs);
$driver = new DriverChain([
new YamlDriver($fileLocator, $this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator),
new XmlDriver($fileLocator, $this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator),
$driver,
]);

array_unshift($metadataDrivers, new XmlDriver($fileLocator, $this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator));

if (class_exists(Yaml::class)) {
array_unshift($metadataDrivers, new YamlDriver($fileLocator, $this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator));
}
}

$driver = new DriverChain($metadataDrivers);
$driver->addDriver(new NullDriver($this->propertyNamingStrategy));

if ($this->enableEnumSupport) {
Expand Down

0 comments on commit 7befd1d

Please sign in to comment.