Inital commit
This commit is contained in:
@@ -12,20 +12,23 @@
|
||||
|
||||
namespace Thelia\Command;
|
||||
|
||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\Install\CheckPermission;
|
||||
use Thelia\Install\Database;
|
||||
use Thelia\Tools\TokenProvider;
|
||||
|
||||
/**
|
||||
* try to install a new instance of Thelia
|
||||
*
|
||||
* Class Install
|
||||
* @package Thelia\Command
|
||||
* @author Manuel Raynaud <mraynaud@openstudio.fr>
|
||||
* @author Manuel Raynaud <manu@raynaud.io>
|
||||
*/
|
||||
class Install extends ContainerAwareCommand
|
||||
{
|
||||
@@ -42,7 +45,8 @@ class Install extends ContainerAwareCommand
|
||||
"db_host",
|
||||
null,
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
"host for your database"
|
||||
"host for your database",
|
||||
"localhost"
|
||||
)
|
||||
->addOption(
|
||||
"db_username",
|
||||
@@ -62,8 +66,14 @@ class Install extends ContainerAwareCommand
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
"database name"
|
||||
)
|
||||
->addOption(
|
||||
"db_port",
|
||||
null,
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
"database port",
|
||||
"3306"
|
||||
)
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
@@ -84,11 +94,12 @@ class Install extends ContainerAwareCommand
|
||||
"host" => $input->getOption("db_host"),
|
||||
"dbName" => $input->getOption("db_name"),
|
||||
"username" => $input->getOption("db_username"),
|
||||
"password" => $input->getOption("db_password")
|
||||
"password" => $input->getOption("db_password"),
|
||||
"port" => $input->getOption("db_port")
|
||||
);
|
||||
|
||||
while (false === $connection = $this->tryConnection($connectionInfo, $output)) {
|
||||
$connectionInfo = $this->getConnectionInfo($input, $output);
|
||||
$connectionInfo = $this->getConnectionInfo($input, $output);
|
||||
}
|
||||
|
||||
$database = new Database($connection);
|
||||
@@ -101,6 +112,7 @@ class Install extends ContainerAwareCommand
|
||||
""
|
||||
));
|
||||
$database->insertSql($connectionInfo["dbName"]);
|
||||
$this->manageSecret($database);
|
||||
|
||||
$output->writeln(array(
|
||||
"",
|
||||
@@ -118,6 +130,13 @@ class Install extends ContainerAwareCommand
|
||||
));
|
||||
}
|
||||
|
||||
protected function manageSecret(Database $database)
|
||||
{
|
||||
$secret = TokenProvider::generateToken();
|
||||
$sql = "UPDATE `config` SET `value`=? WHERE `name`='form.secret'";
|
||||
$database->execute($sql, [$secret]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if needed directories have write permission
|
||||
*
|
||||
@@ -129,26 +148,32 @@ class Install extends ContainerAwareCommand
|
||||
"Checking some permissions"
|
||||
));
|
||||
|
||||
$permissions = new CheckPermission(false, $this->getContainer()->get('thelia.translator'));
|
||||
/** @var Translator $translator */
|
||||
$translator = $this->getContainer()->get('thelia.translator');
|
||||
|
||||
$permissions = new CheckPermission(false, $translator);
|
||||
$isValid = $permissions->exec();
|
||||
|
||||
foreach ($permissions->getValidationMessages() as $item => $data) {
|
||||
if ($data['status']) {
|
||||
$output->writeln(array(
|
||||
sprintf("<info>%s ...</info> %s",
|
||||
$data['text'],
|
||||
"<info>Ok</info>")
|
||||
$output->writeln(
|
||||
array(
|
||||
sprintf(
|
||||
"<info>%s ...</info> %s",
|
||||
$data['text'],
|
||||
"<info>Ok</info>"
|
||||
)
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$output->writeln(array(
|
||||
sprintf("<error>%s </error>%s",
|
||||
sprintf(
|
||||
"<error>%s </error>%s",
|
||||
$data['text'],
|
||||
sprintf("<error>%s</error>", $data["hint"])
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (false === $isValid) {
|
||||
@@ -177,14 +202,13 @@ class Install extends ContainerAwareCommand
|
||||
$configContent = str_replace("%PASSWORD%", $connectionInfo["password"], $configContent);
|
||||
$configContent = str_replace(
|
||||
"%DSN%",
|
||||
sprintf("mysql:host=%s;dbname=%s", $connectionInfo["host"], $connectionInfo["dbName"]),
|
||||
sprintf("mysql:host=%s;dbname=%s;port=%s", $connectionInfo["host"], $connectionInfo["dbName"], $connectionInfo['port']),
|
||||
$configContent
|
||||
);
|
||||
|
||||
file_put_contents($configFile, $configContent);
|
||||
|
||||
$fs->remove($this->getContainer()->getParameter("kernel.cache_dir"));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -196,16 +220,15 @@ class Install extends ContainerAwareCommand
|
||||
*/
|
||||
protected function tryConnection($connectionInfo, OutputInterface $output)
|
||||
{
|
||||
|
||||
if (is_null($connectionInfo["dbName"])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$dsn = "mysql:host=%s";
|
||||
$dsn = "mysql:host=%s;port=%s";
|
||||
|
||||
try {
|
||||
$connection = new \PDO(
|
||||
sprintf($dsn, $connectionInfo["host"]),
|
||||
sprintf($dsn, $connectionInfo["host"], $connectionInfo["port"]),
|
||||
$connectionInfo["username"],
|
||||
$connectionInfo["password"]
|
||||
);
|
||||
@@ -230,62 +253,93 @@ class Install extends ContainerAwareCommand
|
||||
*/
|
||||
protected function getConnectionInfo(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$dialog = $this->getHelperSet()->get('dialog');
|
||||
/** @var QuestionHelper $helper */
|
||||
$helper = $this->getHelper('question');
|
||||
|
||||
$connectionInfo = array();
|
||||
|
||||
$connectionInfo["host"] = $dialog->askAndValidate(
|
||||
$connectionInfo['host'] = $this->enterData(
|
||||
$helper,
|
||||
$input,
|
||||
$output,
|
||||
$this->decorateInfo("Database host : "),
|
||||
function ($answer) {
|
||||
$answer = trim($answer);
|
||||
if (is_null($answer)) {
|
||||
throw new \RuntimeException("You must specify a database host");
|
||||
}
|
||||
|
||||
return $answer;
|
||||
}
|
||||
"Database host [default: localhost] : ",
|
||||
"You must specify a database host",
|
||||
false,
|
||||
"localhost"
|
||||
);
|
||||
|
||||
$connectionInfo["dbName"] = $dialog->askAndValidate(
|
||||
$connectionInfo['port'] = $this->enterData(
|
||||
$helper,
|
||||
$input,
|
||||
$output,
|
||||
$this->decorateInfo("Database name (if database does not exist, Thelia will try to create it) : "),
|
||||
function ($answer) {
|
||||
$answer = trim($answer);
|
||||
|
||||
if (is_null($answer)) {
|
||||
throw new \RuntimeException("You must specify a database name");
|
||||
}
|
||||
|
||||
return $answer;
|
||||
}
|
||||
"Database port [default: 3306] : ",
|
||||
"You must specify a database port",
|
||||
false,
|
||||
"3306"
|
||||
);
|
||||
|
||||
$connectionInfo["username"] = $dialog->askAndValidate(
|
||||
$connectionInfo['dbName'] = $this->enterData(
|
||||
$helper,
|
||||
$input,
|
||||
$output,
|
||||
$this->decorateInfo("Database username : "),
|
||||
function ($answer) {
|
||||
$answer = trim($answer);
|
||||
|
||||
if (is_null($answer)) {
|
||||
throw new \RuntimeException("You must specify a database username");
|
||||
}
|
||||
|
||||
return $answer;
|
||||
}
|
||||
"Database name (if database does not exist, Thelia will try to create it) : ",
|
||||
"You must specify a database name"
|
||||
);
|
||||
|
||||
$connectionInfo["password"] = $dialog->askHiddenResponse(
|
||||
$connectionInfo['username'] = $this->enterData(
|
||||
$helper,
|
||||
$input,
|
||||
$output,
|
||||
$this->decorateInfo("Database password : ")
|
||||
"Database username : ",
|
||||
"You must specify a database username"
|
||||
);
|
||||
|
||||
$connectionInfo['password'] = $this->enterData(
|
||||
$helper,
|
||||
$input,
|
||||
$output,
|
||||
"Database password : ",
|
||||
"You must specify a database username",
|
||||
true,
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
return $connectionInfo;
|
||||
}
|
||||
|
||||
protected function enterData(
|
||||
QuestionHelper $helper,
|
||||
InputInterface $input,
|
||||
OutputInterface $output,
|
||||
$label,
|
||||
$errorMessage,
|
||||
$hidden = false,
|
||||
$defaultValue = null,
|
||||
$beEmpty = false
|
||||
) {
|
||||
$question = new Question($label, $defaultValue);
|
||||
|
||||
if ($hidden) {
|
||||
$question->setHidden(true);
|
||||
$question->setHiddenFallback(false);
|
||||
}
|
||||
|
||||
$question->setValidator(function ($value) use (&$errorMessage, &$beEmpty) {
|
||||
if (trim($value) == '') {
|
||||
if (is_null($value) && !$beEmpty) {
|
||||
throw new \Exception($errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
});
|
||||
|
||||
return $helper->ask($input, $output, $question);
|
||||
}
|
||||
|
||||
protected function decorateInfo($text)
|
||||
{
|
||||
return sprintf("<info>%s</info>", $text);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user