Source for file Ethna_Plugin_Handle_AddProject.php

Documentation is available at Ethna_Plugin_Handle_AddProject.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Ethna_Plugin_Handle_AddProject.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_Handle_AddProject
  13. /**
  14.  *  add-project handler
  15.  *
  16.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  17.  *  @access     public
  18.  *  @package    Ethna
  19.  */
  20. {
  21.     /**
  22.      *  add project:)
  23.      *
  24.      *  @access public
  25.      */
  26.     function perform()
  27.     {
  28.         $r = $this->_getopt(array('basedir=', 'skeldir=', 'locale=', 'encoding='));
  29.         if (Ethna::isError($r)) {
  30.             return $r;
  31.         }
  32.         list($opt_list, $arg_list) = $r;
  33.  
  34.         // app_id
  35.         $app_id = array_shift($arg_list);
  36.         if ($app_id == null) {
  37.             return Ethna::raiseError('Application id isn\'t set.', 'usage');
  38.         }
  39.         $r = Ethna_Controller::checkAppId($app_id);
  40.         if (Ethna::isError($r)) {
  41.             return $r;
  42.         }
  43.  
  44.         // basedir
  45.         if (isset($opt_list['basedir'])) {
  46.             $dir = end($opt_list['basedir']);
  47.             $basedir = realpath($dir);
  48.             if ($basedir === false) {  //  e.x file does not exist
  49.                 $basedir = $dir;
  50.             }
  51.         } else {
  52.             $basedir = sprintf("%s/%s", getcwd(), strtolower($app_id));
  53.         }
  54.  
  55.         // skeldir
  56.         if (isset($opt_list['skeldir'])) {
  57.             $selected_dir = end($opt_list['skeldir']);
  58.             $skeldir = realpath($selected_dir);
  59.             if ($skeldir == false || is_dir($skeldir) == false || file_exists($skeldir) == false) {
  60.                 return Ethna::raiseError("You specified skeldir, but invalid : $selected_dir", 'usage');
  61.             }
  62.         } else {
  63.             $skeldir = null;
  64.         }
  65.  
  66.         // locale
  67.         if (isset($opt_list['locale'])) {
  68.             $locale = end($opt_list['locale']);
  69.             if (!preg_match('/^[A-Za-z_]+$/', $locale)) {
  70.                 return Ethna::raiseError("You specified locale, but invalid : $locale", 'usage');
  71.             }
  72.         } else {
  73.             $locale = 'ja_JP';  //  default locale. 
  74.         }
  75.  
  76.         // encoding
  77.         if (isset($opt_list['encoding'])) {
  78.             $encoding = end($opt_list['encoding']);
  79.             if (function_exists('mb_list_encodings')) {
  80.                 $supported_enc = mb_list_encodings();
  81.                 if (!in_array($encoding, $supported_enc)) {
  82.                     return Ethna::raiseError("Unknown Encoding : $encoding", 'usage');
  83.                 }
  84.             }
  85.         } else {
  86.             $encoding = 'UTF-8';  //  default encoding. 
  87.         }
  88.  
  89.         $r = Ethna_Generator::generate('Project', null, $app_id, $basedir, $skeldir, $locale, $encoding);
  90.         if (Ethna::isError($r)) {
  91.             printf("error occurred while generating skelton. please see also error messages given above\n\n");
  92.             return $r;
  93.         }
  94.  
  95.         printf("\nproject skelton for [%s] is successfully generated at [%s]\n\n", $app_id, $basedir);
  96.         return true;
  97.     }
  98.  
  99.     /**
  100.      *  get handler's description
  101.      *
  102.      *  @access public
  103.      */
  104.     function getDescription()
  105.     {
  106.         return <<<EOS
  107. add new project:
  108.     {$this->id} [-b|--basedir=dir] [-s|--skeldir] [-l|--locale] [-e|--encoding] [Application id]
  109.  
  110. EOS;
  111.     }
  112.  
  113.     /**
  114.      *  get usage
  115.      *
  116.      *  @access public
  117.      */
  118.     function getUsage()
  119.     {
  120.         return <<<EOS
  121. ethna {$this->id} [-b|--basedir=dir] [-s|--skeldir] [-l|--locale] [-e|--encoding] [Application id]
  122. EOS;
  123.     }
  124. }
  125. // }}}
  126. ?>

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