Skip to content

Commit f970d61

Browse files
committed
Add documentation for Symfony integration for #5
Thanks to Zeljko Mitic (@zmitic) and Thomas Landauer (@ThomasLandauer)!
1 parent 6d289d3 commit f970d61

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

README.md

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,58 @@ is likely disabled. A bug for sure, but perhaps not a critical one.
4848
However, `user.getRoleLabel(usr.role)` will cause an uncaught `TypeError` if that method's parameter is not nullable,
4949
since Twig will call that method with `null`. Instead of just having a buggy badge, *the whole page breaks*.
5050

51-
# Usage
51+
# Installation
5252
First, install using
5353
```bash
5454
composer require --dev alisqi/twigqi
5555
```
5656

57-
Next, add the extension to your `Twig\Environment`:
57+
## Symfony integration
58+
In a Symfony application, you can enable the extension in your `config\services.yaml`
59+
```yaml
60+
when@dev:
61+
services:
62+
AlisQI\TwigQI\Extension:
63+
autowire: true
64+
tags: [ 'twig.extension' ]
65+
```
66+
67+
Alternatively, you can extend `AlisQI\TwigQI\Extension` and add the `When` attribute.
5868
```php
59-
$logger = new AlisQI\TwigQI\Logger\TriggerErrorLogger();
60-
$twig->addExtension(new AlisQI\TwigQI\Extension($logger));
69+
<?php
70+
71+
namespace App\Twig;
72+
73+
use AlisQI\TwigQI\Extension;
74+
use Symfony\Component\DependencyInjection\Attribute\When;
75+
76+
#[When('dev')]
77+
class QualityExtension extends Extension {}
6178
```
79+
This allows you to configure which inspections to enable. See details below.
6280

63-
The `TriggerErrorLogger` will report issues using PHP's `trigger_error` with `E_USER_*` levels.
64-
Alternatively, you can use any other logger `\Psr\Log\LoggerInterface` implementation.
81+
### Logging
82+
Either way, all inspection results will [show up](docs/error-on-page.png) in the Web Debug Toolbar's logs.
6583

66-
Set up your app and/or CI build to report these as you see fit.
84+
![Error shown in Web Debug Toolbar](docs/error-in-wdt.png)
6785

68-
And that's it! 😎
86+
This example is based on the [Symfony demo application](https://github.com/symfony/demo), where
87+
`src/templates/blog/post_show.html.twig` was amended to include the following in the `main` block:
88+
```twig
89+
{% types post: '\\App\\Entity\\Post' %}
90+
{% if false %} {# to demonstrate static typing during template compilation #}
91+
{{ post.tilte }}
92+
{% endif %}
93+
```
94+
95+
## Non-Symfony projects
96+
You can also add the extension manually to your `Twig\Environment`:
97+
```php
98+
$twig->addExtension(new AlisQI\TwigQI\Extension($logger));
99+
```
69100

70101
## Configuration
102+
### Select inspections
71103
You can cherry-pick your inspections (see below):
72104
```php
73105
use AlisQI\TwigQI\Extension;
@@ -80,6 +112,12 @@ new Extension($logger, [
80112
]);
81113
```
82114

115+
### Logging
116+
The extension class requires a `\Psr\Log\LoggerInterface` implementation.
117+
118+
This package includes the `TriggerErrorLogger` class, which reports issues using PHP's `trigger_error()`
119+
with appropriate `E_USER_*` levels.
120+
83121
# Design
84122
The current design uses `NodeVisitor` classes for every inspection. That allows for easy testing and configurability.
85123

docs/error-in-wdt.png

59.5 KB
Loading

docs/error-on-page.png

111 KB
Loading

0 commit comments

Comments
 (0)