From 19cc5cba49eeaa646840d82eb6258ab3b1ab96c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20M=C3=BCller?= <2566282+brotkrueml@users.noreply.github.com> Date: Fri, 20 Sep 2024 10:23:01 +0200 Subject: [PATCH] docs: move code into separate files --- .gitattributes | 2 +- .github/workflows/ci.yml | 1 + Documentation/Events/Index.rst | 38 ++----------------- .../_BeforeMatomoApiRequestEventListener.php | 32 ++++++++++++++++ Documentation/Events/_Services.yaml | 7 ++++ Makefile | 1 + ecs.docs.php | 13 +++++++ 7 files changed, 58 insertions(+), 36 deletions(-) create mode 100644 Documentation/Events/_BeforeMatomoApiRequestEventListener.php create mode 100644 Documentation/Events/_Services.yaml create mode 100644 ecs.docs.php diff --git a/.gitattributes b/.gitattributes index 6942264..a4be3a3 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8368ec1..e6c95f8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/Documentation/Events/Index.rst b/Documentation/Events/Index.rst index b392644..1cbdaa7 100644 --- a/Documentation/Events/Index.rst +++ b/Documentation/Events/Index.rst @@ -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 `. diff --git a/Documentation/Events/_BeforeMatomoApiRequestEventListener.php b/Documentation/Events/_BeforeMatomoApiRequestEventListener.php new file mode 100644 index 0000000..ec3644a --- /dev/null +++ b/Documentation/Events/_BeforeMatomoApiRequestEventListener.php @@ -0,0 +1,32 @@ +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']; + } +} diff --git a/Documentation/Events/_Services.yaml b/Documentation/Events/_Services.yaml new file mode 100644 index 0000000..0b0f7fa --- /dev/null +++ b/Documentation/Events/_Services.yaml @@ -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' diff --git a/Makefile b/Makefile index 9eed1ab..a85ed1b 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/ecs.docs.php b/ecs.docs.php new file mode 100644 index 0000000..2f6f652 --- /dev/null +++ b/ecs.docs.php @@ -0,0 +1,13 @@ +import(__DIR__ . '/.Build/vendor/brotkrueml/coding-standards/config/common.php'); + + $config->paths([ + __DIR__ . '/Documentation', + ]); +};