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

/php4delphi/ExtensionBuilder/phpExtensionBuilder.pas

http://php5delphi.googlecode.com/
Pascal | 380 lines | 329 code | 33 blank | 18 comment | 5 complexity | cfa40af182d951b7afb3552b6726eb06 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. {$I Builder.INC}
  2. unit phpExtensionBuilder;
  3. interface
  4. uses
  5. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  6. Dialogs,
  7. {$IFDEF VERSION6}
  8. DesignIntf,
  9. DesignEditors,
  10. DMForm,
  11. {$ELSE}
  12. dsgnintf,
  13. dmdesigner,
  14. {$ENDIF}
  15. ToolsAPI,
  16. frm_functions;
  17. type
  18. TExtensionExpert = class(TNotifierObject, IOTAWizard, IOTARepositoryWizard, IOTAFormWizard)
  19. public
  20. // IOTAWizard
  21. function GetIDString: string;
  22. function GetName: string;
  23. function GetState: TWizardState;
  24. procedure Execute;
  25. // IOTARepositoryWizard
  26. function GetAuthor: string;
  27. function GetComment: string;
  28. function GetPage: string;
  29. {$IFDEF VERSION6}
  30. function GetGlyph : cardinal;
  31. {$ELSE}
  32. function GetGlyph: HICON;
  33. {$ENDIF}
  34. end;
  35. TExtensionProjectCreator = class(TInterfacedObject, IOTACreator, IOTAProjectCreator)
  36. public
  37. // IOTACreator
  38. function GetCreatorType: string;
  39. function GetExisting: Boolean;
  40. function GetFileSystem: string;
  41. function GetOwner: IOTAModule;
  42. function GetUnnamed: Boolean;
  43. // IOTAProjectCreator
  44. function GetFileName: string;
  45. function GetOptionFileName: string;
  46. function GetShowSource: Boolean;
  47. procedure NewDefaultModule;
  48. function NewOptionSource(const ProjectName: string): IOTAFile;
  49. procedure NewProjectResource(const Project: IOTAProject);
  50. function NewProjectSource(const ProjectName: string): IOTAFile;
  51. end;
  52. TExtensionProjectSourceFile = class(TInterfacedObject, IOTAFile)
  53. private
  54. FSource: string;
  55. FProjectName : string;
  56. public
  57. function GetSource: string;
  58. function GetAge: TDateTime;
  59. constructor Create(const Source: string);
  60. constructor CreateNamedProject(AProjectName : string);
  61. end;
  62. const CRLF = #13#10;
  63. procedure Register;
  64. implementation
  65. {$R PHPEXT.RES}
  66. procedure Register;
  67. begin
  68. RegisterPackageWizard(TExtensionExpert.Create);
  69. end;
  70. function GetActiveProjectGroup: IOTAProjectGroup;
  71. var
  72. ModuleServices: IOTAModuleServices;
  73. i: Integer;
  74. begin
  75. Result := nil;
  76. ModuleServices := BorlandIDEServices as IOTAModuleServices;
  77. for i := 0 to ModuleServices.ModuleCount - 1 do
  78. if Succeeded(ModuleServices.Modules[i].QueryInterface(IOTAProjectGroup, Result)) then
  79. Break;
  80. end;
  81. { TExtensionExpert }
  82. procedure TExtensionExpert.Execute;
  83. begin
  84. frmFunctions := TfrmFunctions.Create(Application);
  85. if frmFunctions.ShowModal = mrOK then
  86. begin
  87. (BorlandIDEServices as IOTAModuleServices).CreateModule(TExtensionProjectCreator.Create);
  88. end;
  89. frmFunctions.Free;
  90. end;
  91. function TExtensionExpert.GetAuthor: string;
  92. begin
  93. Result := 'Serhiy Perevoznyk';
  94. end;
  95. function TExtensionExpert.GetComment: string;
  96. begin
  97. Result := 'PHP Extensions builder';
  98. end;
  99. {$IFDEF VERSION6}
  100. function TExtensionExpert.GetGlyph: cardinal;
  101. {$ELSE}
  102. function TExtensionExpert.GetGlyph: HICON;
  103. {$ENDIF}
  104. begin
  105. Result := LoadIcon(hInstance, 'PHPEXTWIZ');
  106. end;
  107. function TExtensionExpert.GetIDString: string;
  108. begin
  109. Result := '7E497181-FBF6-4070-BFD8-D98522713DE3';
  110. end;
  111. function TExtensionExpert.GetName: string;
  112. begin
  113. Result := 'PHP Extensions Wizard';
  114. end;
  115. function TExtensionExpert.GetPage: string;
  116. begin
  117. Result := 'New';
  118. end;
  119. function TExtensionExpert.GetState: TWizardState;
  120. begin
  121. Result := [wsEnabled];
  122. end;
  123. { TExtensionProjectSourceFile }
  124. constructor TExtensionProjectSourceFile.Create(const Source: string);
  125. begin
  126. FSource := Source;
  127. end;
  128. constructor TExtensionProjectSourceFile.CreateNamedProject(
  129. AProjectName: string);
  130. begin
  131. inherited Create;
  132. FProjectName := AProjectName;
  133. end;
  134. function TExtensionProjectSourceFile.GetAge: TDateTime;
  135. begin
  136. Result := -1;
  137. end;
  138. function TExtensionProjectSourceFile.GetSource: string;
  139. var
  140. S : string;
  141. I : integer;
  142. begin
  143. S := 'library ' + FProjectName + ';' + CRLF +
  144. '{$I PHP.INC}' + CRLF +
  145. 'uses' + CRLF +
  146. ' Windows,' + CRLF +
  147. ' SysUtils,' + CRLF +
  148. ' ZendAPI,' + CRLF +
  149. ' ZendTypes,' + CRLF +
  150. ' PHPAPI,' + CRLF +
  151. ' PHPTypes;' + CRLF + CRLF +
  152. '{$R *.RES}' + CRLF + CRLF +
  153. 'function rinit (_type : integer; module_number : integer; TSRMLS_DC : pointer) : integer; cdecl;' + CRLF +
  154. 'begin' + CRLF +
  155. ' Result := SUCCESS;' + CRLF +
  156. 'end;' + CRLF +
  157. '' + CRLF +
  158. 'function rshutdown (_type : integer; module_number : integer; TSRMLS_DC : pointer) : integer; cdecl;' + CRLF +
  159. 'begin' + CRLF +
  160. ' Result := SUCCESS;' + CRLF +
  161. 'end;' + CRLF +
  162. '' + CRLF +
  163. 'procedure php_info_module(zend_module : Pzend_module_entry; TSRMLS_DC : pointer); cdecl;' + CRLF +
  164. 'begin' + CRLF +
  165. ' php_info_print_table_start();' + CRLF +
  166. ' php_info_print_table_row(2, PChar(''module name''), PChar(''enabled''));' + CRLF +
  167. ' php_info_print_table_end();' + CRLF +
  168. 'end;' + CRLF +
  169. '' + CRLF +
  170. 'function minit (_type : integer; module_number : integer; TSRMLS_DC : pointer) : integer; cdecl;' + CRLF +
  171. 'begin' + CRLF +
  172. ' RESULT := SUCCESS;' + CRLF +
  173. 'end;' + CRLF +
  174. '' + CRLF +
  175. 'function mshutdown (_type : integer; module_number : integer; TSRMLS_DC : pointer) : integer; cdecl;' + CRLF +
  176. 'begin' + CRLF +
  177. ' RESULT := SUCCESS;' + CRLF +
  178. 'end;' + CRLF +
  179. '' + CRLF + CRLF;
  180. for i := 0 to frmFunctions.Functions.Lines.Count - 1 do
  181. begin
  182. S := S +
  183. '{$IFDEF PHP510}' + CRLF +
  184. 'procedure ' + LowerCase(frmFunctions.Functions.Lines[i])+' (ht : integer; return_value : pzval; return_value_ptr : ppzval; this_ptr : pzval;' + CRLF +
  185. ' return_value_used : integer; TSRMLS_DC : pointer); cdecl;' + CRLF +
  186. '{$ELSE}'+ CRLF +
  187. 'procedure ' + LowerCase(frmFunctions.Functions.Lines[i])+' (ht : integer; return_value : pzval; this_ptr : pzval;' + CRLF +
  188. ' return_value_used : integer; TSRMLS_DC : pointer); cdecl;' + CRLF +
  189. '{$ENDIF}'+ CRLF +
  190. 'var'+ CRLF +
  191. ' param : pzval_array;' + CRLF +
  192. 'begin' + CRLF +
  193. ' if ( not (zend_get_parameters_ex(ht, Param) = SUCCESS )) then' + CRLF +
  194. ' begin' + CRLF +
  195. ' zend_wrong_param_count(TSRMLS_DC);' + CRLF +
  196. ' Exit;' + CRLF +
  197. ' end;' + CRLF +
  198. '' + CRLF +
  199. ' dispose_pzval_array(param);' + CRLF +
  200. '' + CRLF +
  201. 'end;' + CRLF + CRLF;
  202. end;
  203. S := S +
  204. 'var' + CRLF +
  205. ' moduleEntry : Tzend_module_entry;' + CRLF +
  206. ' module_entry_table : array[0..'+IntToStr(frmFunctions.functions.Lines.count)+'] of zend_function_entry;' + CRLF +
  207. '' + CRLF +
  208. '' + CRLF +
  209. 'function get_module : Pzend_module_entry; cdecl;' + CRLF +
  210. 'begin' + CRLF +
  211. ' if not PHPLoaded then' + CRLF +
  212. ' LoadPHP;' + CRLF +
  213. ' ModuleEntry.size := sizeof(Tzend_module_entry);' + CRLF +
  214. ' ModuleEntry.zend_api := ZEND_MODULE_API_NO;' + CRLF +
  215. ' ModuleEntry.zts := USING_ZTS;' + CRLF +
  216. ' ModuleEntry.Name := ''module name'';' + CRLF +
  217. ' ModuleEntry.version := ''1.0'';' + CRLF +
  218. ' ModuleEntry.module_startup_func := @minit;' + CRLF +
  219. ' ModuleEntry.module_shutdown_func := @mshutdown;' + CRLF +
  220. ' ModuleEntry.request_startup_func := @rinit;' + CRLF +
  221. ' ModuleEntry.request_shutdown_func := @rshutdown;' + CRLF +
  222. ' ModuleEntry.info_func := @php_info_module;' + CRLF +
  223. '' + CRLF;
  224. for i := 0 to frmFunctions.Functions.Lines.Count - 1 do
  225. begin
  226. S := S +
  227. ' Module_entry_table['+IntToStr(i)+'].fname := '+ QuotedStr(LowerCase(frmFunctions.Functions.Lines[i]))+';' + CRLF +
  228. ' Module_entry_table['+IntToStr(i)+'].handler := @'+frmFunctions.Functions.Lines[i]+';' + CRLF +
  229. '' + CRLF;
  230. end;
  231. S := S + ' ModuleEntry.functions := @module_entry_table[0];' + CRLF +
  232. ' ModuleEntry._type := MODULE_PERSISTENT;' + CRLF +
  233. ' Result := @ModuleEntry;' + CRLF +
  234. 'end;' + CRLF +
  235. '' + CRLF +
  236. '' + CRLF +
  237. 'exports' + CRLF +
  238. ' get_module;' + CRLF +
  239. '' + CRLF +
  240. 'end.';
  241. Result := S;
  242. end;
  243. { TExtensionProjectCreator }
  244. function TExtensionProjectCreator.GetCreatorType: string;
  245. begin
  246. Result := sLibrary;
  247. end;
  248. function TExtensionProjectCreator.GetExisting: Boolean;
  249. begin
  250. Result := false;
  251. end;
  252. function TExtensionProjectCreator.GetFileName: string;
  253. var
  254. i: Integer;
  255. j: Integer;
  256. ProjGroup: IOTAProjectGroup;
  257. Found: Boolean;
  258. TempFileName: string;
  259. TempFileName2: string;
  260. begin
  261. Result := GetCurrentDir + '\' + 'Project%d' + '.dpr'; { do not localize }
  262. ProjGroup := GetActiveProjectGroup;
  263. if ProjGroup <> nil then
  264. begin
  265. for j := 0 to ProjGroup.ProjectCount-1 do
  266. begin
  267. Found := False;
  268. TempFileName2 := Format(Result, [j+1]);
  269. for i := 0 to ProjGroup.ProjectCount-1 do
  270. begin
  271. try
  272. TempFileName := ProjGroup.Projects[i].FileName;
  273. if AnsiCompareFileName(ExtractFileName(TempFileName), ExtractFileName(TempFileName2)) = 0 then
  274. begin
  275. Found := True;
  276. Break;
  277. end;
  278. except on E: Exception do
  279. if not (E is EIntfCastError) then
  280. raise;
  281. end;
  282. end;
  283. if not Found then
  284. begin
  285. Result := TempFileName2;
  286. Exit;
  287. end;
  288. end;
  289. Result := Format(Result, [ProjGroup.ProjectCount+1]);
  290. end
  291. else
  292. Result := Format(Result, [1]);
  293. end;
  294. function TExtensionProjectCreator.GetFileSystem: string;
  295. begin
  296. Result := '';
  297. end;
  298. function TExtensionProjectCreator.GetOptionFileName: string;
  299. begin
  300. Result := '';
  301. end;
  302. function TExtensionProjectCreator.GetOwner: IOTAModule;
  303. begin
  304. Result := GetActiveProjectGroup;
  305. end;
  306. function TExtensionProjectCreator.GetShowSource: Boolean;
  307. begin
  308. Result := true;
  309. end;
  310. function TExtensionProjectCreator.GetUnnamed: Boolean;
  311. begin
  312. Result := true;
  313. end;
  314. procedure TExtensionProjectCreator.NewDefaultModule;
  315. begin
  316. end;
  317. function TExtensionProjectCreator.NewOptionSource(
  318. const ProjectName: string): IOTAFile;
  319. begin
  320. Result := nil;
  321. end;
  322. procedure TExtensionProjectCreator.NewProjectResource(
  323. const Project: IOTAProject);
  324. begin
  325. end;
  326. function TExtensionProjectCreator.NewProjectSource(
  327. const ProjectName: string): IOTAFile;
  328. begin
  329. Result := TExtensionProjectSourceFile.CreateNamedProject(ProjectName) as IOTAFile;
  330. end;
  331. end.