1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Cli\Command\AbstractSimpleImportCommand |
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 2016 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-cli-simple |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cli\Command; |
22
|
|
|
|
23
|
|
|
use JMS\Serializer\SerializerBuilder; |
24
|
|
|
use TechDivision\Import\Cli\Simple; |
25
|
|
|
use TechDivision\Import\Cli\Configuration; |
26
|
|
|
use TechDivision\Import\ConfigurationInterface; |
27
|
|
|
use Symfony\Component\Console\Command\Command; |
28
|
|
|
use Symfony\Component\Console\Input\InputOption; |
29
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
30
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Abstract command implementation for simple import commands (not using Importer class). |
34
|
|
|
* |
35
|
|
|
* @author Tim Wagner <[email protected]> |
36
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
37
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
38
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
39
|
|
|
* @link http://www.techdivision.com |
40
|
|
|
*/ |
41
|
|
|
abstract class AbstractSimpleImportCommand extends Command |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Factory implementation to create a new initialized configuration instance. |
46
|
|
|
* |
47
|
|
|
* If command line options are specified, they will always override the |
48
|
|
|
* values found in the configuration file. |
49
|
|
|
* |
50
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input The Symfony console input instance |
51
|
|
|
* |
52
|
|
|
* @return \TechDivision\Import\Cli\Configuration The configuration instance |
53
|
|
|
* @throws \Exception Is thrown, if the specified configuration file doesn't exist |
54
|
|
|
*/ |
55
|
|
|
protected function configurationFactory(InputInterface $input) |
56
|
|
|
{ |
57
|
|
|
|
58
|
|
|
// load the configuration filename we want to use |
59
|
|
|
$filename = $input->getOption(InputOptionKeys::CONFIGURATION); |
60
|
|
|
|
61
|
|
|
// load the JSON data |
62
|
|
|
if (!$jsonData = file_get_contents($filename)) { |
63
|
|
|
throw new \Exception(sprintf('Can\'t load configuration file %s', $filename)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// initialize the JMS serializer and load the configuration |
67
|
|
|
$serializer = SerializerBuilder::create()->build(); |
68
|
|
|
/** @var \TechDivision\Import\Cli\Configuration $instance */ |
69
|
|
|
$instance = $serializer->deserialize($jsonData, 'TechDivision\Import\Cli\Configuration', 'json'); |
70
|
|
|
|
71
|
|
|
// query whether or not a PID filename has been specified as command line |
72
|
|
|
// option, if yes override the value from the configuration file |
73
|
|
|
if ($pidFilename = $input->getOption(InputOptionKeys::PID_FILENAME)) { |
74
|
|
|
$instance->setPidFilename($pidFilename); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// return the initialized configuration instance |
78
|
|
|
return $instance; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Executes the current command. |
83
|
|
|
* |
84
|
|
|
* This method is not abstract because you can use this class |
85
|
|
|
* as a concrete class. In this case, instead of defining the |
86
|
|
|
* execute() method, you set the code to execute by passing |
87
|
|
|
* a Closure to the setCode() method. |
88
|
|
|
* |
89
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
90
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
91
|
|
|
* |
92
|
|
|
* @return null|int null or 0 if everything went fine, or an error code |
93
|
|
|
* @throws \LogicException When this abstract method is not implemented |
94
|
|
|
* @see \Symfony\Component\Console\Command\Command::execute() |
95
|
|
|
*/ |
96
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
97
|
|
|
{ |
98
|
|
|
|
99
|
|
|
// initialize the flag, whether the JMS annotations has been loaded or not |
100
|
|
|
$loaded = false; |
101
|
|
|
|
102
|
|
|
// the possible paths to the JMS annotations |
103
|
|
|
$annotationDirectories = array( |
104
|
|
|
dirname(__DIR__) . '/../../../jms/serializer/src', |
105
|
|
|
dirname(__DIR__) . '/../vendor/jms/serializer/src' |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
// register the JMS Serializer annotations |
109
|
|
View Code Duplication |
foreach ($annotationDirectories as $annotationDirectory) { |
|
|
|
|
110
|
|
|
if (file_exists($annotationDirectory)) { |
111
|
|
|
\Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace( |
112
|
|
|
'JMS\Serializer\Annotation', |
113
|
|
|
$annotationDirectory |
114
|
|
|
); |
115
|
|
|
$loaded = true; |
116
|
|
|
break; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// stop processing, if the JMS annotations can't be loaded |
121
|
|
|
if (!$loaded) { |
122
|
|
|
throw new \Exception( |
123
|
|
|
sprintf( |
124
|
|
|
'The JMS annotations can not be found in one of paths %s', |
125
|
|
|
implode(', ', $annotationDirectories) |
126
|
|
|
) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// load the specified configuration |
131
|
|
|
$configuration = $this->configurationFactory($input); |
132
|
|
|
|
133
|
|
|
// finally execute the simple command |
134
|
|
|
$this->executeSimpleCommand($configuration, $input, $output); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Finally executes the simple command. |
139
|
|
|
* |
140
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
141
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
142
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
abstract protected function executeSimpleCommand( |
147
|
|
|
ConfigurationInterface $configuration, |
148
|
|
|
InputInterface $input, |
149
|
|
|
OutputInterface $output |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.