1<?php 2 3declare(strict_types=1); 4 5namespace JMS\Serializer\Builder; 6 7use Doctrine\Common\Annotations\Reader; 8use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface; 9use JMS\Serializer\Metadata\Driver\AnnotationDriver; 10use JMS\Serializer\Metadata\Driver\XmlDriver; 11use JMS\Serializer\Metadata\Driver\YamlDriver; 12use JMS\Serializer\Naming\PropertyNamingStrategyInterface; 13use JMS\Serializer\Type\Parser; 14use JMS\Serializer\Type\ParserInterface; 15use Metadata\Driver\DriverChain; 16use Metadata\Driver\DriverInterface; 17use Metadata\Driver\FileLocator; 18 19final class DefaultDriverFactory implements DriverFactoryInterface 20{ 21 /** 22 * @var ParserInterface 23 */ 24 private $typeParser; 25 26 /** 27 * @var PropertyNamingStrategyInterface 28 */ 29 private $propertyNamingStrategy; 30 31 /** 32 * @var CompilableExpressionEvaluatorInterface 33 */ 34 private $expressionEvaluator; 35 36 public function __construct(PropertyNamingStrategyInterface $propertyNamingStrategy, ?ParserInterface $typeParser = null, ?CompilableExpressionEvaluatorInterface $expressionEvaluator = null) 37 { 38 $this->typeParser = $typeParser ?: new Parser(); 39 $this->propertyNamingStrategy = $propertyNamingStrategy; 40 $this->expressionEvaluator = $expressionEvaluator; 41 } 42 43 public function createDriver(array $metadataDirs, Reader $annotationReader): DriverInterface 44 { 45 if (!empty($metadataDirs)) { 46 $fileLocator = new FileLocator($metadataDirs); 47 48 return new DriverChain([ 49 new YamlDriver($fileLocator, $this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator), 50 new XmlDriver($fileLocator, $this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator), 51 new AnnotationDriver($annotationReader, $this->propertyNamingStrategy, $this->typeParser, $this->expressionEvaluator), 52 ]); 53 } 54 55 return new AnnotationDriver($annotationReader, $this->propertyNamingStrategy, $this->typeParser); 56 } 57} 58