From 6cee0bd52d3fad0af016274336031e7d355abda5 Mon Sep 17 00:00:00 2001 From: Keisuke Miyako Date: Thu, 22 Aug 2024 16:41:41 +0900 Subject: [PATCH] classes --- .gitignore | 4 + php/Documentation/Classes/CGI_PHPForm.md | 3 + php/Documentation/Classes/PHP_CGI.md | 28 +++++ php/Documentation/Classes/_CGI.md | 39 +++++++ php/Documentation/Classes/_PHP_CGI_CLI.md | 31 ++++++ .../Classes/_PHP_CGI_Controller.md | 5 + php/Project/Sources/Classes/CGI_PHPForm.4dm | 53 +++++++++ php/Project/Sources/Classes/PHP_CGI.4dm | 102 ++++++++++++++++++ php/Project/Sources/Classes/_CGI.4dm | 76 +++++++++++++ php/Project/Sources/Classes/_PHP_CGI_CLI.4dm | 26 +++++ .../Sources/Classes/_PHP_CGI_Controller.4dm | 16 +++ php/Project/Sources/Methods/TEST_CGI.4dm | 12 +++ .../Sources/Methods/TEST_Interactive.4dm | 12 +++ php/Project/Sources/Methods/TEST_Run.4dm | 12 +++ php/Project/Sources/Methods/TEST_Worker.4dm | 19 ++++ 15 files changed, 438 insertions(+) create mode 100644 .gitignore create mode 100644 php/Documentation/Classes/CGI_PHPForm.md create mode 100644 php/Documentation/Classes/PHP_CGI.md create mode 100644 php/Documentation/Classes/_CGI.md create mode 100644 php/Documentation/Classes/_PHP_CGI_CLI.md create mode 100644 php/Documentation/Classes/_PHP_CGI_Controller.md create mode 100644 php/Project/Sources/Classes/CGI_PHPForm.4dm create mode 100644 php/Project/Sources/Classes/PHP_CGI.4dm create mode 100644 php/Project/Sources/Classes/_CGI.4dm create mode 100644 php/Project/Sources/Classes/_PHP_CGI_CLI.4dm create mode 100644 php/Project/Sources/Classes/_PHP_CGI_Controller.4dm create mode 100644 php/Project/Sources/Methods/TEST_CGI.4dm create mode 100644 php/Project/Sources/Methods/TEST_Interactive.4dm create mode 100644 php/Project/Sources/Methods/TEST_Run.4dm create mode 100644 php/Project/Sources/Methods/TEST_Worker.4dm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff195d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +php/userPreferences.* +php/Project/DerivedData/* +php/Data/* +php/Settings/backup.4DSettings diff --git a/php/Documentation/Classes/CGI_PHPForm.md b/php/Documentation/Classes/CGI_PHPForm.md new file mode 100644 index 0000000..81c45ca --- /dev/null +++ b/php/Documentation/Classes/CGI_PHPForm.md @@ -0,0 +1,3 @@ +# CGI_PHPForm : _PHPForm : _Form + +`CGI_PHPForm` is a subclass of `_PHPForm` to call `php-cgi` through the native command `PHP Execute`. \ No newline at end of file diff --git a/php/Documentation/Classes/PHP_CGI.md b/php/Documentation/Classes/PHP_CGI.md new file mode 100644 index 0000000..21d7175 --- /dev/null +++ b/php/Documentation/Classes/PHP_CGI.md @@ -0,0 +1,28 @@ +# PHP_CGI : _CGI + +`PHP_CGI` is a subclass of `_CGI` to control `php-cgi`, the FastCGI version of PHP. Use this class to reactivate the native command `PHP Execute` in v20 R3 or later. + +## .new() + +**.new**($controller : 4D.Class; $ini : 4D.File) : cs.PHP_CGI + +Pass a subclass of `_CLI_Controller` and a php.ini file. + +Properties: + +|Property|Type|Description| +|:-|:-|:-| +|utilityFile|4D.File|the *_4D_Execute_PHP.php* file in /RESOURCES/ (read-only)| +|FULL_PATH_TO_4D_Execute_PHP|Text|the quoted platform path to This.utilityFile (read-only)| + +## .cgi() + +**.cgi**() : cs.PHP_CGI + +Launch the `php-cgi` program as an external PHP interpreter in a worker using `_PHP_CGI_CLI`. The `_PHP_CGI_CLI` instance is stored in a process variable in the worker's execution context. The path to `This.utilityFile` is automatically added to the php.ini file. + +## .terminate() + +**.terminate**() + +Terminate the `php-cgi` program. \ No newline at end of file diff --git a/php/Documentation/Classes/_CGI.md b/php/Documentation/Classes/_CGI.md new file mode 100644 index 0000000..86e92e3 --- /dev/null +++ b/php/Documentation/Classes/_CGI.md @@ -0,0 +1,39 @@ +# _CGI + +`_CGI` is a class for managing local CGI instances in a worker. Extend this class to control a specific CGI program. The CGI should use be launched as a `_CLI` instance. + +## .new() + +**.new**($name : Text) : cs._CGI + +`$name` will be the worker name spawned to start the CGI. The callee process need not be a worker. The name is also used as a global semaphore to prevent the creation of multiple instance of the same CGI. + +Properties: + +|Property|Type|Description| +|:-|:-|:-| +|name|Text|unique name that identifies the CGI (read-only)| + +## .expand() + +**.expand**($in : Object) : Object + +Expand a file system path of a `4D.File` or `4D.Folder` to a platform path suitable for the command line interface. + +## .quote() + +**.quote**($in : Text) : Text + +Double-quote a path string. + +## .start() + +**.start**($function : 4D.Function) + +Invoke `$function` in a worker `This.name` with a global semaphore. The function should launch the CGI and return an instance of `_CLI`. The instance is stored in a process variable in the worker's execution context and used to terminate the CGI in `This.stop()`. + +## .stop() + +**.stop**() + +Invoke `4D.SystemWorker.terminate()` with a global semaphore. \ No newline at end of file diff --git a/php/Documentation/Classes/_PHP_CGI_CLI.md b/php/Documentation/Classes/_PHP_CGI_CLI.md new file mode 100644 index 0000000..6106971 --- /dev/null +++ b/php/Documentation/Classes/_PHP_CGI_CLI.md @@ -0,0 +1,31 @@ +# _PHP_CGI_CLI : _CLI + +`_PHP_CGI_CLI` is a subclass of `_CLI` to launch `php-cgi`, the FastCGI version of PHP. + +## .new() + +**.new**($controller : 4D.Class; $ini : 4D.File) : cs._PHP_CGI_CLI + +Pass a subclass of `_CLI_Controller` and a php.ini file. + +Properties: + +|Property|Type|Description| +|:-|:-|:-| +|utilityFile|4D.File|the *_4D_Execute_PHP.php* file in /RESOURCES/ (read-only)| +|FULL_PATH_TO_4D_Execute_PHP|Text|the quoted platform path to This.utilityFile (read-only)| + +## .cgi() + +**.cgi**($options : Object) : cs._PHP_CGI_CLI + +Launch `php-cgi` with the command line arguments `-b` (binding) and `-c` or `-n`. + +Options: + +|Property|Type|Description| +|:-|:-|:-| +|address|Text|normally database parameter `PHP interpreter IP address`| +|port|Number|normally database parameter `PHP interpreter IP address`| +|children|Number|normally database parameter `_o_PHP number of children`| +|requests|Number|normally database parameter `_o_PHP max requests`| \ No newline at end of file diff --git a/php/Documentation/Classes/_PHP_CGI_Controller.md b/php/Documentation/Classes/_PHP_CGI_Controller.md new file mode 100644 index 0000000..e666c3b --- /dev/null +++ b/php/Documentation/Classes/_PHP_CGI_Controller.md @@ -0,0 +1,5 @@ +# _PHP_CGI_Controller : _PHP_Controller : _CLI_Controller + +`_PHP_CGI_Controller` is a subclass of `_CLI_Controller` for the `PHP_CGI` class. + +This class is prototype only. \ No newline at end of file diff --git a/php/Project/Sources/Classes/CGI_PHPForm.4dm b/php/Project/Sources/Classes/CGI_PHPForm.4dm new file mode 100644 index 0000000..2aeb224 --- /dev/null +++ b/php/Project/Sources/Classes/CGI_PHPForm.4dm @@ -0,0 +1,53 @@ +Class extends _PHPForm + +Class constructor + + Super:C1705() + + $window:=Open form window:C675("PHP") + DIALOG:C40("PHP"; This:C1470; *) + +Function onLoad() + + If (Is Windows:C1573) + $ini:=File:C1566("/RESOURCES/php/php.ini") + End if + + Form:C1466.PHP:=cs:C1710.PHP_CGI.new(cs:C1710._PHP_CGI_Controller; $ini) + + Form:C1466.clearOutput().clearInput().clearHistory() + + Form:C1466.PHP.cgi() + +Function onUnload() + + Form:C1466.PHP.terminate() + +Function post() + + If (OBJECT Get name:C1087(Object with focus:K67:3)="Input") + $input:=Get edited text:C655 + Else + $input:=Form:C1466.input + End if + + Form:C1466.history.insert(-1; $input) + Form:C1466.selectHistory(Form:C1466.history.length-1) + + $f:=Folder:C1567(Temporary folder:C486; fk platform path:K87:2).file(Generate UUID:C1066+".php") + $f.setText(" ") + break + End if + Form:C1466.push($line) + End for each + End if + + return Form:C1466 \ No newline at end of file diff --git a/php/Project/Sources/Classes/PHP_CGI.4dm b/php/Project/Sources/Classes/PHP_CGI.4dm new file mode 100644 index 0000000..481504e --- /dev/null +++ b/php/Project/Sources/Classes/PHP_CGI.4dm @@ -0,0 +1,102 @@ +Class extends _CGI + +shared singleton Class constructor($controller : 4D:C1709.Class; $ini : 4D:C1709.File) + + Super:C1705("PHP") + + This:C1470.controller:=$controller + + If (OB Instance of:C1731($ini; 4D:C1709.File)) + If ($ini.exists) + This:C1470.ini:=This:C1470.expand($ini) + End if + End if + + var $temporaryFolder : 4D:C1709.Folder + $temporaryFolder:=Folder:C1567(Temporary folder:C486; fk platform path:K87:2).folder(Generate UUID:C1066) + $temporaryFolder.create() + + var $utilityFile : 4D:C1709.File + $utilityFile:=File:C1566("/RESOURCES/php/_4D_Execute_PHP.php") + $utilityFile:=This:C1470.expand($utilityFile.copyTo($temporaryFolder)) + + Use (This:C1470) + This:C1470._utilityFile:=$utilityFile + End use + +Function get utilityFile() + + return This:C1470._utilityFile + +Function get FULL_PATH_TO_4D_Execute_PHP() + + return This:C1470.quote(This:C1470.utilityFile.path) + +Function _auto_prepend_file() + + If (This:C1470.ini#Null:C1517) + + var $php : Text + + $php:=This:C1470.ini.getText() + + ARRAY LONGINT:C221($pos; 0) + ARRAY LONGINT:C221($len; 0) + + If (Match regex:C1019("([;[:Zs:]]*(auto_prepend_file)(\\s*=))"; $php; 1; $pos; $len)) + + $c:=Substring:C12($php; $pos{2}; $len{2}) //auto_prepend_file + $c+=" = " + $c+=This:C1470.FULL_PATH_TO_4D_Execute_PHP + + $b:=Substring:C12($php; 1; $pos{1}-1) + $a:=Substring:C12($php; $pos{1}+$len{1}) + + This:C1470._setPhpIni($b+$c+$a) + + Else + //don't prepend file + End if + + Else + This:C1470._setPhpIni("auto_prepend_file = "+This:C1470.FULL_PATH_TO_4D_Execute_PHP) + End if + +Function _cgi($class : 4D:C1709.Class; $ini : 4D:C1709.File)->$controller : cs:C1710._PHP_CGI_CLI + + $options:={} + var $stringValue : Text + + $options.address:=(Get database parameter:C643(PHP interpreter IP address:K37:59; $stringValue)) && $stringValue + $options.port:=Get database parameter:C643(PHP interpreter port:K37:55) + $options.children:=Get database parameter:C643(_o_PHP number of children:K37:56) + $options.requests:=Get database parameter:C643(_o_PHP max requests:K37:57) + + SET DATABASE PARAMETER:C642(_o_PHP use external interpreter:K37:58; 1) + + $controller:=cs:C1710._PHP_CGI_CLI.new($class; $ini).cgi($options) + +shared Function _setPhpIni($ini : Text) + + This:C1470.ini:=This:C1470.expand(File:C1566("/LOGS/php-cgi.ini")) + This:C1470.ini.setText($ini) + +Function cgi()->$this : cs:C1710.PHP_CGI + + $this:=This:C1470 + + If (Not:C34(This:C1470.isRunning)) + + This:C1470._auto_prepend_file() + + This:C1470.start(This:C1470._cgi; This:C1470.controller; This:C1470.ini) + + End if + +Function terminate() + + If (This:C1470.isRunning) + + This:C1470.stop() + + End if \ No newline at end of file diff --git a/php/Project/Sources/Classes/_CGI.4dm b/php/Project/Sources/Classes/_CGI.4dm new file mode 100644 index 0000000..0a48768 --- /dev/null +++ b/php/Project/Sources/Classes/_CGI.4dm @@ -0,0 +1,76 @@ +shared singleton Class constructor($name : Text) + + This:C1470._name:=$name + This:C1470._isRunning:=False:C215 + + var __CLI__ : Object + +Function get name() + + return This:C1470._name + +Function get isRunning : Boolean + + return This:C1470._isRunning + +shared Function setRunning($isRunning : Boolean) + + This:C1470._isRunning:=$isRunning + +Function expand($in : Object)->$out : Object + + $out:=OB Class:C1730($in).new($in.platformPath; fk platform path:K87:2) + +Function quote($in : Text)->$out : Text + + $out:="\""+$in+"\"" + +Function start($function : 4D:C1709.Function; ... ) + + $signal:=New signal:C1641 + + CALL WORKER:C1389(This:C1470.name; This:C1470._start; This:C1470; $function; $signal; Copy parameters:C1790(2)) + + $signal.wait() + +Function _start($that : cs:C1710._CGI; $function : 4D:C1709.Function; $signal : 4D:C1709.Signal; $params : Collection) + + var $CLI : cs:C1710._CLI + + $CLI:=$function.apply($that; $params) + + //can't use OB Copy because a SystemWorker can't be cloned as a shared object + + __CLI__:=$CLI + + $that.setRunning(True:C214) + + $signal.trigger() + +Function stop() + + $signal:=New signal:C1641 + + CALL WORKER:C1389(This:C1470.name; This:C1470._stop; This:C1470; $signal) + + $signal.wait() + +Function _stop($that : cs:C1710._CGI; $signal : 4D:C1709.Signal) + + var $CLI : cs:C1710._CLI + + $CLI:=__CLI__ + + If ($CLI.controller.worker#Null:C1517) + + $CLI.controller.worker.terminate() + + End if + + CLEAR VARIABLE:C89(__CLI__) + + $that.setRunning(False:C215) + + $signal.trigger() + + KILL WORKER:C1390 \ No newline at end of file diff --git a/php/Project/Sources/Classes/_PHP_CGI_CLI.4dm b/php/Project/Sources/Classes/_PHP_CGI_CLI.4dm new file mode 100644 index 0000000..1fb8163 --- /dev/null +++ b/php/Project/Sources/Classes/_PHP_CGI_CLI.4dm @@ -0,0 +1,26 @@ +Class extends _CLI + +Class constructor($controller : 4D:C1709.Class; $ini : 4D:C1709.File) + + Super:C1705("php-cgi"; $controller) + + If (OB Instance of:C1731($ini; 4D:C1709.File)) + If ($ini.exists) + This:C1470.ini:=This:C1470.expand($ini).path + End if + End if + +Function cgi($options : Object)->$this : cs:C1710._PHP_CGI_CLI + + $this:=This:C1470 + + $command:=This:C1470.escape(This:C1470.executablePath) + $command+=" -b "+$options.address+":"+String:C10($options.port) + + If (This:C1470.ini#Null:C1517) + $command+=" -c "+This:C1470.escape(This:C1470.ini) + Else + $command+=" -n " + End if + + This:C1470.controller.execute($command) \ No newline at end of file diff --git a/php/Project/Sources/Classes/_PHP_CGI_Controller.4dm b/php/Project/Sources/Classes/_PHP_CGI_Controller.4dm new file mode 100644 index 0000000..585d8b4 --- /dev/null +++ b/php/Project/Sources/Classes/_PHP_CGI_Controller.4dm @@ -0,0 +1,16 @@ +Class extends _PHP_Controller + +Class constructor($CLI : cs:C1710._CLI) + + Super:C1705($CLI) + +Function onData($worker : 4D:C1709.SystemWorker; $params : Object) + +Function onDataError($worker : 4D:C1709.SystemWorker; $params : Object) + +Function onResponse($worker : 4D:C1709.SystemWorker; $params : Object) + +Function onError($worker : 4D:C1709.SystemWorker; $params : Object) + +Function onTerminate($worker : 4D:C1709.SystemWorker; $params : Object) + \ No newline at end of file diff --git a/php/Project/Sources/Methods/TEST_CGI.4dm b/php/Project/Sources/Methods/TEST_CGI.4dm new file mode 100644 index 0000000..9c87641 --- /dev/null +++ b/php/Project/Sources/Methods/TEST_CGI.4dm @@ -0,0 +1,12 @@ +//%attributes = {} +#DECLARE($params : Object) + +If (Count parameters:C259=0) + + CALL WORKER:C1389(1; Current method name:C684; {}) + +Else + + $form:=cs:C1710.CGI_PHPForm.new() + +End if \ No newline at end of file diff --git a/php/Project/Sources/Methods/TEST_Interactive.4dm b/php/Project/Sources/Methods/TEST_Interactive.4dm new file mode 100644 index 0000000..2b8525d --- /dev/null +++ b/php/Project/Sources/Methods/TEST_Interactive.4dm @@ -0,0 +1,12 @@ +//%attributes = {} +#DECLARE($params : Object) + +If (Count parameters:C259=0) + + CALL WORKER:C1389(1; Current method name:C684; {}) + +Else + + $form:=cs:C1710.Interactive_PHPForm.new() + +End if \ No newline at end of file diff --git a/php/Project/Sources/Methods/TEST_Run.4dm b/php/Project/Sources/Methods/TEST_Run.4dm new file mode 100644 index 0000000..b1753e1 --- /dev/null +++ b/php/Project/Sources/Methods/TEST_Run.4dm @@ -0,0 +1,12 @@ +//%attributes = {} +#DECLARE($params : Object) + +If (Count parameters:C259=0) + + CALL WORKER:C1389(1; Current method name:C684; {}) + +Else + + $form:=cs:C1710.Run_PHPForm.new() + +End if diff --git a/php/Project/Sources/Methods/TEST_Worker.4dm b/php/Project/Sources/Methods/TEST_Worker.4dm new file mode 100644 index 0000000..f970beb --- /dev/null +++ b/php/Project/Sources/Methods/TEST_Worker.4dm @@ -0,0 +1,19 @@ +//%attributes = {} +If (Is Windows:C1573) + $ini:=File:C1566("/RESOURCES/php/php.ini") +End if + +$CGI:=cs:C1710.PHP_CGI.new(cs:C1710._PHP_CGI_Controller; $ini).cgi() + +$CGI.terminate() + +$f:=File:C1566("/RESOURCES/php/function.php") +$f:=OB Class:C1730($f).new($f.platformPath; fk platform path:K87:2) + +var $returnValue : Text + +If (PHP Execute:C1058($f.platformPath; "sum"; $returnValue; 5; 3)) + + ALERT:C41($returnValue) + +End if \ No newline at end of file