Skip to content

Commit

Permalink
[3.x] New version
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Mar 6, 2024
1 parent f88575c commit 130bd5d
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 137 deletions.
146 changes: 73 additions & 73 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,119 +9,119 @@ on:
jobs:

byte_level:
name: "0️⃣ Byte-level"
runs-on: "ubuntu-latest"
name: 0️⃣ Byte-level
runs-on: ubuntu-latest
steps:
- name: "Checkout code"
uses: "actions/checkout@v3"
- name: Checkout code
uses: actions/checkout@v4

- name: "Check file permissions"
- name: Check file permissions
run: |
test "$(find . -type f -not -path './.git/*' -executable)" == ""
- name: "Find non-printable ASCII characters"
test $(find . -type f -not -path './.git/*' -executable) ==
- name: Find non-printable ASCII characters
run: |
! LC_ALL=C.UTF-8 find ./src -type f -name "*.php" -print0 | xargs -0 -- grep -PHn "[^ -~]"
! LC_ALL=C.UTF-8 find ./src -type f -name *.php -print0 | xargs -0 -- grep -PHn [^ -~]
syntax_errors:
name: "1️⃣ Syntax errors"
runs-on: "ubuntu-latest"
name: 1️⃣ Syntax errors
runs-on: ubuntu-latest
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "latest"
tools: "parallel-lint"
php-version: latest
tools: parallel-lint

- name: "Checkout code"
uses: "actions/checkout@v3"
- name: Checkout code
uses: actions/checkout@v4

- name: "Validate Composer configuration"
run: "composer validate --strict"
- name: Validate Composer configuration
run: composer validate --strict

- name: "Check source code for syntax errors"
run: "composer exec -- parallel-lint src/"
- name: Check source code for syntax errors
run: composer exec -- parallel-lint src/

unit_tests:
name: "2️⃣ Unit and Feature tests"
name: 2️⃣ Unit and Feature tests
needs:
- "byte_level"
- "syntax_errors"
runs-on: "ubuntu-latest"
- byte_level
- syntax_errors
runs-on: ubuntu-latest
strategy:
matrix:
php-version:
- "8.0"
- "8.1"
- "8.2"
- 8.1
- 8.2
- 8.3
laravel-constraint:
- "9.*"
- "10.*"
- 10.*
- 11.*
dependencies:
- "lowest"
- "highest"
- lowest
- highest
exclude:
- laravel-constraint: "10.*"
php-version: "8.0"
- laravel-constraint: 11.*
php-version: 8.1
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php-version }}"
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
coverage: xdebug

- name: "Checkout code"
uses: "actions/checkout@v3"
- name: Checkout code
uses: actions/checkout@v4

- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
- name: Install dependencies
uses: ramsey/composer-install@v3
with:
dependency-versions: "${{ matrix.dependencies }}"
composer-options: "--with=laravel/framework:${{ matrix.laravel-constraint }}"
dependency-versions: ${{ matrix.dependencies }}
composer-options: --with=laravel/framework:${{ matrix.laravel-constraint }}

- name: "Execute unit tests"
run: "composer run-script test"
- name: Execute unit tests
run: composer run-script test

- name: "Upload coverage to Codecov"
uses: "codecov/codecov-action@v3"
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4

static_analysis:
name: "3️⃣ Static Analysis"
name: 3️⃣ Static Analysis
needs:
- "byte_level"
- "syntax_errors"
runs-on: "ubuntu-latest"
- byte_level
- syntax_errors
runs-on: ubuntu-latest
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
tools: "phpstan"
php-version: "latest"
coverage: "none"
tools: phpstan
php-version: latest
coverage: none

- name: "Checkout code"
uses: "actions/checkout@v3"
- name: Checkout code
uses: actions/checkout@v4

- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
- name: Install dependencies
uses: ramsey/composer-install@v3

- name: "Execute static analysis"
run: "composer exec -- phpstan analyze -l 5 src/"
- name: Execute static analysis
run: composer exec -- phpstan analyze -l 5 src/

exported_files:
name: "4️⃣ Exported files"
name: 4️⃣ Exported files
needs:
- "byte_level"
- "syntax_errors"
runs-on: "ubuntu-latest"
- byte_level
- syntax_errors
runs-on: ubuntu-latest
steps:
- name: "Checkout code"
uses: "actions/checkout@v3"
- name: Checkout code
uses: actions/checkout@v4

- name: "Check exported files"
- name: Check exported files
run: |
EXPECTED="LICENSE.md,README.md,composer.json"
CURRENT="$(git archive HEAD | tar --list --exclude="src" --exclude="src/*" --exclude=".stubs" --exclude=".stubs/*" --exclude="routes" --exclude="routes/*" --exclude="stubs" --exclude="stubs/*" --exclude="lang" --exclude="lang/*" --exclude="config" --exclude="config/*" --exclude="database" --exclude="database/*" --exclude="resources" --exclude="resources/*" | paste -s -d ",")"
echo "CURRENT =${CURRENT}"
echo "EXPECTED=${EXPECTED}"
test "${CURRENT}" == "${EXPECTED}"
EXPECTED=LICENSE.md,README.md,composer.json
CURRENT=$(git archive HEAD | tar --list --exclude=src --exclude=src/* --exclude=.stubs --exclude=.stubs/* --exclude=routes --exclude=routes/* --exclude=stubs --exclude=stubs/* --exclude=lang --exclude=lang/* --exclude=config --exclude=config/* --exclude=database --exclude=database/* --exclude=resources --exclude=resources/* | paste -s -d ,)
echo CURRENT =${CURRENT}
echo EXPECTED=${EXPECTED}
test ${CURRENT} == ${EXPECTED}
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Codecov coverage](https://codecov.io/gh/Laragear/Poke/branch/1.x/graph/badge.svg?token=0ELJR5X90J)](https://codecov.io/gh/Laragear/Poke)
[![Maintainability](https://api.codeclimate.com/v1/badges/3954af3144475603ae67/maintainability)](https://codeclimate.com/github/Laragear/Poke/maintainability)
[![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=Laragear_Poke&metric=alert_status)](https://sonarcloud.io/dashboard?id=Laragear_Poke)
[![Laravel Octane Compatibility](https://img.shields.io/badge/Laravel%20Octane-Compatible-success?style=flat&logo=laravel)](https://laravel.com/docs/9.x/octane#introduction)
[![Laravel Octane Compatibility](https://img.shields.io/badge/Laravel%20Octane-Compatible-success?style=flat&logo=laravel)](https://laravel.com/docs/11.x/octane#introduction)

Keep your forms alive, avoid `TokenMismatchException` by gently poking your Laravel app.

Expand All @@ -16,8 +16,7 @@ Your support allows me to keep this package free, up-to-date and maintainable. A

## Requirements

* PHP 8 or later.
* Laravel 9, 10 or later.
* Laravel 10 or later.

## Installation

Expand Down Expand Up @@ -66,6 +65,8 @@ If there is any match, this will inject the Poke script in charge to keep the fo

This mode won't inject the script on error responses or redirections.

> [!INFO]
>
> It's recommended to use the other modes if your application has many routes or Responses with a lot of text.
### `middleware`
Expand Down Expand Up @@ -112,6 +113,8 @@ The `blade` mode disables middleware injection, so you can use the `<x-poke-scri

This may be useful if you have large responses, like blog posts, articles or galleries, since the framework won't spend resources inspecting the response, but just rendering the component.

> [!TIP]
>
> Don't worry if you have duplicate Poke components in your view. The script is rendered only once, and even if not, the script only runs once.
## Configuration
Expand Down Expand Up @@ -180,6 +183,8 @@ return [
];
```

> [!INFO]
>
> The poke routes are registered at boot time.
#### Name
Expand Down Expand Up @@ -268,4 +273,4 @@ If you discover any security related issues, please email darkghosthunter@gmail.

This specific package version is licensed under the terms of the [MIT License](LICENSE.md), at time of publishing.

[Laravel](https://laravel.com) is a Trademark of [Taylor Otwell](https://github.com/TaylorOtwell/). Copyright © 2011-2023 Laravel LLC.
[Laravel](https://laravel.com) is a Trademark of [Taylor Otwell](https://github.com/TaylorOtwell/). Copyright © 2011-2024 Laravel LLC.
19 changes: 9 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Keep your forms alive, avoid TokenMismatchException by gently poking your Laravel app",
"type": "library",
"license": "MIT",
"minimum-stability": "stable",
"minimum-stability": "dev",
"prefer-stable": true,
"keywords": [
"laravel",
Expand All @@ -25,17 +25,16 @@
"issues": "https://github.com/laragear/poke/issues"
},
"require": {
"php": "8.*",
"laragear/meta": "^2.0.2",
"illuminate/http": "9.*|10.*",
"illuminate/routing": "9.*|10.*",
"illuminate/support": "9.*|10.*",
"illuminate/view": "9.*|10.*"
"php": "^8.1",
"laragear/meta": "3.*",
"illuminate/http": "10.*|11.*",
"illuminate/routing": "10.*|11.*",
"illuminate/support": "10.*|11.*",
"illuminate/view": "10.*|11.*"
},
"require-dev": {
"laragear/meta-testing": "^1.2.2",
"orchestra/testbench": "^7.22|8.*",
"nikic/php-parser": "^4.15.3"
"laragear/meta-testing": "2.*",
"orchestra/testbench": "8.*|9.*"
},
"autoload-dev": {
"psr-4": {
Expand Down
8 changes: 2 additions & 6 deletions src/Blade/Components/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@

namespace Laragear\Poke\Blade\Components;

use function config;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
use function config;
use function url;
use function view;

class Script extends Component
{
/**
* Create a new component instance.
*
* @param bool $force
*/
public function __construct(protected bool $force = false)
{
//
}

/**
* Get the view / view contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
* @inheritdoc
*/
public function render(): View|string
{
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Controllers/PokeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class PokeController
{
/**
* Return an empty Ok response to the Poke script.
*
* @return \Illuminate\Http\Response
*/
public function __invoke(): Response
{
Expand Down
29 changes: 7 additions & 22 deletions src/Http/Middleware/InjectScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
namespace Laragear\Poke\Http\Middleware;

use Closure;
use function csrf_field;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
use Laragear\Poke\Blade\Components\Script;
use Symfony\Component\HttpFoundation\Response;
use function csrf_field;
use function strpos;
use function substr_replace;
use Symfony\Component\HttpFoundation\Response;

/**
* @internal
*/
class InjectScript
{
/**
* Create a new middleware instance.
*
* @param string $mode
*/
public function __construct(protected string $mode)
{
Expand All @@ -25,11 +26,6 @@ public function __construct(protected string $mode)

/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $force
* @return mixed
*/
public function handle(Request $request, Closure $next, string $force = null): mixed
{
Expand All @@ -44,11 +40,6 @@ public function handle(Request $request, Closure $next, string $force = null): m

/**
* Determine if we should inject the script into the response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Response $response
* @param bool $force
* @return bool
*/
public function shouldInject(Request $request, Response $response, bool $force): bool
{
Expand All @@ -68,26 +59,20 @@ public function shouldInject(Request $request, Response $response, bool $force):

/**
* Detect if the Response has form or CSRF Token.
*
* @param \Illuminate\Http\Response $response
* @return bool
*/
protected function hasCsrfInput(Response $response): bool
{
return strpos($response->content(), csrf_field());
return strpos($response->getContent(), csrf_field());
}

/**
* Sets the Script in the body.
*
* @param \Illuminate\Http\Response $response
* @return void
*
* @see https://github.com/php/php-src/blob/05023a281ddb62186fa47f51192ea51ba10f3a9b/ext/standard/string.c#L1845
*/
protected function injectScript(Response $response): void
{
$content = $response->content();
$content = $response->getContent();

// With an offset of just 32 characters, we'll speed up the lookup
// since the ending `</body>` tag can be found at the end of the
Expand Down
Loading

0 comments on commit 130bd5d

Please sign in to comment.