1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Cli\Command\AbstractSimpleImportCommand |
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\Naming\IdenticalPropertyNamingStrategy; |
18
|
|
|
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; |
19
|
|
|
use JMS\Serializer\Serializer; |
20
|
|
|
use JMS\Serializer\SerializerBuilder; |
21
|
|
|
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory; |
22
|
|
|
use Symfony\Component\Console\Command\Command; |
23
|
|
|
use Symfony\Component\Console\Input\InputOption; |
24
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
use TechDivision\Import\Configuration\ConfigurationInterface; |
27
|
|
|
use TechDivision\Import\Configuration\Jms\Configuration; |
28
|
|
|
use TechDivision\Import\Cli\Utils\DependencyInjectionKeys; |
29
|
|
|
use TechDivision\Import\Utils\InputOptionKeysInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Abstract command implementation for simple import commands (not using Importer class). |
33
|
|
|
* |
34
|
|
|
* @author Tim Wagner <[email protected]> |
35
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
36
|
|
|
* @license https://opensource.org/licenses/MIT |
37
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
38
|
|
|
* @link http://www.techdivision.com |
39
|
|
|
*/ |
40
|
|
|
abstract class AbstractSimpleImportCommand extends Command |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Configures the current command. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
48
|
|
|
*/ |
49
|
|
|
protected function configure() |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
// configure the command |
53
|
|
|
$this->addOption(InputOptionKeysInterface::PID_FILENAME, null, InputOption::VALUE_REQUIRED, 'The explicit PID filename to use', sprintf('%s/%s', sys_get_temp_dir(), Configuration::PID_FILENAME)) |
54
|
|
|
->addOption(InputOptionKeysInterface::INSTALLATION_DIR, null, InputOption::VALUE_REQUIRED, 'The Magento installation directory to which the files has to be imported', getcwd()) |
55
|
|
|
->addOption(InputOptionKeysInterface::CONFIGURATION_DIR, null, InputOption::VALUE_OPTIONAL, 'The Magento configuration directory') |
56
|
|
|
->addOption(InputOptionKeysInterface::SYSTEM_NAME, null, InputOption::VALUE_REQUIRED, 'Specify the system name to use', gethostname()) |
57
|
|
|
->addOption(InputOptionKeysInterface::SOURCE_DIR, null, InputOption::VALUE_REQUIRED, 'The directory that has to be watched for new files') |
58
|
|
|
->addOption(InputOptionKeysInterface::MAGENTO_VERSION, null, InputOption::VALUE_REQUIRED, 'The Magento version to be used, e. g. "2.1.2"') |
59
|
|
|
->addOption(InputOptionKeysInterface::MAGENTO_EDITION, null, InputOption::VALUE_REQUIRED, 'The Magento edition to be used, either one of "CE" or "EE"', 'CE') |
60
|
|
|
->addOption(InputOptionKeysInterface::CONFIGURATION, null, InputOption::VALUE_REQUIRED, 'Specify the pathname to the configuration file to use') |
61
|
|
|
->addOption(InputOptionKeysInterface::CUSTOM_CONFIGURATION_DIR, null, InputOption::VALUE_REQUIRED, 'The path to the custom configuration directory') |
62
|
|
|
->addOption(InputOptionKeysInterface::LOG_LEVEL, null, InputOption::VALUE_REQUIRED, 'The log level to use') |
63
|
|
|
->addOption(InputOptionKeysInterface::LOG_FILE, null, InputOption::VALUE_REQUIRED, 'The log file to use'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return's the container instance. |
68
|
|
|
* |
69
|
|
|
* @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance |
70
|
|
|
*/ |
71
|
|
|
protected function getContainer() |
72
|
|
|
{ |
73
|
|
|
return $this->getApplication()->getContainer(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Executes the current command. |
78
|
|
|
* |
79
|
|
|
* This method is not abstract because you can use this class |
80
|
|
|
* as a concrete class. In this case, instead of defining the |
81
|
|
|
* execute() method, you set the code to execute by passing |
82
|
|
|
* a Closure to the setCode() method. |
83
|
|
|
* |
84
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
85
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
86
|
|
|
* |
87
|
|
|
* @return null|int null or 0 if everything went fine, or an error code |
88
|
|
|
* @throws \LogicException When this abstract method is not implemented |
89
|
|
|
* @see \Symfony\Component\Console\Command\Command::execute() |
90
|
|
|
*/ |
91
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
// try to load the configuration file |
95
|
|
|
$configuration = $this->getContainer()->get(DependencyInjectionKeys::CONFIGURATION_SIMPLE); |
96
|
|
|
|
97
|
|
|
// finally execute the simple command |
98
|
|
|
return $this->executeSimpleCommand($configuration, $input, $output); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* create json serializer for configs |
103
|
|
|
* |
104
|
|
|
* @return Serializer |
105
|
|
|
*/ |
106
|
|
|
protected function createSerializer(): Serializer |
107
|
|
|
{ |
108
|
|
|
$format = 'json'; |
109
|
|
|
$builder = SerializerBuilder::create(); |
110
|
|
|
$builder->addDefaultSerializationVisitors(); |
111
|
|
|
$namingStrategy = new SerializedNameAnnotationStrategy(new IdenticalPropertyNamingStrategy()); |
112
|
|
|
|
113
|
|
|
// register the visitor in the builder instance |
114
|
|
|
$visitor = new JsonSerializationVisitorFactory($namingStrategy); |
|
|
|
|
115
|
|
|
$visitor->setOptions(JSON_PRETTY_PRINT); |
116
|
|
|
$builder->setSerializationVisitor($format, $visitor); |
117
|
|
|
return $builder->build(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Finally executes the simple command. |
122
|
|
|
* |
123
|
|
|
* @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance |
124
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
125
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
abstract protected function executeSimpleCommand( |
130
|
|
|
ConfigurationInterface $configuration, |
131
|
|
|
InputInterface $input, |
132
|
|
|
OutputInterface $output |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|