🆙 Add cms i using 🆙

This commit is contained in:
Remco
2025-11-25 22:42:56 +01:00
parent 94704e0925
commit d44196149e
35591 changed files with 3601123 additions and 0 deletions
@@ -0,0 +1,68 @@
# Changelog
All notable changes to `blade-capture-directive` will be documented in this file.
## v1.0.0 - 2024-02-26
### What's Changed
* Laravel 11 support, drop Laravel 9 and PHP 8.0 support by @danharrin in https://github.com/ryangjchandler/blade-capture-directive/pull/19
* chore(deps): bump dependabot/fetch-metadata from 1.3.6 to 1.5.0 by @dependabot in https://github.com/ryangjchandler/blade-capture-directive/pull/14
* chore(deps): bump dependabot/fetch-metadata from 1.5.0 to 1.5.1 by @dependabot in https://github.com/ryangjchandler/blade-capture-directive/pull/15
* chore(deps): bump dependabot/fetch-metadata from 1.5.1 to 1.6.0 by @dependabot in https://github.com/ryangjchandler/blade-capture-directive/pull/16
* chore(deps): bump ramsey/composer-install from 1 to 2 by @dependabot in https://github.com/ryangjchandler/blade-capture-directive/pull/10
* chore(deps): bump actions/checkout from 2 to 4 by @dependabot in https://github.com/ryangjchandler/blade-capture-directive/pull/17
* chore(deps): bump stefanzweifel/git-auto-commit-action from 4 to 5 by @dependabot in https://github.com/ryangjchandler/blade-capture-directive/pull/18
**Full Changelog**: https://github.com/ryangjchandler/blade-capture-directive/compare/v0.3.0...v1.0.0
## v0.3.0 - 2023-02-14
### What's Changed
- Add Laravel 10 support by @jyrkidn in https://github.com/ryangjchandler/blade-capture-directive/pull/12
- Remove support for Laravel 8
### New Contributors
- @jyrkidn made their first contribution in https://github.com/ryangjchandler/blade-capture-directive/pull/12
**Full Changelog**: https://github.com/ryangjchandler/blade-capture-directive/compare/v0.2.2...v0.3.0
## v0.2.2 - 2022-09-02
**Full Changelog**: https://github.com/ryangjchandler/blade-capture-directive/compare/v0.2.1...v0.2.2
## v0.2.1 - 2022-08-23
### What's Changed
- chore(deps): bump dependabot/fetch-metadata from 1.3.0 to 1.3.1 by @dependabot in https://github.com/ryangjchandler/blade-capture-directive/pull/3
- chore(deps): bump dependabot/fetch-metadata from 1.3.1 to 1.3.3 by @dependabot in https://github.com/ryangjchandler/blade-capture-directive/pull/4
- fix: maintain `$this` binding of captured closure by @ryangjchandler in https://github.com/ryangjchandler/blade-capture-directive/pull/5
### New Contributors
- @dependabot made their first contribution in https://github.com/ryangjchandler/blade-capture-directive/pull/3
**Full Changelog**: https://github.com/ryangjchandler/blade-capture-directive/compare/v0.2.0...v0.2.1
## v0.2.0 - 2022-03-11
## What's Changed
- feature: capture enclosing environment by @ryangjchandler in https://github.com/ryangjchandler/blade-capture-directive/pull/2
## New Contributors
- @ryangjchandler made their first contribution in https://github.com/ryangjchandler/blade-capture-directive/pull/2
**Full Changelog**: https://github.com/ryangjchandler/blade-capture-directive/compare/v0.1.0...v0.1.1
## v0.1.0 - 2022-03-10
**Full Changelog**: https://github.com/ryangjchandler/blade-capture-directive/commits/v0.1.0
## 1.0.0 - 202X-XX-XX
- initial release
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) ryangjchandler <support@ryangjchandler.co.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,116 @@
# Create inline partials in your Blade templates with ease.
[![Latest Version on Packagist](https://img.shields.io/packagist/v/ryangjchandler/blade-capture-directive.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/blade-capture-directive)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/blade-capture-directive/run-tests?label=tests)](https://github.com/ryangjchandler/blade-capture-directive/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/blade-capture-directive/Check%20&%20fix%20styling?label=code%20style)](https://github.com/ryangjchandler/blade-capture-directive/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/ryangjchandler/blade-capture-directive.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/blade-capture-directive)
This package introduces a new `@capture` directive that allows you to capture small parts of your Blade templates and re-use them later on without needing to extract them into partials.
## Installation
You can install the package via Composer:
```bash
composer require ryangjchandler/blade-capture-directive
```
## Usage
This package adds a new pair of directives: `@capture` and `@endcapture`.
The `@capture` directive will capture all of your Blade until it reaches an `@endcapture` directive. It takes the code and stores it inside of a variable for usage later on.
```blade
@capture($hello)
Hello, world!
@endcapture
```
The directive requires at least 1 argument. This argument should be a PHP variable that you would like to assign your partial to. The variable itself will become a `Closure` that can be invoked inside of Blade echo tags (`{{ }}`) anywhere after it's definition.
```blade
@capture($hello)
Hello, world!
@endcapture
{{ $hello() }}
```
The above code will invoke your captured Blade code and output `Hello, world!` when compiled by Laravel and rendered in the browser.
The `@capture` directive also supports arguments. This means you can capture generalised chunks of Blade and change the output dynamically. This is achieved by specifying a comma-separated list of PHP variables like so:
```blade
@capture($hello, $name)
Hello, {{ $name }}!
@endcapture
```
The above code will require that a name is passed to `$hello()`, like below:
```blade
@capture($hello, $name)
Hello, {{ $name }}!
@endcapture
{{ $hello('Ryan') }}
```
The Blade will compile this and your view will output `Hello, Ryan!`. Cool, right?
The list of arguments can be treated like any set of arguments defined on a function. This means you can assign default values and specify multiple arguments:
```blade
@capture($hello, $name, $greeting = 'Hello, ')
{{ $greeting }} {{ $name }}!
@endcapture
{{ $hello('Ryan') }}
{{ $hello('Taylor', 'Yo, ') }}
```
The above code will now output `Hello, Ryan!` as well as `Yo, Taylor!`. This is really cool, I know!
### Inheriting scope
All captured blocks will inherit the parent scope, just like a regular partial would in Blade. This means you can use any data passed to the view without having to pass it through to the block manually.
```blade
@php($name = 'Ryan')
@capture($hello)
Hello, {{ $name }}!
@endcapture
{{ $hello() }}
```
> If your captured block has a parameter with the same name as a predefined variable from the inherited scope, the block's parameter will always take precedence.
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Ryan Chandler](https://github.com/ryangjchandler)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
@@ -0,0 +1,70 @@
{
"name": "ryangjchandler/blade-capture-directive",
"description": "Create inline partials in your Blade templates with ease.",
"keywords": [
"ryangjchandler",
"laravel",
"blade-capture-directive"
],
"homepage": "https://github.com/ryangjchandler/blade-capture-directive",
"license": "MIT",
"authors": [
{
"name": "Ryan Chandler",
"email": "support@ryangjchandler.co.uk",
"role": "Developer"
}
],
"require": {
"php": "^8.1",
"spatie/laravel-package-tools": "^1.9.2",
"illuminate/contracts": "^10.0|^11.0|^12.0"
},
"require-dev": {
"nunomaduro/collision": "^7.0|^8.0",
"nunomaduro/larastan": "^2.0|^3.0",
"orchestra/testbench": "^8.0|^9.0|^10.0",
"pestphp/pest": "^2.0|^3.7",
"pestphp/pest-plugin-laravel": "^2.0|^3.1",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0|^2.0",
"phpstan/phpstan-phpunit": "^1.0|^2.0",
"phpunit/phpunit": "^10.0|^11.5.3",
"spatie/laravel-ray": "^1.26"
},
"autoload": {
"psr-4": {
"RyanChandler\\BladeCaptureDirective\\": "src",
"RyanChandler\\BladeCaptureDirective\\Database\\Factories\\": "database/factories"
}
},
"autoload-dev": {
"psr-4": {
"RyanChandler\\BladeCaptureDirective\\Tests\\": "tests"
}
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
}
},
"extra": {
"laravel": {
"providers": [
"RyanChandler\\BladeCaptureDirective\\BladeCaptureDirectiveServiceProvider"
],
"aliases": {
"BladeCaptureDirective": "RyanChandler\\BladeCaptureDirective\\Facades\\BladeCaptureDirective"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
@@ -0,0 +1,30 @@
<?php
namespace RyanChandler\BladeCaptureDirective;
use Illuminate\Support\Str;
final class BladeCaptureDirective
{
public static function open(string $expression): string
{
[$name, $args] = Str::contains($expression, ',') ?
Str::of($expression)->trim()->explode(',', 2)->map(fn ($part) => trim($part))->toArray() :
[$expression, ''];
return "
<?php {$name} = (function (\$args) {
return function ({$args}) use (\$args) {
extract(\$args, EXTR_SKIP);
ob_start(); ?>
";
}
public static function close()
{
return "
<?php return new \Illuminate\Support\HtmlString(ob_get_clean()); };
})(get_defined_vars()); ?>
";
}
}
@@ -0,0 +1,22 @@
<?php
namespace RyanChandler\BladeCaptureDirective;
use Illuminate\Support\Facades\Blade;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
class BladeCaptureDirectiveServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package->name('blade-capture-directive');
}
public function packageBooted()
{
Blade::directive('capture', fn (string $expression) => BladeCaptureDirective::open($expression));
Blade::directive('endcapture', fn () => BladeCaptureDirective::close());
}
}
@@ -0,0 +1,10 @@
<?php
namespace Illuminate\Validation {
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
class Rule {
public static function turnstile(): Turnstile {}
}
}
@@ -0,0 +1,115 @@
# Changelog
All notable changes to `laravel-cloudflare-turnstile` will be documented in this file.
## v2.1.0 - 2025-10-29
### What's Changed
* Update livewire website url by @Erulezz in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/54
* Allow any custom attributes on the turnstile div by @tobz-nz in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/52
### New Contributors
* @Erulezz made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/54
* @tobz-nz made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/52
**Full Changelog**: https://github.com/ryangjchandler/laravel-cloudflare-turnstile/compare/v2.0.3...v2.1.0
## v2.0.3 - 2025-06-08
### What's Changed
* Javascript callback function declarations changed by @AndrasMa in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/51
### New Contributors
* @AndrasMa made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/51
**Full Changelog**: https://github.com/ryangjchandler/laravel-cloudflare-turnstile/compare/v2.0.2...v2.0.3
## v2.0.2 - 2025-05-16
### What's Changed
* Fix: Wait for Livewire initialization by @soymgomez in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/50
### New Contributors
* @soymgomez made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/50
**Full Changelog**: https://github.com/ryangjchandler/laravel-cloudflare-turnstile/compare/v2.0.1...v2.0.2
## v2.0.1 - 2025-03-02
### What's Changed
* Add PHP 8.2 back by @ryangjchandler in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/44
### New Contributors
* @ryangjchandler made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/44
**Full Changelog**: https://github.com/ryangjchandler/laravel-cloudflare-turnstile/compare/v2.0.0...v2.0.1
## v2.0.0 - 2025-02-26
### What's Changed
* Fix workflow run badges by @stevebauman in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/30
* Localization support by @200-0K in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/34
* Laravel 12.x Compatibility by @laravel-shift in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/40
* Allow use more then one turnstile on single page with Livewire. by @Macsimka in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/33
* Remove support for PHP 8.1 and PHP 8.2.
* Remove support for Laravel 10.x.
* Reset Turnstile widget when Livewire property is reset.
* Prevent widget from expiring with Livewire.
### New Contributors
* @stevebauman made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/30
* @200-0K made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/34
* @Macsimka made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/33
**Full Changelog**: https://github.com/ryangjchandler/laravel-cloudflare-turnstile/compare/v1.1.0...v2.0.0
## v1.1.0 - 2024-03-12
* Add support for Laravel 11.x.
## v1.0.0 - 2023-04-14
### What's Changed
- chore(deps): bump dependabot/fetch-metadata from 1.3.4 to 1.3.5 by @dependabot in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/8
- chore(deps): bump dependabot/fetch-metadata from 1.3.5 to 1.3.6 by @dependabot in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/10
- Support Laravel 10 by @parkourben99 in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/15
### New Contributors
- @parkourben99 made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/15
**Full Changelog**: https://github.com/ryangjchandler/laravel-cloudflare-turnstile/compare/v0.2.0...v1.0.0
## v0.2.0 - 2022-12-13
### What's Changed
- chore(deps): bump dependabot/fetch-metadata from 1.3.3 to 1.3.4 by @dependabot in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/3
- chore(deps): bump dependabot/fetch-metadata from 1.3.4 to 1.3.5 by @dependabot in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/5
- Widget configurations by @kaptk2 in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/7
### Upgrade Guide
The original `@turnstile()` Blade directive has been removed in favour of `<x-turnstile />`. Please refer to the documentation for the new attribute options found on the Blade component.
### New Contributors
- @dependabot made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/3
- @kaptk2 made their first contribution in https://github.com/ryangjchandler/laravel-cloudflare-turnstile/pull/7
**Full Changelog**: https://github.com/ryangjchandler/laravel-cloudflare-turnstile/compare/v0.1.1...v0.2.0
## v0.1.1 - 2022-09-30
**Full Changelog**: https://github.com/ryangjchandler/laravel-cloudflare-turnstile/compare/v0.1.0...v0.1.1
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) ryangjchandler <support@ryangjchandler.co.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,150 @@
# A simple package to help integrate Cloudflare Turnstile.
[![Latest Version on Packagist](https://img.shields.io/packagist/v/ryangjchandler/laravel-cloudflare-turnstile.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/laravel-cloudflare-turnstile)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/ryangjchandler/laravel-cloudflare-turnstile/run-tests.yml?branch=main&style=flat-square&label=tests)](https://github.com/ryangjchandler/laravel-cloudflare-turnstile/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/ryangjchandler/laravel-cloudflare-turnstile/fix-php-code-style-issues.yml?branch=main&style=flat-square&label=code+style)](https://github.com/ryangjchandler/laravel-cloudflare-turnstile/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/ryangjchandler/laravel-cloudflare-turnstile.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/laravel-cloudflare-turnstile)
This packages provides an API integrating Laravel with [Cloudflare's Turnstile](https://www.cloudflare.com/en-gb/application-services/products/turnstile/) product.
## Installation
You can install the package via Composer:
```bash
composer require ryangjchandler/laravel-cloudflare-turnstile
```
You should then add the following configuration values to your `config/services.php` file:
```php
return [
// ...,
'turnstile' => [
'key' => env('TURNSTILE_SITE_KEY'),
'secret' => env('TURNSTILE_SECRET_KEY'),
],
];
```
Create your Turnstile keys through Cloudflare and add them to your `.env` file.
```
TURNSTILE_SITE_KEY="1x00000000000000000000AA"
TURNSTILE_SECRET_KEY="2x0000000000000000000000000000000AA"
```
## Usage
In your layout file, include the Turnstile scripts using the `<x-turnstile.scripts>` component. This should be added to the `<head>` of your document.
```blade
<html>
<head>
<x-turnstile.scripts />
</head>
<body>
{{ $slot }}
</body>
</html>
```
Once that's done, you can use the `<x-turnstile />` component inside of a `<form>` to output the appropriate markup with your site key configured.
```blade
<form action="/" method="POST">
<x-turnstile />
<button>
Submit
</button>
</form>
```
To validate the Turnstile response, use the `Turnstile` rule when validating your request.
```php
use Illuminate\Validation\Rule;
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
public function submit(Request $request)
{
$request->validate([
'cf-turnstile-response' => ['required', new Turnstile],
]);
}
```
### Customizing the widget
You can customize the widget by passing attributes to the `<x-turnstile />` component.
> To learn more about these parameters, refer to [the offical documentation](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#configurations).
```blade
<form action="/" method="POST">
<x-turnstile
data-action="login"
data-cdata="sessionid-123456789"
data-callback="callback"
data-expired-callback="expiredCallback"
data-error-callback="errorCallback"
data-theme="dark"
data-tabindex="1"
/>
<button>
Submit
</button>
</form>
```
### Livewire support
This package can also integrate seamlessly with [Livewire](https://livewire.laravel.com).
You can use `wire:model` to bind the Turnstile response to a Livewire property directly.
```blade
<x-turnstile wire:model="yourModel" />
```
#### Multiple widgets with Livewire
If you're using Livewire and need to have multiple widgets on the same page, each widget requires a unique ID.
```blade
<x-turnstile id="my_widget" wire:model="captcha" />
```
The `id` property must match this RegEx: `/^[a-zA-Z_][a-zA-Z0-9_-]*$/`. IDs that do not match the RegEx will trigger an exception.
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Ryan Chandler](https://github.com/ryangjchandler)
- [All contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
@@ -0,0 +1,63 @@
# Upgrading from 2.x to 3.0
This document contains notable breaking changes and the steps required to successfully upgrade your application from 2.x to 3.0.
## `@turnstileScripts` removed in favor of component
The `@turnstileScripts` directive has been removed from the package and replaced with a Blade component instead.
```diff
- @turnstileScripts
+ <x-turnstile.scripts />
```
This syntax is now inline with the Turnstile component itself and improves the consistency of the package.
## `turnstile` rule removed
The `turnstile` stringified validation rule provided this package has now been removed in favor of the `Turnstile` rule object.
```diff
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
$request->validate([
'cf-turnstile-response' => [
'required',
- 'turnstile',
+ new Turnstile,
]
]);
```
## `Rule::turnstile()` macro removed
The `Rule::turnstile()` macro provided this package has now been removed in favor of the `Turnstile` rule object.
```diff
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
$request->validate([
'cf-turnstile-response' => [
'required',
- Rule::turnstile(),
+ new Turnstile,
]
]);
```
## `TurnstileClient` class renamed
`TurnstileClient` has been renamed to just `Client`.
```diff
- use RyanChandler\LaravelCloudflareTurnstile\TurnstileClient;
+ use RyanChandler\LaravelCloudflareTurnstile\Client;
```
## `Client` is now a scoped singleton
The `Client` was previously registered as a singleton, but to ensure compatibility with Octane and other single-boot lifecycles, the `Client` is now a scoped singleton.
This shouldn't have a huge impact on userland code, but if you're interacting with this manually there could be breaking changes in behaviour.
Be sure to thoroughly test your application code to find breaking changes.
@@ -0,0 +1,73 @@
{
"name": "ryangjchandler/laravel-cloudflare-turnstile",
"description": "A simple package to help integrate Cloudflare Turnstile.",
"keywords": [
"ryangjchandler",
"laravel",
"laravel-cloudflare-turnstile"
],
"homepage": "https://github.com/ryangjchandler/laravel-cloudflare-turnstile",
"license": "MIT",
"authors": [
{
"name": "Ryan Chandler",
"email": "support@ryangjchandler.co.uk",
"role": "Developer"
}
],
"require": {
"php": "^8.2",
"spatie/laravel-package-tools": "^1.13.0",
"illuminate/contracts": "^11.0|^12.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^8.0",
"larastan/larastan": "^3.1",
"orchestra/testbench": "^9.0|^10.0",
"pestphp/pest": "^3.7",
"pestphp/pest-plugin-laravel": "^3.1",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0|^2.0",
"phpstan/phpstan-phpunit": "^1.0|^2.0",
"phpunit/phpunit": "^10.0|^11.5.3",
"spatie/laravel-ray": "^1.26",
"spatie/pest-plugin-snapshots": "^2.0"
},
"autoload": {
"psr-4": {
"RyanChandler\\LaravelCloudflareTurnstile\\": "src",
"RyanChandler\\LaravelCloudflareTurnstile\\Database\\Factories\\": "database/factories"
}
},
"autoload-dev": {
"psr-4": {
"RyanChandler\\LaravelCloudflareTurnstile\\Tests\\": "tests"
}
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse --memory-limit=-1",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
}
},
"extra": {
"laravel": {
"providers": [
"RyanChandler\\LaravelCloudflareTurnstile\\LaravelCloudflareTurnstileServiceProvider"
],
"aliases": {
"LaravelCloudflareTurnstile": "RyanChandler\\LaravelCloudflareTurnstile\\Facades\\LaravelCloudflareTurnstile"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
@@ -0,0 +1,12 @@
<?php
return [
'missing-input-secret' => 'لم يتم إرسال البيانات اللازمة للتحقق من Captcha.',
'invalid-input-secret' => 'البيانات اللازمة للتحقق من Captcha غير صحيحة أو غير موجودة.',
'missing-input-response' => 'لم يتم إرسال الرد المطلوب للتحقق من Captcha.',
'invalid-input-response' => 'الرد المطلوب للتحقق من Captcha غير صالح أو انتهت صلاحيته.',
'bad-request' => 'تم رفض الطلب لأنه غير مكتمل أو يحتوي على خطأ.',
'timeout-or-duplicate' => 'تم استخدام رد Captcha هذا مسبقًا.',
'internal-error' => 'حدث خطأ داخلي أثناء التحقق من Captcha.',
'unexpected' => 'حدث خطأ غير متوقع أثناء التحقق من Captcha.',
];
@@ -0,0 +1,12 @@
<?php
return [
'missing-input-secret' => 'The secret parameter was not passed.',
'invalid-input-secret' => 'The secret parameter was invalid or did not exist.',
'missing-input-response' => 'The response parameter was not passed.',
'invalid-input-response' => 'The response parameter is invalid or has expired.',
'bad-request' => 'The request was rejected because it was malformed.',
'timeout-or-duplicate' => 'The response parameter has already been validated before.',
'internal-error' => 'An internal error happened while validating the response.',
'unexpected' => 'An unexpected error occurred.',
];
@@ -0,0 +1 @@
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer {{ $attributes }}></script>
@@ -0,0 +1,45 @@
@props([
'id' => 'captcha',
])
@php
if (! preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $id)) {
throw new InvalidArgumentException("The Turnstile ID [{$id}] must start start with a letter or underscore, and can only contain alphanumeric or underscore characters.");
}
$model = $attributes->has('wire:model') ? $attributes->get('wire:model') : null;
@endphp
<div
data-sitekey="{{ config('services.turnstile.key') }}"
@if ($model)
wire:ignore
data-callback="{{ $id }}Callback"
data-expired-callback="{{ $id }}ExpiredCallback"
data-timeout-callback="{{ $id }}ExpiredCallback"
{{ $attributes->filter(fn($value, $key) => ! in_array($key, ['data-callback', 'data-expired-callback', 'data-timeout-callback', 'wire:model', 'id']))->class(['cf-turnstile']) }}
@else
{{ $attributes->class(['cf-turnstile']) }}
@endif
></div>
@if ($model)
<script>
document.addEventListener('livewire:initialized', () => {
window.{{ $id }}Callback = function (token) {
@this.set("{{ $model }}", token);
}
window.{{ $id }}ExpiredCallback = function () {
window.turnstile.reset();
}
@this.watch("{{ $model }}", (value, old) => {
// If there was a value, and now there isn't, reset the Turnstile.
if (!!old && !value) {
window.turnstile.reset();
}
})
});
</script>
@endif
@@ -0,0 +1,36 @@
<?php
namespace RyanChandler\LaravelCloudflareTurnstile;
use Illuminate\Support\Facades\Http;
use RyanChandler\LaravelCloudflareTurnstile\Contracts\ClientInterface;
use RyanChandler\LaravelCloudflareTurnstile\Responses\SiteverifyResponse;
class Client implements ClientInterface
{
public function __construct(
protected string $secret,
) {}
public function siteverify(string $response): SiteverifyResponse
{
$response = Http::retry(3, 100)
->asForm()
->acceptJson()
->post('https://challenges.cloudflare.com/turnstile/v0/siteverify', [
'secret' => config('services.turnstile.secret'),
'response' => $response,
]);
if (! $response->ok()) {
return SiteverifyResponse::success();
}
return SiteverifyResponse::failure($response->json('error-codes'));
}
public function dummy(): string
{
return self::RESPONSE_DUMMY_TOKEN;
}
}
@@ -0,0 +1,12 @@
<?php
namespace RyanChandler\LaravelCloudflareTurnstile\Contracts;
use RyanChandler\LaravelCloudflareTurnstile\Responses\SiteverifyResponse;
interface ClientInterface
{
const RESPONSE_DUMMY_TOKEN = 'XXXX.DUMMY.TOKEN.XXXX';
public function siteverify(string $response): SiteverifyResponse;
}
@@ -0,0 +1,26 @@
<?php
namespace RyanChandler\LaravelCloudflareTurnstile\Facades;
use Illuminate\Support\Facades\Facade;
use RyanChandler\LaravelCloudflareTurnstile\Contracts\ClientInterface;
use RyanChandler\LaravelCloudflareTurnstile\Testing\FakeClient;
/**
* @method static string dummy()
* @method static \RyanChandler\LaravelCloudflareTurnstile\Responses\SiteverifyResponse siteverify(string $response)
*/
class Turnstile extends Facade
{
protected static function getFacadeAccessor(): string
{
return ClientInterface::class;
}
public static function fake(): FakeClient
{
static::swap($fake = new FakeClient);
return $fake;
}
}
@@ -0,0 +1,40 @@
<?php
namespace RyanChandler\LaravelCloudflareTurnstile;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Blade;
use RyanChandler\LaravelCloudflareTurnstile\Contracts\ClientInterface;
use RyanChandler\LaravelCloudflareTurnstile\View\Components\Scripts;
use RyanChandler\LaravelCloudflareTurnstile\View\Components\Turnstile as TurnstileComponent;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
class LaravelCloudflareTurnstileServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package
->name('laravel-cloudflare-turnstile')
->hasTranslations()
->hasViews();
}
public function packageRegistered(): void
{
$this->app->scoped(ClientInterface::class, static function (Application $app): Client {
return new Client($app['config']->get('services.turnstile.secret'));
});
}
public function packageBooted(): void
{
$this->bootBlade();
}
private function bootBlade(): void
{
Blade::component('turnstile.scripts', Scripts::class);
Blade::component('turnstile', TurnstileComponent::class);
}
}
@@ -0,0 +1,31 @@
<?php
namespace RyanChandler\LaravelCloudflareTurnstile\Responses;
use Illuminate\Contracts\Support\Arrayable;
class SiteverifyResponse implements Arrayable
{
public function __construct(
public readonly bool $success,
public readonly array $errorCodes,
) {}
public static function success(): self
{
return new self(true, []);
}
public static function failure(array $errorCodes = []): self
{
return new self(false, $errorCodes);
}
public function toArray(): array
{
return [
'success' => $this->success,
'error-codes' => $this->errorCodes,
];
}
}
@@ -0,0 +1,34 @@
<?php
namespace RyanChandler\LaravelCloudflareTurnstile\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use RyanChandler\LaravelCloudflareTurnstile\Facades\Turnstile as Facade;
class Turnstile implements ValidationRule
{
protected array $messages = [];
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$response = Facade::siteverify($value);
if ($response->success) {
return;
}
foreach ($response->errorCodes as $errorCode) {
$fail(match ($errorCode) {
'missing-input-secret' => __('cloudflare-turnstile::errors.missing-input-secret'),
'invalid-input-secret' => __('cloudflare-turnstile::errors.invalid-input-secret'),
'missing-input-response' => __('cloudflare-turnstile::errors.missing-input-response'),
'invalid-input-response' => __('cloudflare-turnstile::errors.invalid-input-response'),
'bad-request' => __('cloudflare-turnstile::errors.bad-request'),
'timeout-or-duplicate' => __('cloudflare-turnstile::errors.timeout-or-duplicate'),
'internal-error' => __('cloudflare-turnstile::errors.internal-error'),
default => __('cloudflare-turnstile::errors.unexpected'),
});
}
}
}
@@ -0,0 +1,53 @@
<?php
namespace RyanChandler\LaravelCloudflareTurnstile\Testing;
use Illuminate\Support\Testing\Fakes\Fake;
use RyanChandler\LaravelCloudflareTurnstile\Contracts\ClientInterface;
use RyanChandler\LaravelCloudflareTurnstile\Responses\SiteverifyResponse;
class FakeClient implements ClientInterface, Fake
{
protected bool $shouldPass = true;
protected bool $isExpired = false;
public function fail(): self
{
$this->shouldPass = false;
return $this;
}
public function expired(): self
{
$this->isExpired = true;
return $this;
}
public function pass(): self
{
$this->shouldPass = true;
return $this;
}
public function siteverify(string $response): SiteverifyResponse
{
if ($this->isExpired) {
return SiteverifyResponse::failure(['timeout-or-duplicate']);
}
if (! $this->shouldPass) {
return SiteverifyResponse::failure(['invalid-input-response']);
}
return SiteverifyResponse::success();
}
public function dummy(): string
{
return self::RESPONSE_DUMMY_TOKEN;
}
}
@@ -0,0 +1,14 @@
<?php
namespace RyanChandler\LaravelCloudflareTurnstile\View\Components;
use Illuminate\View\Component;
class Scripts extends Component
{
public function render()
{
// @phpstan-ignore-next-line argument.type
return view('cloudflare-turnstile::components.scripts');
}
}
@@ -0,0 +1,14 @@
<?php
namespace RyanChandler\LaravelCloudflareTurnstile\View\Components;
use Illuminate\View\Component;
class Turnstile extends Component
{
public function render()
{
// @phpstan-ignore argument.type
return view('cloudflare-turnstile::components.turnstile');
}
}