|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Cli\Command\ImportCreateOkFileCommand |
|
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 TechDivision\Import\Utils\CommandNames; |
|
24
|
|
|
use TechDivision\Import\ConfigurationInterface; |
|
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
26
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
27
|
|
|
use TechDivision\Import\Cli\Utils\DependencyInjectionKeys; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The command implementation that creates a OK file from a directory with CSV files. |
|
31
|
|
|
* |
|
32
|
|
|
* @author Tim Wagner <[email protected]> |
|
33
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
35
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
|
36
|
|
|
* @link http://www.techdivision.com |
|
37
|
|
|
*/ |
|
38
|
|
|
class ImportCreateOkFileCommand 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_OK_FILE) |
|
52
|
|
|
->setDescription('Create\'s the OK file for the CSV files of the configured source directory'); |
|
53
|
|
|
|
|
54
|
|
|
// invoke the parent method |
|
55
|
|
|
parent::configure(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Executes the current command. |
|
60
|
|
|
* |
|
61
|
|
|
* This method is not abstract because you can use this class |
|
62
|
|
|
* as a concrete class. In this case, instead of defining the |
|
63
|
|
|
* execute() method, you set the code to execute by passing |
|
64
|
|
|
* a Closure to the setCode() method. |
|
65
|
|
|
* |
|
66
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
|
67
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
|
68
|
|
|
* |
|
69
|
|
|
* @return null|int null or 0 if everything went fine, or an error code |
|
70
|
|
|
* @throws \LogicException When this abstract method is not implemented |
|
71
|
|
|
* @see \Symfony\Component\Console\Command\Command::execute() |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
74
|
|
|
{ |
|
75
|
|
|
|
|
76
|
|
|
// initialize the configuration instance |
|
77
|
|
|
$configuration = $this->getContainer()->get(DependencyInjectionKeys::CONFIGURATION_SIMPLE); |
|
78
|
|
|
|
|
79
|
|
|
// finally execute the simple command |
|
80
|
|
|
return $this->executeSimpleCommand($configuration, $input, $output); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Finally executes the simple command. |
|
86
|
|
|
* |
|
87
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
|
88
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
|
89
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
|
90
|
|
|
* |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function executeSimpleCommand( |
|
94
|
|
|
ConfigurationInterface $configuration, |
|
95
|
|
|
InputInterface $input, |
|
96
|
|
|
OutputInterface $output |
|
97
|
|
|
) { |
|
98
|
|
|
|
|
99
|
|
|
// load the source directory, ALWAYS remove the directory separator, if appended |
|
100
|
|
|
$sourceDir = rtrim($configuration->getSourceDir(), DIRECTORY_SEPARATOR); |
|
101
|
|
|
|
|
102
|
|
|
// load the array with the unique prefixes |
|
103
|
|
|
$prefixes = $configuration->getPrefixes(); |
|
104
|
|
|
|
|
105
|
|
|
// sort the prefixes |
|
106
|
|
|
usort($prefixes, function ($a, $b) { |
|
107
|
|
|
return strcmp($a, $b); |
|
108
|
|
|
}); |
|
109
|
|
|
|
|
110
|
|
|
// initialize the counter for the CSV files |
|
111
|
|
|
$csvFilesFound = 0; |
|
112
|
|
|
|
|
113
|
|
|
// iterate over the prefixes and create the .ok files |
|
114
|
|
|
foreach ($prefixes as $prefix) { |
|
115
|
|
|
// load the CSVfiles from the source directory |
|
116
|
|
|
$csvFiles = glob(sprintf('%s/%s_*.csv', $sourceDir, $prefix)); |
|
117
|
|
|
// raise the counter for the CSV files we've found |
|
118
|
|
|
$csvFilesFound += sizeof($csvFiles); |
|
|
|
|
|
|
119
|
|
|
// query whether or not any CSV files are available |
|
120
|
|
|
if (sizeof($csvFiles) > 0) { |
|
121
|
|
|
// prepare the OK file's content |
|
122
|
|
|
$okfileContent = ''; |
|
123
|
|
|
foreach ($csvFiles as $filename) { |
|
124
|
|
|
$okfileContent .= basename($filename) . PHP_EOL; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// prepare the OK file's name |
|
128
|
|
|
$okFilename = sprintf('%s/%s.ok', $sourceDir, $prefix); |
|
129
|
|
|
|
|
130
|
|
|
// write the OK file |
|
131
|
|
|
if (file_put_contents($okFilename, $okfileContent)) { |
|
132
|
|
|
// write a message to the console |
|
133
|
|
|
$output->writeln(sprintf('<info>Successfully written OK file %s</info>', $okFilename)); |
|
134
|
|
|
} else { |
|
135
|
|
|
// write a message to the console |
|
136
|
|
|
$output->writeln(sprintf('<error>Can\'t write OK file %s</error>', $okFilename)); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
// query whether or not we've found any CSV files |
|
142
|
|
|
if ($csvFilesFound === 0) { |
|
|
|
|
|
|
143
|
|
|
// write a message to the console, if we can't find any CSV files |
|
144
|
|
|
$output->writeln(sprintf('<error>Can\'t find any CSV files in source directory %s</error>', $sourceDir)); |
|
145
|
|
|
// return 1 to signal an error |
|
146
|
|
|
return 1; |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
// return 0 to signal success |
|
150
|
|
|
return 0; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|