Skip to content

Instantly share code, notes, and snippets.

@77web
Created April 1, 2011 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 77web/897640 to your computer and use it in GitHub Desktop.
Save 77web/897640 to your computer and use it in GitHub Desktop.
1行コマンドでインストールができるopenpne:fast-installタスクの提案。
<?php
/**
* This file is part of the OpenPNE package.
* (c) OpenPNE Project (http://www.openpne.jp/)
*
* For the full copyright and license information, please view the LICENSE
* file and the NOTICE file that were distributed with this source code.
*/
/**
* openpne:fast-install task. enables one-liner install.
*
* @auther Hiromi Hishida <info@77-web.com>
*/
class openpneFastInstallTask extends sfDoctrineBaseTask
{
protected function configure()
{
$this->namespace = 'openpne';
$this->name = 'fast-install';
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'prod'),
new sfCommandOption('dbms', 'mysql', sfCommandOption::PARAMETER_OPTIONAL, 'The dbms for database connection. mysql or sqlite'),
new sfCommandOption('dbuser', null, sfCommandOption::PARAMETER_OPTIONAL, 'A username for database connection.'),
new sfCommandOption('dbpassword', null, sfCommandOption::PARAMETER_OPTIONAL, 'A password for database connection.'),
new sfCommandOption('dbhost', null, sfCommandOption::PARAMETER_OPTIONAL, 'A hostname for database connection.'),
new sfCommandOption('dbname', null, sfCommandOption::PARAMETER_REQUIRED, 'A database name for database connection.'),
));
$this->briefDescription = 'Install OpenPNE';
$this->detailedDescription = <<<EOF
The [openpne:fast-install] task installs and configures OpenPNE.
Call it with:
[./symfony openpne:fast-install --dbms=mysql --dbuser=your-username --dbpassword=your-password --dbname=your-dbname]
EOF;
}
protected function execute($arguments = array(), $options = array())
{
$dbms = $options['dbms'];
$username = $options['dbuser'];
$password = $options['dbpassword'];
$hostname = $options['dbhost'];
$dbname = $options['dbname'];
$port = $options['dbport'];
$sock = $options['dbsock'];
if (empty($dbms))
{
$this->logSection('installer', 'task aborted: empty dbms');
return 1;
}
if ($dbms !== 'sqlite')
{
if(empty($username))
{
$this->logSection('installer', 'task aborted: dbuser is empty');
return 1;
}
if(empty($hostname))
{
$hostname = '127.0.0.1';
}
}
else
{
$dbname = realpath(dirname($dbname)).DIRECTORY_SEPARATOR.basename($dbname);
}
unset($options['dbms'], $options['dbuser'], $options['dbpassword'], $options['dbname'], $options['dbhost'], $options['dbport'], $options['dbsock']);
$this->doInstall($dbms, $username, $password, $hostname, $port, $dbname, $sock, $options);
if ($dbms === 'sqlite')
{
$this->getFilesystem()->chmod($dbname, 0666);
}
$this->publishAssets();
// _PEAR_call_destructors() causes an E_STRICT error
error_reporting(error_reporting() & ~E_STRICT);
$this->logSection('installer', 'installation is completed!');
}
protected function doInstall($dbms, $username, $password, $hostname, $port, $dbname, $sock, $options)
{
$this->installPlugins();
@$this->fixPerms();
@$this->clearCache();
$this->configureDatabase($dbms, $username, $password, $hostname, $port, $dbname, $sock, $options);
$this->buildDb($options);
}
protected function createDSN($dbms, $hostname, $port, $dbname, $sock)
{
$result = $dbms.':';
$data = array();
if ($dbname)
{
if ($dbms === 'sqlite')
{
$data[] = $dbname;
}
else
{
$data[] = 'dbname='.$dbname;
}
}
if ($hostname)
{
$data[] = 'host='.$hostname;
}
if ($port)
{
$data[] = 'port='.$port;
}
if ($sock)
{
$data[] = 'unix_socket='.$sock;
}
$result .= implode(';', $data);
return $result;
}
protected function configureDatabase($dbms, $username, $password, $hostname, $port, $dbname, $sock, $options)
{
$dsn = $this->createDSN($dbms, $hostname, $port, $dbname, $sock);
$file = sfConfig::get('sf_config_dir').'/databases.yml';
$config = array();
if (file_exists($file))
{
$config = sfYaml::load($file);
}
$env = 'all';
if ('prod' !== $options['env'])
{
$env = $options['env'];
}
$config[$env]['doctrine'] = array(
'class' => 'sfDoctrineDatabase',
'param' => array(
'dsn' => $dsn,
'username' => $username,
'encoding' => 'utf8',
'attributes' => array(
Doctrine::ATTR_USE_DQL_CALLBACKS => true,
),
),
);
if ($password)
{
$config[$env]['doctrine']['param']['password'] = $password;
}
file_put_contents($file, sfYaml::dump($config, 4));
}
protected function clearCache()
{
$cc = new sfCacheClearTask($this->dispatcher, $this->formatter);
$cc->run();
}
protected function publishAssets()
{
$publishAssets = new sfPluginPublishAssetsTask($this->dispatcher, $this->formatter);
$publishAssets->run();
}
protected function buildDb($options)
{
$tmpdir = sfConfig::get('sf_data_dir').'/fixtures_tmp';
$this->getFilesystem()->mkdirs($tmpdir);
$this->getFilesystem()->remove(sfFinder::type('file')->in(array($tmpdir)));
$pluginDirs = sfFinder::type('dir')->name('data')->in(sfFinder::type('dir')->name('op*Plugin')->maxdepth(1)->in(sfConfig::get('sf_plugins_dir')));
$fixturesDirs = sfFinder::type('dir')->name('fixtures')
->prune('migrations', 'upgrade')
->in(array_merge(array(sfConfig::get('sf_data_dir')), $this->configuration->getPluginSubPaths('/data'), $pluginDirs));
$i = 0;
foreach ($fixturesDirs as $fixturesDir)
{
$files = sfFinder::type('file')->name('*.yml')->sort_by_name()->in(array($fixturesDir));
foreach ($files as $file)
{
$this->getFilesystem()->copy($file, $tmpdir.'/'.sprintf('%03d_%s_%s.yml', $i, basename($file, '.yml'), md5(uniqid(rand(), true))));
}
$i++;
}
$task = new sfDoctrineBuildTask($this->dispatcher, $this->formatter);
$task->setCommandApplication($this->commandApplication);
$task->setConfiguration($this->configuration);
$task->run(array(), array(
'no-confirmation' => true,
'db' => true,
'model' => true,
'forms' => true,
'filters' => true,
'sql' => true,
'and-load' => $tmpdir,
'application' => $options['application'],
'env' => $options['env'],
));
$this->getFilesystem()->remove(sfFinder::type('file')->in(array($tmpdir)));
$this->getFilesystem()->remove($tmpdir);
}
protected function installPlugins()
{
$task = new opPluginSyncTask($this->dispatcher, $this->formatter);
$task->run();
}
protected function fixPerms()
{
$permissions = new openpnePermissionTask($this->dispatcher, $this->formatter);
$permissions->run();
}
}
@kashiwasan
Copy link

少しだけコードを修正させて頂きました。
https://gist.github.com/1393063/c800ee11c52523123008fde0e2dd44c735e017ea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment