PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/php-pear-CodeGen-PECL-1.1.3/CodeGen_PECL-1.1.3/PECL/Command.php

#
PHP | 265 lines | 166 code | 40 blank | 59 comment | 19 complexity | 434d9ff71cb9aed4623f29028dd314dc MD5 | raw file
  1. <?php
  2. /**
  3. * Command wrapper class
  4. *
  5. * PHP versions 5
  6. *
  7. * LICENSE: This source file is subject to version 3.0 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category Tools and Utilities
  14. * @package CodeGen
  15. * @author Hartmut Holzgraefe <hartmut@php.net>
  16. * @copyright 2005-2008 Hartmut Holzgraefe
  17. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  18. * @version CVS: $Id: Command.php,v 1.8 2007/03/19 18:15:08 hholzgra Exp $
  19. * @link http://pear.php.net/package/CodeGen
  20. */
  21. /**
  22. * includes
  23. */
  24. require_once "CodeGen/Command.php";
  25. require_once "CodeGen/PECL/Extension.php";
  26. require_once "CodeGen/PECL/ExtensionParser.php";
  27. /**
  28. * Command wrapper class
  29. *
  30. * This class wraps up the functionality needed for the
  31. * command line script.
  32. *
  33. * @category Tools and Utilities
  34. * @package CodeGen
  35. * @author Hartmut Holzgraefe <hartmut@php.net>
  36. * @copyright 2005-2008 Hartmut Holzgraefe
  37. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  38. * @version Release: @package_version@
  39. * @link http://pear.php.net/package/CodeGen
  40. */
  41. class CodeGen_PECL_Command
  42. extends CodeGen_Command
  43. {
  44. /**
  45. * Command constructor
  46. *
  47. * @param object Extension to work on
  48. */
  49. function __construct(CodeGen_Extension $extension)
  50. {
  51. parent::__construct($extension);
  52. if ($this->options->have("linespecs")) {
  53. $this->extension->setLinespecs(true);
  54. }
  55. }
  56. /**
  57. * Add pecl-gen specific command line options
  58. *
  59. * @return array extended options
  60. */
  61. function commandOptions()
  62. {
  63. list($shortOptions, $longOptions) = parent::commandOptions();
  64. $longOptions= array_merge($longOptions, array("extname=",
  65. "full-xml",
  66. "function=",
  67. "linespecs",
  68. "no-help",
  69. "proto=",
  70. "skel=",
  71. "stubs=",
  72. "xml=="));
  73. return array($shortOptions, $longOptions);
  74. }
  75. /**
  76. * Show usage/help information
  77. *
  78. * @param string otpional additional message
  79. */
  80. function showUsage($message = false)
  81. {
  82. $fp = fopen("php://stderr", "w");
  83. if ($message) fputs($fp, "$message\n\n");
  84. fputs($fp, "Usage:
  85. pecl-gen [-h] [--force] [--experimental] [--version]
  86. [--extname=name] [--proto=file] [--skel=dir] [--stubs=file]
  87. [--no-help] [--xml[=file]] [--full-xml] [--function=proto] [specfile.xml]
  88. -h|--help this message
  89. -f|--force overwrite existing directories
  90. -d|--dir output directory (defaults to extension name)
  91. -l|--lint check syntax only, don't create output
  92. --linespecs generate #line specs
  93. -x|--experimental deprecated
  94. --function create a function skeleton from a proto right away
  95. --version show version info
  96. the following options are inherited from ext_skel:
  97. --extname=module module is the name of your extension
  98. --proto=file file contains prototypes of functions to create
  99. --xml generate xml documentation to be added to phpdoc-cvs
  100. these wait for functionality to be implemented and are ignored for now ...
  101. --stubs=file generate only function stubs in file
  102. --no-help don't try to be nice and create comments in the code
  103. and helper functions to test if the module compiled
  104. these are accepted for backwards compatibility reasons but not used ...
  105. --full-xml generate xml documentation for a self-contained extension
  106. (this was also a no-op in ext_skel)
  107. --skel=dir path to the skeleton directory
  108. (skeleton stuff is now self-contained)
  109. ");
  110. fclose($fp);
  111. }
  112. /**
  113. * Generate just a single function stub file
  114. *
  115. */
  116. function singleFunction()
  117. {
  118. $func = new CodeGen_PECL_Element_Function;
  119. $func->setRole("public");
  120. $err = $func->setProto(trim($this->options->value("function")), $this->extension);
  121. if (PEAR::isError($err)) {
  122. $this->terminate($err->getMessage());
  123. }
  124. $err = $this->extension->addFunction($func);
  125. if (PEAR::isError($err)) {
  126. $this->terminate($err->getMessage());
  127. }
  128. echo $this->extension->publicFunctionsC();
  129. echo "\n\n/*----------------------------------------------------------------------*/\n\n";
  130. foreach ($this->extension->getFunctions() as $name => $function) {
  131. echo sprintf("\tPHP_FE(%-20s, NULL)\n", $name);
  132. }
  133. echo "\n\n/*----------------------------------------------------------------------*/\n\n";
  134. foreach ($this->extension->getFunctions() as $name => $function) {
  135. echo "PHP_FUNCTION($name);\n";
  136. }
  137. }
  138. /**
  139. * ext-skel compatibility mode
  140. *
  141. */
  142. function extSkelCompat()
  143. {
  144. $extname = $this->options->value("extname");
  145. $err = $this->extension->setName($extname);
  146. if (PEAR::isError($err)) {
  147. $this->terminate($err->getMessage());
  148. }
  149. if ($this->options->have("proto")) {
  150. $proto_file = $this->options->value("proto");
  151. if (!file_exists($proto_file) || !is_readable($proto_file)) {
  152. $this->terminate("cannot open proto file");
  153. }
  154. foreach (file($proto_file) as $line) {
  155. $func = new CodeGen_PECL_Element_Function;
  156. $func->setRole("public");
  157. $err = $func->setProto(trim($line));
  158. if (PEAR::isError($err)) {
  159. $this->terminate($err->getMessage());
  160. }
  161. $err = $this->extension->addFunction($func);
  162. if (PEAR::isError($err)) {
  163. $this->terminate($err->getMessage());
  164. }
  165. }
  166. }
  167. if ($this->options->have("stubs")) {
  168. $stubname = $this->options->value("stubs");
  169. if (file_exists("$stubname") && !$this->options->have("f", "force")) {
  170. $this->terminate("'$stubname' already exists (use '--force' to overwrite)");
  171. }
  172. $fp = fopen($stubname, "w");
  173. fputs($fp, $this->extension->publicFunctionsC());
  174. fputs($fp, "\n\n/*----------------------------------------------------------------------*/\n\n");
  175. foreach ($this->extension->functions as $name => $function) {
  176. fputs($fp, sprintf("\tPHP_FE(%-20s, NULL)\n", $name));
  177. }
  178. fputs($fp, "\n\n/*----------------------------------------------------------------------*/\n\n");
  179. foreach ($this->extension->functions as $name => $function) {
  180. fputs($fp, "PHP_FUNCTION($name);\n");
  181. }
  182. fclose($fp);
  183. echo "$stubname successfully written\n";
  184. } else {
  185. if (file_exists("./$extname") && !$this->options->have("f", "force")) {
  186. $this->terminate("'$extname' already exists, can't create directory (use '--force' to override)");
  187. }
  188. $err = System::mkdir($extname);
  189. if (PEAR::isError($err)) {
  190. $this->terminate($err->getMessage());
  191. }
  192. $this->extension->dirpath = realpath("./$extname");
  193. $err = $this->extension->generateSource("./$extname");
  194. if (PEAR::isError($err)) {
  195. $this->terminate($err->getMessage());
  196. }
  197. if ($this->options->have("xml")) {
  198. $manpath = "$extname/manual/". str_replace('_', '-', $extname);
  199. $err = System::mkdir("-p $manpath");
  200. if (PEAR::isError($err)) {
  201. $this->terminate($err->getMessage());
  202. }
  203. $err = $this->extension->generateDocumentation($manpath);
  204. if (PEAR::isError($err)) {
  205. $this->terminate($err->getMessage());
  206. }
  207. }
  208. $this->extension->writeReadme("./$extname");
  209. if (!$this->options->have("quiet")) {
  210. echo $this->extension->successMsg();
  211. }
  212. }
  213. }
  214. }