Skip to content

Commit

Permalink
classes
Browse files Browse the repository at this point in the history
  • Loading branch information
miyako committed Aug 22, 2024
1 parent d8cc1c6 commit 6cee0bd
Show file tree
Hide file tree
Showing 15 changed files with 438 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
php/userPreferences.*
php/Project/DerivedData/*
php/Data/*
php/Settings/backup.4DSettings
3 changes: 3 additions & 0 deletions php/Documentation/Classes/CGI_PHPForm.md
Original file line number Diff line number Diff line change
@@ -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`.
28 changes: 28 additions & 0 deletions php/Documentation/Classes/PHP_CGI.md
Original file line number Diff line number Diff line change
@@ -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.
39 changes: 39 additions & 0 deletions php/Documentation/Classes/_CGI.md
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 31 additions & 0 deletions php/Documentation/Classes/_PHP_CGI_CLI.md
Original file line number Diff line number Diff line change
@@ -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`|
5 changes: 5 additions & 0 deletions php/Documentation/Classes/_PHP_CGI_Controller.md
Original file line number Diff line number Diff line change
@@ -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.
53 changes: 53 additions & 0 deletions php/Project/Sources/Classes/CGI_PHPForm.4dm
Original file line number Diff line number Diff line change
@@ -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("<?php\n"+$input)

$success:=PHP Execute:C1058($f.platformPath; ""; $returnValue)

If ($success)
var $stdOut : Text
PHP GET FULL RESPONSE:C1061($stdOut)
For each ($line; Split string:C1554($stdOut; "\n"; sk ignore empty strings:K86:1))
If ($line="php > ")
break
End if
Form:C1466.push($line)
End for each
End if

return Form:C1466
102 changes: 102 additions & 0 deletions php/Project/Sources/Classes/PHP_CGI.4dm
Original file line number Diff line number Diff line change
@@ -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
76 changes: 76 additions & 0 deletions php/Project/Sources/Classes/_CGI.4dm
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions php/Project/Sources/Classes/_PHP_CGI_CLI.4dm
Original file line number Diff line number Diff line change
@@ -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)
Loading

0 comments on commit 6cee0bd

Please sign in to comment.