Skip to content

Commit b1234e6

Browse files
committedAug 3, 2023
Revert "Blade precompilers + L10 test app (#492)"
This reverts commit 591c516.
1 parent 591c516 commit b1234e6

17 files changed

+255
-207
lines changed
 

‎.github/workflows/run-table-tests.yml

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@ jobs:
88
strategy:
99
fail-fast: true
1010
matrix:
11-
php: [8.2, 8.1]
12-
laravel: [10.*]
11+
php: [8.2, 8.1, 8.0]
12+
laravel: [10.*, 9.*]
1313
db: [mysql, postgres, sqlite]
1414
ssr: [true, false]
1515
dependency-version: [prefer-lowest, prefer-stable]
1616
include:
17+
- laravel: 9.*
18+
testbench: 7.*
1719
- laravel: 10.*
1820
testbench: 8.*
1921
exclude:
2022
- ssr: true
2123
dependency-version: prefer-lowest
2224
- ssr: true
2325
php: 8.1
26+
- ssr: true
27+
php: 8.0
28+
- laravel: 10.*
29+
php: 8.0
2430
- db: mysql
2531
ssr: true
2632
- db: postgres

‎.github/workflows/run-tests.yml

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ jobs:
88
strategy:
99
fail-fast: true
1010
matrix:
11-
php: [8.2, 8.1]
12-
laravel: [10.*]
11+
php: [8.2, 8.1, 8.0]
12+
laravel: [10.*, 9.*]
1313
ssr: [true, false]
1414
dependency-version: [prefer-lowest, prefer-stable]
1515
include:
16+
- laravel: 9.*
17+
testbench: 7.*
1618
- laravel: 10.*
1719
testbench: 8.*
1820
exclude:
1921
- ssr: true
2022
dependency-version: prefer-lowest
2123
- ssr: true
2224
php: 8.1
25+
- ssr: true
26+
php: 8.0
27+
- laravel: 10.*
28+
php: 8.0
2329

2430
name: Test P${{ matrix.php }} - L${{ matrix.laravel }} - SSR ${{ matrix.ssr }} - ${{ matrix.dependency-version }}
2531

‎app/composer.json

+10-8
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
],
99
"license": "MIT",
1010
"require": {
11-
"php": "^8.1|^8.2",
11+
"php": "^8.0.2",
1212
"beyondcode/laravel-websockets": "^1.13",
1313
"guzzlehttp/guzzle": "^7.2",
1414
"kirschbaum-development/eloquent-power-joins": "^2.6",
15-
"laravel/framework": "^10.16",
16-
"laravel/sanctum": "^3.2",
15+
"laravel/framework": "^9.19",
16+
"laravel/sanctum": "^2.14.1",
1717
"laravel/tinker": "^2.7",
1818
"maatwebsite/excel": "^3.1",
1919
"nesbot/carbon": "^2.63",
@@ -26,16 +26,18 @@
2626
"require-dev": {
2727
"barryvdh/laravel-debugbar": "^3.7",
2828
"fakerphp/faker": "^1.9.1",
29-
"laravel/dusk": "^7.9",
29+
"laravel/dusk": "^6.25",
30+
"laravel/sail": "^1.0.1",
3031
"mockery/mockery": "^1.4.4",
31-
"nunomaduro/collision": "^7.0",
32-
"phpunit/phpunit": "^10.0",
32+
"nunomaduro/collision": "^6.1",
33+
"phpunit/phpunit": "^9.5.10",
3334
"protonemedia/laravel-splade": "*",
35+
"spatie/ignition": "^1.4.1",
3436
"spatie/invade": "^1.1",
3537
"spatie/fractalistic": "^2.9",
36-
"spatie/laravel-ignition": "^2.0",
38+
"spatie/laravel-ignition": "^1.0",
3739
"spatie/laravel-ray": "^1.31",
38-
"spatie/phpunit-snapshot-assertions": "^5.0",
40+
"spatie/phpunit-snapshot-assertions": "^4.2",
3941
"thiagocordeiro/laravel-translator": "^1.2"
4042
},
4143
"autoload": {

‎app/phpunit.xml.dist

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
3-
<testsuites>
4-
<testsuite name="Unit">
5-
<directory suffix="Test.php">./tests/Unit</directory>
6-
</testsuite>
7-
<testsuite name="Feature">
8-
<directory suffix="Test.php">./tests/Feature</directory>
9-
</testsuite>
10-
</testsuites>
11-
<coverage/>
12-
<php>
13-
<env name="APP_ENV" value="testing"/>
14-
<env name="BCRYPT_ROUNDS" value="4"/>
15-
<env name="CACHE_DRIVER" value="array"/>
16-
<env name="DB_CONNECTION" value="sqlite"/>
17-
<env name="MAIL_MAILER" value="array"/>
18-
<env name="QUEUE_CONNECTION" value="sync"/>
19-
<env name="SESSION_DRIVER" value="array"/>
20-
<env name="TELESCOPE_ENABLED" value="false"/>
21-
</php>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
3+
<testsuites>
4+
<testsuite name="Unit">
5+
<directory suffix="Test.php">./tests/Unit</directory>
6+
</testsuite>
7+
<testsuite name="Feature">
8+
<directory suffix="Test.php">./tests/Feature</directory>
9+
</testsuite>
10+
</testsuites>
11+
<coverage processUncoveredFiles="true">
12+
<include>
13+
<directory suffix=".php">./app</directory>
14+
</include>
15+
</coverage>
16+
<php>
17+
<env name="APP_ENV" value="testing"/>
18+
<env name="BCRYPT_ROUNDS" value="4"/>
19+
<env name="CACHE_DRIVER" value="array"/>
20+
<env name="DB_CONNECTION" value="sqlite"/>
21+
<env name="MAIL_MAILER" value="array"/>
22+
<env name="QUEUE_CONNECTION" value="sync"/>
23+
<env name="SESSION_DRIVER" value="array"/>
24+
<env name="TELESCOPE_ENABLED" value="false"/>
25+
</php>
2226
</phpunit>

‎composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^8.1 || ^8.2",
20-
"illuminate/contracts": "^10.16"
19+
"php": "^8.0 || ^8.1 || ^8.2",
20+
"illuminate/contracts": "^9.41|^10.0"
2121
},
2222
"require-dev": {
2323
"laravel/pint": "^1.0",
24-
"nunomaduro/collision": "^7.0",
24+
"nunomaduro/collision": "^6.0",
2525
"nunomaduro/larastan": "^2.0.1",
26-
"orchestra/testbench": "^8.0",
27-
"phpunit/phpunit": "^10.0"
26+
"orchestra/testbench": "^7.7|^8.0",
27+
"phpunit/phpunit": "^9.5"
2828
},
2929
"suggest": {
3030
"kirschbaum-development/eloquent-power-joins": "Required to enable support sorting by (nested) relationships in Splade Tables (^2.6)",

‎phpstan-baseline.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ parameters:
128128
-
129129
message: "#^Parameter \\#2 \\$array of function implode expects array\\<string\\>, array\\<int, array\\<int, string\\>\\|string\\> given\\.$#"
130130
count: 1
131-
path: src/BladeHelpers.php
131+
path: src/CustomBladeCompiler.php
132132

133133
-
134134
message: "#^Method ProtoneMedia\\\\Splade\\\\EventRedirect\\:\\:jsonSerialize\\(\\) should return array but returns void\\.$#"

‎phpunit.xml.dist

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
bootstrap="vendor/autoload.php"
8+
colors="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnFailure="false"
14+
executionOrder="random"
15+
failOnWarning="true"
16+
failOnRisky="true"
17+
failOnEmptyTestSuite="true"
18+
beStrictAboutOutputDuringTests="true"
19+
verbose="true"
20+
>
21+
<testsuites>
22+
<testsuite name="ProtoneMedia Test Suite">
23+
<directory>tests</directory>
24+
</testsuite>
25+
</testsuites>
26+
<coverage>
27+
<include>
28+
<directory suffix=".php">./src</directory>
29+
</include>
30+
<report>
31+
<html outputDirectory="build/coverage"/>
32+
<text outputFile="build/coverage.txt"/>
33+
<clover outputFile="build/logs/clover.xml"/>
34+
</report>
35+
</coverage>
36+
<logging>
37+
<junit outputFile="build/report.junit.xml"/>
38+
</logging>
39+
</phpunit>

‎src/BladeHelpers.php

-14
This file was deleted.

‎src/CustomBladeCompiler.php

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace ProtoneMedia\Splade;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\View\Compilers\BladeCompiler;
7+
use ProtoneMedia\Splade\Components\SpladeComponent;
8+
use ProtoneMedia\Splade\Http\PrepareTableCells;
9+
10+
class CustomBladeCompiler extends BladeCompiler
11+
{
12+
/**
13+
* This method is overridden to prepare some Splade components before they are compiled.
14+
*
15+
* @param string $view
16+
* @return string
17+
*/
18+
protected function compileComponentTags($view)
19+
{
20+
$this->prepareLazyComponents($view);
21+
$this->prepareRehydrateComponents($view);
22+
$this->replaceCellComponentWithCellDirective($view);
23+
24+
return parent::compileComponentTags($view);
25+
}
26+
27+
/**
28+
* Returns a regex pattern to match an HTML tag and its contents.
29+
*/
30+
public static function regexForTag(string $tag): string
31+
{
32+
return '/(<\s*' . $tag . '[^>]*>)(.|\n)*?(<\/' . $tag . '>)/';
33+
}
34+
35+
/**
36+
* Replaces the <x-splace-cell> component with the @cell directive.
37+
*/
38+
protected function replaceCellComponentWithCellDirective(&$view)
39+
{
40+
$view = preg_replace_callback(static::regexForTag(SpladeComponent::tag('table')), function ($table) {
41+
$tableHtml = $table[0];
42+
$tableOpening = $table[1];
43+
44+
preg_match_all(PrepareTableCells::HTML_ATTRIBUTES_REGEX, $tableOpening, $matches, PREG_SET_ORDER);
45+
46+
$arguments = collect($matches)->mapWithKeys(function ($match) use (&$tableOpening) {
47+
$attribute = $match['attribute'];
48+
$value = $match['value'] ?? null;
49+
50+
if ($value && in_array($attribute, ['as', 'key', 'use'])) {
51+
// Remove these attributes from the table tag, so Vue won't be mad.
52+
$tableOpening = str_replace($match[0], '', $tableOpening);
53+
54+
return [$attribute => PrepareTableCells::stripQuotes($value)];
55+
}
56+
57+
return [];
58+
});
59+
60+
// Replace the original tag opening with the modified one, without the attributes.
61+
$tableHtml = str_replace($table[1], $tableOpening, $tableHtml);
62+
63+
// Replace the custom cells.
64+
return PrepareTableCells::replaceCellComponentWithCellDirective(
65+
$tableHtml,
66+
$arguments->get('as', '$item'),
67+
$arguments->get('key', '$key'),
68+
$arguments->get('use', ''),
69+
);
70+
}, $view);
71+
}
72+
73+
/**
74+
* It adds an additional unless-statement around the placeholder, so it's only rendered
75+
* when the component is not rehydrated.
76+
*/
77+
protected function prepareRehydrateComponents(&$view)
78+
{
79+
// Find the rehydrate components within the view
80+
preg_match_all(static::regexForTag(SpladeComponent::tag('rehydrate')), $view, $matches);
81+
82+
// Extract the (optional) placeholder
83+
collect($matches[0] ?? [])->each(function (string $rehydrateComponent) use (&$view) {
84+
preg_match_all(static::regexForTag('x-slot:placeholder'), $rehydrateComponent, $placeholderMatches);
85+
86+
$placeholder = $placeholderMatches[0][0] ?? '';
87+
88+
if (!$placeholder) {
89+
return;
90+
}
91+
92+
$vuePlaceholder = str_replace($placeholderMatches[1][0], '<template v-slot:placeholder>', $placeholder);
93+
$vuePlaceholder = str_replace($placeholderMatches[3][0], '</template>', $vuePlaceholder);
94+
95+
$vuePlaceholder = implode(PHP_EOL, [
96+
'@unless(\ProtoneMedia\Splade\Facades\Splade::isRehydrateRequest())',
97+
$vuePlaceholder,
98+
'@endunless',
99+
]);
100+
101+
$view = str_replace($placeholder, $vuePlaceholder, $view);
102+
});
103+
}
104+
105+
/**
106+
* It adds an additional if-statement around the slot, so it's only rendered
107+
* when the component is lazy-loaded.
108+
*/
109+
protected function prepareLazyComponents(&$view)
110+
{
111+
// Find the lazy components within the view
112+
preg_match_all(static::regexForTag(SpladeComponent::tag('lazy')), $view, $matches);
113+
114+
// Replace all lazy components with just the placeholder
115+
collect($matches[0] ?? [])->each(function (string $lazyComponent, $key) use ($matches, &$view) {
116+
preg_match_all(static::regexForTag('x-slot:placeholder'), $lazyComponent, $placeholderMatches);
117+
118+
$innerLazy = Str::between($lazyComponent, $matches[1][$key], $matches[3][$key]);
119+
120+
$placeholder = $placeholderMatches[0][0] ?? '';
121+
122+
$innerLazyWithoutPlaceholder = $placeholder ? str_replace($placeholder, '', $innerLazy) : $innerLazy;
123+
124+
$newLazyComponent = implode(PHP_EOL, array_filter([
125+
$matches[1][$key],
126+
$placeholder,
127+
'@if(\ProtoneMedia\Splade\Facades\Splade::isLazyRequest())',
128+
$innerLazyWithoutPlaceholder,
129+
'@endif',
130+
$matches[3][$key],
131+
]));
132+
133+
$view = str_replace($lazyComponent, $newLazyComponent, $view);
134+
});
135+
}
136+
}

‎src/Http/PrepareTableCells.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace ProtoneMedia\Splade\Http;
44

55
use Illuminate\Support\Str;
6-
use ProtoneMedia\Splade\BladeHelpers;
76
use ProtoneMedia\Splade\Components\SpladeComponent;
7+
use ProtoneMedia\Splade\CustomBladeCompiler;
88

99
class PrepareTableCells
1010
{
@@ -38,7 +38,7 @@ public static function replaceCellComponentWithCellDirective(
3838
string $defaultUse
3939
): string {
4040
$cellTag = SpladeComponent::tag('cell');
41-
$cellRegex = BladeHelpers::regexForTag($cellTag);
41+
$cellRegex = CustomBladeCompiler::regexForTag($cellTag);
4242

4343
return preg_replace_callback($cellRegex, function ($cell) use ($cellTag, $defaultAs, $defaultKey, $defaultUse) {
4444
$cellHtml = $cell[0];

‎src/Precompilers/CustomTableCell.php

-50
This file was deleted.

‎src/Precompilers/LazyLoading.php

-44
This file was deleted.

‎src/Precompilers/Precompiler.php

-8
This file was deleted.

‎src/Precompilers/Rehydrate.php

-43
This file was deleted.

‎src/ServiceProvider.php

+20-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Support\Facades\Route;
1313
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
1414
use Illuminate\Support\Str;
15+
use Illuminate\View\Compilers\BladeCompiler;
1516
use Illuminate\View\ComponentAttributeBag;
1617
use Illuminate\View\Factory;
1718
use Illuminate\View\View;
@@ -37,9 +38,6 @@
3738
use ProtoneMedia\Splade\Http\FileUploadController;
3839
use ProtoneMedia\Splade\Http\TableBulkActionController;
3940
use ProtoneMedia\Splade\Http\TableExportController;
40-
use ProtoneMedia\Splade\Precompilers\CustomTableCell;
41-
use ProtoneMedia\Splade\Precompilers\LazyLoading;
42-
use ProtoneMedia\Splade\Precompilers\Rehydrate;
4341

4442
class ServiceProvider extends BaseServiceProvider
4543
{
@@ -60,7 +58,7 @@ public function register()
6058
$this->mergeConfigFrom($defaultSeoPath, 'splade.seo');
6159
}
6260

63-
$this->registerBladePrecompilers();
61+
$this->registerCustomBladeCompiler();
6462
}
6563

6664
/**
@@ -149,11 +147,25 @@ private function registerPublishedPaths()
149147
*
150148
* @return void
151149
*/
152-
protected function registerBladePrecompilers()
150+
protected function registerCustomBladeCompiler()
153151
{
154-
Blade::prepareStringsForCompilationUsing(new CustomTableCell);
155-
Blade::prepareStringsForCompilationUsing(new LazyLoading);
156-
Blade::prepareStringsForCompilationUsing(new Rehydrate);
152+
$this->app->extend('blade.compiler', function (BladeCompiler $service, $app) {
153+
return tap(new CustomBladeCompiler(
154+
$app['files'],
155+
$app['config']['view.compiled'],
156+
$app['config']->get('view.relative_hash', false) ? $app->basePath() : '',
157+
$app['config']->get('view.cache', true),
158+
$app['config']->get('view.compiled_extension', 'php'),
159+
), function ($blade) use ($service) {
160+
foreach ($service->getClassComponentAliases() as $alias => $component) {
161+
$blade->component($component, $alias);
162+
}
163+
164+
foreach ($service->getCustomDirectives() as $name => $directive) {
165+
$blade->directive($name, $directive);
166+
}
167+
});
168+
});
157169
}
158170

159171
/**

‎src/SpladeForm.php

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public function stay(bool $stay = true, string $actionOnSuccess = ''): self
197197
* If one or morge fieldnames are provided in $watch_fields,
198198
* the form will only be submitted on changes on these fields.
199199
*
200+
* @param array|string|null $watchFields
200201
* @return $this
201202
*/
202203
public function submitOnChange(

‎src/Table/HasColumns.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static function defaultHighlightFirstColumn(bool $state = true)
3838
/**
3939
* Adds a new column to the table.
4040
*
41+
* @param bool|null $canBeHidden
4142
* @param bool $searchable
4243
* @param callable|null $exportFormat
4344
* @param callable|null $exportStyling

0 commit comments

Comments
 (0)
Please sign in to comment.