Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 29 additions & 15 deletions cli/Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
TARGET := dphp-linux-*
BIN_PATH := ../bin
DOCKER_IMAGE := builder
DOCKER_TARGET := cli-base-alpine
BUILD_PATH := /go/src/app/cli/dist
DEBUG ?= 0

${BIN_PATH}/${TARGET}: cli.go */* go.mod build.sh build-php.sh ../Dockerfile
mkdir -p ${BIN_PATH}
cd .. && docker buildx build --pull --load --target ${DOCKER_TARGET} -t ${DOCKER_IMAGE} .
docker create --name ${DOCKER_IMAGE} ${DOCKER_IMAGE} || ( docker rm -f ${DOCKER_IMAGE} && false )
docker cp ${DOCKER_IMAGE}:${BUILD_PATH}/dphp ${BIN_PATH}/ || ( docker rm -f ${DOCKER_IMAGE} && false )
docker rm -f ${DOCKER_IMAGE}
upx -9 --force-pie ../bin/dphp-*
PHP_INCLUDES := $(shell php-config --includes)
PHP_LDFLAGS := $(shell php-config --ldflags)
PHP_LIBS := $(shell php-config --libs)

../dist: ${BIN_PATH}/${TARGET}
docker create --name builder builder
docker cp ${DOCKER_IMAGE}:${BUILD_PATH} ../dist
ifeq ($(DEBUG),1)
XCADDY_FLAGS := -gcflags='all=-N -l' -tags=nobadger,nomysql,nopgx,nodphp,nobrotli
else
XCADDY_FLAGS := -ldflags='-w -s' -tags=nobadger,nomysql,nopgx,nodphp,nobrotli
endif

LOCAL_MODULE := /home/withinboredom/code/durable-php/cli
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove hardcoded absolute paths for portability

The hardcoded paths will break when run on different machines or environments.

Use relative paths or environment variables instead:

-LOCAL_MODULE := /home/withinboredom/code/durable-php/cli
+LOCAL_MODULE := $(shell pwd)

 # ... other code ...

-	GEN_STUB_SCRIPT=/home/withinboredom/code/php-src/build/gen_stub.php \
+	GEN_STUB_SCRIPT=$${GEN_STUB_SCRIPT:-$(shell which gen_stub.php)} \

Also applies to: 22-22

🤖 Prompt for AI Agents
In cli/Makefile at lines 8 and 22, replace the hardcoded absolute paths with
relative paths or environment variables to improve portability across different
machines and environments. Identify the base directory relative to the Makefile
location or use environment variables to dynamically set the path, ensuring the
Makefile works correctly regardless of the user's file system structure.

GEN_STUB := /home/withinboredom/code/php-src/build/gen_stub.php
LOCAL_FRANKENPHP_CADDY := /home/withinboredom/code/frankenphp/caddy
LOCAL_FRANKENPHP := /home/withinboredom/code/frankenphp

# Targets
frankenphp: ext/build/ext.go ext/build/ext_arginfo.h
CGO_ENABLED=1 \
XCADDY_GO_BUILD_FLAGS="$(XCADDY_FLAGS)" \
CGO_CFLAGS="$(PHP_INCLUDES)" \
CGO_LDFLAGS="$(PHP_LDFLAGS) $(PHP_LIBS)" \
xcaddy build \
--output frankenphp \
--with github.com/dunglas/frankenphp/caddy=$(LOCAL_FRANKENPHP_CADDY) \
--with github.com/dunglas/frankenphp=$(LOCAL_FRANKENPHP) \
--with github.com/bottledcode/durable-php/cli=$(LOCAL_MODULE)
Comment on lines +19 to +28
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add phony targets and improve build organization

The Makefile is missing standard phony targets and could benefit from better organization.

Apply this diff to add missing phony targets and improve structure:

+.PHONY: all clean test frankenphp
+
+all: frankenphp
+
+clean:
+	rm -f frankenphp
+	rm -f ext/build/ext_arginfo.h
+
+test:
+	@echo "No tests defined"
+
 # Targets
 frankenphp: ext/build/ext.go ext/build/ext_arginfo.h
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
frankenphp: ext/build/ext.go ext/build/ext_arginfo.h
CGO_ENABLED=1 \
XCADDY_GO_BUILD_FLAGS="$(XCADDY_FLAGS)" \
CGO_CFLAGS="$(PHP_INCLUDES)" \
CGO_LDFLAGS="$(PHP_LDFLAGS) $(PHP_LIBS)" \
xcaddy build \
--output frankenphp \
--with github.com/dunglas/frankenphp/caddy=$(LOCAL_FRANKENPHP_CADDY) \
--with github.com/dunglas/frankenphp=$(LOCAL_FRANKENPHP) \
--with github.com/bottledcode/durable-php/cli=$(LOCAL_MODULE)
.PHONY: all clean test frankenphp
all: frankenphp
clean:
rm -f frankenphp
rm -f ext/build/ext_arginfo.h
test:
@echo "No tests defined"
# Targets
frankenphp: ext/build/ext.go ext/build/ext_arginfo.h
CGO_ENABLED=1 \
XCADDY_GO_BUILD_FLAGS="$(XCADDY_FLAGS)" \
CGO_CFLAGS="$(PHP_INCLUDES)" \
CGO_LDFLAGS="$(PHP_LDFLAGS) $(PHP_LIBS)" \
xcaddy build \
--output frankenphp \
--with github.com/dunglas/frankenphp/caddy=$(LOCAL_FRANKENPHP_CADDY) \
--with github.com/dunglas/frankenphp=$(LOCAL_FRANKENPHP) \
--with github.com/bottledcode/durable-php/cli=$(LOCAL_MODULE)
🧰 Tools
🪛 checkmake (0.2.2)

[warning] 19-19: Target body for "frankenphp" exceeds allowed length of 5 (9).

(maxbodylength)

🤖 Prompt for AI Agents
In cli/Makefile around lines 19 to 28, the build recipe for the frankenphp
target lacks standard phony declarations and top-level organization; add a
.PHONY line listing targets (e.g., .PHONY: all frankenphp build clean install)
near the top, introduce a simple top-level target like all that depends on
frankenphp, and keep the existing recipe under the frankenphp target; optionally
add a clean target to remove the built binary and a build alias that forwards to
frankenphp to make invocation and maintenance clearer.


ext/build/ext_arginfo.h: ext/build/ext.stub.php
$(GEN_STUB) ext/build/ext.stub.php ext/build
4 changes: 2 additions & 2 deletions cli/auth/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package auth

import (
"context"
"durable_php/appcontext"
"durable_php/config"
"encoding/base64"
"errors"
"fmt"
"github.com/bottledcode/durable-php/cli/appcontext"
"github.com/bottledcode/durable-php/cli/config"
"github.com/golang-jwt/jwt/v4"
"net/http"
"strings"
Expand Down
4 changes: 2 additions & 2 deletions cli/auth/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package auth

import (
"context"
"durable_php/appcontext"
"durable_php/config"
"github.com/bottledcode/durable-php/cli/appcontext"
"github.com/bottledcode/durable-php/cli/config"
"testing"
)

Expand Down
65 changes: 47 additions & 18 deletions cli/auth/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ package auth

import (
"context"
"durable_php/appcontext"
"durable_php/glue"
"durable_php/ids"
"encoding/json"
"errors"
"fmt"
"github.com/nats-io/nats.go/jetstream"
"go.uber.org/zap"
"net/http"
"os"
"slices"
"sync"
"time"

"github.com/bottledcode/durable-php/cli/appcontext"
"github.com/bottledcode/durable-php/cli/glue"
"github.com/bottledcode/durable-php/cli/ids"
"github.com/nats-io/nats.go/jetstream"
"go.uber.org/zap"
)

// LocalMessageSender is a function type for sending local messages
type LocalMessageSender func(method, stateId string, context map[string]interface{}) (interface{}, error)

// SendLocalMessage is a global function pointer that gets set by the build package
var SendLocalMessage LocalMessageSender

// Resource represents a resource with owners, shares, mode, expiration, and revision.
// It allows operations such as updating, sharing ownership, applying permissions,
// checking permissions, and serializing/deserializing to/from bytes.
Expand Down Expand Up @@ -139,19 +146,41 @@ func (r *Resource) getOrCreatePermissions(id *ids.StateId, ctx context.Context,
if cached, found := cache.Load(id.Name()); found {
perms = cached.(CreatePermissions)
} else {
result, err := os.CreateTemp("", "")
if err != nil {
return perms, err
}
defer os.Remove(result.Name())
result.Close()

glu := glue.NewGlue(ctx.Value("bootstrap").(string), glue.GetPermissions, make([]any, 0), result.Name())
env := map[string]string{"STATE_ID": id.String()}
_, headers, _, _ := glu.Execute(ctx, make(http.Header), logger, env, nil, id, ids.SystemSource)
data := headers.Get("Permissions")
if err = json.Unmarshal([]byte(data), &perms); err != nil {
return perms, err
if SendLocalMessage != nil {
// Use the new local message system
contextData := map[string]interface{}{
"bootstrap": ctx.Value("bootstrap"),
}

response, err := SendLocalMessage("getPermissions", id.String(), contextData)
if err != nil {
return perms, err
}

// The response should be JSON string
if responseStr, ok := response.(string); ok {
if err = json.Unmarshal([]byte(responseStr), &perms); err != nil {
return perms, err
}
} else {
return perms, errors.New("invalid response format from local message")
}
} else {
// Fallback to old glue system
result, err := os.CreateTemp("", "")
if err != nil {
return perms, err
}
defer os.Remove(result.Name())
result.Close()

glu := glue.NewGlue(ctx.Value("bootstrap").(string), glue.GetPermissions, make([]any, 0), result.Name())
env := map[string]string{"STATE_ID": id.String()}
_, headers, _, _ := glu.Execute(ctx, make(http.Header), logger, env, nil, id, ids.SystemSource)
data := headers.Get("Permissions")
if err = json.Unmarshal([]byte(data), &perms); err != nil {
return perms, err
}
}
cache.Store(id.Name(), perms)
}
Expand Down
6 changes: 3 additions & 3 deletions cli/auth/resourceManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package auth

import (
"context"
"durable_php/appcontext"
"durable_php/glue"
"durable_php/ids"
"encoding/json"
"github.com/bottledcode/durable-php/cli/appcontext"
"github.com/bottledcode/durable-php/cli/glue"
"github.com/bottledcode/durable-php/cli/ids"
"github.com/modern-go/concurrent"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
Expand Down
2 changes: 1 addition & 1 deletion cli/auth/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package auth

import (
"context"
"durable_php/appcontext"
"errors"
"github.com/bottledcode/durable-php/cli/appcontext"
"github.com/stretchr/testify/assert"
"testing"
"time"
Expand Down
2 changes: 1 addition & 1 deletion cli/auth/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth

import (
"context"
"durable_php/appcontext"
"github.com/bottledcode/durable-php/cli/appcontext"
"slices"
)

Expand Down
14 changes: 8 additions & 6 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nodphp

/*
* Copyright ©2024 Robert Landers
*
Expand All @@ -24,14 +26,14 @@ package main

import (
"context"
"durable_php/auth"
"durable_php/config"
"durable_php/glue"
"durable_php/ids"
di "durable_php/init"
"durable_php/lib"
"encoding/json"
"fmt"
"github.com/bottledcode/durable-php/cli/auth"
"github.com/bottledcode/durable-php/cli/config"
"github.com/bottledcode/durable-php/cli/glue"
"github.com/bottledcode/durable-php/cli/ids"
di "github.com/bottledcode/durable-php/cli/init"
"github.com/bottledcode/durable-php/cli/lib"
"github.com/dunglas/frankenphp"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats-server/v2/test"
Expand Down
5 changes: 5 additions & 0 deletions cli/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build nodphp

package cli

import _ "github.com/bottledcode/durable-php/cli/ext/build"
37 changes: 37 additions & 0 deletions cli/ext/build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ext Extension

Auto-generated PHP extension from Go code.

## Functions

### emit_event

```php
emit_event(?array $userContext, array $event, string $from): int
```

**Parameters:**

- `userContext` (array) (nullable)
- `event` (array)
- `from` (string)

**Returns:** int

## Classes

### Worker

**Properties:**

- `kind`: mixed
- `started`: bool
- `consumer`: mixed (nullable)
- `activeId`: mixed (nullable)
- `state`: mixed (nullable)
- `pendingEvents`: array
- `authContext`: array
- `currentCtx`: mixed
- `currentMsg`: mixed


Loading
Loading