Skip to content

Commit b939e39

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: Remove 5.4 versionadded Document new Logstash handler argument Example remote ElasticsearchLogstashHandler [#15428] Add a bit more detail to bootstrap article Fix example code of customization of bootstrapping in test [#15186] Update example Adding full subscriber example [Validator] Add hint for testing custom constraints
2 parents aa5f86b + 03ecb46 commit b939e39

File tree

4 files changed

+123
-43
lines changed

4 files changed

+123
-43
lines changed

logging/handlers.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ To use it, declare it as a service:
2626
services:
2727
Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler: ~
2828
29+
# optionally, configure the handler using the constructor arguments (shown values are default)
30+
Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler: ~
31+
arguments:
32+
$endpoint: "http://127.0.0.1:9200"
33+
$index: "monolog"
34+
$client: null
35+
$level: !php/const Monolog\Logger::DEBUG
36+
$bubble: true
37+
$elasticsearchVersion: '1.0.0'
38+
2939
.. code-block:: xml
3040
3141
<!-- config/services.xml -->
@@ -40,16 +50,39 @@ To use it, declare it as a service:
4050
4151
<services>
4252
<service id="Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler"/>
53+
54+
<!-- optionally, configure the handler using the constructor arguments (shown values are default) -->
55+
<service id="Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler">
56+
<argument key="endpoint">http://127.0.0.1:9200</argument>
57+
<argument key="index">monolog</argument>
58+
<argument key="client"/>
59+
<argument key="level" type="constant">Monolog\Logger::DEBUG</argument>
60+
<argument key="bubble">true</argument>
61+
<argument key="elasticsearchVersion">1.0.0</argument>
62+
</service>
4363
</services>
4464
</container>
4565
4666
.. code-block:: php
4767
4868
// config/services.php
69+
use Monolog\Logger;
4970
use Symfony\Bridge\Monolog\Handler\ElasticsearchLogstashHandler;
5071
5172
$container->register(ElasticsearchLogstashHandler::class);
5273
74+
// optionally, configure the handler using the constructor arguments (shown values are default)
75+
$container->register(ElasticsearchLogstashHandler::class)
76+
->setArguments(
77+
'$endpoint' => "http://127.0.0.1:9200",
78+
'$index' => "monolog",
79+
'$client' => null,
80+
'$level' => Logger::DEBUG,
81+
'$bubble' => true,
82+
'$elasticsearchVersion' => '1.0.0',
83+
)
84+
;
85+
5386
Then reference it in the Monolog configuration:
5487

5588
.. configuration-block::

security.rst

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,17 +1691,47 @@ In some cases you need to run extra logic upon logout (e.g. invalidate
16911691
some tokens) or want to customize what happens after a logout. During
16921692
logout, a :class:`Symfony\\Component\\Security\\Http\\Event\\LogoutEvent`
16931693
is dispatched. Register an :doc:`event listener or subscriber </event_dispatcher>`
1694-
to run custom logic. The following information is available in the
1695-
event class:
1696-
1697-
``getToken()``
1698-
Returns the security token of the session that is about to be logged
1699-
out.
1700-
``getRequest()``
1701-
Returns the current request.
1702-
``getResponse()``
1703-
Returns a response, if it is already set by a custom listener. Use
1704-
``setResponse()`` to configure a custom logout response.
1694+
to execute custom logic::
1695+
1696+
// src/EventListener/LogoutSubscriber.php
1697+
namespace App\EventListener;
1698+
1699+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1700+
use Symfony\Component\HttpFoundation\RedirectResponse;
1701+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1702+
use Symfony\Component\Security\Http\Event\LogoutEvent;
1703+
1704+
class LogoutSubscriber implements EventSubscriberInterface
1705+
{
1706+
public function __construct(
1707+
private UrlGeneratorInterface $urlGenerator
1708+
) {
1709+
}
1710+
1711+
public static function getSubscribedEvents(): array
1712+
{
1713+
return [LogoutEvent::class => 'onLogout'];
1714+
}
1715+
1716+
public function onLogout(LogoutEvent $event): void
1717+
{
1718+
// get the security token of the session that is about to be logged out
1719+
$token = $event->getToken();
1720+
1721+
// get the current request
1722+
$request = $event->getRequest();
1723+
1724+
// get the current response, if it is already set by another listener
1725+
$response = $event->getResponse();
1726+
1727+
// configure a custom logout response to the homepage
1728+
$response = new RedirectResponse(
1729+
$this->urlGenerator->generate('homepage'),
1730+
RedirectResponse::HTTP_SEE_OTHER
1731+
);
1732+
$event->setResponse($response);
1733+
}
1734+
}
17051735

17061736
.. _retrieving-the-user-object:
17071737

@@ -2423,7 +2453,7 @@ for these events.
24232453
services:
24242454
# ...
24252455
2426-
App\EventListener\CustomLogoutSubscriber:
2456+
App\EventListener\LogoutSubscriber:
24272457
tags:
24282458
- name: kernel.event_subscriber
24292459
dispatcher: security.event_dispatcher.main
@@ -2440,7 +2470,7 @@ for these events.
24402470
<services>
24412471
<!-- ... -->
24422472
2443-
<service id="App\EventListener\CustomLogoutSubscriber">
2473+
<service id="App\EventListener\LogoutSubscriber">
24442474
<tag name="kernel.event_subscriber"
24452475
dispatcher="security.event_dispatcher.main"
24462476
/>
@@ -2453,14 +2483,12 @@ for these events.
24532483
// config/services.php
24542484
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
24552485
2456-
use App\EventListener\CustomLogoutListener;
2457-
use App\EventListener\CustomLogoutSubscriber;
2458-
use Symfony\Component\Security\Http\Event\LogoutEvent;
2486+
use App\EventListener\LogoutSubscriber;
24592487
24602488
return function(ContainerConfigurator $configurator) {
24612489
$services = $configurator->services();
24622490
2463-
$services->set(CustomLogoutSubscriber::class)
2491+
$services->set(LogoutSubscriber::class)
24642492
->tag('kernel.event_subscriber', [
24652493
'dispatcher' => 'security.event_dispatcher.main',
24662494
]);

testing.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Whenever you write a new line of code, you also potentially add new bugs.
88
To build better and more reliable applications, you should test your code
99
using both functional and unit tests.
1010

11+
.. _testing-installation:
12+
1113
The PHPUnit Testing Framework
1214
-----------------------------
1315

testing/bootstrap.rst

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,64 @@ running those tests. For example, if you're running a functional test and
66
have introduced a new translation resource, then you will need to clear your
77
cache before running those tests.
88

9-
Symfony already created the following ``tests/bootstrap.php`` file when installing
10-
the package to work with tests. If you don't have this file, create it::
9+
When :ref:`installing testing <testing-installation>` using Symfony Flex,
10+
it already created a ``tests/bootstrap.php`` file that is run by PHPUnit
11+
before your tests.
1112

12-
// tests/bootstrap.php
13-
use Symfony\Component\Dotenv\Dotenv;
13+
You can modify this file to add custom logic:
1414

15-
require dirname(__DIR__).'/vendor/autoload.php';
15+
.. code-block:: diff
1616
17-
if (file_exists(dirname(__DIR__).'/config/bootstrap.php')) {
18-
require dirname(__DIR__).'/config/bootstrap.php';
19-
} elseif (method_exists(Dotenv::class, 'bootEnv')) {
20-
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
21-
}
17+
// tests/bootstrap.php
18+
use Symfony\Component\Dotenv\Dotenv;
2219
23-
Then, check that your ``phpunit.xml.dist`` file runs this ``bootstrap.php`` file
24-
before running the tests:
20+
require dirname(__DIR__).'/vendor/autoload.php';
2521
26-
.. code-block:: xml
22+
if (file_exists(dirname(__DIR__).'/config/bootstrap.php')) {
23+
require dirname(__DIR__).'/config/bootstrap.php';
24+
} elseif (method_exists(Dotenv::class, 'bootEnv')) {
25+
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
26+
}
2727
28-
<!-- phpunit.xml.dist -->
29-
<?xml version="1.0" encoding="UTF-8" ?>
30-
<phpunit
31-
bootstrap="tests/bootstrap.php"
32-
>
33-
<!-- ... -->
34-
</phpunit>
28+
+ if (isset($_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'])) {
29+
+ // executes the "php bin/console cache:clear" command
30+
+ passthru(sprintf(
31+
+ 'APP_ENV=%s php "%s/../bin/console" cache:clear --no-warmup',
32+
+ $_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'],
33+
+ __DIR__
34+
+ ));
35+
+ }
36+
37+
.. note::
38+
39+
If you don't use Symfony Flex, make sure this file is configured as
40+
bootstrap file in your ``phpunit.xml.dist`` file:
3541

36-
Now, you can define in your ``phpunit.xml.dist`` file which environment you want the
37-
cache to be cleared:
42+
.. code-block:: xml
43+
44+
<!-- phpunit.xml.dist -->
45+
<?xml version="1.0" encoding="UTF-8" ?>
46+
<phpunit
47+
bootstrap="tests/bootstrap.php"
48+
>
49+
<!-- ... -->
50+
</phpunit>
51+
52+
Now, you can update the ``phpunit.xml.dist`` file to declare the custom
53+
environment variable introduced to ``tests/bootstrap.php``:
3854

3955
.. code-block:: xml
4056
4157
<!-- phpunit.xml.dist -->
4258
<?xml version="1.0" encoding="UTF-8" ?>
4359
<phpunit>
44-
<!-- ... -->
45-
4660
<php>
4761
<env name="BOOTSTRAP_CLEAR_CACHE_ENV" value="test"/>
62+
<!-- ... -->
4863
</php>
64+
65+
<!-- ... -->
4966
</phpunit>
5067
51-
This now becomes an environment variable (i.e. ``$_ENV``) that's available
52-
in the custom bootstrap file (``tests/bootstrap.php``).
68+
Now, when running ``vendor/bin/phpunit``, the cache will be cleared
69+
automatically by the bootstrap file before running all tests.

0 commit comments

Comments
 (0)