OSDN Git Service

- added type check.
authormumumu-org <mumumu-org@2ef88817-412d-0410-a32c-8029a115e976>
Sat, 20 Jun 2009 14:22:45 +0000 (14:22 +0000)
committermumumu-org <mumumu-org@2ef88817-412d-0410-a32c-8029a115e976>
Sat, 20 Jun 2009 14:22:45 +0000 (14:22 +0000)
- changed behavior to require to in Ethna project.
- added -p|--plugin-package option.

class/Plugin/Generator/CreatePlugin.php
class/Plugin/Handle/CreatePlugin.php

index a2258a7..502b867 100644 (file)
@@ -25,37 +25,46 @@ class Ethna_Plugin_Generator_CreatePlugin extends Ethna_Plugin_Generator
      *  @access public
      *  @param  string  $basedir        ベースディレクトリ
      *  @param  array   $types          プラグインのtype (Validator, Handle等)
-     *  @param  string  $no_ini         iniファイル生成フラグ 
+     *  @param  string  $forpackage     iniファイル生成フラグ 
      *  @param  string  $plugin_name    プラグイン名 
      *  @return true|Ethna_Error        true:成功 Ethna_Error:失敗
      */
-    function &generate($basedir, $types = array(), $no_ini = false, $plugin_name)
+    function &generate($basedir, $types = array(), $forpackage = false, $plugin_name)
     {
-        //  create plugin directory
         $plugin_dir = "$basedir/plugin";
+        if (!$forpackage) {
+            $chk_ctl = Ethna_Handle::getAppController(getcwd());
+            if (Ethna::isError($chk_ctl)) {
+                return Ethna::raiseError(
+                           "ERROR: You are not in Ethna project. specify [-p|--plugin-package] option, or change directory to the Ethna Project\n"
+                );
+            }
+            $plugin_dir = $chk_ctl->getDirectory('plugin');
+        }
+
+        //  create plugin directory
         if (!file_exists($plugin_dir)) {
             Ethna_Util::mkdir($plugin_dir, 0755);
-        } else {
-            printf("directory [$plugin_dir] already exists -> skip.\n");
         }
+
         //   type check.
         if (empty($types)) {
             return Ethna::raiseError('please specify plugin type.');
         }
 
-        //   generate ini file
-        if ($no_ini == false) {
-            $ini_skel = 'plugin/skel.plugin.ini';
-            $ini_file = strtolower($plugin_name) . '.ini';
-            $ini_path = "$plugin_dir/$ini_file";
-        
-            $macro['plugin_name'] = $plugin_name;
-            if (file_exists($ini_path)) {
-                printf("file [%s] already exists -> skip\n", $ini_file);
-            } else if ($this->_generateFile($ini_skel, $ini_path, $macro) == false) {
-                printf("[warning] file creation failed [%s]\n", $ini_file);
-            } else {
-                printf("plugin ini file successfully created [%s]\n", $ini_file);
+        //
+        //   type check
+        //
+        foreach ($types as $type) {
+            switch (strtolower($type)) {
+            case 'f':
+            case 'v':
+            case 'sm':
+            case 'sb':
+            case 'sf':
+                break;
+            default:
+                return Ethna::raiseError("unknown plugin type: ${type}", 'usage');
             }
         }
 
@@ -64,6 +73,7 @@ class Ethna_Plugin_Generator_CreatePlugin extends Ethna_Plugin_Generator
         //
         $plugin_name = ucfirst(strtolower($plugin_name));
         $lplugin_name = strtolower($plugin_name);
+        $macro['plugin_name'] = $plugin_name;
         foreach ($types as $type) {
             $ltype = strtolower($type);
             $macro['plugin_type'] = $type;
@@ -82,14 +92,17 @@ class Ethna_Plugin_Generator_CreatePlugin extends Ethna_Plugin_Generator
             case 'sm':
                 $type = 'Smarty';
                 $pfilename = "modifier.${lplugin_name}.php";
+                $macro['plugin_name'] = $lplugin_name;
                 break;
             case 'sb':
                 $type = 'Smarty';
                 $pfilename = "block.${lplugin_name}.php";
+                $macro['plugin_name'] = $lplugin_name;
                 break;
             case 'sf':
                 $type = 'Smarty';
                 $pfilename = "function.${lplugin_name}.php";
+                $macro['plugin_name'] = $lplugin_name;
                 break;
             }
             $type_dir = "$plugin_dir/$type";
@@ -109,6 +122,21 @@ class Ethna_Plugin_Generator_CreatePlugin extends Ethna_Plugin_Generator
             }
         } 
 
+        //   generate ini file
+        if ($forpackage) {
+            $ini_skel = 'plugin/skel.plugin.ini';
+            $ini_file = strtolower($plugin_name) . '.ini';
+            $ini_path = "$plugin_dir/$ini_file";
+        
+            if (file_exists($ini_path)) {
+                printf("file [%s] already exists -> skip\n", $ini_file);
+            } else if ($this->_generateFile($ini_skel, $ini_path, $macro) == false) {
+                printf("[warning] file creation failed [%s]\n", $ini_file);
+            } else {
+                printf("plugin ini file successfully created [%s]\n", $ini_file);
+            }
+        }
+
         $true = true;
         return $true;
     }
index 78b4c07..0b2c45a 100644 (file)
@@ -32,7 +32,8 @@ class Ethna_Plugin_Handle_CreatePlugin extends Ethna_Plugin_Handle
             array(
                 'basedir=',
                 'type=',
-                'noini',
+                'no-inifile',
+                'plugin-package',
             )
         ); 
         if (Ethna::isError($r)) {
@@ -61,14 +62,14 @@ class Ethna_Plugin_Handle_CreatePlugin extends Ethna_Plugin_Handle
         }
 
         //  no-ini file flag.
-        $no_ini = (isset($opt_list['noini'])) ? true : false; 
+        $forpackage = (isset($opt_list['plugin-package'])) ? true : false; 
 
-        $r = Ethna_Generator::generate('CreatePlugin', NULL, $basedir, $types, $no_ini, $plugin_name);
+        $r = Ethna_Generator::generate('CreatePlugin', NULL, $basedir, $types, $forpackage, $plugin_name);
         if (Ethna::isError($r)) {
             printf("error occurred while generating plugin skelton. please see also error messages given above\n\n");
             return $r;
         }
-        printf("\nplugin skelton for [%s] is successfully generated at [%s]\n\n", $plugin_name, "$basedir/$plugin_name");
+        printf("\nplugin skelton for [%s] is successfully generated.\n\n", $plugin_name);
         return true;
     }
     // }}}
@@ -80,13 +81,14 @@ class Ethna_Plugin_Handle_CreatePlugin extends Ethna_Plugin_Handle
     function getUsage()
     {
         return <<<EOS
-ethna {$this->id} [-b|--basedir=dir] [-t|--type=f,v,sb,sf,sm...] [-n|--no-inifile] plugin-name
-    type is as follows (separated by comma):
+ethna {$this->id} [-b|--basedir=dir] [-t|--type=f,v,sb,sf,sm...] [-p|--plugin-package] plugin-name
+    -t: type is as follows (separated by comma):
         f = Filter (default),
         v = Validator (default),
         sm = Smarty modifier (default)
         sb = Smarty block,
         sf = Smarty function,
+    -p: if you want to make plugin package, set this option.
 EOS;
     }
     // }}}
@@ -99,7 +101,7 @@ EOS;
     {
         return <<<EOS
 make plugin package:
-    {$this->id} [-b|--basedir=dir] [-t|--type=f,v,sb,sf,sm...] [-n|--no-inifile] plugin-name
+    {$this->id} [-b|--basedir=dir] [-t|--type=f,v,sb,sf,sm...] [-p|--plugin-package] plugin-name
 EOS;
     }
     // }}}