Source for file Project.php

Documentation is available at Project.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  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: de15d10669a2fa66edb7d1943704be6a57d99ed1 $
  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.htaccess" => "$basedir/www/.htaccess",
  135.             "www.index.php" => "$basedir/www/index.php",
  136.             "www.info.php"  => "$basedir/www/info.php",
  137.             "www.unittest.php" => "$basedir/www/unittest.php",
  138.             "www.xmlrpc.php" => "$basedir/www/xmlrpc.php",
  139.             "www.css.ethna.css" => "$basedir/www/css/ethna.css",
  140.             "dot.ethna" => "$basedir/.ethna",
  141.             "app.controller.php" => sprintf("$basedir/app/%s_Controller.php"$macro['project_id']),
  142.             "app.error.php" => sprintf("$basedir/app/%s_Error.php"$macro['project_id']),
  143.             "app.actionclass.php" => sprintf("$basedir/app/%s_ActionClass.php"$macro['project_id']),
  144.             "app.actionform.php" => sprintf("$basedir/app/%s_ActionForm.php"$macro['project_id']),
  145.             "app.viewclass.php" => sprintf("$basedir/app/%s_ViewClass.php"$macro['project_id']),
  146.             "app.action.default.php" => "$basedir/app/action/Index.php",
  147.             "app.plugin.filter.default.php" => sprintf("$basedir/app/plugin/Filter/ExecutionTime.php"$macro['project_id']),
  148.             "app.view.default.php" => "$basedir/app/view/Index.php",
  149.             "app.unittest.php" => sprintf("$basedir/app/%s_UnitTestManager.php"$macro['project_id']),
  150.             "app.url_handler.php" => sprintf("$basedir/app/%s_UrlHandler.php"$macro['project_id']),
  151.             "etc.ini.php" => sprintf("$basedir/etc/%s-ini.php"$macro['project_prefix']),
  152.             "template.index.tpl" => sprintf("$basedir/template/$locale/index.tpl"),
  153.             "template.layout.tpl" => sprintf("$basedir/template/$locale/layout.tpl"),
  154.             "template.403.tpl" => sprintf("$basedir/template/$locale/error403.tpl"),
  155.             "template.404.tpl" => sprintf("$basedir/template/$locale/error404.tpl"),
  156.             "template.500.tpl" => sprintf("$basedir/template/$locale/error500.tpl"),
  157.         );
  158.  
  159.         $skelfile_maps array(
  160.             "skel.action.php" => sprintf("$basedir/skel/skel.action.php"),
  161.             "skel.action_cli.php" => sprintf("$basedir/skel/skel.action_cli.php"),
  162.             "skel.action_test.php" => sprintf("$basedir/skel/skel.action_test.php"),
  163.             "skel.app_object.php" => sprintf("$basedir/skel/skel.app_object.php"),
  164.             "skel.entry_www.php" => sprintf("$basedir/skel/skel.entry_www.php"),
  165.             "skel.entry_cli.php" => sprintf("$basedir/skel/skel.entry_cli.php"),
  166.             "skel.view.php" => sprintf("$basedir/skel/skel.view.php"),
  167.             "skel.template.tpl" => sprintf("$basedir/skel/skel.template.tpl"),
  168.             "skel.view_test.php" => sprintf("$basedir/skel/skel.view_test.php"),
  169.         );
  170.  
  171.         //    also copy user defined skel file.
  172.         if (!empty($skeldir)) {
  173.             $handle opendir($skeldir);
  174.             while (($file readdir($handle)) !== false{
  175.                 if (is_dir(realpath("$skeldir/$file"))) {
  176.                     continue;
  177.                 }
  178.                 if (array_key_exists($file$skelfile_maps== false{
  179.                     $skelfile_maps[$filesprintf("$basedir/skel/$file");
  180.                 }
  181.             }
  182.         }
  183.  
  184.         $real_r $this->_generate($realfile_maps$macro$skeldir);
  185.         if (Ethna::isError($real_r)) {
  186.             return $real_r;
  187.         }
  188.  
  189.         //  skelファイルにはエンコーディングマクロは適用しない
  190.         //  skel.template.tpl は、add-[view|template]時に適用させるため。
  191.         unset($default_macro['client_enc']);
  192.         $skel_r $this->_generate($skelfile_maps$default_macro$skeldir);
  193.         if (Ethna::isError($skel_r)) {
  194.             return $skel_r;
  195.         }
  196.  
  197.         return true;
  198.     }
  199.  
  200.     /**
  201.      *  実際のプロジェクトスケルトンを生成処理を行う
  202.      *
  203.      *  @access private
  204.      *  @param  string  $maps       スケルトン名と生成されるファイルの配列
  205.      *  @param  string  $macro      適用マクロ
  206.      *  @param  string  $skeldir    スケルトンディレクトリ。これが指定されると、そこにある
  207.      *                               ファイルが優先される。また、ETHNA_HOME/skel にないもの
  208.      *                               も追加してコピーする
  209.      *  @return bool     true:成功  Ethna_Error:失敗
  210.      */
  211.  
  212.     function _generate($maps$macro$skeldir)
  213.     {
  214.         foreach ($maps as $skel => $realfile{
  215.             if (!empty($skeldir&& file_exists("$skeldir/$skel")) {
  216.                 $skel "$skeldir/$skel";
  217.             }
  218.             if ($this->_generateFile($skel$realfile$macro== false{
  219.                 return Ethna::raiseError("generating files failed");
  220.             }
  221.         }
  222.         return true;
  223.     }
  224. }
  225. // }}}

Documentation generated on Fri, 11 Nov 2011 03:58:58 +0900 by phpDocumentor 1.4.3