Source for file Ethna_Plugin_Generator_Project.php

Documentation is available at Ethna_Plugin_Generator_Project.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Ethna_Plugin_Generator_Project.php
  5.  *
  6.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id$
  10.  */
  11.  
  12. // {{{ Ethna_Plugin_Generator_Project
  13. /**
  14.  *  スケルトン生成クラス
  15.  *
  16.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  17.  *  @access     public
  18.  *  @package    Ethna
  19.  */
  20. {
  21.     /**
  22.      *  プロジェクトスケルトンを生成する
  23.      *
  24.      *  @access public
  25.      *  @param  string  $id         プロジェクトID
  26.      *  @param  string  $basedir    プロジェクトベースディレクトリ
  27.      *  @param  string  $skeldir    スケルトンディレクトリ。これが指定されると、そこにある
  28.      *                               ファイルが優先される。また、ETHNA_HOME/skel にないもの
  29.      *                               も追加してコピーする
  30.      *  @param  string  $locale     ロケール名
  31.      *                               (ロケール名は、ll_cc の形式。ll = 言語コード cc = 国コード)
  32.      *  @param  string  $encoding   プロジェクトで使用するエンコーディング
  33.      *  @return bool    true:成功   Ethna_Error:失敗
  34.      */
  35.     function generate($id$basedir$skeldir$locale$encoding)
  36.     {
  37.         $dir_list array(
  38.             array("app"0755),
  39.             array("app/action"0755),
  40.             array("app/action_cli"0755),
  41.             array("app/action_xmlrpc"0755),
  42.             array("app/plugin"0755),
  43.             array("app/plugin/Filter"0755),
  44.             array("app/plugin/Validator"0755),
  45.             array("app/plugin/Smarty"0755),
  46.             array("app/view"0755),
  47.             array("app/test"0755),
  48.             array("bin"0755),
  49.             array("etc"0755),
  50.             array("lib"0755),
  51.             array("locale"0755),
  52.             array("locale/$locale"0755),
  53.             array("locale/$locale/LC_MESSAGES"0755),
  54.             array("log"0777),
  55.             array("schema"0755),
  56.             array("skel"0755),
  57.             array("template"0755),
  58.             array("template/$locale"0755),
  59.             array("tmp"0777),
  60.             array("www"0755),
  61.             array("www/css"0755),
  62.             array("www/js"0755),
  63.         );
  64.  
  65.         // double check.
  66.         $id strtolower($id);
  67.         $r Ethna_Controller::checkAppId($id);
  68.         if (Ethna::isError($r)) {
  69.             return $r;
  70.         }
  71.  
  72.         // ディレクトリ作成
  73.         if (is_dir($basedir== false{
  74.             // confirm
  75.             printf("creating directory ($basedir) [y/n]: ");
  76.             flush();
  77.             $fp fopen("php://stdin""r");
  78.             $r trim(fgets($fp128));
  79.             fclose($fp);
  80.             if (strtolower($r!= 'y'{
  81.                 return Ethna::raiseError('aborted by user');
  82.             }
  83.  
  84.             if (mkdir($basedir0775== false{
  85.                 return Ethna::raiseError('directory creation failed');
  86.             }
  87.         }
  88.         foreach ($dir_list as $dir{
  89.             $mode $dir[1];
  90.             $dir $dir[0];
  91.             $target "$basedir/$dir";
  92.             if (is_dir($target)) {
  93.                 printf("%s already exists -> skipping...\n"$target);
  94.                 continue;
  95.             }
  96.             if (mkdir($target$mode== false{
  97.                 return Ethna::raiseError('directory creation failed');
  98.             else {
  99.                 printf("project sub directory created [%s]\n"$target);
  100.             }
  101.             if (chmod($target$mode== false{
  102.                 return Ethna::raiseError('chmod failed');
  103.             }
  104.         }
  105.  
  106.         // スケルトンファイル作成
  107.         $macro['ethna_version'ETHNA_VERSION;
  108.         $macro['application_id'strtoupper($id);
  109.         $macro['project_id'ucfirst($id);
  110.         $macro['project_prefix'$id;
  111.         $macro['basedir'realpath($basedir);
  112.         $macro['locale'$locale;
  113.         $macro['client_enc'$encoding;
  114.  
  115.         $macro['action_class''{$action_class}';
  116.         $macro['action_form''{$action_form}';
  117.         $macro['action_name''{$action_name}';
  118.         $macro['action_path''{$action_path}';
  119.         $macro['forward_name''{$forward_name}';
  120.         $macro['view_name''{$view_name}';
  121.         $macro['view_path''{$view_path}';
  122.  
  123.         $user_macro $this->_getUserMacro();
  124.         $default_macro $macro;
  125.         $macro array_merge($macro$user_macro);
  126.  
  127.         //  select locale file.
  128.         $locale_file (file_exists(ETHNA_BASE "/skel/locale/$locale/ethna_sysmsg.ini"))
  129.                      ? "locale/$locale/ethna_sysmsg.ini"
  130.                      : 'locale/ethna_sysmsg.default.ini';
  131.  
  132.         $realfile_maps array(
  133.             $locale_file    => "$basedir/locale/$locale/LC_MESSAGES/ethna_sysmsg.ini",
  134.             "www.index.php" => "$basedir/www/index.php",
  135.             "www.info.php"  => "$basedir/www/info.php",
  136.             "www.unittest.php" => "$basedir/www/unittest.php",
  137.             "www.xmlrpc.php" => "$basedir/www/xmlrpc.php",
  138.             "www.css.ethna.css" => "$basedir/www/css/ethna.css",
  139.             "dot.ethna" => "$basedir/.ethna",
  140.             "app.controller.php" => sprintf("$basedir/app/%s_Controller.php"$macro['project_id']),
  141.             "app.error.php" => sprintf("$basedir/app/%s_Error.php"$macro['project_id']),
  142.             "app.actionclass.php" => sprintf("$basedir/app/%s_ActionClass.php"$macro['project_id']),
  143.             "app.actionform.php" => sprintf("$basedir/app/%s_ActionForm.php"$macro['project_id']),
  144.             "app.viewclass.php" => sprintf("$basedir/app/%s_ViewClass.php"$macro['project_id']),
  145.             "app.action.default.php" => "$basedir/app/action/Index.php",
  146.             "app.plugin.filter.default.php" => sprintf("$basedir/app/plugin/Filter/%s_Plugin_Filter_ExecutionTime.php"$macro['project_id']),
  147.             "app.view.default.php" => "$basedir/app/view/Index.php",
  148.             "app.unittest.php" => sprintf("$basedir/app/%s_UnitTestManager.php"$macro['project_id']),
  149.             "app.url_handler.php" => sprintf("$basedir/app/%s_UrlHandler.php"$macro['project_id']),
  150.             "etc.ini.php" => sprintf("$basedir/etc/%s-ini.php"$macro['project_prefix']),
  151.             "template.index.tpl" => sprintf("$basedir/template/$locale/index.tpl"),
  152.         );
  153.  
  154.         $skelfile_maps array(
  155.             "skel.action.php" => sprintf("$basedir/skel/skel.action.php"),
  156.             "skel.action_cli.php" => sprintf("$basedir/skel/skel.action_cli.php"),
  157.             "skel.action_test.php" => sprintf("$basedir/skel/skel.action_test.php"),
  158.             "skel.app_object.php" => sprintf("$basedir/skel/skel.app_object.php"),
  159.             "skel.entry_www.php" => sprintf("$basedir/skel/skel.entry_www.php"),
  160.             "skel.entry_cli.php" => sprintf("$basedir/skel/skel.entry_cli.php"),
  161.             "skel.view.php" => sprintf("$basedir/skel/skel.view.php"),
  162.             "skel.template.tpl" => sprintf("$basedir/skel/skel.template.tpl"),
  163.             "skel.view_test.php" => sprintf("$basedir/skel/skel.view_test.php"),
  164.         );
  165.  
  166.         //    also copy user defined skel file.
  167.         if (!empty($skeldir)) {
  168.             $handle opendir($skeldir);
  169.             while (($file readdir($handle)) !== false{
  170.                 if (is_dir(realpath("$skeldir/$file"))) {
  171.                     continue;
  172.                 }
  173.                 if (array_key_exists($file$skelfile_maps== false{
  174.                     $skelfile_maps[$filesprintf("$basedir/skel/$file");
  175.                 }
  176.             }
  177.         }
  178.  
  179.         $real_r $this->_generate($realfile_maps$macro$skeldir);
  180.         if (Ethna::isError($real_r)) {
  181.             return $real_r;
  182.         }
  183.  
  184.         //  skelファイルにはエンコーディングマクロは適用しない
  185.         //  skel.template.tpl は、add-[view|template]時に適用させるため。
  186.         unset($default_macro['client_enc']);
  187.         $skel_r $this->_generate($skelfile_maps$default_macro$skeldir);
  188.         if (Ethna::isError($skel_r)) {
  189.             return $skel_r;
  190.         }
  191.  
  192.         return true;
  193.     }
  194.  
  195.     /**
  196.      *  実際のプロジェクトスケルトンを生成処理を行う
  197.      *
  198.      *  @access private
  199.      *  @param  string  $maps       スケルトン名と生成されるファイルの配列
  200.      *  @param  string  $macro      適用マクロ
  201.      *  @param  string  $skeldir    スケルトンディレクトリ。これが指定されると、そこにある
  202.      *                               ファイルが優先される。また、ETHNA_HOME/skel にないもの
  203.      *                               も追加してコピーする
  204.      *  @return bool     true:成功  Ethna_Error:失敗
  205.      */
  206.  
  207.     function _generate($maps$macro$skeldir)
  208.     {
  209.         foreach ($maps as $skel => $realfile{
  210.             if (!empty($skeldir&& file_exists("$skeldir/$skel")) {
  211.                 $skel "$skeldir/$skel";
  212.             }
  213.             if ($this->_generateFile($skel$realfile$macro== false{
  214.                 return Ethna::raiseError("generating files failed");
  215.             }
  216.         }
  217.         return true;
  218.     }
  219. }
  220. // }}}
  221.  
  222. ?>

Documentation generated on Fri, 11 Nov 2011 04:00:18 +0900 by phpDocumentor 1.4.3