1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Cli\Command\ImportCreateConfigurationFileCommand |
5
|
|
|
* |
6
|
|
|
* PHP version 7 |
7
|
|
|
* |
8
|
|
|
* @author Tim Wagner <[email protected]> |
9
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/MIT |
11
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
12
|
|
|
* @link http://www.techdivision.com |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TechDivision\Import\Cli\Command; |
16
|
|
|
|
17
|
|
|
use JMS\Serializer\SerializerBuilder; |
18
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
19
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
20
|
|
|
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; |
21
|
|
|
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; |
22
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
use Symfony\Component\Console\Input\InputOption; |
25
|
|
|
use TechDivision\Import\Utils\CommandNames; |
26
|
|
|
use TechDivision\Import\Configuration\ConfigurationInterface; |
27
|
|
|
use TechDivision\Import\Utils\InputOptionKeysInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The command implementation that creates a configuration file from one of the templates. |
31
|
|
|
* |
32
|
|
|
* @author Tim Wagner <[email protected]> |
33
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
34
|
|
|
* @license https://opensource.org/licenses/MIT |
35
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
36
|
|
|
* @link http://www.techdivision.com |
37
|
|
|
*/ |
38
|
|
|
class ImportCreateConfigurationFileCommand extends AbstractSimpleImportCommand |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Configures the current command. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
46
|
|
|
*/ |
47
|
|
|
protected function configure() |
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
// initialize the command with the required/optional options |
51
|
|
|
$this->setName(CommandNames::IMPORT_CREATE_CONFIGURATION_FILE) |
52
|
|
|
->setDescription('Create\'s a configuration file from the given entity\'s template') |
53
|
|
|
->addOption(InputOptionKeysInterface::DEST, null, InputOption::VALUE_REQUIRED, 'The relative/absolut pathname of the destination file'); |
54
|
|
|
|
55
|
|
|
// invoke the parent method |
56
|
|
|
parent::configure(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Finally executes the simple command. |
62
|
|
|
* |
63
|
|
|
* @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance |
64
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
65
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
protected function executeSimpleCommand( |
70
|
|
|
ConfigurationInterface $configuration, |
71
|
|
|
InputInterface $input, |
72
|
|
|
OutputInterface $output |
73
|
|
|
) { |
74
|
|
|
|
75
|
|
|
// initialie the directory where we want to export the configuration to |
76
|
|
|
$exportDir = $input->hasParameterOption(InputOptionKeysInterface::CUSTOM_CONFIGURATION_DIR) ? $input->getOption(InputOptionKeysInterface::CUSTOM_CONFIGURATION_DIR) : $configuration->getInstallationDir(); |
77
|
|
|
|
78
|
|
|
// initialize the configuration filename |
79
|
|
|
$configurationFilename = $input->getOption(InputOptionKeysInterface::DEST); |
80
|
|
|
if ($configurationFilename === null) { |
81
|
|
|
$configurationFilename = sprintf('%s/app/etc/techdivision-import.json', $exportDir); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// extract the format from the configuration file suffix |
85
|
|
|
$format = pathinfo($configurationFilename, PATHINFO_EXTENSION); |
86
|
|
|
|
87
|
|
|
// initialize the serializer |
88
|
|
|
$builder = SerializerBuilder::create(); |
89
|
|
|
$builder->addDefaultSerializationVisitors(); |
90
|
|
|
|
91
|
|
|
// initialize the naming strategy |
92
|
|
|
$namingStrategy = new SerializedNameAnnotationStrategy(new IdenticalPropertyNamingStrategy()); |
93
|
|
|
|
94
|
|
|
// create the configuration based on the given configuration file suffix |
95
|
|
|
switch ($format) { |
96
|
|
|
// initialize the JSON visitor |
97
|
|
|
case 'json': |
98
|
|
|
// initialize the visitor because we want to set JSON options |
99
|
|
|
$visitor = new JsonSerializationVisitor($namingStrategy); |
|
|
|
|
100
|
|
|
$visitor->setOptions(JSON_PRETTY_PRINT); |
|
|
|
|
101
|
|
|
break; |
102
|
|
|
|
103
|
|
|
// initialize the XML visitor |
104
|
|
|
case 'xml': |
105
|
|
|
$visitor = new XmlSerializationVisitor($namingStrategy); |
|
|
|
|
106
|
|
|
break; |
107
|
|
|
|
108
|
|
|
// throw an execption in all other cases |
109
|
|
|
default: |
110
|
|
|
throw new \Exception(sprintf('Found invalid configuration format "%s"', $format)); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// register the visitor in the builder instance |
114
|
|
|
$builder->setSerializationVisitor($format, $visitor); |
|
|
|
|
115
|
|
|
|
116
|
|
|
// finally create the serializer instance |
117
|
|
|
$serializer = $builder->build(); |
118
|
|
|
|
119
|
|
|
// try to write the configuration file to the actual working directory |
120
|
|
|
if (file_put_contents($configurationFilename, $serializer->serialize($configuration, $format))) { |
121
|
|
|
$output->writeln(sprintf('<info>Successfully written configuration file %s</info>', $configurationFilename)); |
122
|
|
|
} else { |
123
|
|
|
$output->writeln(sprintf('<error>Can\'t write configuration file %s</error>', $configurationFilename)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|