From 785b45053771ae8427460bea9c52bc3371fc4fe7 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 5 Jun 2013 14:23:05 +0200 Subject: [PATCH] create new command allowing to install thelia using cli tools --- .gitignore | 1 + core/lib/Thelia/Command/Install.php | 236 ++++++++++++++++++ core/lib/Thelia/Config/Resources/config.xml | 1 + .../Tests/Config/Loader/XmlFileLoaderTest.php | 30 --- local/config/database.yml.sample | 8 +- 5 files changed, 242 insertions(+), 34 deletions(-) create mode 100644 core/lib/Thelia/Command/Install.php delete mode 100644 core/lib/Thelia/Tests/Config/Loader/XmlFileLoaderTest.php diff --git a/.gitignore b/.gitignore index 0a0909bf2..d241dc853 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ local/config/build.properties local/config/config_db.php local/config/build local/config/database.yml +local/config/database.yml.sample.save core/vendor local/config/runtime-conf.xml cache/* diff --git a/core/lib/Thelia/Command/Install.php b/core/lib/Thelia/Command/Install.php new file mode 100644 index 000000000..0e8694b50 --- /dev/null +++ b/core/lib/Thelia/Command/Install.php @@ -0,0 +1,236 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Command; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Filesystem\Filesystem; +use Thelia\Command\ContainerAwareCommand; +use Thelia\Core\Event\TheliaEvents; + + +class Install extends ContainerAwareCommand +{ + protected function configure() + { + $this + ->setName("thelia:install") + ->setDescription("Install thelia using cli tools. For now Thelia only use mysql database") + ; + + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $output->writeln(array( + '', + 'Welcome to Thelia install process', + 'You need information about your database configuration (host, username, password, database name, etc)', + '' + )); + + $connectionInfo = $this->getConnectionInfo($input, $output); + + if (false === $connection = $this->tryConnection($connectionInfo)) { + throw new \RuntimeException("Wrong information, impossible to connect to database"); + } + + $this->createDatabase($connection, $connectionInfo["dbName"]); + + $output->writeln(array( + "", + "Creating Thelia database, please wait", + "" + )); + $this->insertSql($connection, $connectionInfo["dbName"]); + + $output->writeln(array( + "", + "Database created without errors", + "Creating file configuration, please wait", + "" + )); + + $this->createConfigFile($connectionInfo); + + $output->writeln(array( + "", + "Config file created with success. Your thelia is installed", + "" + )); + } + + protected function createConfigFile($connectionInfo) + { + $fs = new Filesystem(); + + $sampleConfigFile = THELIA_ROOT . "/local/config/database.yml.sample"; + $configFile = THELIA_ROOT . "/local/config/database.yml"; + + + $fs->copy($sampleConfigFile, $configFile, true); + + $configContent = file_get_contents($configFile); + + $configContent = str_replace("%DRIVER%", "mysql", $configContent); + $configContent = str_replace("%USERNAME%", $connectionInfo["username"], $configContent); + $configContent = str_replace("%PASSWORD%", $connectionInfo["password"], $configContent); + $configContent = str_replace( + "%DSN%", + sprintf("mysql:host=%s;dbname=%s", $connectionInfo["host"], $connectionInfo["dbName"]), + $configContent + ); + + file_put_contents($configFile, $configContent); + + $fs->remove($sampleConfigFile); + + + + + } + + protected function insertSql(\PDO $connection, $dbName) + { + $connection->query(sprintf("use %s", $dbName)); + $sql = array(); + $sql = array_merge( + $sql, + $this->prepareSql(file_get_contents(THELIA_ROOT . "/install/thelia.sql")), + $this->prepareSql(file_get_contents(THELIA_ROOT . "/install/insert.sql")) + ); + + for ($i = 0; $i < count($sql); $i ++) { + $connection->query($sql[$i]); + } + } + + protected function prepareSql($sql) + { + $sql = str_replace(";',", "-CODE-", $sql); + $query = array(); + + $tab = explode(";", $sql); + + for($i=0; $iquery( + sprintf( + "CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8", + $dbName + ) + ); + } + + protected function tryConnection($connectionInfo) + { + + $dsn = "mysql:host=%s"; + + try { + $connection = new \PDO( + sprintf($dsn, $connectionInfo["host"]), + $connectionInfo["username"], + $connectionInfo["password"] + ); + } catch (\PDOException $e) { + return false; + } + + + + return $connection; + } + + protected function getConnectionInfo(InputInterface $input, OutputInterface $output) + { + $dialog = $this->getHelperSet()->get('dialog'); + + $connectionInfo = array(); + + $connectionInfo["host"] = $dialog->askAndValidate( + $output, + $this->decorateInfo("Database host : "), + function ($answer) { + $answer = trim($answer); + if (is_null($answer)) { + throw new \RuntimeException("You must specify database host"); + } + + return $answer; + } + ); + + $connectionInfo["dbName"] = $dialog->askAndValidate( + $output, + $this->decorateInfo("Database Name (if database does not exists, Thelia will try to create it) : "), + function ($answer) { + $answer = trim($answer); + + if (is_null($answer)) { + throw new \RuntimeException("You must specify database name"); + } + + return $answer; + } + ); + + $connectionInfo["username"] = $dialog->askAndValidate( + $output, + $this->decorateInfo("Databse username : "), + function ($answer) { + $answer = trim($answer); + + if (is_null($answer)) { + throw new \RuntimeException("You must sprcify database username"); + } + + return $answer; + } + ); + + $connectionInfo["password"] = $dialog->askHiddenResponse( + $output, + $this->decorateInfo("Database password : ") + ); + + return $connectionInfo; + } + + protected function decorateInfo($text) + { + return sprintf("%s", $text); + } + +} \ No newline at end of file diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index e9210368d..46f3aa391 100644 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -14,6 +14,7 @@ + diff --git a/core/lib/Thelia/Tests/Config/Loader/XmlFileLoaderTest.php b/core/lib/Thelia/Tests/Config/Loader/XmlFileLoaderTest.php deleted file mode 100644 index d75adc8ce..000000000 --- a/core/lib/Thelia/Tests/Config/Loader/XmlFileLoaderTest.php +++ /dev/null @@ -1,30 +0,0 @@ -. */ -/* */ -/*************************************************************************************/ - -namespace Thelia\Tests\Config\Loader; - - -class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase -{ - -} \ No newline at end of file diff --git a/local/config/database.yml.sample b/local/config/database.yml.sample index 1f9263531..3bb829965 100644 --- a/local/config/database.yml.sample +++ b/local/config/database.yml.sample @@ -1,6 +1,6 @@ database: connection: - driver: mysql - user: root - password: - dsn: mysql:dbname=thelia;host:localhost + driver: %DRIVER% + user: %USERNAME% + password: %PASSWORD% + dsn: %DSN%