diff --git a/.dockerignore b/.dockerignore index 2080adcf..f9102acb 100644 --- a/.dockerignore +++ b/.dockerignore @@ -21,3 +21,4 @@ infection* **/test* build* **/.* +bin/helper diff --git a/CHANGELOG.md b/CHANGELOG.md index 0003adfb..93025616 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this ### Changed * [#912](https://github.com/shlinkio/shlink/issues/912) Changed error templates to be plain html files, removing the dependency on `league/plates` package. +* [#875](https://github.com/shlinkio/shlink/issues/875) Updated to `mezzio/mezzio-swoole` v3.1. ### Deprecated * [#917](https://github.com/shlinkio/shlink/issues/917) Deprecated `/{shortCode}/qr-code/{size}` URL, in favor of providing the size in the query instead, `/{shortCode}/qr-code?size={size}`. diff --git a/README.md b/README.md index a54c20c8..533d5b50 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ Once Shlink is configured, you need to expose it to the web, either by using a t First you need to install the swoole PHP extension with [pecl](https://pecl.php.net/package/swoole), `pecl install swoole`. - Once installed, it's actually pretty easy to get shlink up and running with swoole. Run `./vendor/bin/mezzio-swoole start -d` and you will get shlink running on port 8080. + Once installed, it's actually pretty easy to get shlink up and running with swoole. Run `./vendor/bin/laminas mezzio:swoole:start -d` and you will get shlink running on port 8080. However, by doing it this way, you are loosing all the access logs, and the service won't be automatically run if the server has to be restarted. @@ -147,7 +147,7 @@ Once Shlink is configured, you need to expose it to the web, either by using a t # Description: Shlink non-blocking server with swoole ### END INIT INFO - SCRIPT=/path/to/shlink/vendor/bin/mezzio-swoole\ start + SCRIPT=/path/to/shlink/vendor/bin/laminas\ mezzio:swoole:start RUNAS=root PIDFILE=/var/run/shlink_swoole.pid diff --git a/bin/helper/mezzio-swoole b/bin/helper/mezzio-swoole new file mode 100755 index 00000000..2c341326 --- /dev/null +++ b/bin/helper/mezzio-swoole @@ -0,0 +1,51 @@ +#!/usr/bin/env php +get('config')['laminas-cli']['commands'] ?? [], + fn ($c, string $command) => str_starts_with($command, $commandsPrefix), +); +$registeredCommands = []; + +foreach ($commands as $newName => $commandServiceName) { + [, $oldName] = explode($commandsPrefix, $newName); + $registeredCommands[$oldName] = $commandServiceName; + + $container->addDelegator($commandServiceName, static function ($c, $n, callable $factory) use ($oldName) { + /** @var Command $command */ + $command = $factory(); + $command->setAliases([$oldName]); + + return $command; + }); +} + +$commandLine = new CommandLine('Mezzio web server', $version); +$commandLine->setAutoExit(true); +$commandLine->setCommandLoader(new ContainerCommandLoader($container, $registeredCommands)); +$commandLine->run(); diff --git a/bin/test/run-api-tests.sh b/bin/test/run-api-tests.sh index f3236d1b..06708d18 100755 --- a/bin/test/run-api-tests.sh +++ b/bin/test/run-api-tests.sh @@ -4,16 +4,16 @@ export DB_DRIVER=mysql export TEST_ENV=api # Try to stop server just in case it hanged in last execution -vendor/bin/mezzio-swoole stop +vendor/bin/laminas mezzio:swoole:stop echo 'Starting server...' -vendor/bin/mezzio-swoole start -d +vendor/bin/laminas mezzio:swoole:start -d sleep 2 vendor/bin/phpunit --order-by=random -c phpunit-api.xml --testdox --colors=always --log-junit=build/coverage-api/junit.xml $* testsExitCode=$? -vendor/bin/mezzio-swoole stop +vendor/bin/laminas mezzio:swoole:stop # Exit this script with the same code as the tests. If tests failed, this script has to fail exit $testsExitCode diff --git a/build.sh b/build.sh index 2f5a23bd..16610a8b 100755 --- a/build.sh +++ b/build.sh @@ -28,6 +28,9 @@ echo "Installing dependencies with $composerBin..." ${composerBin} self-update ${composerBin} install --no-dev --optimize-autoloader --prefer-dist --no-progress --no-interaction +# Copy mezzio helper script to vendor (deprecated - Remove with Shlink 3.0.0) +cp "${projectdir}/bin/helper/mezzio-swoole" "./vendor/bin" + # Delete development files echo 'Deleting dev files...' rm composer.* diff --git a/composer.json b/composer.json index 70454cfa..ada2facc 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "mezzio/mezzio-fastroute": "^3.1", "mezzio/mezzio-helpers": "^5.3", "mezzio/mezzio-problem-details": "^1.1", - "mezzio/mezzio-swoole": "^2.6.4", + "mezzio/mezzio-swoole": "^3.1", "monolog/monolog": "^2.0", "nikolaposa/monolog-factory": "^3.1", "ocramius/proxy-manager": "^2.11", @@ -49,7 +49,7 @@ "ramsey/uuid": "^3.9", "shlinkio/shlink-common": "dev-main#1311861 as 3.4", "shlinkio/shlink-config": "^1.0", - "shlinkio/shlink-event-dispatcher": "^1.6", + "shlinkio/shlink-event-dispatcher": "^2.0", "shlinkio/shlink-importer": "^2.1", "shlinkio/shlink-installer": "^5.3", "shlinkio/shlink-ip-geolocation": "^1.5", diff --git a/data/infra/examples/shlink-daemon.sh b/data/infra/examples/shlink-daemon.sh index a18ca65a..ce905721 100644 --- a/data/infra/examples/shlink-daemon.sh +++ b/data/infra/examples/shlink-daemon.sh @@ -8,7 +8,7 @@ # Description: Shlink non-blocking server with swoole ### END INIT INFO -SCRIPT=/path/to/shlink/vendor/bin/mezzio-swoole\ start +SCRIPT=/path/to/shlink/vendor/bin/laminas\ mezzio:swoole:start RUNAS=root PIDFILE=/var/run/shlink_swoole.pid diff --git a/data/infra/swoole.Dockerfile b/data/infra/swoole.Dockerfile index b6bfb5a7..bb1f084c 100644 --- a/data/infra/swoole.Dockerfile +++ b/data/infra/swoole.Dockerfile @@ -95,4 +95,4 @@ CMD \ if [[ ! -d "./vendor" ]]; then /usr/local/bin/composer install ; fi && \ # When restarting the container, swoole might think it is already in execution # This forces the app to be started every second until the exit code is 0 - until php ./vendor/bin/mezzio-swoole start; do sleep 1 ; done + until php ./vendor/bin/laminas mezzio:swoole:start; do sleep 1 ; done diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index 055e315f..df480d2f 100644 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -17,4 +17,4 @@ php vendor/doctrine/orm/bin/doctrine.php orm:clear-cache:metadata -n -q # When restarting the container, swoole might think it is already in execution # This forces the app to be started every second until the exit code is 0 -until php vendor/mezzio/mezzio-swoole/bin/mezzio-swoole start; do sleep 1 ; done +until php vendor/bin/laminas mezzio:swoole:start; do sleep 1 ; done diff --git a/module/Core/config/event_dispatcher.config.php b/module/Core/config/event_dispatcher.config.php index c72e2d7a..83390fdd 100644 --- a/module/Core/config/event_dispatcher.config.php +++ b/module/Core/config/event_dispatcher.config.php @@ -14,13 +14,13 @@ return [ 'events' => [ 'regular' => [ - EventDispatcher\VisitLocated::class => [ + EventDispatcher\Event\VisitLocated::class => [ EventDispatcher\NotifyVisitToMercure::class, EventDispatcher\NotifyVisitToWebHooks::class, ], ], 'async' => [ - EventDispatcher\ShortUrlVisited::class => [ + EventDispatcher\Event\ShortUrlVisited::class => [ EventDispatcher\LocateShortUrlVisit::class, ], ], diff --git a/module/Core/src/EventDispatcher/VisitLocated.php b/module/Core/src/EventDispatcher/Event/AbstractVisitEvent.php similarity index 69% rename from module/Core/src/EventDispatcher/VisitLocated.php rename to module/Core/src/EventDispatcher/Event/AbstractVisitEvent.php index 0e1c1176..09869cb2 100644 --- a/module/Core/src/EventDispatcher/VisitLocated.php +++ b/module/Core/src/EventDispatcher/Event/AbstractVisitEvent.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Shlinkio\Shlink\Core\EventDispatcher; +namespace Shlinkio\Shlink\Core\EventDispatcher\Event; use JsonSerializable; -final class VisitLocated implements JsonSerializable +abstract class AbstractVisitEvent implements JsonSerializable { - private string $visitId; + protected string $visitId; public function __construct(string $visitId) { diff --git a/module/Core/src/EventDispatcher/Event/ShortUrlVisited.php b/module/Core/src/EventDispatcher/Event/ShortUrlVisited.php new file mode 100644 index 00000000..f177721f --- /dev/null +++ b/module/Core/src/EventDispatcher/Event/ShortUrlVisited.php @@ -0,0 +1,21 @@ +originalIpAddress = $originalIpAddress; + } + + public function originalIpAddress(): ?string + { + return $this->originalIpAddress; + } +} diff --git a/module/Core/src/EventDispatcher/Event/VisitLocated.php b/module/Core/src/EventDispatcher/Event/VisitLocated.php new file mode 100644 index 00000000..99b7a05e --- /dev/null +++ b/module/Core/src/EventDispatcher/Event/VisitLocated.php @@ -0,0 +1,9 @@ +visitId = $visitId; - $this->originalIpAddress = $originalIpAddress; - } - - public function visitId(): string - { - return $this->visitId; - } - - public function originalIpAddress(): ?string - { - return $this->originalIpAddress; - } - - public function jsonSerialize(): array - { - return ['visitId' => $this->visitId, 'originalIpAddress' => $this->originalIpAddress]; - } -} diff --git a/module/Core/src/Service/VisitsTracker.php b/module/Core/src/Service/VisitsTracker.php index fc35499f..46d4bd6b 100644 --- a/module/Core/src/Service/VisitsTracker.php +++ b/module/Core/src/Service/VisitsTracker.php @@ -10,7 +10,7 @@ use Psr\EventDispatcher\EventDispatcherInterface; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Tag; use Shlinkio\Shlink\Core\Entity\Visit; -use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited; +use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlVisited; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\Exception\TagNotFoundException; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; diff --git a/module/Core/test/EventDispatcher/LocateShortUrlVisitTest.php b/module/Core/test/EventDispatcher/LocateShortUrlVisitTest.php index ab12a349..8c9119a5 100644 --- a/module/Core/test/EventDispatcher/LocateShortUrlVisitTest.php +++ b/module/Core/test/EventDispatcher/LocateShortUrlVisitTest.php @@ -17,9 +17,9 @@ use Shlinkio\Shlink\Common\Util\IpAddress; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\VisitLocation; +use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlVisited; +use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; use Shlinkio\Shlink\Core\EventDispatcher\LocateShortUrlVisit; -use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited; -use Shlinkio\Shlink\Core\EventDispatcher\VisitLocated; use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException; use Shlinkio\Shlink\IpGeolocation\Model\Location; diff --git a/module/Core/test/EventDispatcher/NotifyVisitToMercureTest.php b/module/Core/test/EventDispatcher/NotifyVisitToMercureTest.php index 90891db3..b8e71297 100644 --- a/module/Core/test/EventDispatcher/NotifyVisitToMercureTest.php +++ b/module/Core/test/EventDispatcher/NotifyVisitToMercureTest.php @@ -13,8 +13,8 @@ use Psr\Log\LoggerInterface; use RuntimeException; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; +use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; use Shlinkio\Shlink\Core\EventDispatcher\NotifyVisitToMercure; -use Shlinkio\Shlink\Core\EventDispatcher\VisitLocated; use Shlinkio\Shlink\Core\Mercure\MercureUpdatesGeneratorInterface; use Shlinkio\Shlink\Core\Model\Visitor; use Symfony\Component\Mercure\PublisherInterface; diff --git a/module/Core/test/EventDispatcher/NotifyVisitToWebHooksTest.php b/module/Core/test/EventDispatcher/NotifyVisitToWebHooksTest.php index 8319f448..e7021e18 100644 --- a/module/Core/test/EventDispatcher/NotifyVisitToWebHooksTest.php +++ b/module/Core/test/EventDispatcher/NotifyVisitToWebHooksTest.php @@ -19,8 +19,8 @@ use Prophecy\Prophecy\ObjectProphecy; use Psr\Log\LoggerInterface; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; +use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; use Shlinkio\Shlink\Core\EventDispatcher\NotifyVisitToWebHooks; -use Shlinkio\Shlink\Core\EventDispatcher\VisitLocated; use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Options\AppOptions; diff --git a/module/Core/test/Service/VisitsTrackerTest.php b/module/Core/test/Service/VisitsTrackerTest.php index ef894aaf..17135f57 100644 --- a/module/Core/test/Service/VisitsTrackerTest.php +++ b/module/Core/test/Service/VisitsTrackerTest.php @@ -15,7 +15,7 @@ use Shlinkio\Shlink\Common\Util\DateRange; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Tag; use Shlinkio\Shlink\Core\Entity\Visit; -use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited; +use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlVisited; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\Exception\TagNotFoundException; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;