-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExportYmlItems.php
251 lines (217 loc) · 7.15 KB
/
ExportYmlItems.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* JBZoo CCK Cli
*
* This file is part of the JBZoo CCK package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package CCK Cli
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/CCK-Cli
* @author Sergey Kalistratov <kalistratov.s.m@gmail.com>
*/
namespace JBZoo\Console\Command;
use JBZoo\Utils\FS;
use JBZoo\Utils\Slug;
use JBZoo\Console\CommandJBZoo;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class ExportYmlItems
*
* @package JBZoo\Console\Command
*/
class ExportYmlItems extends CommandJBZoo
{
/**
* @var \JBConfigHelper
*/
protected $_jbconfig = null;
/**
* @var \JBYmlHelper
*/
protected $_jbyml = null;
/**
* @var string
*/
protected $_commandName = 'export:yml-items';
/**
* @return void
*/
protected function _browserEmulator()
{
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['REQUEST_URI'] = '/index.php';
$_SERVER['PHP_SELF'] = '/index.php';
$_SERVER['HTTP_HOST'] = $this->_getSiteUrl();
$_SERVER['SERVER_NAME'] = $this->_getSiteUrl();
$_SERVER['DOCUMENT_ROOT'] = JBZOO_CLI_JOOMLA_ROOT;
}
/**
* @return mixed|string
*/
protected function _getFileName()
{
return $this->_config->find('params.file_name', 'yml');
}
/**
* @return mixed|string
*/
protected function _getFilePath()
{
return $this->_config->find('params.file_path', 'cli/jbzoo/resources/sources');
}
/**
* @return string
*/
protected function _getProfFileName()
{
return Slug::filter($this->_getFileName() . '-' . $this->_getOpt('profile'));
}
/**
* @return mixed
*/
protected function _getSiteUrl()
{
return $this->_globConfig->get('host');
}
/**
* {@inheritdoc}
*/
protected function _init()
{
parent::_init();
$this->_jbyml = $this->app->jbyml;
$this->_jbconfig = $this->app->jbconfig;
}
/**
* Init Joomla Framework.
*
* @SuppressWarnings(PHPMD.Superglobals)
* @return void
*/
protected function _loadJoomla()
{
define('_JEXEC', 1);
define('JPATH_BASE', JBZOO_CLI_JOOMLA_ROOT); // website root directory
/** @noinspection PhpIncludeInspection */
require_once JPATH_BASE . '/includes/defines.php';
/** @noinspection PhpIncludeInspection */
require_once JPATH_BASE . '/includes/framework.php';
// prepare env (emulate browser)
$this->_browserEmulator();
if (!$_SERVER['HTTP_HOST']) {
$this->_('Host is undefined. Please, check global config "./config/_global.php"', 'Error');
$this->_('Joomla need it for browser simulation', 'Error', 1);
}
// no output
$_GET['tmpl'] = $_REQUEST['tmpl'] = 'raw';
$_GET['lang'] = $_REQUEST['lang'] = 'ru';
// init Joomla App ( Front-end emulation )
\JFactory::getApplication('site');
}
/**
* @return \JBModelConfig
*/
protected function _setupConfigure()
{
$configModel = \JBModelConfig::model();
$configModel->setGroup('config.yml', array(
'site_url' => rtrim(\JUri::root(), '/'),
'file_path' => $this->_getFilePath(),
'file_name' => $this->_getProfFileName(),
'site_name' => $this->_config->find('params.site_name', 'My site name'),
'company_name' => $this->_config->find('params.company_name', 'My company name'),
'currency_rate' => $this->_config->find('params.currency_rate', 'default'),
));
$configModel->set('app_list', $this->_config->find('params.app_list', array(1)));
$configModel->set('type_list', $this->_config->find('params.type_list', array('product')));
return $configModel;
}
/**
* Configuration of command.
*
* @return void
*/
protected function configure() // @codingStandardsIgnoreLine
{
$this
->setName($this->_commandName)
->setDescription('Export items to YML file')
->addOption(
'profile',
null,
InputOption::VALUE_REQUIRED,
'Profile name is PHP-file in \'config\' directory like \'export-yml-items-<NAME>.php\' ',
'default'
)->addOption(
'stepmode',
null,
InputOption::VALUE_NONE,
'Enable step mode. Each step call itself. Experimental for memory optimization'
)
->addOption(
'step',
null,
InputOption::VALUE_OPTIONAL,
'Current step number for step mode (Set automatically if enabled stepmode)',
-1
);
}
/**
* Executes the current command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output) // @codingStandardsIgnoreLine
{
$this->_executePrepare($input, $output);
$this->_init();
$this->_setupConfigure();
$this->_jbyml->init();
$totalItems = $this->_jbyml->getTotal();
$filePath = $this->_getFilePath() . '/' . $this->_getProfFileName() . '.xml';
$fullPath = FS::clean(JBZOO_CLI_JOOMLA_ROOT . '/' . $filePath);
$stepMode = $this->_getOpt('stepmode');
$stepSize = $this->_config->find('params.step_size', 25);
$this->_showProfiler('YML Export - prepared');
$this->_('YML File: ' . $fullPath, 'Info');
$this->_('Total items: ' . $totalItems, 'Info');
$this->_('Step size: ' . $stepSize, 'Info');
$this->_('Step mode: ' . ($stepMode ? 'on' : 'off'), 'Info');
$this->_progressWrap(
$this->_commandName,
$totalItems,
$stepSize,
// On start.
function () use ($totalItems, $stepSize) {
$this->_jbyml->renderStart();
$this->_progressBar('yml-export', $totalItems, $stepSize, function ($currentStep, $stepSize) {
$offset = $stepSize * $currentStep;
$this->_jbyml->exportItems($offset, $stepSize);
});
return true;
},
// On step.
function ($step) use ($stepSize) {
$offset = $stepSize * $step;
if ($step == 0) {
$this->_jbyml->renderStart();
}
$this->_jbyml->exportItems($offset, $stepSize);
},
// On finish.
function ($isFinished) {
if ($isFinished) {
$this->_jbyml->renderFinish();
$this->_showProfiler('Import - finished');
}
}
);
}
}