Source for file AddView.php

Documentation is available at AddView.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  AddView.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: a1de2283c69191a6617f4bd93dda52e47ef7f71a $
  10.  */
  11.  
  12. require_once 'Ethna/class/Plugin/Handle/AddAction.php';
  13.  
  14. // {{{ Ethna_Plugin_Handle_AddView
  15. /**
  16.  *  add-view handler
  17.  *
  18.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  19.  *  @access     public
  20.  *  @package    Ethna
  21.  */
  22. {
  23.     /**
  24.      *  add view
  25.      *
  26.      *  @access public
  27.      */
  28.     function perform()
  29.     {
  30.         //
  31.         //  '-w[with-unittest]' and '-u[unittestskel]' option
  32.         //  are not intuisive, but I dare to define them because
  33.         //  -t and -s option are reserved by add-[action|view] handle
  34.         //  and Ethna_Getopt cannot interpret two-character option.
  35.         //
  36.         $r $this->_getopt(
  37.                   array('basedir=',
  38.                         'skelfile=',
  39.                         'with-unittest',
  40.                         'unittestskel=',
  41.                         'template',
  42.                         'locale=',
  43.                         'encoding=',
  44.                   )
  45.               );
  46.         if (Ethna::isError($r)) {
  47.             return $r;
  48.         }
  49.         list($opt_list$arg_list$r;
  50.  
  51.         // view_name
  52.         $view_name array_shift($arg_list);
  53.         if ($view_name == null{
  54.             return Ethna::raiseError('view name isn\'t set.''usage');
  55.         }
  56.         $r Ethna_Controller::checkViewName($view_name);
  57.         if (Ethna::isError($r)) {
  58.             return $r;
  59.         }
  60.  
  61.         // add view(invoke parent class method)
  62.         $ret $this->_perform('View'$view_name$opt_list);
  63.         if (Ethna::isError($ret|| $ret === false
  64.             return $ret;
  65.         }
  66.  
  67.         // add template
  68.         if (isset($opt_list['template'])) {
  69.             $ret $this->_performTemplate($view_name$opt_list);
  70.             if (Ethna::isError($ret|| $ret === false
  71.                 return $ret;
  72.             }
  73.         }
  74.  
  75.         return true;
  76.     }
  77.  
  78.     /**
  79.      *  Special Function for generating template.
  80.      *
  81.      *  @param  string $target_name Template Name
  82.      *  @param  array  $opt_list    Option List.
  83.      *  @access protected
  84.      */
  85.     function _performTemplate($target_name$opt_list)
  86.     {
  87.         // basedir
  88.         if (isset($opt_list['basedir'])) {
  89.             $basedir realpath(end($opt_list['basedir']));
  90.         else {
  91.             $basedir getcwd();
  92.         }
  93.  
  94.         // skelfile
  95.         if (isset($opt_list['skelfile'])) {
  96.             $skelfile end($opt_list['skelfile']);
  97.         else {
  98.             $skelfile null;
  99.         }
  100.  
  101.         // locale
  102.         $ctl Ethna_Handle::getAppController(getcwd());
  103.         if (isset($opt_list['locale'])) {
  104.             $locale end($opt_list['locale']);
  105.             if (!preg_match('/^[A-Za-z_]+$/'$locale)) {
  106.                 return Ethna::raiseError("You specified locale, but invalid : $locale"'usage');
  107.             }
  108.         else {
  109.             if (Ethna::isError($ctl)) {
  110.                 $locale 'ja_JP';
  111.             else {
  112.                 $locale $ctl->getLocale();
  113.             }
  114.         }
  115.  
  116.         // encoding
  117.         if (isset($opt_list['encoding'])) {
  118.             $encoding end($opt_list['encoding']);
  119.             if (function_exists('mb_list_encodings')) {
  120.                 $supported_enc mb_list_encodings();
  121.                 if (!in_array($encoding$supported_enc)) {
  122.                     return Ethna::raiseError("Unknown Encoding : $encoding"'usage');
  123.                 }
  124.             }
  125.         else {
  126.             if (Ethna::isError($ctl)) {
  127.                 $encoding 'UTF-8';
  128.             else {
  129.                 $encoding $ctl->getClientEncoding();
  130.             }
  131.         }
  132.  
  133.         $r Ethna_Generator::generate('Template'$basedir,
  134.                                         $target_name$skelfile$locale$encoding);
  135.         if (Ethna::isError($r)) {
  136.             printf("error occurred while generating skelton. please see also following error message(s)\n\n");
  137.             return $r;
  138.         }
  139.  
  140.         $true true;
  141.         return $true;
  142.     }
  143.  
  144.     /**
  145.      *  get handler's description
  146.      *
  147.      *  @access public
  148.      */
  149.     function getDescription()
  150.     {
  151.         return <<<EOS
  152. add new view to project:
  153.     {$this->id} [options... ] [view name]
  154.     [options ...] are as follows.
  155.         [-b|--basedir=dir] [-s|--skelfile=file]
  156.         [-w|--with-unittest] [-u|--unittestskel=file]
  157.         [-t|--template] [-l|--locale] [-e|--encoding]
  158.     NOTICE: "-w" and "-u" options are ignored when you specify -t option.
  159.             "-l" and "-e" options are enabled when you specify -t option.
  160.  
  161. EOS;
  162.     }
  163.  
  164.     /**
  165.      *  @access public
  166.      */
  167.     function getUsage()
  168.     {
  169.         return <<<EOS
  170. ethna {$this->id} [options... ] [view name]
  171.     [options ...] are as follows.
  172.         [-b|--basedir=dir] [-s|--skelfile=file]
  173.         [-w|--with-unittest] [-u|--unittestskel=file]
  174.         [-t|--template] [-l|--locale] [-e|--encoding]
  175.     NOTICE: "-w" and "-u" options are ignored when you specify -t option.
  176.             "-l" and "-e" options are enabled when you specify -t option.
  177. EOS;
  178.     }
  179. }
  180. // }}}

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