Skip to content

Commit 1bd1348

Browse files
authored
Merge pull request #37 from tattersoftware/filter-args
Filter Arguments
2 parents 0a0311d + 4af3eeb commit 1bd1348

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ class Assets extends AssetsConfig
174174
}
175175
```
176176

177+
If you apply the filter via your Routes config file you may also supply bundle class names
178+
as arguments to merge them with any other configured route bundles:
179+
```php
180+
// **app/Config/Routes.php**
181+
$routes->add('files', 'Files::index', ['filter' => 'assets:\App\Filters\FilesFilter']);
182+
```
183+
177184
## Example
178185

179186
You want to make a simple web app for browsing and uploading files, based on Bootstrap's

src/Filters/AssetsFilter.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CodeIgniter\HTTP\RedirectResponse;
77
use CodeIgniter\HTTP\RequestInterface;
88
use CodeIgniter\HTTP\ResponseInterface;
9+
use Tatter\Assets\Bundle;
910
use Tatter\Assets\RouteBundle;
1011

1112
/**
@@ -28,7 +29,7 @@ public function before(RequestInterface $request, $arguments = null)
2829
/**
2930
* Gathers the route-specific assets and adds their tags to the response.
3031
*
31-
* @param array|null $arguments
32+
* @param class-string<Bundle>[]|null $arguments Additional Bundle classes
3233
*/
3334
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): ?ResponseInterface
3435
{
@@ -47,7 +48,15 @@ public function after(RequestInterface $request, ResponseInterface $response, $a
4748
return null;
4849
}
4950

50-
$bundle = RouteBundle::createFromRoute(ltrim($request->getPath(), '/ '));
51+
$bundle = RouteBundle::createFromRoute(ltrim($request->getPath(), '/ '));
52+
53+
// Check for additional Bundles specified in the arguments
54+
if (! empty($arguments)) {
55+
foreach ($arguments as $class) {
56+
$bundle->merge(new $class());
57+
}
58+
}
59+
5160
$headTags = $bundle->head();
5261
$bodyTags = $bundle->body();
5362

tests/FilterTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,33 @@ public function testFilter()
7373
$this->assertSame($expected, $result->getBody());
7474
}
7575

76+
public function testFilterWithArguments()
77+
{
78+
$expected = <<<'EOD'
79+
<html>
80+
<head>
81+
<title>Test</title>
82+
<link href="http://example.com/assets/apple.css" rel="stylesheet" type="text/css" />
83+
<link href="https://water.com/glassof.css" rel="stylesheet" type="text/css" />
84+
</head>
85+
<body>
86+
<h1>Hello</h1>
87+
<script src="https://pagecdn.io/lib/cleave/1.6.0/cleave.min.js" type="text/javascript"></script>
88+
<script src="http://example.com/assets/banana.js" type="text/javascript"></script>
89+
<script src="http://example.com/assets/directory/machines.js" type="text/javascript"></script>
90+
</body>
91+
</html>
92+
EOD;
93+
94+
$this->request->setPath('foobar');
95+
96+
$caller = $this->getFilterCaller(AssetsFilter::class, 'after');
97+
$result = $caller([LunchBreak::class]);
98+
99+
$this->assertInstanceOf(ResponseInterface::class, $result);
100+
$this->assertSame($expected, $result->getBody());
101+
}
102+
76103
public function testEmptyTags()
77104
{
78105
$this->config->routes = [];

0 commit comments

Comments
 (0)