Skip to content

Commit

Permalink
docs: move code into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
brotkrueml committed Sep 20, 2024
1 parent bf07b2e commit 19cc5cb
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/.gitignore export-ignore
/.noai export-ignore
/.rector.php export-ignore
/ecs.php export-ignore
/ecs*.php export-ignore
/Makefile export-ignore
/phpstan*.neon export-ignore
/rector.php export-ignore
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ jobs:
- name: Check coding standards
run: |
.Build/bin/ecs check --no-progress-bar
.Build/bin/ecs check --config=ecs.docs.php
- name: Run phpstan
run: |
make phpstan
Expand Down
38 changes: 3 additions & 35 deletions Documentation/Events/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,12 @@ Example
Dependent on the host name the current backend user is using we change the
site ID:

.. code-block:: php
.. literalinclude:: _BeforeMatomoApiRequestEventListener.php
:caption: EXT:your_extension/Classes/EventListener/BeforeMatomoApiRequestEventListener.php

namespace YourVendor\YourExtension\EventListener;
use Psr\Http\Message\ServerRequestInterface;
use YourVendor\YourExtension\Mapping\MatomoSiteMapper;
final class BeforeMatomoApiRequestEventListener
{
private MatomoSiteMapper $matomoSiteMapper;
public function __construct(MatomoSiteMapper $matomoSiteMapper)
{
$this->matomoSiteMapper = $matomoSiteMapper;
}
public function __invoke(BeforeMatomoApiRequestEvent $event): void
{
$hostName = $this->request->getServerParams()['REMOTE_HOST'];
if ($idSiteFromHostName = $this->matomoSiteMapper->getIdSiteFromHostName($hostName)) {
$event->setIdSite($idSiteFromHostName);
}
}
private function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}
Registration of the event listener:

.. code-block:: yaml
.. literalinclude:: _Services.yaml
:caption: EXT:your_extension/Configuration/Services.yaml

services:
YourVendor\YourExtension\EventListener\BeforeMatomoApiRequestEventListener:
tags:
- name: event.listener
identifier: 'myMatomoApiRequestListener'
Read :ref:`how to configure dependency injection in extensions <t3coreapi:dependency-injection-in-extensions>`.
32 changes: 32 additions & 0 deletions Documentation/Events/_BeforeMatomoApiRequestEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace YourVendor\YourExtension\EventListener;

use Brotkrueml\MatomoWidgets\Event\BeforeMatomoApiRequestEvent;
use Psr\Http\Message\ServerRequestInterface;
use YourVendor\YourExtension\Mapping\MatomoSiteMapper;

final class BeforeMatomoApiRequestEventListener
{
private MatomoSiteMapper $matomoSiteMapper;

public function __construct(MatomoSiteMapper $matomoSiteMapper)
{
$this->matomoSiteMapper = $matomoSiteMapper;
}

public function __invoke(BeforeMatomoApiRequestEvent $event): void
{
$hostName = $this->getRequest()->getServerParams()['REMOTE_HOST'];
if ($idSiteFromHostName = $this->matomoSiteMapper->getIdSiteFromHostName($hostName)) {
$event->setIdSite($idSiteFromHostName);
}
}

private function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}
7 changes: 7 additions & 0 deletions Documentation/Events/_Services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
# Place here the default dependency injection configuration

YourVendor\YourExtension\EventListener\BeforeMatomoApiRequestEventListener:
tags:
- name: event.listener
identifier: 'your-extension/before-matomo-api-request'
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ code-coverage: vendor
.PHONY: cs
cs: vendor
.Build/bin/ecs check --fix
.Build/bin/ecs check --fix --config=ecs.docs.php

.PHONY: docs
docs:
Expand Down
13 changes: 13 additions & 0 deletions ecs.docs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare (strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $config): void {
$config->import(__DIR__ . '/.Build/vendor/brotkrueml/coding-standards/config/common.php');

$config->paths([
__DIR__ . '/Documentation',
]);
};

0 comments on commit 19cc5cb

Please sign in to comment.