/
forest-lynx
/
project
Обзор
Документация
Войти
/
forest-lynx
/
project
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/Command/ProjectInitCommand.php
83 строки
3 KB
Дмитрий
update src/Command/ProjectInitCommand.php
04 дек 2024, 11:06
04 дек 2024, 11:06
1501662
Код
Авторство
О чём код?
<?php namespace App\Command; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressIndicator; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Process\Process; #[AsCommand( name: 'project:init', description: 'Initialize project', )] class ProjectInitCommand extends Command { protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $io->note('Using MySQL database for this project'); $dbHost = $io->ask('Database host', '127.0.0.1'); $dbPort = $io->ask('Database port', '3306'); $dbName = $io->ask('Database name'); $dbUser = $io->ask('Database user'); $dbPassword = $io->askHidden('Database password'); $dbVersion = $io->ask('Database version', '8.4.0'); $envContent = sprintf( 'DATABASE_URL="mysql://%s:%s@%s:%s/%s?serverVersion=%s&charset=utf8mb4"', $dbUser, $dbPassword, $dbHost, $dbPort, $dbName, $dbVersion ); try { $connection = new \PDO( sprintf('mysql:host=%s;port=%s', $dbHost, $dbPort), $dbUser, $dbPassword ); $io->info('Database connection successful'); } catch (\PDOException $e) { $io->error('Database connection failed: ' . $e->getMessage()); return Command::FAILURE; } file_put_contents('.env.local', $envContent); $commands = [ ['php', 'bin/console', 'doctrine:database:create', '--if-not-exists'], ['php', 'bin/console', 'doctrine:migrations:migrate', '--no-interaction'], ['php', 'bin/console', 'doctrine:fixtures:load', '--no-interaction'] ]; $indicator = new ProgressIndicator($output); $indicator->start('Running commands...'); foreach ($commands as $command) { $process = new Process($command); $process->run(); if (!$process->isSuccessful()) { $indicator->finish('Failed'); $io->error($process->getErrorOutput()); return Command::FAILURE; } $indicator->advance(); } $indicator->finish('Done!'); $io->success('Project initialized successfully'); return Command::SUCCESS; } }