Skip to content

Commit a0817ce

Browse files
committed
update readme and tests
1 parent 1484919 commit a0817ce

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

.github/workflows/php.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
strategy:
1717
matrix:
1818
php-versions: ['8.2', '8.3']
19+
dependencies: [ 'locked', 'latest' ]
1920

2021
steps:
2122
- uses: actions/checkout@v2
@@ -26,10 +27,13 @@ jobs:
2627
php-version: ${{ matrix.php-versions }}
2728

2829
- name: Validate composer.json and composer.lock
29-
run: composer validate
30+
run: composer validate --strict -n
3031

31-
- name: Install dependencies
32-
run: composer install --prefer-dist --no-progress --no-suggest
32+
- name: Install Composer dependencies
33+
uses: ramsey/composer-install@v2
34+
with:
35+
dependency-versions: ${{ matrix.dependencies }}
36+
composer-options: --prefer-dist
3337

3438
- name: Run Check
3539
run: composer check

README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ services:
6262
namespace Shapecode\Bundle\TwigTemplateEventBundle\EventListener;
6363

6464
use Shapecode\Bundle\TwigTemplateEventBundle\Event\Code\TwigEventString;
65+
use Shapecode\Bundle\TwigTemplateEventBundle\Event\Code\TwigEventInclude;
6566
use Shapecode\Bundle\TwigTemplateEventBundle\Event\TwigTemplateEvent;
6667
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
6768

@@ -70,10 +71,31 @@ class TestTwigEventListener
7071
{
7172
public function __invoke(TwigTemplateEvent $event): void
7273
{
73-
if ($event->getEventName() == 'test') {
74-
$event->addCode(new TwigEventString('hello {{ world }}', array(
75-
'world' => 'World'
76-
)));
74+
if ($event->getEventName() == 'foo') {
75+
76+
// to add a string
77+
$event->addCode(
78+
new TwigEventString(
79+
'hello {{ world }}',
80+
[
81+
'world' => 'World'
82+
],
83+
10 // default is 0. The higher the number the later the code will be executed. The lower the number the earlier the code will be executed.
84+
)
85+
);
86+
}
87+
88+
if ($event->getEventName() == 'bar') {
89+
90+
// to include a twig template
91+
$event->addCode(
92+
new TwigEventInclude(
93+
'@App/Layout/Header/_search.html.twig',
94+
[
95+
'world' => 'World'
96+
],
97+
)
98+
);
7799
}
78100
}
79101
}

src/Event/TwigTemplateEvent.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class TwigTemplateEvent extends Event
2121
* @param array<string, mixed> $parameters
2222
*/
2323
public function __construct(
24-
private string $eventName,
25-
private Environment $environment,
26-
private array $context,
27-
private array $parameters,
28-
private RequestStack $request,
24+
private readonly string $eventName,
25+
private readonly Environment $environment,
26+
private readonly array $context,
27+
private readonly array $parameters,
28+
private readonly RequestStack $requestStack,
2929
) {
3030
$this->codes = [];
3131
}
@@ -47,9 +47,14 @@ public function getContext(): array
4747
return $this->context;
4848
}
4949

50+
public function getRequestStack(): RequestStack
51+
{
52+
return $this->requestStack;
53+
}
54+
5055
public function getRequest(): Request
5156
{
52-
$request = $this->request->getCurrentRequest();
57+
$request = $this->requestStack->getCurrentRequest();
5358

5459
if ($request === null) {
5560
throw new RuntimeException('request can not be null', 1594818314245);

0 commit comments

Comments
 (0)