Source for file Ethna_Plugin_Generator.php

Documentation is available at Ethna_Plugin_Generator.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Ethna_Plugin_Generator.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
  13. /**
  14.  *  スケルトン生成プラグイン
  15.  *
  16.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  17.  *  @access     public
  18.  *  @package    Ethna
  19.  */
  20. {
  21.     /** @var    object  Ethna_Controller    スケルトン生成に使うコントローラ */
  22.     var $ctl;
  23.  
  24.     /**
  25.      *  コンストラクタ
  26.      *
  27.      *  @access public
  28.      */
  29.     function Ethna_Plugin_Generator(&$controller$type$name)
  30.     {
  31.         // Ethna_Generatorでpluginを取得するときに使ったコントローラ
  32.         // ex, add-projectではEthna_Controller, app-actionではApp_Controller
  33.         $this->ctl =$controller;
  34.     }
  35.  
  36.     /**
  37.      *  スケルトンファイルの絶対パスを解決する
  38.      *
  39.      *  @access private
  40.      *  @param  string  $skel   スケルトンファイル
  41.      */
  42.     function _resolveSkelfile($skel)
  43.     {
  44.         $file realpath($skel);
  45.         if (file_exists($file)) {
  46.             return $file;
  47.         }
  48.  
  49.         // アプリの skel ディレクトリ
  50.         $base $this->ctl->getBasedir();
  51.         $file "$base/skel/$skel";
  52.         if (file_exists($file)) {
  53.             return $file;
  54.         }
  55.  
  56.         // Ethna本体の skel ディレクトリ
  57.         $base dirname(dirname(dirname(__FILE__)));
  58.         $file "$base/skel/$skel";
  59.         if (file_exists($file)) {
  60.             return $file;
  61.         }
  62.  
  63.         return false;
  64.     }
  65.  
  66.     /**
  67.      *  スケルトンファイルにマクロを適用してファイルを生成する
  68.      *
  69.      *  @access private
  70.      *  @param  string  $skel       スケルトンファイル
  71.      *  @param  string  $entity     生成ファイル名
  72.      *  @param  array   $macro      置換マクロ
  73.      *  @param  bool    $overwrite  上書きフラグ
  74.      *  @return bool    true:正常終了 false:エラー
  75.      */
  76.     function _generateFile($skel$entity$macro$overwrite false)
  77.     {
  78.         if (file_exists($entity)) {
  79.             if ($overwrite === false{
  80.                 printf("file [%s] already exists -> skip\n"$entity);
  81.                 return true;
  82.             else {
  83.                 printf("file [%s] already exists, to be overwriten.\n"$entity);
  84.             }
  85.         }
  86.  
  87.         $resolved $this->_resolveSkelfile($skel);
  88.         if ($resolved === false{
  89.             printf("skelton file [%s] not found.\n"$skel);
  90.             return false;
  91.         else {
  92.             $skel $resolved;
  93.         }
  94.  
  95.         $rfp fopen($skel"r");
  96.         if ($rfp == null{
  97.             return false;
  98.         }
  99.         $wfp fopen($entity"w");
  100.         if ($wfp == null{
  101.             fclose($rfp);
  102.             return false;
  103.         }
  104.  
  105.         for (;;{
  106.             $s fread($rfp4096);
  107.             if (strlen($s== 0{
  108.                 break;
  109.             }
  110.  
  111.             foreach ($macro as $k => $v{
  112.                 $s preg_replace("/{\\\$$k}/"$v$s);
  113.             }
  114.             fwrite($wfp$s);
  115.         }
  116.  
  117.         fclose($wfp);
  118.         fclose($rfp);
  119.  
  120.         $st stat($skel);
  121.         if (chmod($entity$st[2]== false{
  122.             return false;
  123.         }
  124.  
  125.         printf("file generated [%s -> %s]\n"$skel$entity);
  126.  
  127.         return true;
  128.     }
  129.  
  130.     /**
  131.      *  ユーザ定義のマクロを設定する(~/.ethna)
  132.      *
  133.      *  @access private
  134.      */
  135.     function _getUserMacro()
  136.     {
  137.         if (isset($_SERVER['USERPROFILE']&& is_dir($_SERVER['USERPROFILE'])) {
  138.             $home $_SERVER['USERPROFILE'];
  139.         else {
  140.             $home $_SERVER['HOME'];
  141.         }
  142.  
  143.         if (is_file("$home/.ethna"== false{
  144.             return array();
  145.         }
  146.  
  147.         $user_macro parse_ini_file("$home/.ethna");
  148.         return $user_macro;
  149.     }
  150. }
  151. // }}}
  152. ?>

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