2013-05-15 19:27:13 +02:00
|
|
|
<?php
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2013, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link http://www.phptesting.org/
|
|
|
|
*/
|
2013-05-15 19:27:13 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Command;
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use b8\Store\Factory;
|
|
|
|
use PHPCI\Builder;
|
|
|
|
|
2013-05-16 03:30:48 +02:00
|
|
|
/**
|
|
|
|
* Install console command - Installs PHPCI.
|
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Console
|
|
|
|
*/
|
2013-05-15 19:27:13 +02:00
|
|
|
class InstallCommand extends Command
|
|
|
|
{
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('phpci:install')
|
|
|
|
->setDescription('Install PHPCI.');
|
|
|
|
}
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Installs PHPCI - Can be run more than once as long as you ^C instead of entering an email address.
|
|
|
|
*/
|
2013-05-16 21:43:17 +02:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
2013-05-15 19:27:13 +02:00
|
|
|
{
|
2013-05-16 18:17:29 +02:00
|
|
|
// Gather initial data from the user:
|
2013-05-23 13:10:01 +02:00
|
|
|
$conf = array();
|
|
|
|
$conf['b8']['database']['servers']['read'] = $this->ask('Enter your MySQL host: ');
|
|
|
|
$conf['b8']['database']['servers']['write'] = $conf['b8']['database']['servers']['read'];
|
|
|
|
$conf['b8']['database']['name'] = $this->ask('Enter the database name PHPCI should use: ');
|
|
|
|
$conf['b8']['database']['username'] = $this->ask('Enter your MySQL username: ');
|
|
|
|
$conf['b8']['database']['password'] = $this->ask('Enter your MySQL password: ', true);
|
2013-05-28 12:22:54 +02:00
|
|
|
$conf['phpci']['url'] = $this->ask('Your PHPCI URL (without trailing slash): ', false, array(FILTER_VALIDATE_URL,"/[^\/]$/i"));
|
2013-05-23 13:10:01 +02:00
|
|
|
$conf['phpci']['github']['id'] = $this->ask('(Optional) Github Application ID: ', true);
|
|
|
|
$conf['phpci']['github']['secret'] = $this->ask('(Optional) Github Application Secret: ', true);
|
|
|
|
|
|
|
|
$dbUser = $conf['b8']['database']['username'];
|
|
|
|
$dbPass = $conf['b8']['database']['password'];
|
|
|
|
$dbHost = $conf['b8']['database']['servers']['write'];
|
|
|
|
$dbName = $conf['b8']['database']['name'];
|
2013-05-15 19:27:13 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
// Create the database if it doesn't exist:
|
2013-05-16 16:14:10 +02:00
|
|
|
$cmd = 'mysql -u' . $dbUser . (!empty($dbPass) ? ' -p' . $dbPass : '') . ' -h' . $dbHost .
|
|
|
|
' -e "CREATE DATABASE IF NOT EXISTS ' . $dbName . '"';
|
|
|
|
|
2013-05-15 19:27:13 +02:00
|
|
|
shell_exec($cmd);
|
|
|
|
|
2013-05-23 13:10:01 +02:00
|
|
|
$dumper = new \Symfony\Component\Yaml\Dumper();
|
|
|
|
$yaml = $dumper->dump($conf);
|
2013-05-15 19:27:13 +02:00
|
|
|
|
2013-05-23 13:10:01 +02:00
|
|
|
file_put_contents(PHPCI_DIR . 'PHPCI/config.yml', $yaml);
|
2013-05-15 19:27:13 +02:00
|
|
|
|
|
|
|
require(PHPCI_DIR . 'bootstrap.php');
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
// Update the database:
|
2013-05-15 19:27:13 +02:00
|
|
|
$gen = new \b8\Database\Generator(\b8\Database::getConnection(), 'PHPCI', './PHPCI/Model/Base/');
|
|
|
|
$gen->generate();
|
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
// Try to create a user account:
|
2013-05-23 11:31:58 +02:00
|
|
|
$adminEmail = $this->ask('Enter your email address (leave blank if updating): ', true, FILTER_VALIDATE_EMAIL);
|
2013-05-16 18:17:29 +02:00
|
|
|
|
|
|
|
if (empty($adminEmail)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-15 19:27:13 +02:00
|
|
|
$adminPass = $this->ask('Enter your desired admin password: ');
|
|
|
|
$adminName = $this->ask('Enter your name: ');
|
|
|
|
|
2013-05-16 16:14:10 +02:00
|
|
|
try {
|
2013-05-15 19:27:13 +02:00
|
|
|
$user = new \PHPCI\Model\User();
|
|
|
|
$user->setEmail($adminEmail);
|
|
|
|
$user->setName($adminName);
|
|
|
|
$user->setIsAdmin(1);
|
|
|
|
$user->setHash(password_hash($adminPass, PASSWORD_DEFAULT));
|
|
|
|
|
|
|
|
$store = \b8\Store\Factory::getStore('User');
|
|
|
|
$store->save($user);
|
|
|
|
|
|
|
|
print 'User account created!' . PHP_EOL;
|
2013-05-16 16:14:10 +02:00
|
|
|
} catch (\Exception $ex) {
|
2013-05-15 19:27:13 +02:00
|
|
|
print 'There was a problem creating your account. :(' . PHP_EOL;
|
|
|
|
print $ex->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 11:31:58 +02:00
|
|
|
protected function ask($question, $emptyOk = false, $validationFilter = null)
|
2013-05-15 19:27:13 +02:00
|
|
|
{
|
|
|
|
print $question . ' ';
|
|
|
|
|
|
|
|
$rtn = '';
|
2013-05-16 17:46:30 +02:00
|
|
|
$stdin = fopen('php://stdin', 'r');
|
|
|
|
$rtn = fgets($stdin);
|
|
|
|
fclose($stdin);
|
2013-05-15 19:27:13 +02:00
|
|
|
|
|
|
|
$rtn = trim($rtn);
|
|
|
|
|
2013-05-16 16:14:10 +02:00
|
|
|
if (!$emptyOk && empty($rtn)) {
|
2013-05-23 11:31:58 +02:00
|
|
|
$rtn = $this->ask($question, $emptyOk, $validationFilter);
|
|
|
|
} elseif ($validationFilter != null && ! empty($rtn)) {
|
2013-05-23 12:14:42 +02:00
|
|
|
if (! $this -> controlFormat($rtn, $validationFilter, $statusMessage)) {
|
2013-05-23 11:31:58 +02:00
|
|
|
print $statusMessage;
|
|
|
|
$rtn = $this->ask($question, $emptyOk, $validationFilter);
|
|
|
|
}
|
2013-05-15 19:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
2013-05-21 17:57:24 +02:00
|
|
|
protected function controlFormat($valueToInspect,$filter,&$statusMessage)
|
|
|
|
{
|
|
|
|
$filters = !(is_array($filter))? array($filter) : $filter;
|
|
|
|
$statusMessage = '';
|
|
|
|
$status = true;
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
foreach ($filters as $filter) {
|
|
|
|
if (! is_int($filter)) {
|
|
|
|
$regexp = $filter;
|
|
|
|
$filter = FILTER_VALIDATE_REGEXP;
|
|
|
|
$options = array(
|
|
|
|
'options' => array(
|
|
|
|
'regexp' => $regexp,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2013-05-22 09:58:04 +02:00
|
|
|
if (! filter_var($valueToInspect, $filter, $options)) {
|
2013-05-21 17:57:24 +02:00
|
|
|
$status = false;
|
2013-05-23 12:14:42 +02:00
|
|
|
|
2013-05-21 17:57:24 +02:00
|
|
|
switch ($filter)
|
|
|
|
{
|
|
|
|
case FILTER_VALIDATE_URL :
|
|
|
|
$statusMessage = 'Incorrect url format.' . PHP_EOL;
|
|
|
|
break;
|
|
|
|
case FILTER_VALIDATE_EMAIL :
|
|
|
|
$statusMessage = 'Incorrect e-mail format.' . PHP_EOL;
|
|
|
|
break;
|
|
|
|
case FILTER_VALIDATE_REGEXP :
|
|
|
|
$statusMessage = 'Incorrect format.' . PHP_EOL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-23 12:14:42 +02:00
|
|
|
|
2013-05-21 17:57:24 +02:00
|
|
|
return $status;
|
|
|
|
}
|
2013-05-16 16:14:10 +02:00
|
|
|
}
|