-
-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moves driver creation to dedicated class (closes #247)
- Loading branch information
1 parent
740c63a
commit d8b4c0f
Showing
4 changed files
with
85 additions
and
11 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
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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} |
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