Skip to content

Commit

Permalink
update readme and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklog committed Jan 7, 2024
1 parent 1484919 commit a0817ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
strategy:
matrix:
php-versions: ['8.2', '8.3']
dependencies: [ 'locked', 'latest' ]

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

- name: Validate composer.json and composer.lock
run: composer validate
run: composer validate --strict -n

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Install Composer dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependencies }}
composer-options: --prefer-dist

- name: Run Check
run: composer check
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ services:
namespace Shapecode\Bundle\TwigTemplateEventBundle\EventListener;

use Shapecode\Bundle\TwigTemplateEventBundle\Event\Code\TwigEventString;
use Shapecode\Bundle\TwigTemplateEventBundle\Event\Code\TwigEventInclude;
use Shapecode\Bundle\TwigTemplateEventBundle\Event\TwigTemplateEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

Expand All @@ -70,10 +71,31 @@ class TestTwigEventListener
{
public function __invoke(TwigTemplateEvent $event): void
{
if ($event->getEventName() == 'test') {
$event->addCode(new TwigEventString('hello {{ world }}', array(
'world' => 'World'
)));
if ($event->getEventName() == 'foo') {

// to add a string
$event->addCode(
new TwigEventString(
'hello {{ world }}',
[
'world' => 'World'
],
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.
)
);
}

if ($event->getEventName() == 'bar') {

// to include a twig template
$event->addCode(
new TwigEventInclude(
'@App/Layout/Header/_search.html.twig',
[
'world' => 'World'
],
)
);
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/Event/TwigTemplateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class TwigTemplateEvent extends Event
* @param array<string, mixed> $parameters
*/
public function __construct(
private string $eventName,
private Environment $environment,
private array $context,
private array $parameters,
private RequestStack $request,
private readonly string $eventName,
private readonly Environment $environment,
private readonly array $context,
private readonly array $parameters,
private readonly RequestStack $requestStack,
) {
$this->codes = [];
}
Expand All @@ -47,9 +47,14 @@ public function getContext(): array
return $this->context;
}

public function getRequestStack(): RequestStack
{
return $this->requestStack;
}

public function getRequest(): Request
{
$request = $this->request->getCurrentRequest();
$request = $this->requestStack->getCurrentRequest();

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

0 comments on commit a0817ce

Please sign in to comment.