1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Configuration\Jms\ConfigurationParserFactory |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import-configuration-jms |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Configuration\Jms; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The configuration parser factory implementation. |
27
|
|
|
* |
28
|
|
|
* @author Tim Wagner <[email protected]> |
29
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
31
|
|
|
* @link https://github.com/techdivision/import-configuration-jms |
32
|
|
|
* @link http://www.techdivision.com |
33
|
|
|
*/ |
34
|
|
|
class ConfigurationParserFactory implements ConfigurationParserFactoryInterface |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The container instance. |
39
|
|
|
* |
40
|
|
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface |
41
|
|
|
*/ |
42
|
|
|
protected $container; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The format => parser mappings. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $parserMappings; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initializes the configuration parser factory. |
53
|
|
|
* |
54
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container The container instance |
55
|
|
|
* @param array $parserMappings The parser mappings |
56
|
|
|
*/ |
57
|
|
|
public function __construct(ContainerInterface $container, array $parserMappings) |
58
|
|
|
{ |
59
|
|
|
$this->container = $container; |
60
|
|
|
$this->parserMappings = $parserMappings; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Factory implementation to create a new configuration parser instance for the passed file format. |
65
|
|
|
* |
66
|
|
|
* @param string $format The format of the configuration file to create a configuration parser for (either one of json, yaml or xml) |
67
|
|
|
* |
68
|
|
|
* @return \TechDivision\Import\Configuration\Jms\ConfigurationParserInterface The configuration parser instance |
69
|
|
|
* @throws \Exception Is thrown if NO configuration parser mapping for the passed format has been specified |
70
|
|
|
*/ |
71
|
|
|
public function factory($format = 'json') |
72
|
|
|
{ |
73
|
|
|
|
74
|
|
|
// query whether or not a configuration parser mapping is available |
75
|
|
|
if (isset($this->parserMappings[$format])) { |
76
|
|
|
return $this->container->get( $this->parserMappings[$format]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// throw an exception, if NO mapping for the passed format has been available |
80
|
|
|
throw new \Exception(sprintf('Can\t find a configuration parser mapping for format "%s"', $format)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|