Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 80 |
InitCommand | |
0.00% |
0 / 1 |
|
0.00% |
0 / 11 |
306 | |
0.00% |
0 / 80 |
configure | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 4 |
|||
execute | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 10 |
|||
generateConfiguration | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 7 |
|||
askConfigTargetPath | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 7 |
|||
continueWithInvalidData | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 4 |
|||
askConnectionDetails | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 12 |
|||
isConnectionValid | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 13 |
|||
askQuestion | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 4 |
|||
askIntegerQuestion | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 5 |
|||
getQuestionHelper | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
compileDirectoryList | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 11 |
<?php | |
namespace Siesta\Console; | |
use Siesta\Config\Config; | |
use Siesta\Database\ConnectionData; | |
use Siesta\Database\ConnectionFactory; | |
use Siesta\Database\Exception\ConnectException; | |
use Siesta\Driver\MySQL\MySQLDriver; | |
use Siesta\Exception\InvalidConfigurationException; | |
use Siesta\Util\File; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Helper\QuestionHelper; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Question\ConfirmationQuestion; | |
use Symfony\Component\Console\Question\Question; | |
/** | |
* @author Gregor Müller | |
*/ | |
class InitCommand extends Command | |
{ | |
/** | |
* @var InputInterface | |
*/ | |
protected $input; | |
/** | |
* @var OutputInterface | |
*/ | |
protected $output; | |
/** | |
* @var QuestionHelper | |
*/ | |
protected $questionHelper; | |
/** | |
* | |
*/ | |
protected function configure() | |
{ | |
$this->setName('init'); | |
$this->setDescription('Creates a config file for siesta'); | |
} | |
/** | |
* @param InputInterface $input | |
* @param OutputInterface $output | |
* | |
* @return void | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$this->input = $input; | |
$this->output = $output; | |
$this->questionHelper = $this->getQuestionHelper(); | |
do { | |
$cd = $this->askConnectionDetails(); | |
} while (!$this->isConnectionValid($cd) and !$this->continueWithInvalidData()); | |
$targetPath = $this->askConfigTargetPath(); | |
$this->generateConfiguration($cd, $targetPath); | |
} | |
/** | |
* @param ConnectionData $connectionData | |
* @param string $targetPath | |
*/ | |
private function generateConfiguration(ConnectionData $connectionData, $targetPath) | |
{ | |
$file = new File($targetPath . '/' . Config::CONFIG_FILE_NAME); | |
$file->createDirForFile(); | |
$configuration = Config::buildConfiguration($connectionData); | |
$file->putContents(json_encode($configuration, JSON_PRETTY_PRINT)); | |
$this->output->writeln("Config file generated in " . $targetPath); | |
} | |
/** | |
* @return string | |
*/ | |
private function askConfigTargetPath() | |
{ | |
$directoryList = []; | |
$this->compileDirectoryList($directoryList, getcwd()); | |
$question = new Question('<question>Please enter target path for configuration</question> ', ''); | |
$question->setAutocompleterValues($directoryList); | |
return $this->questionHelper->ask($this->input, $this->output, $question); | |
} | |
/** | |
* @return bool | |
*/ | |
private function continueWithInvalidData() | |
{ | |
$question = new ConfirmationQuestion('<question>Connection not possible, use connection data anyway? (y/n)</question>', false); | |
return $this->questionHelper->ask($this->input, $this->output, $question); | |
} | |
/** | |
* @return ConnectionData | |
*/ | |
private function askConnectionDetails() | |
{ | |
$cd = new ConnectionData(); | |
$cd->name = $this->askQuestion("Please enter connection name", "default"); | |
$cd->host = $this->askQuestion("Please enter host", "127.0.0.1"); | |
$cd->port = $this->askIntegerQuestion("Please enter port", 3306); | |
$cd->driver = $this->askQuestion("Please enter driver", MySQLDriver::DRIVER_CLASS); | |
$cd->database = $this->askQuestion("Please enter database name", "test"); | |
$cd->user = $this->askQuestion("Please enter user", "root"); | |
$cd->password = $this->askQuestion("Please enter password", ""); | |
$cd->charSet = $this->askQuestion("Please enter charset", "utf8"); | |
return $cd; | |
} | |
/** | |
* @param ConnectionData $cd | |
* | |
* @return bool | |
*/ | |
private function isConnectionValid(ConnectionData $cd) : bool | |
{ | |
try { | |
ConnectionFactory::addConnection($cd); | |
$this->output->writeln("Connection successful"); | |
return true; | |
} catch (InvalidConfigurationException $e) { | |
$this->output->writeln("Error " . $e->getMessage()); | |
return false; | |
} catch (ConnectException $e) { | |
$this->output->writeln("Error " . $e->getMessage()); | |
return false; | |
} | |
} | |
/** | |
* @param $question | |
* @param $default | |
* | |
* @return string | |
*/ | |
private function askQuestion(string $question, string $default) : string | |
{ | |
$q = new Question("<question>$question ($default)</question> ", $default); | |
return $this->questionHelper->ask($this->input, $this->output, $q); | |
} | |
/** | |
* @param $question | |
* @param $default | |
* | |
* @return int | |
*/ | |
private function askIntegerQuestion(string $question, int $default) : int | |
{ | |
$q = new Question("<question>$question ($default)</question> ", $default); | |
$stringValue = $this->questionHelper->ask($this->input, $this->output, $q); | |
return (int)$stringValue; | |
} | |
/** | |
* @return QuestionHelper | |
*/ | |
private function getQuestionHelper() | |
{ | |
return $this->getHelper('question'); | |
} | |
/** | |
* @param array $directoryList | |
* @param string $currentDirectory | |
*/ | |
private function compileDirectoryList(&$directoryList, $currentDirectory) | |
{ | |
$dir = new File($currentDirectory); | |
$fileList = $dir->scanDir(); | |
foreach ($fileList as $file) { | |
if (!$file->isDir()) { | |
continue; | |
} | |
$directoryList[] = str_replace(getcwd() . "/", "", $file->getAbsoluteFileName()); | |
$this->compileDirectoryList($directoryList, $file->getAbsoluteFileName()); | |
} | |
} | |
} |