|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Cli\Command\ImportClearPidCommand |
|
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 Psr\Log\LogLevel; |
|
24
|
|
|
use Monolog\Logger; |
|
25
|
|
|
use Monolog\Handler\ErrorLogHandler; |
|
26
|
|
|
use JMS\Serializer\SerializerBuilder; |
|
27
|
|
|
use TechDivision\Import\Cli\Simple; |
|
28
|
|
|
use TechDivision\Import\Cli\Configuration; |
|
29
|
|
|
use Symfony\Component\Console\Command\Command; |
|
30
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
31
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
32
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The remove PID command implementation. |
|
36
|
|
|
* |
|
37
|
|
|
* @author Tim Wagner <[email protected]> |
|
38
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
39
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
40
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
|
41
|
|
|
* @link http://www.techdivision.com |
|
42
|
|
|
*/ |
|
43
|
|
|
class ImportClearPidCommand extends Command |
|
44
|
|
|
{ |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Configures the current command. |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function configure() |
|
53
|
|
|
{ |
|
54
|
|
|
|
|
55
|
|
|
// initialize the command with the required/optional options |
|
56
|
|
|
$this->setName('import:clear:pid') |
|
57
|
|
|
->setDescription('Clears the PID file from a previous import process, if it has not been cleaned up') |
|
58
|
|
|
->addOption( |
|
59
|
|
|
InputOptionKeys::CONFIGURATION, |
|
60
|
|
|
null, |
|
61
|
|
|
InputOption::VALUE_REQUIRED, |
|
62
|
|
|
'Specify the pathname to the configuration file to use', |
|
63
|
|
|
sprintf('%s/techdivision-import.json', getcwd()) |
|
64
|
|
|
) |
|
65
|
|
|
->addOption( |
|
66
|
|
|
InputOptionKeys::LOG_LEVEL, |
|
67
|
|
|
null, |
|
68
|
|
|
InputOption::VALUE_REQUIRED, |
|
69
|
|
|
'The log level to use' |
|
70
|
|
|
) |
|
71
|
|
|
->addOption( |
|
72
|
|
|
InputOptionKeys::PID_FILENAME, |
|
73
|
|
|
null, |
|
74
|
|
|
InputOption::VALUE_REQUIRED, |
|
75
|
|
|
'The explicit PID filename to use', |
|
76
|
|
|
sprintf('%s/%s', sys_get_temp_dir(), Configuration::PID_FILENAME) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Factory implementation to create a new initialized configuration instance. |
|
82
|
|
|
* |
|
83
|
|
|
* If command line options are specified, they will always override the |
|
84
|
|
|
* values found in the configuration file. |
|
85
|
|
|
* |
|
86
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input The Symfony console input instance |
|
87
|
|
|
* |
|
88
|
|
|
* @return \TechDivision\Import\Cli\Configuration The configuration instance |
|
89
|
|
|
* @throws \Exception Is thrown, if the specified configuration file doesn't exist |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function configurationFactory(InputInterface $input) |
|
92
|
|
|
{ |
|
93
|
|
|
|
|
94
|
|
|
// load the configuration filename we want to use |
|
95
|
|
|
$filename = $input->getOption(InputOptionKeys::CONFIGURATION); |
|
96
|
|
|
|
|
97
|
|
|
// load the JSON data |
|
98
|
|
|
if (!$jsonData = file_get_contents($filename)) { |
|
99
|
|
|
throw new \Exception(sprintf('Can\'t load configuration file %s', $filename)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// initialize the JMS serializer and load the configuration |
|
103
|
|
|
$serializer = SerializerBuilder::create()->build(); |
|
104
|
|
|
/** @var \TechDivision\Import\Cli\Configuration $instance */ |
|
105
|
|
|
$instance = $serializer->deserialize($jsonData, 'TechDivision\Import\Cli\Configuration', 'json'); |
|
106
|
|
|
|
|
107
|
|
|
// query whether or not the log level has been specified as command line |
|
108
|
|
|
// option, if yes override the value from the configuration file |
|
109
|
|
|
if ($logLevel = $input->getOption(InputOptionKeys::LOG_LEVEL)) { |
|
110
|
|
|
$instance->setLogLevel($logLevel); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// query whether or not a PID filename has been specified as command line |
|
114
|
|
|
// option, if yes override the value from the configuration file |
|
115
|
|
|
if ($pidFilename = $input->getOption(InputOptionKeys::PID_FILENAME)) { |
|
116
|
|
|
$instance->setPidFilename($pidFilename); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// return the initialized configuration instance |
|
120
|
|
|
return $instance; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Executes the current command. |
|
125
|
|
|
* |
|
126
|
|
|
* This method is not abstract because you can use this class |
|
127
|
|
|
* as a concrete class. In this case, instead of defining the |
|
128
|
|
|
* execute() method, you set the code to execute by passing |
|
129
|
|
|
* a Closure to the setCode() method. |
|
130
|
|
|
* |
|
131
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
|
132
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
|
133
|
|
|
* |
|
134
|
|
|
* @return null|int null or 0 if everything went fine, or an error code |
|
135
|
|
|
* @throws \LogicException When this abstract method is not implemented |
|
136
|
|
|
* @see \Symfony\Component\Console\Command\Command::execute() |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
139
|
|
|
{ |
|
140
|
|
|
|
|
141
|
|
|
// register the JMS Serializer annotations |
|
142
|
|
|
\Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace( |
|
143
|
|
|
'JMS\Serializer\Annotation', |
|
144
|
|
|
dirname(__DIR__).'/../vendor/jms/serializer/src' |
|
145
|
|
|
); |
|
146
|
|
|
|
|
147
|
|
|
// load the specified configuration |
|
148
|
|
|
$configuration = $this->configurationFactory($input); |
|
149
|
|
|
|
|
150
|
|
|
// initialize the system logger |
|
151
|
|
|
$systemLogger = new Logger('techdivision/import'); |
|
152
|
|
|
$systemLogger->pushHandler( |
|
153
|
|
|
new ErrorLogHandler( |
|
154
|
|
|
ErrorLogHandler::OPERATING_SYSTEM, |
|
155
|
|
|
$configuration->getLogLevel() |
|
156
|
|
|
) |
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
// initialize and run the importer |
|
160
|
|
|
$importer = new Simple(); |
|
161
|
|
|
$importer->setInput($input); |
|
162
|
|
|
$importer->setOutput($output); |
|
163
|
|
|
$importer->setSystemLogger($systemLogger); |
|
164
|
|
|
$importer->setConfiguration($configuration); |
|
165
|
|
|
$importer->clearPid(); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|