build_api_class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file dev/skeletons/build_api_class.php
  20. * \ingroup core
  21. * \brief Create a complete API class file from existant class file
  22. */
  23. $sapi_type = php_sapi_name();
  24. $script_file = basename(__FILE__);
  25. $path=dirname(__FILE__).'/';
  26. // Test if batch mode
  27. if (substr($sapi_type, 0, 3) == 'cgi') {
  28. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  29. exit;
  30. }
  31. // Include Dolibarr environment
  32. require_once($path."../../htdocs/master.inc.php");
  33. // After this $db is a defined handler to database.
  34. // Main
  35. $version='1';
  36. @set_time_limit(0);
  37. $error=0;
  38. $langs->load("main");
  39. print "***** $script_file ($version) *****\n";
  40. // -------------------- START OF BUILD_API_FROM_CLASS --------------------
  41. // Check parameters
  42. if (! isset($argv[1]) && ! isset($argv[2]))
  43. {
  44. print "Usage: $script_file phpClassFile phpClassName\n";
  45. exit;
  46. }
  47. // Show parameters
  48. print 'Classfile='.$argv[1]."\n";
  49. print 'Classname='.$argv[2]."\n";
  50. $classfile=$argv[1];
  51. $classname=$argv[2];
  52. $classmin=strtolower($classname);
  53. $classnameApi = $classname.'Api';
  54. $property=array();
  55. $targetcontent='';
  56. // Load the class and read properties
  57. require_once($classfile);
  58. $property=array();
  59. $class = new $classname($db);
  60. $values=get_class_vars($classname);
  61. unset($values['db']);
  62. unset($values['error']);
  63. unset($values['errors']);
  64. unset($values['element']);
  65. unset($values['table_element']);
  66. unset($values['table_element_line']);
  67. unset($values['fk_element']);
  68. unset($values['ismultientitymanaged']);
  69. // Read skeleton_api_class.class.php file
  70. $skeletonfile=$path.'skeleton_api_class.class.php';
  71. $sourcecontent=file_get_contents($skeletonfile);
  72. if (! $sourcecontent)
  73. {
  74. print "\n";
  75. print "Error: Failed to read skeleton sample '".$skeletonfile."'\n";
  76. print "Try to run script from skeletons directory.\n";
  77. exit;
  78. }
  79. // Define output variables
  80. $outfile='out.api_'.$classmin.'.class.php';
  81. $targetcontent=$sourcecontent;
  82. // Substitute class name
  83. $targetcontent=preg_replace('/skeleton_api_class\.class\.php/', 'api_'.$classmin.'.class.php', $targetcontent);
  84. $targetcontent=preg_replace('/skeleton/', $classmin, $targetcontent);
  85. //$targetcontent=preg_replace('/\$table_element=\'skeleton\'/', '\$table_element=\''.$tablenoprefix.'\'', $targetcontent);
  86. $targetcontent=preg_replace('/SkeletonApi/', $classnameApi, $targetcontent);
  87. $targetcontent=preg_replace('/Skeleton/', $classname, $targetcontent);
  88. // Build file
  89. $fp=fopen($outfile,"w");
  90. if ($fp)
  91. {
  92. fputs($fp, $targetcontent);
  93. fclose($fp);
  94. print "\n";
  95. print "File '".$outfile."' has been built in current directory.\n";
  96. }
  97. else $error++;
  98. // -------------------- END OF BUILD_CLASS_FROM_TABLE SCRIPT --------------------
  99. print "You can now rename generated files by removing the 'out.' prefix in their name and store them into directory /module/class.\n";
  100. return $error;