-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add query collection and streaming PHP execution #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ILDaviz
wants to merge
5
commits into
main
Choose a base branch
from
feature/update-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
84d3990
feat: implement query collection and streaming execution for PHP code
ILDaviz f537d3e
feat: add loaders and output modifiers for TweakPHP client
ILDaviz 49dfda6
fix: restore MIT license file to the project
ILDaviz dedbe69
fix: enhance error handling in executeStreaming method and include qu…
ILDaviz 58e65a0
fix: restore configuration for box.json with compression and output s…
ILDaviz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| name: tests | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| tests: | ||
| runs-on: ubuntu-22.04 | ||
|
|
||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| php: [ "7.4", "8.0", "8.1", "8.2", "8.3", "8.4" ] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup PHP | ||
| uses: shivammathur/setup-php@v2 | ||
| with: | ||
| php-version: ${{ matrix.php }} | ||
| extensions: json | ||
| tools: composer | ||
|
|
||
| - name: Install Composer dependencies | ||
| run: composer install --prefer-dist --no-interaction --no-progress --ignore-platform-req=php | ||
|
|
||
| - name: Run tests | ||
| run: composer test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| .idea | ||
| .vscode | ||
|
|
||
| vendor | ||
| client.phar | ||
|
|
||
| .DS_Store | ||
| .DS_Store | ||
| .phpunit.result.cache | ||
| e2e_tests/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| .PHONY: build | ||
|
|
||
| PHP_BIN ?= php | ||
| BOX_PHP ?= $(PHP_BIN) | ||
| COMPOSER_BIN ?= $(shell command -v composer) | ||
| BOX_VERSION ?= 4.6.2 | ||
| BOX_COMPOSER_HOME := $(if $(strip $(COMPOSER_HOME)),$(COMPOSER_HOME),$(shell mktemp -d)) | ||
| BOX_BIN := $(shell COMPOSER_HOME="$(BOX_COMPOSER_HOME)" $(COMPOSER_BIN) global config bin-dir --absolute 2>/dev/null)/box | ||
|
|
||
| ifeq ($(UPDATE_LOCK),true) | ||
| COMPOSER_ACTION := update | ||
| COMPOSER_ACTION_FLAGS := --with-all-dependencies | ||
| else | ||
| COMPOSER_ACTION := install | ||
| COMPOSER_ACTION_FLAGS := | ||
| endif | ||
|
|
||
| build: | ||
| rm -rf vendor | ||
| $(PHP_BIN) $(COMPOSER_BIN) $(COMPOSER_ACTION) --no-dev --prefer-dist --optimize-autoloader --no-interaction --no-progress $(COMPOSER_ACTION_FLAGS) | ||
| COMPOSER_HOME="$(BOX_COMPOSER_HOME)" $(BOX_PHP) $(COMPOSER_BIN) global require humbug/box:$(BOX_VERSION) --with-all-dependencies --no-interaction | ||
| $(BOX_PHP) $(BOX_BIN) compile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,157 @@ | ||
| # TweakPHP Client | ||
|
|
||
| TweakPHP Client is a packaged Phar file that [TweakPHP](https://github.com/tweakphp/tweakphp) uses under the hood to run your tweaks! | ||
| TweakPHP Client is a PHAR used by [TweakPHP](https://github.com/tweakphp/tweakphp) | ||
| to execute PHP code inside a project. | ||
|
|
||
| (Documentation in progress...) | ||
| ## Requirements | ||
|
|
||
| - PHP 7.4 or higher | ||
|
|
||
| ## Build | ||
|
|
||
| Build the PHAR with production dependencies only: | ||
|
|
||
| ```bash | ||
| make build | ||
| ``` | ||
| The build removes `vendor`, installs the project dependencies with `--no-dev`, | ||
| installs the pinned Box version in a temporary Composer home, and creates | ||
| `client.phar`. Development dependencies are not included in the PHAR. | ||
|
|
||
| Release builds resolve the dependencies against the PHP runtime that will | ||
| execute each PHAR. PHP 7.4 and 8.0 use Symfony 5.4, while newer PHP versions | ||
| can use the compatible Symfony 6.4, 7.x, or 8.x release. To do the same for a | ||
| local build, run: | ||
|
|
||
| ```bash | ||
| make build UPDATE_LOCK=true | ||
| ``` | ||
|
|
||
| ## Command Syntax | ||
|
|
||
| The project directory is always required and must come before the command: | ||
|
|
||
| ```bash | ||
| php client.phar <project-directory> <command> [base64-code] | ||
| ``` | ||
|
|
||
| For the current directory, use `.` or `$PWD`: | ||
|
|
||
| ```bash | ||
| php client.phar . info | ||
| php client.phar "$PWD" info | ||
| ``` | ||
|
|
||
| Available commands: | ||
|
|
||
| ```bash | ||
| php client.phar <project-directory> info | ||
| php client.phar <project-directory> execute <base64-code> | ||
| php client.phar <project-directory> execute-stream <base64-code> | ||
| ``` | ||
|
|
||
| ## Check Project Info | ||
|
|
||
| `info` reports the detected project type, project version, and PHP version: | ||
|
|
||
| ```bash | ||
| php client.phar /path/to/project info | ||
| ``` | ||
|
|
||
| Example response: | ||
|
|
||
| ```json | ||
| {"name":"Laravel","version":"11.x","php_version":"8.4.0"} | ||
| ``` | ||
|
|
||
| ## Execute Code | ||
|
|
||
| The PHP code must be Base64-encoded before it is passed to the client. | ||
|
|
||
| ```bash | ||
| code=$(printf '%s' 'echo "Hello";' | base64) | ||
| php client.phar /path/to/project execute "$code" | ||
| ``` | ||
|
|
||
| The command waits until the complete script has finished and returns one JSON | ||
| result prefixed with `TWEAKPHP_RESULT:`: | ||
|
|
||
| ```text | ||
| TWEAKPHP_RESULT:{"output":[{"line":2,"code":"echo \"Hello\";","output":"Hello","queries":[]}],"queries":[]} | ||
| ``` | ||
|
|
||
| If the command fails, it returns `TWEAKPHP_ERROR:` and exits with status `1`: | ||
|
|
||
| ```text | ||
| TWEAKPHP_ERROR:{"class":"InvalidArgumentException","message":"Invalid Base64-encoded PHP code."} | ||
| ``` | ||
|
|
||
| User code calling `exit()` or `die()` preserves the requested exit status. | ||
| Instrumentation failures are reported in the optional `query_errors` field rather | ||
| than being silently discarded. | ||
|
|
||
| Multiple statements are supported: | ||
|
|
||
| ```bash | ||
| code=$(printf '%s' 'echo "First"; sleep(1); echo "Second";' | base64) | ||
| php client.phar "$PWD" execute "$code" | ||
| ``` | ||
|
|
||
| ## Execute As A Stream | ||
|
|
||
| Use `execute-stream` when the caller needs output while the script is running. | ||
| The project directory is still required: | ||
|
|
||
| ```bash | ||
| code=$(printf '%s' 'echo "First"; sleep(1); echo "Second";' | base64) | ||
| php client.phar "$PWD" execute-stream "$code" | ||
| ``` | ||
|
|
||
| The command writes one JSON event per line, each prefixed with | ||
| `TWEAKPHP_STREAM:`: | ||
|
|
||
| ```text | ||
| TWEAKPHP_STREAM:{"type":"statement.started","index":0,"line":2,"code":"echo \"First\";"} | ||
| TWEAKPHP_STREAM:{"type":"output","index":0,"data":"First"} | ||
| TWEAKPHP_STREAM:{"type":"statement.completed","index":0,"queries":[]} | ||
| TWEAKPHP_STREAM:{"type":"completed"} | ||
| ``` | ||
|
|
||
| Event types: | ||
|
|
||
| - `statement.started`: a statement started | ||
| - `output`: the statement produced output | ||
| - `statement.completed`: a statement finished, with collected queries | ||
| - `error`: a statement or the client failed; the process exits with status `1`, | ||
| or with the status requested by `exit()`/`die()` | ||
| - `completed`: all statements finished | ||
|
|
||
| An `exit()` or `die()` call emits an `error` event with its exit status and the | ||
| process exits with that status. | ||
|
|
||
| ## Supported Projects | ||
|
|
||
| The client detects the project type automatically: | ||
|
|
||
| - Laravel: `vendor/autoload.php` and `bootstrap/app.php` | ||
| - Symfony: `vendor/autoload.php`, `symfony.lock`, and `src/Kernel.php` | ||
| - WordPress: `wp-load.php` | ||
| - Pimcore: `vendor/pimcore/pimcore` | ||
| - Composer: `vendor/autoload.php` | ||
| - Plain PHP: any directory not matching a more specific project type | ||
|
|
||
| Examples: | ||
|
|
||
| ```bash | ||
| # Laravel | ||
| code=$(printf '%s' 'return App\Models\User::query()->latest()->first();' | base64) | ||
| php client.phar /path/to/laravel execute "$code" | ||
|
|
||
| # WordPress | ||
| code=$(printf '%s' 'return get_option("blogname");' | base64) | ||
| php client.phar /path/to/wordpress execute "$code" | ||
|
|
||
| # Plain PHP or Composer | ||
| code=$(printf '%s' 'return PHP_VERSION;' | base64) | ||
| php client.phar /path/to/project execute "$code" | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.