/
InstallCommand.php
863 lines (749 loc) · 34.4 KB
/
InstallCommand.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
<?php
namespace N98\Magento\Command\Installer;
use Composer\Composer;
use Composer\Package\CompletePackage;
use Exception;
use InvalidArgumentException;
use N98\Magento\Command\AbstractMagentoCommand;
use N98\Util\Database as DatabaseUtils;
use N98\Util\Filesystem;
use N98\Util\OperatingSystem;
use N98\Util\BinaryString;
use PDO;
use PDOException;
use RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use N98\Util\Exec;
/**
* Class InstallCommand
*
* @codeCoverageIgnore - Travis server uses installer to create a new shop. If it not works complete build fails.
* @package N98\Magento\Command\Installer
*/
class InstallCommand extends AbstractMagentoCommand
{
const EXEC_STATUS_OK = 0;
/**
* @var array
*/
protected $config;
/**
* @var array
*/
protected $_argv;
/**
* @var array
*/
protected $commandConfig;
/**
* @var \Closure
*/
protected $notEmptyCallback;
protected function configure()
{
$this
->setName('install')
->addOption('magentoVersion', null, InputOption::VALUE_OPTIONAL, 'Magento version')
->addOption('magentoVersionByName', null, InputOption::VALUE_OPTIONAL, 'Magento version name instead of order number')
->addOption('installationFolder', null, InputOption::VALUE_OPTIONAL, 'Installation folder')
->addOption('dbHost', null, InputOption::VALUE_OPTIONAL, 'Database host')
->addOption('dbUser', null, InputOption::VALUE_OPTIONAL, 'Database user')
->addOption('dbPass', null, InputOption::VALUE_OPTIONAL, 'Database password')
->addOption('dbName', null, InputOption::VALUE_OPTIONAL, 'Database name')
->addOption('dbPort', null, InputOption::VALUE_OPTIONAL, 'Database port', 3306)
->addOption('dbPrefix', null, InputOption::VALUE_OPTIONAL, 'Table prefix', '')
->addOption('installSampleData', null, InputOption::VALUE_OPTIONAL, 'Install sample data')
->addOption('useDefaultConfigParams', null, InputOption::VALUE_OPTIONAL, 'Use default installation parameters defined in the yaml file')
->addOption('baseUrl', null, InputOption::VALUE_OPTIONAL, 'Installation base url')
->addOption('replaceHtaccessFile', null, InputOption::VALUE_OPTIONAL, 'Generate htaccess file (for non vhost environment)')
->addOption(
'noDownload',
null,
InputOption::VALUE_NONE,
'If set skips download step. Used when installationFolder is already a Magento installation that has ' .
'to be installed on the given database.'
)
->addOption(
'only-download',
null,
InputOption::VALUE_NONE,
'Downloads (and extracts) source code'
)
->addOption('forceUseDb', null, InputOption::VALUE_OPTIONAL, 'If --noDownload passed, force to use given database if it already exists.')
->setDescription('Install magento')
;
$help = <<<HELP
* Download Magento by a list of git repos and zip files (mageplus, magelte, official community packages).
* Try to create database if it does not exist.
* Installs Magento sample data if available (since version 1.2.0).
* Starts Magento installer
* Sets rewrite base in .htaccess file
Example of an unattended Magento CE 1.7.0.2 installation:
$ n98-magerun.phar install --dbHost="localhost" --dbUser="mydbuser" --dbPass="mysecret" --dbName="magentodb" --installSampleData=yes --useDefaultConfigParams=yes --magentoVersionByName="magento-ce-1.7.0.2" --installationFolder="magento" --baseUrl="http://magento.localdomain/"
Additionally, with --noDownload option you can install Magento working copy already stored in --installationFolder on
the given database.
See it in action: http://youtu.be/WU-CbJ86eQc
HELP;
$this->setHelp($help);
$this->notEmptyCallback = function($input)
{
if (empty($input)) {
throw new InvalidArgumentException('Please enter a value');
}
return $input;
};
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @throws RuntimeException
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->commandConfig = $this->getCommandConfig();
$this->writeSection($output, 'Magento Installation');
$this->precheckPhp();
if (!$input->getOption('noDownload')) {
$this->selectMagentoVersion($input, $output);
}
$this->chooseInstallationFolder($input, $output);
if (!$input->getOption('noDownload')) {
$result = $this->downloadMagento($input, $output);
if ($result === false) {
return 1;
}
}
if ($input->getOption('only-download')) {
return 0;
}
$this->createDatabase($input, $output);
if (!$input->getOption('noDownload')) {
$this->installSampleData($input, $output);
}
$this->removeEmptyFolders();
$this->setDirectoryPermissions($output);
$this->installMagento($input, $output, $this->config['installationFolder']);
}
/**
* Check PHP environment agains minimal required settings modules
*/
protected function precheckPhp()
{
$extensions = $this->commandConfig['installation']['pre-check']['php']['extensions'];
$missingExtensions = array();
foreach ($extensions as $extension) {
if (!extension_loaded($extension)) {
$missingExtensions[] = $extension;
}
}
if (count($missingExtensions) > 0) {
throw new RuntimeException(
'The following PHP extensions are required to start installation: ' . implode(',', $missingExtensions)
);
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @throws InvalidArgumentException
*/
protected function selectMagentoVersion(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('magentoVersion') == null && $input->getOption('magentoVersionByName') == null) {
$question = array();
foreach ($this->commandConfig['magento-packages'] as $key => $package) {
$question[] = '<comment>' . str_pad('[' . ($key + 1) . ']', 4, ' ') . '</comment> ' . $package['name'] . "\n";
}
$question[] = "<question>Choose a magento version:</question> ";
$commandConfig = $this->commandConfig;
$type = $this->getHelper('dialog')->askAndValidate($output, $question, function($typeInput) use ($commandConfig) {
if (!in_array($typeInput, range(1, count($commandConfig['magento-packages'])))) {
throw new InvalidArgumentException('Invalid type');
}
return $typeInput;
});
} else {
$type = null;
if ($input->getOption('magentoVersion')) {
$type = $input->getOption('magentoVersion');
if ($type !== (string)(int)$type) {
$type = $this->getPackageNumberByName($type);
}
} elseif ($input->getOption('magentoVersionByName')) {
$type = $this->getPackageNumberByName($input->getOption('magentoVersionByName'));
}
if ($type == null) {
throw new InvalidArgumentException('Unable to locate Magento version');
}
}
$magentoPackages = $this->commandConfig['magento-packages'];
$index = $type - 1;
if (!isset($magentoPackages[$index])) {
throw new InvalidArgumentException(
sprintf(
'Invalid Magento package number %s, must be from 1 to %d.', var_export($type, true),
count($magentoPackages)
)
);
}
$this->config['magentoVersionData'] = $magentoPackages[$index];
}
/**
* @param $name
*
* @return int 1 or greater as the one-based package number, null on failure to resolve the name
*/
private function getPackageNumberByName($name)
{
// directly filter integer strings
if ($name === (string)(int)$name) {
return (int) $name;
}
$magentoPackages = $this->commandConfig['magento-packages'];
foreach ($magentoPackages as $key => $package) {
if ($package['name'] === $name) {
return $key + 1;
}
}
return null;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return bool
*/
public function downloadMagento(InputInterface $input, OutputInterface $output)
{
try {
$package = $this->createComposerPackageByConfig($this->config['magentoVersionData']);
$this->config['magentoPackage'] = $package;
if (file_exists($this->config['installationFolder'] . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'Mage.php')) {
$output->writeln('<error>A magento installation already exists in this folder </error>');
return false;
}
$composer = $this->getComposer($input, $output);
$targetFolder = $this->getTargetFolderByType($composer, $package, $this->config['installationFolder']);
$this->config['magentoPackage'] = $this->downloadByComposerConfig(
$input,
$output,
$package,
$targetFolder,
true
);
if ($this->isSourceTypeRepository($package->getSourceType())) {
$filesystem = new \N98\Util\Filesystem;
$filesystem->recursiveCopy($targetFolder, $this->config['installationFolder'], array('.git', '.hg'));
} else {
$filesystem = new \Composer\Util\Filesystem();
$filesystem->copyThenRemove(
$this->config['installationFolder'] . '/_n98_magerun_download', $this->config['installationFolder']
);
}
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
// Patch installer
$this->patchMagentoInstallerForPHP54($this->config['installationFolder']);
}
} catch (Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
return false;
}
return true;
}
/**
* construct a folder to where magerun will download the source to, cache git/hg repositories under COMPOSER_HOME
*
* @param Composer $composer
* @param CompletePackage $package
* @param $installationFolder
*
* @return string
*/
protected function getTargetFolderByType(Composer $composer, CompletePackage $package, $installationFolder)
{
$type = $package->getSourceType();
if ($this->isSourceTypeRepository($type)) {
$targetPath = sprintf(
'%s/%s/%s/%s',
$composer->getConfig()->get('cache-dir'),
'_n98_magerun_download',
$type,
preg_replace('{[^a-z0-9.]}i', '-', $package->getSourceUrl())
);
} else {
$targetPath = sprintf(
'%s/%s',
$installationFolder,
'_n98_magerun_download'
);
}
return $targetPath;
}
/**
* @param string $magentoFolder
*/
protected function patchMagentoInstallerForPHP54($magentoFolder)
{
$installerConfig = $magentoFolder
. DIRECTORY_SEPARATOR
. 'app/code/core/Mage/Install/etc/config.xml';
if (file_exists($installerConfig)) {
$xml = file_get_contents($installerConfig);
file_put_contents($installerConfig, str_replace('<pdo_mysql/>', '<pdo_mysql>1</pdo_mysql>', $xml));
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @throws InvalidArgumentException
*/
protected function createDatabase(InputInterface $input, OutputInterface $output)
{
$dbOptions = array('--dbHost', '--dbUser', '--dbPass', '--dbName');
$dbOptionsFound = 0;
foreach ($dbOptions as $dbOption) {
foreach ($this->getCliArguments() as $definedCliOption) {
if (BinaryString::startsWith($definedCliOption, $dbOption)) {
$dbOptionsFound++;
}
}
}
$hasAllOptions = $dbOptionsFound == 4;
// if all database options were passed in at cmd line
if ($hasAllOptions) {
$this->config['db_host'] = $input->getOption('dbHost');
$this->config['db_user'] = $input->getOption('dbUser');
$this->config['db_pass'] = $input->getOption('dbPass');
$this->config['db_name'] = $input->getOption('dbName');
$this->config['db_port'] = $input->getOption('dbPort');
$this->config['db_prefix'] = $input->getOption('dbPrefix');
$db = $this->validateDatabaseSettings($output, $input);
if ($db === false) {
throw new InvalidArgumentException("Database configuration is invalid");
}
} else {
$dialog = $this->getHelperSet()->get('dialog');
do {
$dbHostDefault = $input->getOption('dbHost') ? $input->getOption('dbHost') : 'localhost';
$this->config['db_host'] = $dialog->askAndValidate($output, '<question>Please enter the database host</question> <comment>[' . $dbHostDefault . ']</comment>: ', $this->notEmptyCallback, false, $dbHostDefault);
$dbUserDefault = $input->getOption('dbUser') ? $input->getOption('dbUser') : 'root';
$this->config['db_user'] = $dialog->askAndValidate($output, '<question>Please enter the database username</question> <comment>[' . $dbUserDefault . ']</comment>: ', $this->notEmptyCallback, false, $dbUserDefault);
$dbPassDefault = $input->getOption('dbPass') ? $input->getOption('dbPass') : '';
$this->config['db_pass'] = $dialog->ask($output, '<question>Please enter the database password</question> <comment>[' . $dbPassDefault . ']</comment>: ', $dbPassDefault);
$dbNameDefault = $input->getOption('dbName') ? $input->getOption('dbName') : 'magento';
$this->config['db_name'] = $dialog->askAndValidate($output, '<question>Please enter the database name</question> <comment>[' . $dbNameDefault . ']</comment>: ', $this->notEmptyCallback, false, $dbNameDefault);
$dbPortDefault = $input->getOption('dbPort') ? $input->getOption('dbPort') : 3306;
$this->config['db_port'] = $dialog->askAndValidate($output, '<question>Please enter the database port </question> <comment>[' . $dbPortDefault . ']</comment>: ', $this->notEmptyCallback, false, $dbPortDefault);
$dbPrefixDefault = $input->getOption('dbPrefix') ? $input->getOption('dbPrefix') : '';
$this->config['db_prefix'] = $dialog->ask($output, '<question>Please enter the table prefix</question> <comment>[' . $dbPrefixDefault . ']</comment>:', $dbPrefixDefault);
$db = $this->validateDatabaseSettings($output, $input);
} while ($db === false);
}
$this->config['db'] = $db;
}
/**
* @param OutputInterface $output
* @param InputInterface $input
*
* @return bool|PDO
*/
protected function validateDatabaseSettings(OutputInterface $output, InputInterface $input)
{
try {
$dsn = sprintf("mysql:host=%s;port=%s", $this->config['db_host'], $this->config['db_port']);
$db = new PDO($dsn, $this->config['db_user'], $this->config['db_pass']);
if (!$db->query('USE ' . $this->config['db_name'])) {
$db->query("CREATE DATABASE `" . $this->config['db_name'] . "`");
$output->writeln('<info>Created database ' . $this->config['db_name'] . '</info>');
$db->query('USE ' . $this->config['db_name']);
return $db;
}
if ($input->getOption('noDownload') && !$input->getOption('forceUseDb')) {
$output->writeln("<error>Database {$this->config['db_name']} already exists.</error>");
return false;
}
return $db;
} catch (PDOException $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
}
return false;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function installSampleData(InputInterface $input, OutputInterface $output)
{
$magentoPackage = $this->config['magentoPackage']; /* @var $magentoPackage \Composer\Package\MemoryPackage */
$extra = $magentoPackage->getExtra();
if (!isset($extra['sample-data'])) {
return;
}
$dialog = $this->getHelperSet()->get('dialog');
$installSampleData = ($input->getOption('installSampleData') !== null) ? $this->_parseBoolOption($input->getOption('installSampleData')) : $dialog->askConfirmation($output, '<question>Install sample data?</question> <comment>[y]</comment>: ');
if ($installSampleData) {
$filesystem = new Filesystem();
foreach ($this->commandConfig['demo-data-packages'] as $demoPackageData) {
if ($demoPackageData['name'] == $extra['sample-data']) {
$package = $this->downloadByComposerConfig(
$input,
$output,
$demoPackageData,
$this->config['installationFolder'] . '/_temp_demo_data',
false
);
$this->_fixComposerExtractionBug();
$expandedFolder = $this->config['installationFolder']
. '/_temp_demo_data/'
. str_replace(array('.tar.gz', '.tar.bz2', '.zip'), '', basename($package->getDistUrl()));
if (is_dir($expandedFolder)) {
$filesystem->recursiveCopy(
$expandedFolder,
$this->config['installationFolder']
);
$filesystem->recursiveRemoveDirectory($expandedFolder);
}
// Remove empty folder
if (is_dir($this->config['installationFolder'] . '/vendor/composer')) {
$filesystem->recursiveRemoveDirectory($this->config['installationFolder'] . '/vendor/composer');
}
// Install sample data
$sampleDataSqlFile = glob($this->config['installationFolder'] . '/_temp_demo_data/magento_*sample_data*sql');
$db = $this->config['db'];
/* @var $db PDO */
if (isset($sampleDataSqlFile[0])) {
if (OperatingSystem::isProgramInstalled('mysql')) {
$exec = 'mysql '
. '-h' . escapeshellarg(strval($this->config['db_host']))
. ' '
. '-u' . escapeshellarg(strval($this->config['db_user']))
. ' '
. ($this->config['db_port'] != '3306' ? '-P' . escapeshellarg($this->config['db_port']) . ' ' : '')
. (!strval($this->config['db_pass'] == '') ? '-p' . escapeshellarg($this->config['db_pass']) . ' ' : '')
. strval($this->config['db_name'])
. ' < '
. escapeshellarg($sampleDataSqlFile[0]);
$output->writeln('<info>Importing <comment>' . $sampleDataSqlFile[0] . '</comment> with mysql cli client</info>');
Exec::run($exec);
@unlink($sampleDataSqlFile[0]);
} else {
$output->writeln('<info>Importing <comment>' . $sampleDataSqlFile[0] . '</comment> with PDO driver</info>');
// Fallback -> Try to install dump file by PDO driver
$dbUtils = new DatabaseUtils();
$dbUtils->importSqlDump($db, $sampleDataSqlFile[0]);
}
}
}
}
if (is_dir($this->config['installationFolder'] . '/_temp_demo_data')) {
$filesystem->recursiveRemoveDirectory($this->config['installationFolder'] . '/_temp_demo_data');
}
}
}
protected function _fixComposerExtractionBug()
{
$filesystem = new Filesystem();
foreach (array('/_temp_demo_data/media' => '/media', '/_temp_demo_data/skin' => '/skin') as $wrong => $right) {
$wrongFolder = $this->config['installationFolder'] . $wrong;
$rightFolder = $this->config['installationFolder'] . $right;
if (is_dir($wrongFolder)) {
$filesystem->recursiveCopy(
$wrongFolder,
$rightFolder
);
$filesystem->recursiveRemoveDirectory($wrongFolder);
}
}
}
/**
* Remove empty composer extraction folder
*/
protected function removeEmptyFolders()
{
if (is_dir(getcwd() . '/vendor')) {
$finder = new Finder();
$finder->files()->depth(3)->in(getcwd() . '/vendor');
if ($finder->count() == 0) {
$filesystem = new Filesystem();
$filesystem->recursiveRemoveDirectory(getcwd() . '/vendor');
}
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return array
* @throws InvalidArgumentException parameter mismatch (e.g. base-url components like hostname)
* @throws RuntimeException
*/
protected function installMagento(InputInterface $input, OutputInterface $output)
{
$this->getApplication()->setAutoExit(false);
$dialog = $this->getHelperSet()->get('dialog');
$defaults = $this->commandConfig['installation']['defaults'];
$useDefaultConfigParams = $this->_parseBoolOption($input->getOption('useDefaultConfigParams'));
$sessionSave = $useDefaultConfigParams ? $defaults['session_save'] : $dialog->ask(
$output,
'<question>Please enter the session save:</question> <comment>[' . $defaults['session_save'] . ']</comment>: ',
$defaults['session_save']
);
$adminFrontname = $useDefaultConfigParams ? $defaults['admin_frontname'] : $dialog->askAndValidate(
$output,
'<question>Please enter the admin frontname:</question> <comment>[' . $defaults['admin_frontname'] . ']</comment> ',
$this->notEmptyCallback,
false,
$defaults['admin_frontname']
);
$currency = $useDefaultConfigParams ? $defaults['currency'] : $dialog->askAndValidate(
$output,
'<question>Please enter the default currency code:</question> <comment>[' . $defaults['currency'] . ']</comment>: ',
$this->notEmptyCallback,
false,
$defaults['currency']
);
$locale = $useDefaultConfigParams ? $defaults['locale'] : $dialog->askAndValidate(
$output,
'<question>Please enter the locale code:</question> <comment>[' . $defaults['locale'] . ']</comment>: ',
$this->notEmptyCallback,
false,
$defaults['locale']
);
$timezone = $useDefaultConfigParams ? $defaults['timezone'] : $dialog->askAndValidate(
$output,
'<question>Please enter the timezone:</question> <comment>[' . $defaults['timezone'] . ']</comment>: ',
$this->notEmptyCallback,
false,
$defaults['timezone']
);
$adminUsername = $useDefaultConfigParams ? $defaults['admin_username'] : $dialog->askAndValidate(
$output,
'<question>Please enter the admin username:</question> <comment>[' . $defaults['admin_username'] . ']</comment>: ',
$this->notEmptyCallback,
false,
$defaults['admin_username']
);
$adminPassword = $useDefaultConfigParams ? $defaults['admin_password'] : $dialog->askAndValidate(
$output,
'<question>Please enter the admin password:</question> <comment>[' . $defaults['admin_password'] . ']</comment>: ',
$this->notEmptyCallback,
false,
$defaults['admin_password']
);
$adminFirstname = $useDefaultConfigParams ? $defaults['admin_firstname'] : $dialog->askAndValidate(
$output,
'<question>Please enter the admin\'s firstname:</question> <comment>[' . $defaults['admin_firstname'] . ']</comment>: ',
$this->notEmptyCallback,
false,
$defaults['admin_firstname']
);
$adminLastname = $useDefaultConfigParams ? $defaults['admin_lastname'] : $dialog->askAndValidate(
$output,
'<question>Please enter the admin\'s lastname:</question> <comment>[' . $defaults['admin_lastname'] . ']</comment>: ',
$this->notEmptyCallback,
false,
$defaults['admin_lastname']
);
$adminEmail = $useDefaultConfigParams ? $defaults['admin_email'] : $dialog->askAndValidate(
$output,
'<question>Please enter the admin\'s email:</question> <comment>[' . $defaults['admin_email'] . ']</comment>: ',
$this->notEmptyCallback,
false,
$defaults['admin_email']
);
$validateBaseUrl = function($input) {
if (!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $input)) {
throw new InvalidArgumentException('Please enter a valid URL');
}
if (parse_url($input, \PHP_URL_HOST) == 'localhost') {
throw new InvalidArgumentException('localhost cause problems! Please use 127.0.0.1 or another hostname');
}
return $input;
};
$baseUrl = ($input->getOption('baseUrl') !== null) ? $input->getOption('baseUrl') : $dialog->askAndValidate(
$output,
'<question>Please enter the base url:</question> ',
$validateBaseUrl,
false
);
$baseUrl = rtrim($baseUrl, '/') . '/'; // normalize baseUrl
/**
* Correct session save (common mistake)
*/
if ($sessionSave == 'file') {
$sessionSave = 'files';
}
/**
* Try to create session folder
*/
$defaultSessionFolder = $this->config['installationFolder'] . DIRECTORY_SEPARATOR . 'var/session';
if ($sessionSave == 'files' && !is_dir($defaultSessionFolder)) {
@mkdir($defaultSessionFolder);
}
$dbHost = $this->config['db_host'];
if ($this->config['db_port'] != 3306) {
$dbHost .= ':' . $this->config['db_port'];
}
$argv = array(
'license_agreement_accepted' => 'yes',
'locale' => $locale,
'timezone' => $timezone,
'db_host' => $dbHost,
'db_name' => $this->config['db_name'],
'db_user' => $this->config['db_user'],
'db_pass' => $this->config['db_pass'],
'db_prefix' => $this->config['db_prefix'],
'url' => $baseUrl,
'use_rewrites' => 'yes',
'use_secure' => 'no',
'secure_base_url' => '',
'use_secure_admin' => 'no',
'admin_username' => $adminUsername,
'admin_lastname' => $adminLastname,
'admin_firstname' => $adminFirstname,
'admin_email' => $adminEmail,
'admin_password' => $adminPassword,
'session_save' => $sessionSave,
'admin_frontname' => $adminFrontname, /* magento 1 */
'backend_frontname' => $adminFrontname, /* magento 2 */
'default_currency' => $currency,
'skip_url_validation' => 'yes',
);
if ($useDefaultConfigParams) {
if (strlen($defaults['encryption_key']) > 0) {
$argv['encryption_key'] = $defaults['encryption_key'];
}
if (strlen($defaults['use_secure']) > 0) {
$argv['use_secure'] = $defaults['use_secure'];
$argv['secure_base_url'] = str_replace('http://', 'https://', $baseUrl);
}
if (strlen($defaults['use_rewrites']) > 0) {
$argv['use_rewrites'] = $defaults['use_rewrites'];
}
}
$installArgs = '';
foreach ($argv as $argName => $argValue) {
$installArgs .= '--' . $argName . ' ' . escapeshellarg($argValue) . ' ';
}
$output->writeln('<info>Start installation process.</info>');
if (OperatingSystem::isWindows()) {
$installCommand = 'php -f ' . escapeshellarg($this->getInstallScriptPath()) . ' -- ' . $installArgs;
} else {
$installCommand = '/usr/bin/env php -f ' . escapeshellarg($this->getInstallScriptPath()) . ' -- ' . $installArgs;
}
$output->writeln('<comment>' . $installCommand . '</comment>');
Exec::run($installCommand, $installationOutput, $returnStatus);
if ($returnStatus !== self::EXEC_STATUS_OK) {
$this->getApplication()->setAutoExit(true);
throw new RuntimeException('Installation failed.' . $installationOutput, 1);
} else {
$output->writeln('<info>Successfully installed Magento</info>');
$encryptionKey = trim(substr($installationOutput, strpos($installationOutput, ':') + 1));
$output->writeln('<comment>Encryption Key:</comment> <info>' . $encryptionKey . '</info>');
}
$dialog = $this->getHelperSet()->get('dialog');
/**
* Htaccess file
*/
if ($input->getOption('useDefaultConfigParams') == null || $input->getOption('replaceHtaccessFile') != null) {
$replaceHtaccessFile = false;
if ($this->_parseBoolOption($input->getOption('replaceHtaccessFile'))) {
$replaceHtaccessFile = true;
} elseif ($dialog->askConfirmation(
$output,
'<question>Write BaseURL to .htaccess file?</question> <comment>[n]</comment>: ',
false)
) {
$replaceHtaccessFile = true;
}
if ($replaceHtaccessFile) {
$this->replaceHtaccessFile($baseUrl);
}
}
\chdir($this->config['installationFolder']);
$this->getApplication()->reinit();
$output->writeln('<info>Reindex all after installation</info>');
$this->getApplication()->run(new StringInput('index:reindex:all'), $output);
$this->getApplication()->run(new StringInput('sys:check'), $output);
$output->writeln('<info>Successfully installed magento</info>');
}
/**
* Check if we have a magento 2 or 1 installation and return path to install.php
*
* @return string
*/
protected function getInstallScriptPath()
{
$magento1InstallScriptPath = $this->config['installationFolder'] . DIRECTORY_SEPARATOR . 'install.php';
$magento2InstallScriptPath = $this->config['installationFolder'] . DIRECTORY_SEPARATOR . 'dev/shell/install.php';
if (file_exists($magento2InstallScriptPath)) {
return $magento2InstallScriptPath;
}
return $magento1InstallScriptPath;
}
/**
* @param string $baseUrl
*/
protected function replaceHtaccessFile($baseUrl)
{
$content = file_get_contents($this->config['installationFolder'] . DIRECTORY_SEPARATOR . '.htaccess');
copy($this->config['installationFolder'] . DIRECTORY_SEPARATOR . '.htaccess', $this->config['installationFolder'] . DIRECTORY_SEPARATOR . '.htaccess.dist');
$content = str_replace('#RewriteBase /magento/', 'RewriteBase ' . parse_url($baseUrl, PHP_URL_PATH), $content);
file_put_contents($this->config['installationFolder'] . DIRECTORY_SEPARATOR . '.htaccess', $content);
}
/**
* @param OutputInterface $output
*/
protected function setDirectoryPermissions($output)
{
try {
$varFolder = $this->config['installationFolder'] . DIRECTORY_SEPARATOR . 'var';
if (!is_dir($varFolder)) {
@mkdir($varFolder);
}
@chmod($varFolder, 0777);
$varCacheFolder = $this->config['installationFolder'] . DIRECTORY_SEPARATOR . 'var/cache';
if (!is_dir($varCacheFolder)) {
@mkdir($varCacheFolder);
}
@chmod($varCacheFolder, 0777);
$mediaFolder = $this->config['installationFolder'] . DIRECTORY_SEPARATOR . 'media';
if (!is_dir($mediaFolder)) {
@mkdir($mediaFolder);
}
@chmod($mediaFolder, 0777);
$finder = Finder::create();
$finder->directories()
->ignoreUnreadableDirs(true)
->in(array($varFolder, $mediaFolder));
foreach ($finder as $dir) {
@chmod($dir->getRealpath(), 0777);
}
} catch (Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
}
}
/**
* @return array
*/
public function getCliArguments()
{
if ($this->_argv === null) {
$this->_argv = $_SERVER['argv'];
}
return $this->_argv;
}
/**
* @param array $args
*/
public function setCliArguments($args)
{
$this->_argv = $args;
}
}