Skip to content

Commit

Permalink
migration step1
Browse files Browse the repository at this point in the history
  • Loading branch information
yandod committed Apr 3, 2012
1 parent f392682 commit 0ff0c9c
Show file tree
Hide file tree
Showing 1,991 changed files with 243,262 additions and 184,957 deletions.
150 changes: 150 additions & 0 deletions app/Console/Command/AdminShell.php
@@ -0,0 +1,150 @@
<?php
/**
*
* @url http://c-brains.jp/blog/wsg/09/07/22-170044.php
* @url http://d.hatena.ne.jp/yuhei_kagaya/20080730/1217421386
* @url http://blog.takeda-soft.jp/blog/show/187
*/

App::import('Model', 'ConnectionManager');

class AdminShell extends Shell {

//var $tasks = array('Menu1','Menu2','Menu3');

//overrideでcakeメッセージ除去
function startup(){}

function main() {
//メインメニュー作成
$mainMenu = array(
'1' => array('name' => 'Menu2','alt' => '初期設定を行う'),
'2' => array('name' => 'Menu1','alt' => '指定ユーザをadminに昇格'),
//'3' => array('name' => 'Menu3','alt' => 'メニュー3'),
'q' => array('name' => null,'alt' => '終了'),
);
$mainMenuKeys = array_keys($mainMenu);

//メインメニュー表示
$value = "";
$this->out("---------------------------------------------");
foreach($mainMenu as $k => $v) {
$this->out("[{$k}] {$v['alt']}");
}
$this->out("---------------------------------------------");
while ($value <> "q") {
$value = $this->in("実行するメニューの番号を選択してください", $mainMenuKeys, "q" );

if ($value <> 'q') {
//$this->$mainMenu[$value]['name']->execute();
}

switch ($value) {
case '1':
$this->install();
break;
case '2':
$this->upgrade();
break;
}
}
}

function install()
{
$db_config = new DATABASE_CONFIG;

$db_config = $db_config->default;
unset($db_config['persistent']);

$keys = array_flip(array_keys($db_config));
while (true) {
$this->out("---------------------------------------------");
$this->out("DATABASE_CONFIG:");
foreach($db_config as $k => $v) {
$this->out("[{$keys[$k]}] [{$k}] {$v}");
}
$this->out("---------------------------------------------");
$value = $this->in("変更したい項目の番号を選択してください。これでよければyを押してください。", array_values($keys), "q");

if ($value == 'q') {
break;
}

if ($value == 'y') {

// 接続確認
$db = ConnectionManager::getDataSource('default');
$db->config = array_merge($db->_baseConfig, $db_config);
if ($db->connect() === false) {
$this->err("接続できません。データベースの設定が間違っている可能性があります。");
$db->disconnect();
continue;
} else {
$db->disconnect();
}

// @TODO database.php.installの内容をもとにデータをいれる
$this->rewriteConfig($db_config);

// @TODO dump.sqlを実行
$dump_file = APP . 'config' . DS . 'sql' . DS . 'dump.sql';
$db->query(file_get_contents($dump_file));

$this->out("Install finished!");
exit;
break;
}

if (in_array($value, array_values($keys))) {
$buf = array_keys($db_config);
$key_name = $buf[$value];
$db_config_value = $this->in("{$key_name}を入力してください。");
$db_config[$key_name] = $db_config_value;
}
}
}

private function rewriteConfig($db_config)
{
$base_file = APP . 'config' . DS . 'database.php.install';
$target_file = APP . 'config' . DS . 'database.php';

if (!file_exists($base_file)) {
$this->err("database.php.install not found.");
exit;
}

$text = file_get_contents($base_file);
$text = str_replace('{default_host}', $db_config['host'], $text);
$text = str_replace('{default_login}', $db_config['login'], $text);
$text = str_replace('{default_password}', $db_config['password'], $text);
$text = str_replace('{default_database}', $db_config['database'], $text);
$text = str_replace('{default_prefix}', $db_config['prefix'], $text);

file_put_contents($target_file, $text);
}

/**
* upgrade
* 指定ユーザをadminに昇格
*/
function upgrade()
{
$db = ConnectionManager::getDataSource('default');
$value = $this->in("昇格したいユーザ名を入力してください。");
$result = $db->query('SELECT * FROM users WHERE login = ?;', array($value), false);
if (count($result) == 0) {
$this->err('入力されたユーザは存在しません。');
return;
}

if ($result[0]['users']['admin'] === '1') {
$this->err('入力されたユーザはすでにadminです。');
return;
}

$db->query('UPDATE users SET admin = 1 WHERE login = ?', array($value));
$this->out("ユーザの昇格に成功しました。");
}
}
31 changes: 31 additions & 0 deletions app/Console/Command/AppShell.php
@@ -0,0 +1,31 @@
<?php
/**
* AppShell file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('Shell', 'Console');

/**
* Application Shell
*
* Add your application-wide methods in the class below, your shells
* will inherit them.
*
* @package app.Console.Command
*/
class AppShell extends Shell {

}
160 changes: 160 additions & 0 deletions app/Console/Command/CcPluginShell.php
@@ -0,0 +1,160 @@
<?php
/**
* Task class for creating a plugin
*
* @package cake
* @subpackage cake.cake.console.libs.tasks
*/
class CcPluginShell extends Shell {
/**
* initialize
*
* @return void
*/
function initialize() {
$this->path = APP . 'plugins' . DS;
}
/**
* Execution method always used for tasks
*
* @return void
*/
function main() {
if (empty($this->params['skel'])) {
$this->params['skel'] = '';
if (is_dir(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel') === true) {
$this->params['skel'] = CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel';
}
}

$plugin = null;

if (isset($this->args[0])) {
$plugin = Inflector::camelize($this->args[0]);
$pluginPath = Inflector::underscore($plugin) . DS;
$this->bake($plugin);
} else {
$this->__interactive($plugin);
}
}

/**
* Interactive interface
*
* @access private
* @return void
*/
function __interactive($plugin = null) {
while ($plugin === null || substr($plugin,0,2) !== 'Cc') {
$plugin = $this->in(__('Enter the name of the plugin in CamelCase format start with "Cc"'));
}

$this->bake($plugin);
}

/**
* Bake the plugin, create directories and files
*
* @params $plugin name of the plugin in CamelCased format
* @access public
* @return bool
*/
function bake($plugin) {

$pluginPath = Inflector::underscore($plugin);

if ( substr($pluginPath,0,3) !== 'cc_' ) {
$this->err('Plugin name must start with "Cc"');
return;
}

$this->hr();
$this->out("Plugin Name: $plugin");
$this->out("Plugin Directory: {$this->path}{$pluginPath}");
$this->hr();


$looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y');

if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') {
$verbose = $this->in(__('Do you want verbose output?'), array('y', 'n'), 'n');

$Folder = new Folder($this->path . $pluginPath);
$directories = array('models' . DS . 'behaviors', 'controllers' . DS . 'components', 'views' . DS . 'helpers');

foreach ($directories as $directory) {
$Folder->create($this->path . $pluginPath . DS . $directory);
}

if (strtolower($verbose) == 'y' || strtolower($verbose) == 'yes') {
foreach ($Folder->messages() as $message) {
$this->out($message);
}
}

$errors = $Folder->errors();
if (!empty($errors)) {
return false;
}

$controllerFileName = $pluginPath . '_app_controller.php';

$out = "<?php\n\n";
$out .= "class {$plugin}AppController extends AppController {\n\n";
$out .= "}\n\n";
$out .= "?>";
$this->createFile($this->path . $pluginPath. DS . $controllerFileName, $out);

$modelFileName = $pluginPath . '_app_model.php';

$out = "<?php\n\n";
$out .= "class {$plugin}AppModel extends AppModel {\n\n";
$out .= "}\n\n";
$out .= "?>";
$this->createFile($this->path . $pluginPath . DS . $modelFileName, $out);

$out = "<?php\n";
$out .= "\$pluginContainer = ClassRegistry::getObject('PluginContainer');\n";
$out .= "\$pluginContainer->installed('{$pluginPath}','0.1');\n";
$out .= "\n\n";
$this->createFile($this->path . $pluginPath . DS . 'init.php', $out);

$out = "<?php\n";
$out .= "\tclass HomeController extends {$plugin}AppController {\n";
$out .= "\tpublic \$uses = array('Issue');\n";
$out .= "\tpublic function index() {\n";
$out .= "\t\t\$this->set('count',\$this->Issue->find('count'));\n";
$out .= "\t}\n";
$out .= "}\n";
$out .= "\n";
$this->createFile($this->path . $pluginPath . DS . 'controllers/home_controller.php', $out);

$out = "";
$out .= "Hello CandyCane {$plugin} Plugin.<br/>";
$out .= "You have <?php echo \$count ?> of issues.";
$out .= "\n\n";
$this->createFile($this->path . $pluginPath . DS . 'views/home/index.ctp', $out);

$this->hr();
$this->out(sprintf(__("Created: %s in %s"), $plugin, $this->path . $pluginPath));
$this->hr();
}

return true;
}
/**
* Help
*
* @return void
* @access public
*/
function help() {
$this->hr();
$this->out("Usage: cake cc_plugin <name>");
$this->hr();
$this->out('Commands:');
$this->out("\n\tcc_plugin <name>\n\t\tbakes plugin directory structure and init.php.");
$this->out("");
$this->_stop();
}
}
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions app/Console/cake
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
################################################################################
#
# Bake is a shell script for running CakePHP bake script
# PHP 5
#
# CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
# Copyright 2005-2012, Cake Software Foundation, Inc.
#
# Licensed under The MIT License
# Redistributions of files must retain the above copyright notice.
#
# @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
# @link http://cakephp.org CakePHP(tm) Project
# @package app.Console
# @since CakePHP(tm) v 2.0
# @license MIT License (http://www.opensource.org/licenses/mit-license.php)
#
################################################################################
LIB=$(cd -P -- "$(dirname -- "$0")" && pwd -P) && LIB=$LIB/$(basename -- "$0")

while [ -h "$LIB" ]; do
DIR=$(dirname -- "$LIB")
SYM=$(readlink "$LIB")
LIB=$(cd "$DIR" && cd $(dirname -- "$SYM") && pwd)/$(basename -- "$SYM")
done

LIB=$(dirname -- "$LIB")/
APP=`pwd`

exec php -q "$LIB"cake.php -working "$APP" "$@"

exit;

0 comments on commit 0ff0c9c

Please sign in to comment.