Skip to content

Commit

Permalink
moves driver creation to dedicated class (closes #247)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittjoh committed Mar 18, 2014
1 parent 740c63a commit d8b4c0f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
29 changes: 29 additions & 0 deletions src/JMS/Serializer/Builder/CallbackDriverFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace JMS\Serializer\Builder;

use Doctrine\Common\Annotations\Reader;
use Metadata\Driver\DriverInterface;

class CallbackDriverFactory implements DriverFactoryInterface
{
private $callback;

/**
* @param callable $callable
*/
public function __construct($callable)
{
$this->callback = $callable;
}

public function createDriver(array $metadataDirs, Reader $reader)
{
$driver = call_user_func($this->callback, $metadataDirs, $reader);
if ( ! $driver instanceof DriverInterface) {
throw new \LogicException('The callback must return an instance of DriverInterface.');
}

return $driver;
}
}
28 changes: 28 additions & 0 deletions src/JMS/Serializer/Builder/DefaultDriverFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace JMS\Serializer\Builder;

use Doctrine\Common\Annotations\Reader;
use JMS\Serializer\Metadata\Driver\AnnotationDriver;
use JMS\Serializer\Metadata\Driver\XmlDriver;
use JMS\Serializer\Metadata\Driver\YamlDriver;
use Metadata\Driver\DriverChain;
use Metadata\Driver\FileLocator;

class DefaultDriverFactory implements DriverFactoryInterface
{
public function createDriver(array $metadataDirs, Reader $annotationReader)
{
if ( ! empty($this->metadataDirs)) {
$fileLocator = new FileLocator($this->metadataDirs);

return new DriverChain(array(
new YamlDriver($fileLocator),
new XmlDriver($fileLocator),
new AnnotationDriver($annotationReader),
));
}

return new AnnotationDriver($annotationReader);
}
}
17 changes: 17 additions & 0 deletions src/JMS/Serializer/Builder/DriverFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace JMS\Serializer\Builder;

use Doctrine\Common\Annotations\Reader;
use Metadata\Driver\DriverInterface;

interface DriverFactoryInterface
{
/**
* @param array $metadataDirs
* @param Reader $annotationReader
*
* @return DriverInterface
*/
public function createDriver(array $metadataDirs, Reader $annotationReader);
}
22 changes: 11 additions & 11 deletions src/JMS/Serializer/SerializerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

namespace JMS\Serializer;

use JMS\Serializer\Builder\DefaultDriverFactory;
use JMS\Serializer\Builder\DriverFactoryInterface;
use JMS\Serializer\Handler\PhpCollectionHandler;
use JMS\Serializer\Handler\PropelCollectionHandler;
use JMS\Serializer\Exception\RuntimeException;
use Metadata\Driver\DriverInterface;
use Metadata\MetadataFactory;
use JMS\Serializer\Metadata\Driver\AnnotationDriver;
use JMS\Serializer\Handler\HandlerRegistry;
Expand Down Expand Up @@ -68,6 +71,7 @@ class SerializerBuilder
private $cacheDir;
private $annotationReader;
private $includeInterfaceMetadata = false;
private $driverFactory;

public static function create()
{
Expand All @@ -78,6 +82,7 @@ public function __construct()
{
$this->handlerRegistry = new HandlerRegistry();
$this->eventDispatcher = new EventDispatcher();
$this->driverFactory = new DefaultDriverFactory();
$this->serializationVisitors = new Map();
$this->deserializationVisitors = new Map();
}
Expand Down Expand Up @@ -321,6 +326,11 @@ public function replaceMetadataDir($dir, $namespacePrefix = '')
return $this;
}

public function setMetadataDriverFactory(DriverFactoryInterface $driverFactory)
{
$this->driverFactory = $driverFactory;
}

public function build()
{
$annotationReader = $this->annotationReader;
Expand All @@ -333,17 +343,7 @@ public function build()
}
}

if ( ! empty($this->metadataDirs)) {
$fileLocator = new FileLocator($this->metadataDirs);
$metadataDriver = new DriverChain(array(
new YamlDriver($fileLocator),
new XmlDriver($fileLocator),
new AnnotationDriver($annotationReader),
));
} else {
$metadataDriver = new AnnotationDriver($annotationReader);
}

$metadataDriver = $this->driverFactory->createDriver($this->metadataDirs, $annotationReader);
$metadataFactory = new MetadataFactory($metadataDriver, null, $this->debug);

$metadataFactory->setIncludeInterfaces($this->includeInterfaceMetadata);
Expand Down

0 comments on commit d8b4c0f

Please sign in to comment.