PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/class/Plugin/Handle.php

http://github.com/ethna/ethna
PHP | 159 lines | 74 code | 15 blank | 70 comment | 7 complexity | 3266dc26564632e6642eda17d16c7e6c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4. * Handle.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: e3dfa2349bfed09e51fa3f2e09b2fbe3c8f3bebf $
  10. */
  11. require_once ETHNA_BASE . '/class/Getopt.php';
  12. // {{{ Ethna_Plugin_Handle
  13. /**
  14. * ??????????????????????
  15. *
  16. * @author Masaki Fujimoto <fujimoto@php.net>
  17. * @access public
  18. * @package Ethna
  19. */
  20. class Ethna_Plugin_Handle extends Ethna_Plugin_Abstract
  21. {
  22. /** @protected handler's id */
  23. protected $id;
  24. /** @protected command line arguments */
  25. protected $arg_list;
  26. /**
  27. * Ethna_Handle constructor (stub for php4)
  28. *
  29. * @access public
  30. */
  31. public function __construct($controller, $type, $name)
  32. {
  33. parent::__construct($controller, $type, $name);
  34. $id = $name;
  35. $id = preg_replace('/^([A-Z])/e', "strtolower('\$1')", $id);
  36. $id = preg_replace('/([A-Z])/e', "'-' . strtolower('\$1')", $id);
  37. $this->id = $id;
  38. }
  39. /**
  40. * get handler-id
  41. *
  42. * @access public
  43. */
  44. function getId()
  45. {
  46. return $this->id;
  47. }
  48. /**
  49. * get handler's description
  50. *
  51. * @access public
  52. */
  53. function getDescription()
  54. {
  55. return "description of " . $this->id;
  56. }
  57. /**
  58. * get handler's usage
  59. *
  60. * @access public
  61. */
  62. function getUsage()
  63. {
  64. return "usage of " . $this->id;
  65. }
  66. /**
  67. * set arguments
  68. *
  69. * @access public
  70. */
  71. function setArgList($arg_list)
  72. {
  73. $this->arg_list = $arg_list;
  74. }
  75. /**
  76. * easy getopt :)
  77. *
  78. * @param array $lopts long options
  79. * @return array list($opts, $args)
  80. * @access protected
  81. */
  82. function _getopt($lopts = array())
  83. {
  84. // create opts
  85. // ex: $lopts = array('foo', 'bar=');
  86. $lopts = to_array($lopts);
  87. $sopts = '';
  88. $opt_def = array();
  89. foreach ($lopts as $lopt) {
  90. if ($lopt{strlen($lopt) - 2} === '=') {
  91. $opt_def[$lopt{0}] = substr($lopt, 0, strlen($lopt) - 2);
  92. $sopts .= $lopt{0} . '::';
  93. } else if ($lopt{strlen($lopt) - 1} === '=') {
  94. $opt_def[$lopt{0}] = substr($lopt, 0, strlen($lopt) - 1);
  95. $sopts .= $lopt{0} . ':';
  96. } else {
  97. $opt_def[$lopt{0}] = $lopt;
  98. $sopts .= $lopt{0};
  99. }
  100. }
  101. // do getopt
  102. // ex: $sopts = 'fb:';
  103. $opt = new Ethna_Getopt();
  104. $opts_args = $opt->getopt($this->arg_list, $sopts, $lopts);
  105. if (Ethna::isError($opts_args)) {
  106. return $opts_args;
  107. }
  108. // parse opts
  109. // ex: "-ff --bar=baz" gets
  110. // $opts = array('foo' => array(true, true),
  111. // 'bar' => array('baz'));
  112. $opts = array();
  113. foreach ($opts_args[0] as $opt) {
  114. $opt[0] = $opt[0]{0} === '-' ? $opt_def[$opt[0]{2}] : $opt_def[$opt[0]{0}];
  115. $opt[1] = $opt[1] === null ? true : $opt[1];
  116. if (isset($opts[$opt[0]]) === false) {
  117. $opts[$opt[0]] = array($opt[1]);
  118. } else {
  119. $opts[$opt[0]][] = $opt[1];
  120. }
  121. }
  122. $opts_args[0] = $opts;
  123. return $opts_args;
  124. }
  125. /**
  126. * just perform
  127. *
  128. * @access public
  129. */
  130. function perform()
  131. {
  132. }
  133. /**
  134. * show usage
  135. *
  136. * @access public
  137. */
  138. function usage()
  139. {
  140. echo "usage:\n";
  141. echo $this->getUsage() . "\n\n";
  142. }
  143. }
  144. // }}}