Skip to content

Commit 346de15

Browse files
authored
Merge pull request #10 from stayforlong/PLAT-2943_Update-api-Laravel-12-to-latest-patch-version
2 parents 4e3c07c + 75d0151 commit 346de15

15 files changed

Lines changed: 190 additions & 87 deletions

File tree

.github/workflows/ci.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ jobs:
1111
strategy:
1212
fail-fast: true
1313
matrix:
14-
php: [ '8.1', '8.2', '8.3']
15-
laravel: [ '9', '10', '11' ]
14+
php: [ '8.1', '8.2', '8.3', '8.4']
15+
laravel: [ '9', '10', '11', '12' ]
1616
exclude:
17+
- laravel: 12
18+
php: 8.1
1719
- laravel: 11
1820
php: 8.1
21+
- laravel: 9
22+
php: 8.4
1923

2024
name: PHP ${{ matrix.php }}; Laravel ${{ matrix.laravel }}
2125

@@ -57,6 +61,14 @@ jobs:
5761
command: composer require "laravel/framework:11.*" "phpunit/phpunit:^9.3.7" --no-update --no-interaction
5862
if: "matrix.laravel == '11'"
5963

64+
- name: Select Laravel 12
65+
uses: nick-invision/retry@v1
66+
with:
67+
timeout_minutes: 5
68+
max_attempts: 5
69+
command: composer require "laravel/framework:12.*" "phpunit/phpunit:^9.3.7" --no-update --no-interaction
70+
if: "matrix.laravel == '12'"
71+
6072
- name: Install PHP Dependencies
6173
uses: nick-invision/retry@v1
6274
with:

README.md

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
Allows you to use [Twig](http://twig.sensiolabs.org/) seamlessly in [Laravel](http://laravel.com/).
1+
Allows you to use [Twig](https://twig.symfony.com/) seamlessly in [Laravel](http://laravel.com/).
22

3-
[![Latest Stable Version](https://poser.pugx.org/rcrowe/twigbridge/v/stable.png)](https://packagist.org/packages/rcrowe/twigbridge)
4-
[![Total Downloads](https://poser.pugx.org/rcrowe/twigbridge/downloads.png)](https://packagist.org/packages/rcrowe/twigbridge)
5-
[![test](https://github.com/rcrowe/TwigBridge/actions/workflows/ci.yml/badge.svg)](https://github.com/rcrowe/TwigBridge/actions/workflows/ci.yml)
6-
[![License](https://poser.pugx.org/rcrowe/twigbridge/license.png)](https://packagist.org/packages/rcrowe/twigbridge)
3+
[![Latest Stable Version](https://poser.pugx.org/rcrowe/twigbridge/v/stable.png)](https://packagist.org/packages/rcrowe/twigbridge) [![Total Downloads](https://poser.pugx.org/rcrowe/twigbridge/downloads.png)](https://packagist.org/packages/rcrowe/twigbridge) [![test](https://github.com/rcrowe/TwigBridge/actions/workflows/ci.yml/badge.svg)](https://github.com/rcrowe/TwigBridge/actions/workflows/ci.yml) [![License](https://poser.pugx.org/rcrowe/twigbridge/license.png)](https://packagist.org/packages/rcrowe/twigbridge)
74

85
# Requirements
96

@@ -21,34 +18,48 @@ composer require rcrowe/twigbridge
2118

2219
Laravel automatically registers the Service Provider. Use Artisan to publish the twig config file:
2320

24-
```php
21+
```bash
2522
php artisan vendor:publish --provider="TwigBridge\ServiceProvider"
2623
```
2724

28-
At this point you can now begin using twig like you would any other view
25+
Create your Twig file in `resources/views` with the `.twig` file extension, for example:
2926

30-
```php
31-
//app/Http/routes.php
32-
//twig template resources/views/hello.twig
33-
Route::get('/', function () {
34-
return View::make('hello');
35-
});
27+
```html
28+
<!-- resources/views/welcome.twig -->
29+
<h1>
30+
Welcome to TwigBridge!
31+
</h1>
3632
```
3733

38-
You can create the twig files in resources/views with the .twig file extension.
39-
```bash
40-
resources/views/hello.twig
34+
At this point, you can now begin using Twig like you would any other view:
35+
36+
```php
37+
// routes/web.php
38+
39+
use Illuminate\Support\Facades\Route;
40+
41+
Route::get('/', static function () {
42+
return view('welcome');
43+
});
4144
```
4245

4346
# Configuration
4447

45-
Once Composer has installed or updated your packages you need to register TwigBridge with Laravel itself. Open up config/app.php and find the providers key towards the bottom and add:
48+
## Automatic
49+
50+
**For Modern Laravel (5.5+):** Laravel automatically registers the Service Provider and Facade through Package Auto-Discovery. **Manual registration in `config/app.php` is not required**, and you can skip directly to publishing the configuration file below.
51+
52+
## Manual
53+
54+
**For Legacy Laravel (5.4 and below):** If you are using an older version of Laravel without Package Auto-Discovery support, you need to register TwigBridge manually
55+
56+
Once Composer has installed or updated your packages, you need to register TwigBridge with Laravel itself. Open up `config/app.php` and find the providers key towards the bottom and add:
4657

4758
```php
4859
'TwigBridge\ServiceProvider',
4960
```
5061

51-
You can add the TwigBridge Facade, to have easier access to the TwigBridge (or Twig\Environment).
62+
You can add the TwigBridge Facade to have easier access to the TwigBridge (or Twig\Environment).
5263

5364
```php
5465
'Twig' => 'TwigBridge\Facade\Twig',
@@ -59,19 +70,21 @@ Twig::addExtension('TwigBridge\Extension\Loader\Functions');
5970
Twig::render('mytemplate', $data);
6071
```
6172

62-
TwigBridge's configuration file can be extended in your ConfigServiceProvider, under the `twigbridge` key. You can find the default configuration file at `vendor/rcrowe/twigbridge/config`.
73+
## Publishing Configuration
6374

64-
You _should_ use Artisan to copy the default configuration file from the `/vendor` directory to `/config/twigbridge.php` with the following command:
75+
TwigBridge's configuration file can be extended in your ConfigServiceProvider, under the `twigbridge` key. You can find the default configuration file at `vendor/rcrowe/twigbridge/config`.
6576

66-
```bash
77+
You *should* use Artisan to copy the default configuration file from the `/vendor` directory to `/config/twigbridge.php` with the following command:
78+
79+
```php
6780
php artisan vendor:publish --provider="TwigBridge\ServiceProvider"
6881
```
6982

70-
If you make changes to the `/config/twigbridge.php` file you will most likely have to run the `twig:clean` Artisan command for the changes to take effect.
83+
If you make changes to the `/config/twigbridge.php` file, you will most likely have to run the `twig:clean` Artisan command for the changes to take effect.
7184

7285
# Installation on Lumen
7386

74-
For Lumen, you need to load the same Service Provider, but you have to disable the `Auth`, `Translator` and `Url` extensions in your local configuration.
87+
For Lumen, you need to load the same Service Provider, but you have to disable the `Auth`, `Translator,` and `Url` extensions in your local configuration.
7588
Copy the `config/twigbridge.php` file to your local `config` folder and register the configuration + Service Provider in `bootstrap/app.php`:
7689

7790
```php
@@ -117,15 +130,15 @@ And output variables, escaped by default. Use the `raw` filter to skip escaping.
117130

118131
# Extensions
119132

120-
Sometimes you want to extend / add new functions for use in Twig templates. Add to the `enabled` array in config/twigbridge.php a list of extensions for Twig to load.
133+
Sometimes you want to extend/add new functions for use in Twig templates. Add to the `enabled` array in config/twigbridge.php a list of extensions for Twig to load.
121134

122135
```php
123136
'enabled' => array(
124137
'TwigBridge\Extensions\Example'
125138
)
126139
```
127140

128-
TwigBridge supports both a string or a closure as a callback, so for example you might implement the [Assetic](https://github.com/kriswallsmith/assetic) Twig extension as follows:
141+
TwigBridge supports both a string or a closure as a callback, so for example, you might implement the [Assetic](https://github.com/kriswallsmith/assetic) Twig extension as follows:
129142

130143
```php
131144
'enabled' => [
@@ -140,7 +153,7 @@ TwigBridge supports both a string or a closure as a callback, so for example you
140153

141154
TwigBridge comes with the following extensions enabled by default:
142155

143-
- [Twig\Extension\DebugExtension](http://twig.sensiolabs.org/doc/extensions/debug.html)
156+
- [Twig\Extension\DebugExtension](https://twig.symfony.com/doc/3.x/api.html#using-extensions)
144157
- TwigBridge\Extension\Laravel\Auth
145158
- TwigBridge\Extension\Laravel\Config
146159
- TwigBridge\Extension\Laravel\Dump
@@ -164,52 +177,55 @@ To enable '0.5.x' style Facades, enable the Legacy Facades extension:
164177

165178
These loader extensions exposes Laravel helpers as both Twig functions and filters.
166179

167-
Check out the config/twigbridge.php file to see a list of defined function / filters. You can also add your own.
180+
Check out the `config/twigbridge.php` file to see a list of defined functions/filters. You can also add your own.
168181

169182
## FacadeLoader
170183

171-
The FacadeLoader extension allows you to call any facade you have configured in config/twigbridge.php. This gives your Twig templates integration with any Laravel class as well as any other classes you alias.
184+
The FacadeLoader extension allows you to call any facade you have configured in `config/twigbridge.php`. This gives your Twig templates integration with any Laravel class, as well as any other classes you alias.
172185

173186
To use the Laravel integration (or indeed any aliased class and method), just add your facades to the config and call them like `URL.to(link)` (instead of `URL::to($link)`)
174187

175188
## Functions/Filters/Variables
176189

177-
The following helpers/filters are added by the default Extensions. They are based on the helpers and/or facades, so should be self explaining.
178-
179-
Functions:
180-
* asset, action, url, route, secure_url, secure_asset
181-
* auth_check, auth_guest, auth_user
182-
* can
183-
* config_get, config_has
184-
* dump
185-
* form_* (All the Form::* methods, snake_cased)
186-
* html_* (All the Html::* methods, snake_cased)
187-
* input_get, input_old, input_has
188-
* link_to, link_to_asset, link_to_route, link_to_action
189-
* session_has, session_get, csrf_token, csrf_field, method_field
190-
* str_* (All the Str::* methods, snake_cased)
191-
* trans, trans_choice
192-
* url_* (All the URL::* methods, snake_cased)
193-
194-
Filters:
195-
* camel_case, snake_case, studly_case
196-
* str_* (All the Str::* methods, snake_cased)
197-
* trans, trans_choice
198-
199-
Global variables:
200-
* app: the Illuminate\Foundation\Application object
201-
* errors: The $errors MessageBag from the Validator (always available)
190+
The following helpers/filters are added by the default Extensions. They are based on the helpers and/or facades, so they should be self-explanatory.
191+
192+
### Functions:
193+
194+
* `asset, action, url, route, secure_url, secure_asset`
195+
* `auth_check, auth_guest, auth_user`
196+
* `can`
197+
* `config_get, config_has`
198+
* `dump`
199+
* `form_*` (All the Form::* methods, snake_cased)
200+
* `html_*` (All the Html::* methods, snake_cased)
201+
* `input_get`, `input_old`, `input_has`
202+
* `link_to`, `link_to_asset`, `link_to_route`, `link_to_action`
203+
* `session_has`, `session_get`, `csrf_token`, `csrf_field`, `method_field`
204+
* `str_*` (All the Str::* methods, snake_cased)
205+
* `trans`, `trans_choice`
206+
* `url_*` (All the URL::* methods, snake_cased)
207+
208+
### Filters:
209+
210+
* `camel_case`, `snake_case`, `studly_case`
211+
* `str_*` (All the Str::* methods, snake_cased)
212+
* `trans`, `trans_choice`
213+
214+
### Global variables:
215+
216+
* `app`: the Illuminate\Foundation\Application object
217+
* `errors`: The `$errors` MessageBag from the Validator (always available)
202218

203219
# Artisan Commands
204220

205221
TwigBridge offers a command for CLI Interaction.
206222

207223
Empty the Twig cache:
208-
```
224+
```bash
209225
$ php artisan twig:clean
210226
```
211227

212228
Lint all Twig templates:
213-
```
229+
```bash
214230
$ php artisan twig:lint
215231
```

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
],
1616
"require": {
1717
"php": "^8.1",
18-
"twig/twig": "~3.9",
19-
"illuminate/support": "^9|^10|^11",
20-
"illuminate/view": "^9|^10|^11"
18+
"twig/twig": "~3.21",
19+
"illuminate/support": "^9|^10|^11|^12",
20+
"illuminate/view": "^9|^10|^11|^12"
2121
},
2222
"require-dev": {
2323
"ext-json": "*",
24-
"laravel/framework": "^9|^10|^11",
24+
"laravel/framework": "^9|^10|^11|^12",
2525
"mockery/mockery": "^1.3.1",
26-
"phpunit/phpunit": "^8.5.8 || ^9.3.7",
26+
"phpunit/phpunit": "^8.5.8 || ^9.3.7 || ^10.0 || ^11.0 || ^12.0",
2727
"squizlabs/php_codesniffer": "^3.6"
2828
},
2929
"autoload": {

config/twigbridge.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@
122122
'TwigBridge\Extension\Laravel\Translator',
123123
'TwigBridge\Extension\Laravel\Url',
124124
'TwigBridge\Extension\Laravel\Model',
125-
// 'TwigBridge\Extension\Laravel\Gate',
125+
'TwigBridge\Extension\Laravel\Gate',
126+
'TwigBridge\Extension\Laravel\Vite',
126127

127128
// 'TwigBridge\Extension\Laravel\Form',
128129
// 'TwigBridge\Extension\Laravel\Html',

src/Bridge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Bridge extends Environment
3636
/**
3737
* {@inheritdoc}
3838
*/
39-
public function __construct(LoaderInterface $loader, $options = [], Container $app = null)
39+
public function __construct(LoaderInterface $loader, $options = [], ?Container $app = null)
4040
{
4141
// Twig 2.0 doesn't support `true` anymore
4242
if (isset($options['autoescape']) && $options['autoescape'] === true) {

src/Extension/Laravel/Vite.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the TwigBridge package.
5+
*
6+
* @copyright Robert Crowe <hello@vivalacrowe.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace TwigBridge\Extension\Laravel;
13+
14+
use Illuminate\Foundation\Vite as IlluminateVite;
15+
use Twig\TwigFunction;
16+
use Twig\Extension\AbstractExtension;
17+
18+
/**
19+
* Access Laravels vite class in your Twig templates.
20+
*/
21+
class Vite extends AbstractExtension
22+
{
23+
/**
24+
* @var \Illuminate\Foundation\Vite
25+
*/
26+
protected $vite;
27+
28+
/**
29+
* Create a new Vite extension
30+
*
31+
* @param \Illuminate\Foundation\Vite
32+
*/
33+
public function __construct(IlluminateVite $vite)
34+
{
35+
$this->vite = $vite;
36+
}
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
public function getName()
42+
{
43+
return 'TwigBridge_Extension_Laravel_Vite';
44+
}
45+
46+
/**
47+
* {@inheritDoc}
48+
*/
49+
public function getFunctions()
50+
{
51+
return [
52+
new TwigFunction(
53+
'vite',
54+
[$this->vite, '__invoke'],
55+
[
56+
'is_safe' => [
57+
'html',
58+
],
59+
],
60+
),
61+
];
62+
}
63+
}

src/Node/EventNode.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public static function triggerLaravelEvents(string $templateName, array &$contex
2828
if (Str::endsWith($templateName, '.twig')) {
2929
$templateName = Str::substr($templateName, 0, mb_strlen($templateName) - 5);
3030
}
31+
32+
$viewPath = resource_path('views');
33+
if (Str::startsWith($templateName, $viewPath)) {
34+
$templateName = Str::substr($templateName, mb_strlen($viewPath)+1);
35+
}
36+
3137
/** @var \Illuminate\View\Factory $factory */
3238
$env = resolve('view');
3339
$viewName = ViewName::normalize($templateName);

src/Node/GetAttrNode.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ class GetAttrNode extends GetAttrExpression
3131
/**
3232
* @inheritdoc
3333
*/
34-
public function __construct(array $nodes = [], array $attributes = [], int $lineno = 0, string $tag = null)
34+
public function __construct(array $nodes = [], array $attributes = [], int $lineno = 0)
3535
{
3636
// Skip parent::__construct()
37-
Node::__construct($nodes, $attributes, $lineno, $tag);
37+
Node::__construct($nodes, $attributes, $lineno);
3838
}
3939

4040
/**
@@ -47,7 +47,7 @@ public function compile(Compiler $compiler): void
4747
// optimize array calls
4848
if ($this->getAttribute('optimizable')
4949
&& (!$env->isStrictVariables() || $this->getAttribute('ignore_strict_check'))
50-
&& !$this->getAttribute('is_defined_test')
50+
&& !$this->isDefinedTestEnabled()
5151
&& Template::ARRAY_CALL === $this->getAttribute('type')
5252
) {
5353
$var = '$'.$compiler->getVarName();
@@ -91,7 +91,7 @@ public function compile(Compiler $compiler): void
9191

9292
$compiler->raw(', ')
9393
->repr($this->getAttribute('type'))
94-
->raw(', ')->repr($this->getAttribute('is_defined_test'))
94+
->raw(', ')->repr($this->isDefinedTestEnabled())
9595
->raw(', ')->repr($this->getAttribute('ignore_strict_check'))
9696
->raw(', ')->repr($env->hasExtension(SandboxExtension::class))
9797
->raw(', ')->repr($this->getNode('node')->getTemplateLine())

0 commit comments

Comments
 (0)