<?php
/**
 * Ethna_Plugin_Generator_PluginFile.php
 * 
 * @author BoBpp < bobpp@users.sourceforge.jp >
 * @package Ethna
 */

/**
 * プラグイン作成ジェネレータ
 * 
 * @access public
 * @author BoBpp < bobpp@users.sourceforge.jp >
 * @package Ethna
 */
class Ethna_Plugin_Generator_PluginFile extends Ethna_Plugin_Generator
{
	/**
	 * プラグインファイルを生成する
	 * 
	 * @access public
	 * @param $type     string プラグインの種類
	 * @param $name   string プラグイン名
	 * @param $proj_dir string プロジェクトのディレクトリ
	 */
	function generate($type, $name, $proj_dir)
	{
		// get application controller
        $c =& Ethna_Handle::getAppController($proj_dir);
        if (Ethna::isError($c)) {
            return $c;
        }
        
        // File Name
        $makeDir = $c->getDirectory('plugin') . '/' . ucfirst($type) . '/';
        $makeFile = $c->getAppId() . '_Plugin_' . ucfirst($type) . '_' . ucfirst($name) . '.php';
        
        // マクロ
        $macro = array();
        $macro['project_id'] = $c->getAppId();
        $macro['name'] = ucfirst($name);
        $macro['type'] = ucfirst($type);
        $macro['file'] = $makeFile;
                
        // ユーザ固有のマクロ
        $user_macro = $this->_getUserMacro();
        $macro = array_merge($macro, $user_macro);
        
        // 生成ディレクトリ作成
        Ethna_Handle::mkdir(dirname("$makeDir$makeFile"), 0755);
        
        // スケルトンファイル名
        $skel = 'skel.plugin-' . strtolower($type) . '.php';
        
        if (file_exists("$makeDir$makeFile")) {
        	printf("file [%s] already exists -> skip\n", "$makeDir$makeFile");
        } else if ($this->_generateFile($skel, "$makeDir$makeFile", $macro) == false) {
        	printf("[warning] file creation failed [%s]\n", "$makeDir$makeFile");
        } else {
        	printf("plugin successfully created [%s]\n", "$makeDir$makeFile");
        }
    }
}
?>
