-
Notifications
You must be signed in to change notification settings - Fork 6
/
ConfigFormatConverter.php
99 lines (89 loc) · 2.99 KB
/
ConfigFormatConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Tuck\ConverterBundle;
use SplFileInfo;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Tuck\ConverterBundle\Dumper\DumperFactory;
use Tuck\ConverterBundle\Exception\UnknownFileException;
use Tuck\ConverterBundle\File\TempFileFactory;
use Tuck\ConverterBundle\Loader\LoaderFactory;
/**
* Converts a services config file to another format.
*
* Future changes:
* This class exposes the file part rather heavily because that's how it is
* in practice but also because that's how the internal loader and dumpers
* work.
*
* This would probably be improved by refactoring it to return an actual
* ConfigFile object, then we could break the coupling with file extensions
* in the UI.
*
* @author Ross Tuck <[email protected]>
*/
class ConfigFormatConverter
{
/**
* @var LoaderFactory
*/
protected $loaderFactory;
/**
* @var DumperFactory
*/
protected $dumperFactory;
/**
* @var TempFileFactory
*/
protected $tempFileFactory;
public function __construct(
LoaderFactory $loaderFactory,
DumperFactory $dumperFactory,
TempFileFactory $tempFileFactory
) {
$this->loaderFactory = $loaderFactory;
$this->dumperFactory = $dumperFactory;
$this->tempFileFactory = $tempFileFactory;
}
/**
* Convert a service config file from one format to another (xml, yml, etc)
*
* @param SplFileInfo $file The file to convert
* @param string $newFormat Format to convert to, given as a file extension
* @return string Converted config, returned as a raw string
* @throws Exception\UnknownFileException
*/
public function convertFile(SplFileInfo $file, $newFormat)
{
if (!$file->isFile()) {
throw UnknownFileException::create($file->getRealPath());
}
$container = new ContainerBuilder(new ParameterBag());
$loader = $this->loaderFactory->createFileLoader($file->getExtension(), $container, $file->getPath());
$loader->load($file->getFilename());
return $this->dumperFactory->createDumper($newFormat, $container)->dump();
}
/**
* Convert a config represented as a string to some other format
*
* @param string $content
* @param string $oldFormat
* @param string $newFormat
*
* @throws \Exception when file conversion fails
*
* @return string
*/
public function convertString($content, $oldFormat, $newFormat)
{
$tempFile = $this->tempFileFactory->createFile($content, $oldFormat);
try {
$output = $this->convertFile($tempFile, $newFormat);
} catch (\Exception $e) {
// Cleanup the temp file, even with a failure
unlink($tempFile->getRealPath());
throw $e;
}
unlink($tempFile->getRealPath());
return $output;
}
}