1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Cart\Commands; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
8
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
class MigrateLaravelCommand extends Command |
13
|
|
|
{ |
14
|
|
|
protected $listFileMigrations = [ |
15
|
|
|
'cart_items' => '_cart_items_table', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
protected $formatterStyle; |
19
|
|
|
protected $progressBar; |
20
|
|
|
|
21
|
|
|
public function __construct(OutputFormatterStyle $formatterStyle, ProgressBar $bar) |
22
|
|
|
{ |
23
|
|
|
parent::__construct(); |
24
|
|
|
$this->formatterStyle = $formatterStyle; |
25
|
|
|
$this->progressBar = $bar; |
26
|
|
|
$this->progressBar |
27
|
|
|
->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function configure() : void |
31
|
|
|
{ |
32
|
|
|
$this->setName('migrate:laravel')->setHelp('to migrate files to Laravel'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
$output->getFormatter()->setStyle('fire', $this->formatterStyle); |
38
|
|
|
$output->writeln([ |
39
|
|
|
'<fire>There is a migration in the project Laravel</fire>', |
40
|
|
|
'' |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
$this->progressBar->start(); |
44
|
|
|
$this->moveMigrate($this->progressBar); |
45
|
|
|
$this->moveConfig($this->progressBar); |
46
|
|
|
$this->progressBar->finish(); |
47
|
|
|
$output->writeln(['']); |
48
|
|
|
$output->writeln(['<info>All successfully copied!</info>']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function moveMigrate(ProgressBar $progress) |
52
|
|
|
{ |
53
|
|
|
$pathToMigrationsLaravel = $_SERVER["PWD"] . '/database/migrations/'; |
54
|
|
|
$pathToStubs = __DIR__ . '/../../migrations/stubs/'; |
55
|
|
|
|
56
|
|
|
$this->createDir($pathToMigrationsLaravel); |
57
|
|
|
|
58
|
|
|
foreach ($this->listFileMigrations as $name => $migrate) { |
59
|
|
|
$fileName = $pathToMigrationsLaravel . $this->getDateNormalize() . $migrate . '.php'; |
60
|
|
|
file_put_contents($fileName, $this->getContent($pathToStubs . $name)); |
|
|
|
|
61
|
|
|
$progress->advance(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function moveConfig(ProgressBar $progress) : void |
66
|
|
|
{ |
67
|
|
|
$pathToConfig = __DIR__ . '/../../config/app.php'; |
68
|
|
|
$pathToConfigsLaravel = $_SERVER["PWD"] . '/config/'; |
69
|
|
|
$this->createDir($pathToConfigsLaravel); |
70
|
|
|
|
71
|
|
|
copy($pathToConfig, $pathToConfigsLaravel . 'cart.php'); |
72
|
|
|
$progress->advance(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Just create directory |
77
|
|
|
* @param $dir |
78
|
|
|
*/ |
79
|
|
|
protected function createDir(string $dir) : void |
80
|
|
|
{ |
81
|
|
|
if (is_dir($dir) === false) { |
82
|
|
|
mkdir($dir, 0775, true); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
/** |
86
|
|
|
* Data from stubs file |
87
|
|
|
* @param $nameFile |
88
|
|
|
* @return bool|string |
89
|
|
|
*/ |
90
|
|
|
protected function getContent(string $nameFile) : string |
91
|
|
|
{ |
92
|
|
|
return file_get_contents($nameFile); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get date normalize mow |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
protected function getDateNormalize() : string |
100
|
|
|
{ |
101
|
|
|
$date = Carbon::now(); |
102
|
|
|
$date = preg_replace('/-|\s/', '_', $date); |
103
|
|
|
$data = preg_replace('/:/', '', $date); |
104
|
|
|
return $data; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
$fileName
can contain request data and is used in file manipulation context(s) leading to a potential security vulnerability.1 path for user data to reach this point
$_SERVER,
and$_SERVER['PWD'] . '/database/migrations/'
is assigned to$pathToMigrationsLaravel
in src/Commands/MigrateLaravelCommand.php on line 53
$pathToMigrationsLaravel . $this->getDateNormalize() . $migrate . '.php'
is assigned to$fileName
in src/Commands/MigrateLaravelCommand.php on line 59
General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
For numeric data, we recommend to explicitly cast the data: