|
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
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The command implementation that creates a OK file from a directory with CSV files. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Tim Wagner <[email protected]> |
|
32
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
34
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
|
35
|
|
|
* @link http://www.techdivision.com |
|
36
|
|
|
*/ |
|
37
|
|
|
class ImportCreateOkFileCommand extends AbstractSimpleImportCommand |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Configures the current command. |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function configure() |
|
47
|
|
|
{ |
|
48
|
|
|
|
|
49
|
|
|
// initialize the command with the required/optional options |
|
50
|
|
|
$this->setName(CommandNames::IMPORT_CREATE_OK_FILE) |
|
51
|
|
|
->setDescription('Create\'s the OK file for the CSV files of the configured source directory'); |
|
52
|
|
|
|
|
53
|
|
|
// invoke the parent method |
|
54
|
|
|
parent::configure(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Finally executes the simple command. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
|
62
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
|
63
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function executeSimpleCommand( |
|
68
|
|
|
ConfigurationInterface $configuration, |
|
69
|
|
|
InputInterface $input, |
|
70
|
|
|
OutputInterface $output |
|
71
|
|
|
) { |
|
72
|
|
|
|
|
73
|
|
|
// load the source directory |
|
74
|
|
|
$sourceDir = $configuration->getSourceDir(); |
|
75
|
|
|
|
|
76
|
|
|
/** @var TechDivision\Import\Configuration\PluginConfigurationInterface $plugin */ |
|
77
|
|
|
foreach ($configuration->getPlugins() as $plugin) { |
|
78
|
|
|
/** @var TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */ |
|
79
|
|
|
foreach ($plugin->getSubjects() as $subject) { |
|
80
|
|
|
// query whether or not, an OK file is needed |
|
81
|
|
|
if ($subject->isOkFileNeeded()) { |
|
82
|
|
|
// load the prefix |
|
83
|
|
|
$prefix = $subject->getPrefix(); |
|
84
|
|
|
|
|
85
|
|
|
// load the CSVfiles from the source directory |
|
86
|
|
|
$csvFiles = glob(sprintf('%s/%s*.csv', $sourceDir, $prefix)); |
|
87
|
|
|
|
|
88
|
|
|
// query whether or not any CSV files are available |
|
89
|
|
|
if (sizeof($csvFiles) > 0) { |
|
90
|
|
|
// prepare the OK file's content |
|
91
|
|
|
$okfileContent = ''; |
|
92
|
|
|
foreach ($csvFiles as $filename) { |
|
93
|
|
|
$okfileContent .= basename($filename) . PHP_EOL; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// prepare the OK file's name |
|
97
|
|
|
$okFilename = sprintf('%s/%s.ok', $sourceDir, $prefix); |
|
98
|
|
|
|
|
99
|
|
|
// write the OK file |
|
100
|
|
|
if (file_put_contents($okFilename, $okfileContent)) { |
|
101
|
|
|
// write a message to the console |
|
102
|
|
|
$output->writeln(sprintf('<info>Successfully written OK file %s</info>', $okFilename)); |
|
103
|
|
|
|
|
|
|
|
|
|
104
|
|
|
} else { |
|
105
|
|
|
// write a message to the console |
|
106
|
|
|
$output->writeln(sprintf('<error>Can\'t write OK file %s</error>', $okFilename)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
|
109
|
|
|
} else { |
|
110
|
|
|
// write a message to the console |
|
111
|
|
|
$output->writeln(sprintf('<error>Can\'t find any CSV files in source directory %s</error>', $sourceDir)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// stop the process |
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|