Updating PHPCI to use Symfony/Console rather than a series of individual PHP files.

This commit is contained in:
Dan Cryer 2013-05-15 18:27:13 +01:00
parent 505b0c0563
commit 988fa42d92
9 changed files with 231 additions and 132 deletions

View file

@ -17,11 +17,17 @@ class Builder
protected $verbose = false;
protected $plugins = array();
protected $build;
protected $logCallback;
public function __construct(Build $build)
public function __construct(Build $build, $logCallback = null)
{
$this->build = $build;
$this->store = Store\Factory::getStore('Build');
if(!is_null($logCallback) && is_callable($logCallback))
{
$this->logCallback = $logCallback;
}
}
public function execute()
@ -87,24 +93,32 @@ class Builder
protected function log($message, $prefix = '')
{
if(is_array($message))
{
$message = array_map(function($item) use ($prefix)
$cb = $this->logCallback;
$message = array_map(function($item) use ($cb, $prefix)
{
return $prefix . $item;
}, $message);
if(is_callable($cb))
{
$cb($prefix . $item);
}
$message = implode(PHP_EOL, $message);
$this->log .= $message;
print $message . PHP_EOL;
$this->log .= $prefix . $item . PHP_EOL;
}, $message);
}
else
{
$message = $prefix . $message . PHP_EOL;
$message = $prefix . $message;
$this->log .= $message;
print $message;
$this->log .= $message . PHP_EOL;
if(isset($this->logCallback) && is_callable($this->logCallback))
{
$cb = $this->logCallback;
$cb($message);
}
}
$this->build->setLog($this->log);

View file

@ -0,0 +1,26 @@
<?php
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;
class GenerateCommand extends Command
{
protected function configure()
{
$this
->setName('phpci:generate')
->setDescription('Generate models and stores from the database.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$gen = new \b8\Database\CodeGenerator(\b8\Database::getConnection(), 'PHPCI', PHPCI_DIR . '/PHPCI/');
$gen->generateModels();
$gen->generateStores();
}
}

View file

@ -0,0 +1,114 @@
<?php
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;
class InstallCommand extends Command
{
protected function configure()
{
$this
->setName('phpci:install')
->setDescription('Install PHPCI.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$dbHost = $this->ask('Enter your MySQL host: ');
$dbName = $this->ask('Enter the database name PHPCI should use: ');
$dbUser = $this->ask('Enter your MySQL username: ');
$dbPass = $this->ask('Enter your MySQL password: ', true);
$ciUrl = $this->ask('Your PHPCI URL (without trailing slash): ', true);
$ghId = $this->ask('(Optional) Github Application ID: ', true);
$ghSecret = $this->ask('(Optional) Github Application Secret: ', true);
$cmd = 'mysql -u' . $dbUser . (!empty($dbPass) ? ' -p' . $dbPass : '') . ' -h' . $dbHost . ' -e "CREATE DATABASE IF NOT EXISTS ' . $dbName . '"';
shell_exec($cmd);
$str = "<?php
define('PHPCI_DB_HOST', '{$dbHost}');
b8\Database::setDetails('{$dbName}', '{$dbUser}', '{$dbPass}');
b8\Database::setWriteServers(array('{$dbHost}'));
b8\Database::setReadServers(array('{$dbHost}'));
\$registry = b8\Registry::getInstance();
\$registry->set('install_url', '{$ciUrl}');
";
if(!empty($ghId) && !empty($ghSecret))
{
$str .= PHP_EOL . "\$registry->set('github_app', array('id' => '{$ghId}', 'secret' => '{$ghSecret}'));" . PHP_EOL;
}
file_put_contents(PHPCI_DIR . 'config.php', $str);
if(!file_exists(PHPCI_DIR . 'composer.phar'))
{
print 'INSTALLING: Composer' . PHP_EOL;
file_put_contents(PHPCI_DIR . 'composerinstaller.php', file_get_contents('https://getcomposer.org/installer'));
shell_exec('php ' . PHPCI_DIR . 'composerinstaller.php');
unlink(PHPCI_DIR . 'composerinstaller.php');
}
print 'RUNNING: Composer' . PHP_EOL;
shell_exec('php '.PHPCI_DIR.'composer.phar install');
require(PHPCI_DIR . 'bootstrap.php');
$gen = new \b8\Database\Generator(\b8\Database::getConnection(), 'PHPCI', './PHPCI/Model/Base/');
$gen->generate();
$adminEmail = $this->ask('Enter your email address: ');
$adminPass = $this->ask('Enter your desired admin password: ');
$adminName = $this->ask('Enter your name: ');
try
{
$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;
}
catch(\Exception $ex)
{
print 'There was a problem creating your account. :(' . PHP_EOL;
print $ex->getMessage();
}
}
function ask($question, $emptyOk = false)
{
print $question . ' ';
$rtn = '';
$fp = fopen('php://stdin', 'r');
$rtn = fgets($fp);
fclose($fp);
$rtn = trim($rtn);
if(!$emptyOk && empty($rtn))
{
$rtn = $this->ask($question, $emptyOk);
}
return $rtn;
}
}

View file

@ -0,0 +1,46 @@
<?php
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;
class RunCommand extends Command
{
protected function configure()
{
$this
->setName('phpci:run-builds')
->setDescription('Run all pending PHPCI builds.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$store = Factory::getStore('Build');
$result = $store->getByStatus(0);
foreach($result['items'] as $build)
{
if ($input->getOption('verbose')) {
$builder = new Builder($build, array($this, 'logCallback'));
}
else {
$builder = new Builder($build);
}
$builder->execute();
}
}
public function logCallback($log)
{
$this->output->writeln($log);
}
}

View file

@ -1,5 +1,7 @@
<?php
date_default_timezone_set(@date_default_timezone_get());
spl_autoload_register(function ($class)
{
$file = str_replace(array('\\', '_'), '/', $class);
@ -21,7 +23,7 @@ spl_autoload_register(function ($class)
define('APPLICATION_PATH', dirname(__FILE__) . '/');
require_once('vendor/autoload.php');
require_once('config.php');
require('config.php');
b8\Registry::getInstance()->set('app_namespace', 'PHPCI');
b8\Registry::getInstance()->set('DefaultController', 'Index');

17
console Executable file
View file

@ -0,0 +1,17 @@
#!/usr/bin/env php
<?php
require_once('bootstrap.php');
use PHPCI\Command\RunCommand;
use PHPCI\Command\GenerateCommand;
use PHPCI\Command\InstallCommand;
use Symfony\Component\Console\Application;
define('PHPCI_BIN_DIR', dirname(__FILE__) . '/vendor/bin/');
define('PHPCI_DIR', dirname(__FILE__) . '/');
$application = new Application();
$application->add(new RunCommand);
$application->add(new InstallCommand);
$application->add(new GenerateCommand);
$application->run();

View file

@ -1,18 +0,0 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
require_once('bootstrap.php');
define('PHPCI_BIN_DIR', dirname(__FILE__) . '/vendor/bin/');
define('PHPCI_DIR', dirname(__FILE__) . '/');
$store = b8\Store\Factory::getStore('Build');
$result = $store->getByStatus(0);
foreach($result['items'] as $build)
{
$builder = new PHPCI\Builder($build);
$builder->execute();
}

View file

@ -1,7 +0,0 @@
<?php
require_once('bootstrap.php');
$gen = new b8\Database\CodeGenerator(b8\Database::getConnection(), 'PHPCI', './PHPCI/');
$gen->generateModels();
$gen->generateStores();

View file

@ -1,95 +0,0 @@
<?php
ini_set('display_errors', 'on');
error_reporting(E_ALL);
$dbHost = ask('Enter your MySQL host: ');
$dbName = ask('Enter the database name PHPCI should use: ');
$dbUser = ask('Enter your MySQL username: ');
$dbPass = ask('Enter your MySQL password: ', true);
$ciUrl = ask('Your PHPCI URL (without trailing slash): ', true);
$ghId = ask('(Optional) Github Application ID: ', true);
$ghSecret = ask('(Optional) Github Application Secret: ', true);
$cmd = 'mysql -u' . $dbUser . (!empty($dbPass) ? ' -p' . $dbPass : '') . ' -h' . $dbHost . ' -e "CREATE DATABASE IF NOT EXISTS ' . $dbName . '"';
shell_exec($cmd);
$str = "<?php
define('PHPCI_DB_HOST', '{$dbHost}');
b8\Database::setDetails('{$dbName}', '{$dbUser}', '{$dbPass}');
b8\Database::setWriteServers(array('{$dbHost}'));
b8\Database::setReadServers(array('{$dbHost}'));
\$registry = b8\Registry::getInstance();
\$registry->set('install_url', '{$ciUrl}');
";
if(!empty($ghId) && !empty($ghSecret))
{
$str .= PHP_EOL . "\$registry->set('github_app', array('id' => '{$ghId}', 'secret' => '{$ghSecret}'));" . PHP_EOL;
}
file_put_contents('./config.php', $str);
if(!file_exists('./composer.phar'))
{
print 'INSTALLING: Composer' . PHP_EOL;
file_put_contents('./composerinstaller.php', file_get_contents('https://getcomposer.org/installer'));
shell_exec('php composerinstaller.php');
unlink('./composerinstaller.php');
}
print 'RUNNING: Composer' . PHP_EOL;
shell_exec('php composer.phar install');
require_once('bootstrap.php');
$gen = new b8\Database\Generator(b8\Database::getConnection(), 'PHPCI', './PHPCI/Model/Base/');
$gen->generate();
$adminEmail = ask('Enter your email address: ');
$adminPass = ask('Enter your desired admin password: ');
$adminName = ask('Enter your name: ');
try
{
$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;
}
catch(Exception $ex)
{
print 'There was a problem creating your account. :(' . PHP_EOL;
print $ex->getMessage();
}
function ask($question, $emptyOk = false)
{
print $question . ' ';
$rtn = '';
$fp = fopen('php://stdin', 'r');
$rtn = fgets($fp);
fclose($fp);
$rtn = trim($rtn);
if(!$emptyOk && empty($rtn))
{
$rtn = ask($question, $emptyOk);
}
return $rtn;
}