Compare commits

...

97 Commits

Author SHA1 Message Date
Alejandro Celaya
4cf3bc08f9 Merge pull request #1883 from shlinkio/release/v3.6.4
Release 3.6.4
2023-09-23 08:57:10 +02:00
Alejandro Celaya
7e093a3fd8 Fix date in changelog 2023-09-23 08:41:57 +02:00
Alejandro Celaya
abecf3be02 Merge pull request #1882 from acelaya-forks/feature/create-api-key
Feature/create api key
2023-09-23 08:40:53 +02:00
Alejandro Celaya
3d9b48c5fd Create InitialApiKeyCommand cli test 2023-09-23 08:28:57 +02:00
Alejandro Celaya
ba4a66f772 Add InitialApiKeyCommand unit test 2023-09-23 08:16:22 +02:00
Alejandro Celaya
ec839183e8 Add unit test for ApiKeyService::createInitial 2023-09-23 08:01:10 +02:00
Alejandro Celaya
b0ec0601c1 Update to latest shlink-installer 2023-09-22 10:00:19 +02:00
Alejandro Celaya
637d8334f4 New CLI command to create the initial API key idempotently 2023-09-21 09:47:21 +02:00
Alejandro Celaya
6db46b50e9 Roll back change to allow creating API keys with custom value 2023-09-21 08:58:05 +02:00
Alejandro Celaya
f6b1cc7556 Test API key creation with custom key 2023-09-19 10:14:04 +02:00
Alejandro Celaya
65a0a90a51 Allow custom API keys to be created 2023-09-19 09:10:17 +02:00
Alejandro Celaya
49bd230474 Merge pull request #1874 from acelaya-forks/feature/redis-lock-namespace
Make sure locks include the same cache namespace when sent to Redis
2023-09-12 21:44:45 +02:00
Alejandro Celaya
074f2135f6 Make sure locks include the same cache namespace when sent to Redis 2023-09-12 21:20:38 +02:00
Alejandro Celaya
ef073d59ca Merge pull request #1872 from acelaya-forks/bugfix/db-commands-timeout
Fix incorrect timeout in init commands
2023-09-12 08:33:13 +02:00
Alejandro Celaya
a3b2f94339 Make sure local config is not loaded in tests 2023-09-12 08:21:34 +02:00
Alejandro Celaya
b17c576a30 Fix incorrect timeout in init commands 2023-09-11 09:07:18 +02:00
Alejandro Celaya
228bd83b75 Merge pull request #1818 from acelaya-forks/feature/fix-sqlite-db-creation
Fix Shlink trying to create SQLite database tables even if they already exist
2023-06-14 18:22:39 +02:00
Alejandro Celaya
a21dcb852a Fix Shlink trying to create SQLite database tables even if they already exist 2023-06-14 18:08:39 +02:00
Alejandro Celaya
058391cf06 Merge pull request #1809 from acelaya-forks/feature/fix-rr-download
Update to a shlink-installer version that fixes rr download
2023-06-08 18:59:28 +02:00
Alejandro Celaya
24e6acc6e8 Update to a shlink-installer version that fixes rr download 2023-06-08 18:47:55 +02:00
Alejandro Celaya
56d299a7dc Merge pull request #1804 from acelaya-forks/feature/release-3.6.1
Feature/release 3.6.1
2023-06-04 09:30:19 +02:00
Alejandro Celaya
575e6bf707 Downgrade PHPUnit to avoid infection error 2023-06-04 09:13:37 +02:00
Alejandro Celaya
e50c21440f Define default values for env vars used in rr prod config 2023-06-04 09:07:41 +02:00
Alejandro Celaya
7cff11080d Update changelog 2023-06-04 08:57:07 +02:00
Alejandro Celaya
72381f9844 Change order to create initial database to avoid permission errors 2023-06-04 08:54:08 +02:00
Alejandro Celaya
b6792d3fb8 Merge pull request #1792 from shlinkio/develop
Release 3.6.0
2023-05-24 08:46:25 +02:00
Alejandro Celaya
2f0d658432 Merge pull request #1791 from acelaya-forks/feature/fix-cpu-100
Update changelog
2023-05-23 23:25:39 +02:00
Alejandro Celaya
8c1865c3ec Update changelog 2023-05-23 23:15:59 +02:00
Alejandro Celaya
096d2098d6 Update installer 2023-05-23 18:42:50 +02:00
Alejandro Celaya
882d64ae11 Add deprecation note for ENABLE_PERIODIC_VISIT_LOCATE env var 2023-05-23 10:55:49 +02:00
Alejandro Celaya
3352bcd186 Merge pull request #1789 from acelaya-forks/feature/improved-dependency-locks
Feature/improved dependency locks
2023-05-21 18:44:10 +02:00
Alejandro Celaya
9743c1624d Update changelog 2023-05-21 18:10:08 +02:00
Alejandro Celaya
e85d59c5a4 Add locks when creating short URL dependencies, to avoid race condition 2023-05-21 18:08:17 +02:00
Alejandro Celaya
ac0ff8fb94 Merge pull request #1787 from acelaya-forks/feature/shlink-init-command
Feature/shlink init command
2023-05-21 14:44:08 +02:00
Alejandro Celaya
90f93ee4ec Update changelog 2023-05-21 14:32:00 +02:00
Alejandro Celaya
794d926e3a Update docker entry point to use new shlink-installer init command 2023-05-21 14:30:20 +02:00
Alejandro Celaya
bd41ebef9f Merge pull request #1785 from acelaya-forks/feature/non-root-support
Allow running docker container as non-root
2023-05-19 20:29:40 +02:00
Alejandro Celaya
725370704f Update changelog 2023-05-19 19:50:05 +02:00
Alejandro Celaya
f03b7689ce Allow running docker container as non-root 2023-05-19 19:48:20 +02:00
Alejandro Celaya
fb31e2a5e4 Merge pull request #1782 from acelaya-forks/feature/clear-orphan-visits
Feature/clear orphan visits
2023-05-18 09:49:31 +02:00
Alejandro Celaya
d688c6da7e Update changelog 2023-05-18 09:36:50 +02:00
Alejandro Celaya
618784dc3b Create command to delete all orphan visits 2023-05-18 09:35:42 +02:00
Alejandro Celaya
9d64d4ed1d Create abstract base class for commands deleting visits 2023-05-18 09:33:15 +02:00
Alejandro Celaya
7f02243c6c Rename short-url:delete-visits to short-url:visits-delete for consistency with other commands 2023-05-18 09:19:01 +02:00
Alejandro Celaya
3916c68126 Add DeleteOrphanVisitsTest API test 2023-05-18 09:09:44 +02:00
Alejandro Celaya
a6f0c66331 Document endpoint to delete orphan visits 2023-05-18 09:06:52 +02:00
Alejandro Celaya
bdfb220126 Create REST action to delete orphan visits 2023-05-18 09:04:28 +02:00
Alejandro Celaya
abcf2f86be Create service to delete orphan visits 2023-05-18 09:01:57 +02:00
Alejandro Celaya
a4d8ebdfc9 Create DB logic to delete orphan visits 2023-05-18 08:58:07 +02:00
Alejandro Celaya
b51c149c30 Merge pull request #1779 from acelaya-forks/feature/clear-short-url-visits
Feature/clear short url visits
2023-05-17 09:20:28 +02:00
Alejandro Celaya
39095a3098 Fix coding styles 2023-05-17 08:57:36 +02:00
Alejandro Celaya
765199727e Update changelog 2023-05-16 09:29:22 +02:00
Alejandro Celaya
c7043af853 Create DeleteShortUrlVisitsCommandTest 2023-05-16 09:26:29 +02:00
Alejandro Celaya
02a8ef7dd9 Create DeleteShortUrlVisitsCommand 2023-05-15 09:48:24 +02:00
Alejandro Celaya
6bb8c1b2f5 Rename CLI Option namespace to Input 2023-05-15 09:02:23 +02:00
Alejandro Celaya
3cf253fd0f Document short URLs visits deletion endpoint 2023-05-14 18:25:27 +02:00
Alejandro Celaya
0365728337 Create DeleteShortUrlVisitsTest 2023-05-14 13:35:15 +02:00
Alejandro Celaya
b8143a5bb4 Create VisitDeleterRepositoryTest 2023-05-14 13:04:45 +02:00
Alejandro Celaya
531a19dde9 Refactor short URL visits deletion layers 2023-05-14 13:04:17 +02:00
Alejandro Celaya
69ff7de481 Create ShortUrlVisitsDeleterTest 2023-05-14 12:32:54 +02:00
Alejandro Celaya
ffc0555c7c Create DeleteShortUrlVisitsActionTest 2023-05-14 12:15:35 +02:00
Alejandro Celaya
84a7981dfa Create REST action to delete short URL visits 2023-05-14 12:00:08 +02:00
Alejandro Celaya
2573c2bf98 Update roadrunner config 2023-05-14 11:56:49 +02:00
Alejandro Celaya
3b4c1501f3 Set platforms to be used for openswoole docker image 2023-05-07 17:13:26 +02:00
Alejandro Celaya
e836bedecc Merge pull request #1775 from acelaya-forks/feature/default-roadrunner
Feature/default roadrunner
2023-05-07 13:34:53 +02:00
Alejandro Celaya
a797b74a70 Standardize logger for all Shlink execution contexts 2023-05-07 13:18:19 +02:00
Alejandro Celaya
ab497403ca Merge pull request #1773 from acelaya-forks/feature/rr-friendly-installer
Update shlink-installer
2023-05-06 18:07:57 +02:00
Alejandro Celaya
d4dea9a1d2 Update shlink-installer 2023-05-06 10:12:42 +02:00
Alejandro Celaya
28d93ea5e0 Update changelog 2023-05-03 08:59:47 +02:00
Alejandro Celaya
e6a31b16ed Switch to roadrunner as default docker runtime 2023-05-03 08:59:09 +02:00
Alejandro Celaya
9553192281 Merge pull request #1766 from acelaya-forks/feature/rr-cli-2.5
Update to rr-cli 2.5, and do not generate config
2023-05-02 20:01:51 +02:00
Alejandro Celaya
74069f2d24 Skip API tests fetching Twitter during CI 2023-05-02 19:51:37 +02:00
Alejandro Celaya
b4b00a57c1 Update chrome user agent used for anti-bots 2023-05-02 19:40:23 +02:00
Alejandro Celaya
a516ef691d Update to rr-cli 2.5, and do not generate config 2023-05-02 08:43:14 +02:00
Alejandro Celaya
e80b7448f5 Merge pull request #1761 from acelaya-forks/feature/null-default-domain
Feature/null default domain
2023-04-23 15:57:02 +02:00
Alejandro Celaya
f129544f83 Update changelog 2023-04-23 15:22:40 +02:00
Alejandro Celaya
9fa291a32f Update shlink-common 2023-04-23 15:20:33 +02:00
Alejandro Celaya
d06e92ffc2 Created CLI test for short URL importing 2023-04-23 13:26:59 +02:00
Alejandro Celaya
1b83344995 Create CLI test checking default domain is ignored even if explicitly provided 2023-04-23 11:20:54 +02:00
Alejandro Celaya
cf49393ef2 Add --show-domain flag to list short URLs command 2023-04-23 11:19:05 +02:00
Alejandro Celaya
f2ecbceae9 Update changelog 2023-04-22 19:46:28 +02:00
Alejandro Celaya
c582eba753 Make sure short URL domain is resolved as null when default one is provided 2023-04-22 19:44:04 +02:00
Alejandro Celaya
de86b62cdd Merge pull request #1759 from acelaya-forks/feature/fix-docker-build
Fix docker image build
2023-04-20 09:00:39 +02:00
Alejandro Celaya
73150471e9 Fix docker image build 2023-04-19 18:57:35 +02:00
Alejandro Celaya
ec751f4ac2 Merge pull request #1758 from acelaya-forks/feature/roadrunner-2023
Feature/roadrunner 2023
2023-04-19 08:11:18 +02:00
Alejandro Celaya
e652166289 Update changelog 2023-04-18 23:24:21 +02:00
Alejandro Celaya
a671d555cb Update to roadrunner 2023 2023-04-18 23:22:48 +02:00
Alejandro Celaya
6240554f4c Merge pull request #1757 from acelaya-forks/feature/shlink-json
Migrate to shlinkio/shlink-json
2023-04-18 23:14:11 +02:00
Alejandro Celaya
4ee9c9bbe3 Migrate to shlinkio/shlink-json 2023-04-18 23:04:58 +02:00
Alejandro Celaya
c830439085 Merge pull request #1752 from acelaya-forks/feature/phpunit-10.1
Update phpunit configs to fulfil v10.1
2023-04-14 09:55:38 +02:00
Alejandro Celaya
f2196583c8 Update phpunit configs to fulfil v10.1 2023-04-14 09:44:01 +02:00
Alejandro Celaya
3dbca2115c Merge pull request #1751 from acelaya-forks/feature/openswoole-22
Add support for openswoole 22
2023-04-14 09:16:47 +02:00
Alejandro Celaya
b45d8de27d Ignore openswoole dep on roadrunner tests CI 2023-04-14 09:02:17 +02:00
Alejandro Celaya
3ba46bbbfa Add support for openswoole 22 2023-04-14 08:58:54 +02:00
Alejandro Celaya
06f3f0c86c Merge pull request #1750 from acelaya-forks/feature/update-delete-artifacts
Update to geekyeggo/delete-artifact@2
2023-04-13 08:58:24 +02:00
Alejandro Celaya
06f07e3e40 Update to geekyeggo/delete-artifact@2 2023-04-12 19:13:35 +02:00
Alejandro Celaya
740740b8c6 Update to latest JamesIves/github-pages-deploy-action 2023-04-12 19:11:06 +02:00
131 changed files with 1986 additions and 654 deletions

View File

@@ -27,7 +27,7 @@ jobs:
- uses: './.github/actions/ci-setup'
with:
php-version: ${{ matrix.php-version }}
php-extensions: openswoole-4.12.1, pdo_sqlsrv-5.10.1
php-extensions: openswoole-22.0.0, pdo_sqlsrv-5.10.1
extensions-cache-key: db-tests-extensions-${{ matrix.php-version }}-${{ inputs.platform }}
- name: Create test database
if: ${{ inputs.platform == 'ms' }}

View File

@@ -19,7 +19,7 @@ jobs:
- uses: './.github/actions/ci-setup'
with:
php-version: ${{ matrix.php-version }}
php-extensions: openswoole-4.12.1
php-extensions: openswoole-22.0.0
extensions-cache-key: mutation-tests-extensions-${{ matrix.php-version }}-${{ inputs.test-group }}
- uses: actions/download-artifact@v3
with:

View File

@@ -25,7 +25,7 @@ jobs:
- uses: './.github/actions/ci-setup'
with:
php-version: ${{ matrix.php-version }}
php-extensions: openswoole-4.12.1
php-extensions: openswoole-22.0.0
extensions-cache-key: tests-extensions-${{ matrix.php-version }}-${{ inputs.test-group }}
- run: composer test:${{ inputs.test-group }}:ci
- uses: actions/upload-artifact@v3

View File

@@ -36,7 +36,7 @@ jobs:
- uses: './.github/actions/ci-setup'
with:
php-version: ${{ matrix.php-version }}
php-extensions: openswoole-4.12.1
php-extensions: openswoole-22.0.0
extensions-cache-key: tests-extensions-${{ matrix.php-version }}-${{ matrix.command }}
- run: composer ${{ matrix.command }}
@@ -69,8 +69,8 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
tools: composer
- run: composer install --no-interaction --prefer-dist
- run: ./vendor/bin/rr get --no-interaction --location bin/ && chmod +x bin/rr
- run: composer install --no-interaction --prefer-dist --ignore-platform-req=ext-openswoole
- run: ./vendor/bin/rr get --no-interaction --no-config --location bin/ && chmod +x bin/rr
- run: composer test:api:rr
sqlite-db-tests:
@@ -168,10 +168,7 @@ jobs:
- upload-coverage
runs-on: ubuntu-22.04
steps:
- uses: geekyeggo/delete-artifact@v1
- uses: geekyeggo/delete-artifact@v2
with:
name: |
coverage-unit
coverage-db
coverage-api
coverage-cli
coverage-*

View File

@@ -2,8 +2,6 @@ name: Build and publish docker image
on:
push:
branches:
- develop
paths-ignore:
- 'LICENSE'
- '.*'
@@ -12,24 +10,35 @@ on:
- '*.yml*'
- '*.json5'
- '*.neon'
branches:
- develop
tags:
- 'v*'
jobs:
build-openswoole:
build-image:
strategy:
matrix:
include:
- runtime: 'rr'
platforms: 'linux/arm64/v8,linux/amd64'
- runtime: 'rr'
tag-suffix: 'roadrunner'
platforms: 'linux/arm64/v8,linux/amd64'
- runtime: 'openswoole'
tag-suffix: 'openswoole'
platforms: 'linux/arm/v7,linux/arm64/v8,linux/amd64'
- runtime: 'rr'
tag-suffix: 'non-root'
platforms: 'linux/arm64/v8,linux/amd64'
user-id: '1001'
uses: shlinkio/github-actions/.github/workflows/docker-build-and-publish.yml@main
secrets: inherit
with:
image-name: shlinkio/shlink
version-arg-name: SHLINK_VERSION
build-roadrunner:
uses: shlinkio/github-actions/.github/workflows/docker-build-and-publish.yml@main
secrets: inherit
with:
image-name: shlinkio/shlink
version-arg-name: SHLINK_VERSION
platforms: 'linux/arm64/v8,linux/amd64'
tags-suffix: roadrunner
platforms: ${{ matrix.platforms }}
tags-suffix: ${{ matrix.tag-suffix }}
extra-build-args: |
SHLINK_RUNTIME=rr
SHLINK_RUNTIME=${{ matrix.runtime }}
SHLINK_USER_ID=${{ matrix.user-id && matrix.user-id || 'root' }}

View File

@@ -17,7 +17,7 @@ jobs:
- uses: './.github/actions/ci-setup'
with:
php-version: ${{ matrix.php-version }}
php-extensions: openswoole-4.12.1
php-extensions: openswoole-22.0.0
extensions-cache-key: publish-swagger-spec-extensions-${{ matrix.php-version }}
install-deps: 'no'
- if: ${{ matrix.swoole == 'yes' }}
@@ -49,11 +49,7 @@ jobs:
delete-artifacts:
needs: ['publish']
runs-on: ubuntu-22.04
strategy:
matrix:
php-version: ['8.1', '8.2']
swoole: ['yes', 'no']
steps:
- uses: geekyeggo/delete-artifact@v1
- uses: geekyeggo/delete-artifact@v2
with:
name: dist-files-${{ matrix.php-version }}-${{ matrix.swoole }}
name: dist-files-*

View File

@@ -20,13 +20,13 @@ jobs:
- uses: './.github/actions/ci-setup'
with:
php-version: ${{ matrix.php-version }}
php-extensions: openswoole-4.12.1
php-extensions: openswoole-22.0.0
extensions-cache-key: publish-swagger-spec-extensions-${{ matrix.php-version }}
- run: composer swagger:inline
- run: mkdir ${{ steps.determine_version.outputs.version }}
- run: mv docs/swagger/swagger-inlined.json ${{ steps.determine_version.outputs.version }}/open-api-spec.json
- name: Publish spec
uses: JamesIves/github-pages-deploy-action@4.1.7
uses: JamesIves/github-pages-deploy-action@4.4.1
with:
token: ${{ secrets.OAS_PUBLISH_TOKEN }}
repository-name: 'shlinkio/shlink-open-api-specs'

1
.gitignore vendored
View File

@@ -1,5 +1,4 @@
.idea
bin/.rr.*
bin/rr
config/roadrunner/.pid
build

View File

@@ -4,6 +4,126 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).
## [3.6.4] - 2023-09-23
### Added
* *Nothing*
### Changed
* [#1866](https://github.com/shlinkio/shlink/issues/1866) The `INITIAL_API_KEY` env var is now only relevant for the official docker image.
Going forward, new non-docker Shlink installations provisioned with env vars that also wish to provide an initial API key, should do it by using the `vendor/bin/shlink-installer init --initial-api-key=%SOME_KEY%` command, instead of using `INITIAL_API_KEY`.
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1819](https://github.com/shlinkio/shlink/issues/1819) Fix incorrect timeout when running DB commands during Shlink start-up.
* [#1870](https://github.com/shlinkio/shlink/issues/1870) Make sure shared locks include the cache prefix when using Redis.
* [#1866](https://github.com/shlinkio/shlink/issues/1866) Fix error when starting docker image with `INITIAL_API_KEY` env var.
## [3.6.3] - 2023-06-14
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1817](https://github.com/shlinkio/shlink/issues/1817) Fix Shlink trying to create SQLite database tables even if they already exist.
## [3.6.2] - 2023-06-08
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1808](https://github.com/shlinkio/shlink/issues/1808) Fix `rr` binary downloading during Shlink update.
## [3.6.1] - 2023-06-04
### Added
* *Nothing*
### Changed
* *Nothing*
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1413](https://github.com/shlinkio/shlink/issues/1413) Fix error when creating initial DB in Postgres in a cluster where a default `postgres` db does not exist or the credentials do not grant permissions to connect.
* [#1803](https://github.com/shlinkio/shlink/issues/1803) Fix default RoadRunner port when not using docker image.
## [3.6.0] - 2023-05-24
### Added
* [#1148](https://github.com/shlinkio/shlink/issues/1148) Add support to delete short URL visits.
This can be done via `DELETE /short-urls/{shortCode}/visits` REST endpoint or via `short-url:visits-delete` console command.
The CLI command includes a warning and requires the user to confirm before proceeding.
* [#1681](https://github.com/shlinkio/shlink/issues/1681) Add support to delete orphan visits.
This can be done via `DELETE /visits/orphan` REST endpoint or via `visit:orphan-delete` console command.
The CLI command includes a warning and requires the user to confirm before proceeding.
* [#1753](https://github.com/shlinkio/shlink/issues/1753) Add a new `vendor/bin/shlink-installer init` command that can be used to automate Shlink installations.
This command can create the initial database, update it, create proxies, clean cache, download initial GeoLite db files, etc
The official docker image also uses it on its entry point script.
* [#1656](https://github.com/shlinkio/shlink/issues/1656) Add support for openswoole 22
* [#1784](https://github.com/shlinkio/shlink/issues/1784) Add new docker tag where the container runs as a non-root user.
* [#953](https://github.com/shlinkio/shlink/issues/953) Add locks that prevent errors on duplicated keys when creating short URLs in parallel that depend on the same new tag or domain.
### Changed
* [#1755](https://github.com/shlinkio/shlink/issues/1755) Update to roadrunner 2023
* [#1745](https://github.com/shlinkio/shlink/issues/1745) Roadrunner is now the default docker runtime.
There are now three different docker images published:
* Versions without suffix (like `3.6.0`) will contain the default runtime, whichever it is.
* Versions with `-roadrunner` suffix (like `3.6.0-roadrunner`) will always use roadrunner as the runtime, even if default one changes in the future.
* Versions with `-openswoole` suffix (like `3.6.0-openswoole`) will always use openswoole as the runtime, even if default one changes in the future.
### Deprecated
* *Nothing*
### Removed
* *Nothing*
### Fixed
* [#1760](https://github.com/shlinkio/shlink/issues/1760) Fix domain not being set to null when importing short URLs with default domain.
* [#953](https://github.com/shlinkio/shlink/issues/953) Fix duplicated key errors and short URL creation failing when creating short URLs in parallel that depend on the same new tag or domain.
* [#1741](https://github.com/shlinkio/shlink/issues/1741) Fix randomly using 100% CPU in task workers when trying to download GeoLite DB files.
* Fix Shlink trying to connect to RabbitMQ even if configuration set to not connect.
## [3.5.4] - 2023-04-12
### Added
* *Nothing*

View File

@@ -2,13 +2,16 @@ FROM php:8.2-alpine3.17 as base
ARG SHLINK_VERSION=latest
ENV SHLINK_VERSION ${SHLINK_VERSION}
ARG SHLINK_RUNTIME=openswoole
ARG SHLINK_RUNTIME=rr
ENV SHLINK_RUNTIME ${SHLINK_RUNTIME}
ENV OPENSWOOLE_VERSION 4.12.1
ARG SHLINK_USER_ID='root'
ENV SHLINK_USER_ID ${SHLINK_USER_ID}
ENV OPENSWOOLE_VERSION 22.0.0
ENV PDO_SQLSRV_VERSION 5.10.1
ENV MS_ODBC_DOWNLOAD 'b/9/f/b9f3cce4-3925-46d4-9f46-da08869c6486'
ENV MS_ODBC_SQL_VERSION 18_18.1.1.1
ENV LC_ALL "C"
ENV LC_ALL 'C'
WORKDIR /etc/shlink
@@ -43,11 +46,12 @@ FROM base as builder
COPY . .
COPY --from=composer:2 /usr/bin/composer ./composer.phar
RUN apk add --no-cache git && \
php composer.phar install --no-dev --prefer-dist --optimize-autoloader --no-progress --no-interaction && \
# FIXME Ignoring ext-openswoole platform req, as it makes install fail with roadrunner, even though it's a dev dependency and we are passing --no-dev
php composer.phar install --no-dev --prefer-dist --optimize-autoloader --no-progress --no-interaction --ignore-platform-req=ext-openswoole && \
if [ "$SHLINK_RUNTIME" == 'openswoole' ]; then \
php composer.phar remove spiral/roadrunner spiral/roadrunner-jobs --with-all-dependencies --update-no-dev --optimize-autoloader --no-progress --no-interactionc ; \
elif [ $SHLINK_RUNTIME == 'rr' ]; then \
php composer.phar remove mezzio/mezzio-swoole --with-all-dependencies --update-no-dev --optimize-autoloader --no-progress --no-interaction ; \
php composer.phar remove spiral/roadrunner spiral/roadrunner-jobs spiral/roadrunner-cli spiral/roadrunner-http --with-all-dependencies --update-no-dev --optimize-autoloader --no-progress --no-interaction ; \
elif [ "$SHLINK_RUNTIME" == 'rr' ]; then \
php composer.phar remove mezzio/mezzio-swoole --with-all-dependencies --update-no-dev --optimize-autoloader --no-progress --no-interaction --ignore-platform-req=ext-openswoole ; \
fi; \
php composer.phar clear-cache && \
rm -r docker composer.* && \
@@ -58,10 +62,10 @@ RUN apk add --no-cache git && \
FROM base
LABEL maintainer="Alejandro Celaya <alejandro@alejandrocelaya.com>"
COPY --from=builder /etc/shlink .
COPY --from=builder --chown=${SHLINK_USER_ID} /etc/shlink .
RUN ln -s /etc/shlink/bin/cli /usr/local/bin/shlink && \
if [ "$SHLINK_RUNTIME" == 'rr' ]; then \
php ./vendor/bin/rr get --no-interaction --location bin/ && chmod +x bin/rr ; \
php ./vendor/bin/rr get --no-interaction --no-config --location bin/ && chmod +x bin/rr ; \
fi;
# Expose default port
@@ -72,14 +76,6 @@ COPY docker/docker-entrypoint.sh docker-entrypoint.sh
COPY docker/config/shlink_in_docker.local.php config/autoload/shlink_in_docker.local.php
COPY docker/config/php.ini ${PHP_INI_DIR}/conf.d/
# Change the ownership of /etc/shlink/data to be writable, then change the user to non-root
# FIXME Disabled for now, as it conflicts with ENABLE_PERIODIC_VISIT_LOCATE, which is used to configure a cron as root.
# Ref: https://github.com/shlinkio/shlink/issues/1132
#RUN chown 1001 /etc/shlink/data
#RUN chown 1001 /etc/shlink/data/locks
#RUN chown 1001 /etc/shlink/data/proxies
#RUN chown 1001 /etc/shlink/data/cache
#RUN chown 1001 /etc/shlink/data/log
#USER 1001
USER ${SHLINK_USER_ID}
ENTRYPOINT ["/bin/sh", "./docker-entrypoint.sh"]

View File

@@ -39,7 +39,7 @@ if [[ $noSwoole ]]; then
${composerBin} remove mezzio/mezzio-swoole --with-all-dependencies --update-no-dev $composerFlags
else
# If generating a dist for openswoole, uninstall RoadRunner
${composerBin} remove spiral/roadrunner spiral/roadrunner-jobs --with-all-dependencies --update-no-dev $composerFlags
${composerBin} remove spiral/roadrunner spiral/roadrunner-jobs spiral/roadrunner-cli spiral/roadrunner-http --with-all-dependencies --update-no-dev $composerFlags
fi
# Delete development files

View File

@@ -18,7 +18,7 @@
"ext-json": "*",
"ext-pdo": "*",
"akrabat/ip-address-middleware": "^2.1",
"cakephp/chronos": "^2.3",
"cakephp/chronos": "~2.3.3",
"doctrine/migrations": "^3.5",
"doctrine/orm": "^2.14",
"endroid/qr-code": "^4.7",
@@ -45,14 +45,17 @@
"php-middleware/request-id": "^4.1",
"pugx/shortid-php": "^1.1",
"ramsey/uuid": "^4.7",
"shlinkio/shlink-common": "^5.4",
"shlinkio/shlink-common": "^5.6",
"shlinkio/shlink-config": "^2.4",
"shlinkio/shlink-event-dispatcher": "^2.6",
"shlinkio/shlink-importer": "^5.0",
"shlinkio/shlink-installer": "^8.3",
"shlinkio/shlink-event-dispatcher": "^3.0",
"shlinkio/shlink-importer": "^5.1",
"shlinkio/shlink-installer": "^8.5",
"shlinkio/shlink-ip-geolocation": "^3.2",
"spiral/roadrunner": "^2.12",
"spiral/roadrunner-jobs": "^2.7",
"shlinkio/shlink-json": "^1.0",
"spiral/roadrunner": "^2023.1",
"spiral/roadrunner-cli": "^2.5",
"spiral/roadrunner-http": "^3.0",
"spiral/roadrunner-jobs": "^4.0",
"symfony/console": "^6.2",
"symfony/filesystem": "^6.2",
"symfony/lock": "^6.2",
@@ -62,17 +65,17 @@
"require-dev": {
"cebe/php-openapi": "^1.7",
"devster/ubench": "^2.1",
"infection/infection": "^0.26.19",
"openswoole/ide-helper": "~4.11.5",
"infection/infection": "^0.27",
"openswoole/ide-helper": "~22.0.0",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-doctrine": "^1.3",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-symfony": "^1.2",
"phpunit/php-code-coverage": "^10.0",
"phpunit/phpunit": "^10.0",
"phpunit/phpunit": "~10.1.0",
"roave/security-advisories": "dev-master",
"shlinkio/php-coding-standard": "~2.3.0",
"shlinkio/shlink-test-utils": "^3.5",
"shlinkio/shlink-test-utils": "~3.6.0",
"symfony/var-dumper": "^6.2",
"veewee/composer-run-parallel": "^1.2"
},
@@ -107,7 +110,7 @@
"@parallel cs stan swagger:validate test:unit:ci test:db:sqlite:ci test:db:mysql test:db:maria test:db:postgres test:db:ms",
"@parallel infect:test:api infect:test:cli infect:ci:unit infect:ci:db"
],
"cs": "phpcs",
"cs": "phpcs -s",
"cs:fix": "phpcbf",
"stan": "APP_ENV=test php vendor/bin/phpstan analyse module/*/src module/*/test* module/*/config config docker/config data/migrations --level=8",
"test": [

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
use GuzzleHttp\Client;
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Mezzio\Application;
use Mezzio\Container;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
@@ -20,7 +21,7 @@ return [
],
'delegators' => [
Mezzio\Application::class => [
Application::class => [
Container\ApplicationConfigInjectionDelegator::class,
],
],

View File

@@ -86,6 +86,9 @@ return [
InstallationCommand::API_KEY_GENERATE->value => [
'command' => 'bin/cli ' . Command\Api\GenerateKeyCommand::NAME,
],
InstallationCommand::API_KEY_CREATE->value => [
'command' => 'bin/cli ' . Command\Api\InitialApiKeyCommand::NAME,
],
],
],

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Shlinkio\Shlink\Common\Cache\RedisFactory;
use Shlinkio\Shlink\Common\Lock\NamespacedStore;
use Shlinkio\Shlink\Common\Logger\LoggerAwareDelegatorFactory;
use Shlinkio\Shlink\Core\Config\EnvVars;
use Symfony\Component\Lock;
@@ -22,11 +23,12 @@ return [
Lock\Store\RedisStore::class => ConfigAbstractFactory::class,
Lock\LockFactory::class => ConfigAbstractFactory::class,
LOCAL_LOCK_FACTORY => ConfigAbstractFactory::class,
NamespacedStore::class => ConfigAbstractFactory::class,
],
'aliases' => [
'lock_store' => EnvVars::REDIS_SERVERS->existsInEnv() ? 'redis_lock_store' : 'local_lock_store',
'redis_lock_store' => Lock\Store\RedisStore::class,
'redis_lock_store' => NamespacedStore::class,
'local_lock_store' => Lock\Store\FlockStore::class,
],
'delegators' => [
@@ -39,6 +41,8 @@ return [
ConfigAbstractFactory::class => [
Lock\Store\FlockStore::class => ['config.locks.locks_dir'],
Lock\Store\RedisStore::class => [RedisFactory::SERVICE_NAME],
NamespacedStore::class => [Lock\Store\RedisStore::class, 'config.cache.namespace'],
Lock\LockFactory::class => ['lock_store'],
LOCAL_LOCK_FACTORY => ['local_lock_store'],
],

View File

@@ -4,51 +4,63 @@ declare(strict_types=1);
namespace Shlinkio\Shlink;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Monolog\Level;
use Monolog\Logger;
use PhpMiddleware\RequestId;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Shlinkio\Shlink\Common\Logger\LoggerFactory;
use Shlinkio\Shlink\Common\Logger\LoggerType;
use Shlinkio\Shlink\Common\Middleware\AccessLogMiddleware;
$common = [
'level' => Level::Info->value,
'processors' => [RequestId\MonologProcessor::class],
'line_format' => '[%datetime%] [%extra.request_id%] %channel%.%level_name% - %message%',
];
use function Shlinkio\Shlink\Config\runningInRoadRunner;
return [
return (static function (): array {
$common = [
'level' => Level::Info->value,
'processors' => [RequestId\MonologProcessor::class],
'line_format' => '[%datetime%] [%extra.request_id%] %channel%.%level_name% - %message%',
];
'logger' => [
'Shlink' => [
'type' => LoggerType::FILE->value,
...$common,
],
'Access' => [
'type' => LoggerType::STREAM->value,
...$common,
],
],
return [
'dependencies' => [
'factories' => [
'Logger_Shlink' => [LoggerFactory::class, 'Shlink'],
'Logger_Access' => [LoggerFactory::class, 'Access'],
],
'aliases' => [
'logger' => 'Logger_Shlink',
Logger::class => 'Logger_Shlink',
LoggerInterface::class => 'Logger_Shlink',
],
],
'mezzio-swoole' => [
'swoole-http-server' => [
'logger' => [
'logger-name' => 'Logger_Access',
'format' => '%u "%r" %>s %B',
'logger' => [
'Shlink' => [
'type' => LoggerType::FILE->value,
...$common,
],
'Access' => [
'type' => LoggerType::STREAM->value,
'destination' => 'php://stderr',
'add_new_line' => ! runningInRoadRunner(),
...$common,
],
],
],
];
'dependencies' => [
'factories' => [
'Logger_Shlink' => [LoggerFactory::class, 'Shlink'],
'Logger_Access' => [LoggerFactory::class, 'Access'],
NullLogger::class => InvokableFactory::class,
],
'aliases' => [
'logger' => 'Logger_Shlink',
Logger::class => 'Logger_Shlink',
LoggerInterface::class => 'Logger_Shlink',
AccessLogMiddleware::LOGGER_SERVICE_NAME => 'Logger_Access',
],
],
'mezzio-swoole' => [
'swoole-http-server' => [
'logger' => [
// Let's disable mezio-swoole access logging, so that we can provide our own implementation,
// consistent for roadrunner and openswoole
'logger-name' => NullLogger::class,
],
],
],
];
})();

View File

@@ -5,16 +5,12 @@ declare(strict_types=1);
use Monolog\Level;
use Shlinkio\Shlink\Common\Logger\LoggerType;
use function Shlinkio\Shlink\Config\runningInOpenswoole;
$logToStream = runningInOpenswoole();
return [
'logger' => [
'Shlink' => [
// For openswoole, send logs as stream
'type' => $logToStream ? LoggerType::STREAM->value : LoggerType::FILE->value,
'type' => LoggerType::STREAM->value,
'destination' => 'php://stderr',
'level' => Level::Debug->value,
],
],

View File

@@ -9,6 +9,7 @@ use Mezzio\ProblemDetails;
use Mezzio\Router;
use PhpMiddleware\RequestId\RequestIdMiddleware;
use RKA\Middleware\IpAddress;
use Shlinkio\Shlink\Common\Middleware\AccessLogMiddleware;
use Shlinkio\Shlink\Common\Middleware\ContentLengthMiddleware;
return [
@@ -16,6 +17,7 @@ return [
'middleware_pipeline' => [
'error-handler' => [
'middleware' => [
AccessLogMiddleware::class,
ContentLengthMiddleware::class,
RequestIdMiddleware::class,
ErrorHandler::class,

View File

@@ -38,6 +38,7 @@ return (static function (): array {
Action\Visit\DomainVisitsAction::getRouteDef(),
Action\Visit\GlobalVisitsAction::getRouteDef(),
Action\Visit\OrphanVisitsAction::getRouteDef(),
Action\Visit\DeleteOrphanVisitsAction::getRouteDef(),
Action\Visit\NonOrphanVisitsAction::getRouteDef(),
// Short URLs
@@ -53,6 +54,7 @@ return (static function (): array {
]),
Action\ShortUrl\EditShortUrlAction::getRouteDef([$dropDomainMiddleware]),
Action\ShortUrl\DeleteShortUrlAction::getRouteDef([$dropDomainMiddleware]),
Action\ShortUrl\DeleteShortUrlVisitsAction::getRouteDef([$dropDomainMiddleware]),
Action\ShortUrl\ResolveShortUrlAction::getRouteDef([$dropDomainMiddleware]),
Action\ShortUrl\ListShortUrlsAction::getRouteDef(),

View File

@@ -42,10 +42,9 @@ return (new ConfigAggregator\ConfigAggregator([
Core\ConfigProvider::class,
CLI\ConfigProvider::class,
Rest\ConfigProvider::class,
new ConfigAggregator\PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
$isTestEnv
? new ConfigAggregator\PhpFileProvider('config/test/*.global.php')
: new ConfigAggregator\ArrayProvider([]),
new ConfigAggregator\PhpFileProvider('config/autoload/{,*.}global.php'),
// Local config should not be loaded during tests, whereas test config should be loaded ONLY during tests
new ConfigAggregator\PhpFileProvider($isTestEnv ? 'config/test/*.global.php' : 'config/autoload/{,*.}local.php'),
// Routes have to be loaded last
new ConfigAggregator\PhpFileProvider('config/autoload/routes.config.php'),
], 'data/cache/app_config.php', [

View File

@@ -12,6 +12,16 @@ chdir(dirname(__DIR__));
require 'vendor/autoload.php';
// Workaround to make this compatible with both openswoole 22 and earlier versions.
if (! function_exists('swoole_set_process_name')) {
// phpcs:disable
function swoole_set_process_name(string $name): void
{
OpenSwoole\Util::setProcessName($name);
}
// phpcs:enable
}
// This is one of the first files loaded. Configure the timezone here
date_default_timezone_set(EnvVars::TIMEZONE->loadFromEnv(date_default_timezone_get()));
@@ -21,7 +31,6 @@ if (! class_exists(LOCAL_LOCK_FACTORY)) {
class_alias(Lock\LockFactory::class, LOCAL_LOCK_FACTORY);
}
// Build container
return (static function (): ServiceManager {
$config = require __DIR__ . '/config.php';
$container = new ServiceManager($config['dependencies']);

View File

@@ -1,4 +1,4 @@
version: '2.7'
version: '3.0'
rpc:
listen: tcp://127.0.0.1:6001
@@ -14,10 +14,12 @@ http:
forbid: ['.php', '.htaccess']
pool:
num_workers: 1
debug: true
jobs:
pool:
num_workers: 1
debug: true
timeout: 300
consume: ['shlink']
pipelines:
@@ -31,19 +33,8 @@ logs:
mode: development
channels:
http:
level: debug
mode: 'off' # Disable logging as Shlink handles it internally
server:
level: debug
metrics:
level: debug
reload:
interval: 1s
patterns: ['.php']
services:
http:
dirs: ['../../bin', '../../config', '../../data/migrations', '../../module', '../../vendor']
recursive: true
jobs:
dirs: ['../../bin', '../../config', '../../data/migrations', '../../module', '../../vendor']
recursive: true

View File

@@ -1,4 +1,4 @@
version: '2.7'
version: '3.0'
rpc:
listen: tcp://127.0.0.1:6001
@@ -7,18 +7,18 @@ server:
command: 'php -dopcache.enable_cli=1 -dopcache.validate_timestamps=0 ../../bin/roadrunner-worker.php'
http:
address: '0.0.0.0:${PORT}'
address: '0.0.0.0:${PORT:-8080}'
middleware: ['static']
static:
dir: '../../public'
forbid: ['.php', '.htaccess']
pool:
num_workers: ${WEB_WORKER_NUM}
num_workers: ${WEB_WORKER_NUM:-0}
jobs:
timeout: 300 # 5 minutes
pool:
num_workers: ${TASK_WORKER_NUM}
num_workers: ${TASK_WORKER_NUM:-0}
consume: ['shlink']
pipelines:
shlink:
@@ -31,6 +31,6 @@ logs:
mode: production
channels:
http:
level: info # Log all http requests, set to info to disable
mode: 'off' # Disable logging as Shlink handles it internally
server:
level: debug # Everything written to worker stderr is logged

View File

@@ -121,6 +121,7 @@ $buildTestLoggerConfig = static fn (string $filename) => [
'level' => Level::Debug->value,
'type' => LoggerType::STREAM->value,
'destination' => sprintf('data/log/api-tests/%s', $filename),
'add_new_line' => true,
];
return [

View File

@@ -71,6 +71,6 @@ CMD \
# Install dependencies if the vendor dir does not exist
if [[ ! -d "./vendor" ]]; then /usr/local/bin/composer install ; fi && \
# Download roadrunner binary
if [[ ! -f "./bin/rr" ]]; then ./vendor/bin/rr get --no-interaction --location bin/ && chmod +x bin/rr ; fi && \
if [[ ! -f "./bin/rr" ]]; then ./vendor/bin/rr get --no-interaction --no-config --location bin/ && chmod +x bin/rr ; fi && \
# This forces the app to be started every second until the exit code is 0
until ./bin/rr serve -c config/roadrunner/.rr.dev.yml; do sleep 1 ; done

View File

@@ -3,7 +3,7 @@ MAINTAINER Alejandro Celaya <alejandro@alejandrocelaya.com>
ENV APCU_VERSION 5.1.21
ENV INOTIFY_VERSION 3.0.0
ENV OPENSWOOLE_VERSION 4.12.1
ENV OPENSWOOLE_VERSION 22.0.0
ENV PDO_SQLSRV_VERSION 5.10.1
ENV MS_ODBC_DOWNLOAD 'b/9/f/b9f3cce4-3925-46d4-9f46-da08869c6486'
ENV MS_ODBC_SQL_VERSION 18_18.1.1.1

View File

@@ -6,14 +6,12 @@ namespace Shlinkio\Shlink;
use Shlinkio\Shlink\Common\Logger\LoggerType;
use function Shlinkio\Shlink\Config\runningInRoadRunner;
return [
'logger' => [
'Shlink' => [
'type' => LoggerType::STREAM->value,
'destination' => runningInRoadRunner() ? 'php://stderr' : 'php://stdout',
'destination' => 'php://stderr',
],
],

View File

@@ -1,44 +1,30 @@
#!/usr/bin/env sh
set -e
# If SHELL_VERBOSITY was not explicitly provided, run commands in quite mode (-q)
[ $SHELL_VERBOSITY ] && flags="" || flags="-q"
cd /etc/shlink
echo "Creating fresh database if needed..."
php bin/cli db:create -n ${flags}
flags="--clear-db-cache"
echo "Updating database..."
php bin/cli db:migrate -n ${flags}
echo "Generating proxies..."
php bin/doctrine orm:generate-proxies -n ${flags}
echo "Clearing entities cache..."
php bin/doctrine orm:clear-cache:metadata -n ${flags}
# Try to download GeoLite2 db file only if the license key env var was defined and skipping was not explicitly set
if [ ! -z "${GEOLITE_LICENSE_KEY}" ] && [ "${SKIP_INITIAL_GEOLITE_DOWNLOAD}" != "true" ]; then
echo "Downloading GeoLite2 db file..."
php bin/cli visit:download-db -n ${flags}
# Skip downloading GeoLite2 db file if the license key env var was not defined or skipping was explicitly set
if [ -z "${GEOLITE_LICENSE_KEY}" ] || [ "${SKIP_INITIAL_GEOLITE_DOWNLOAD}" == "true" ]; then
flags="${flags} --skip-download-geolite"
fi
# Periodically run visit:locate every hour, if ENABLE_PERIODIC_VISIT_LOCATE=true was provided
if [ "${ENABLE_PERIODIC_VISIT_LOCATE}" = "true" ]; then
# If INITIAL_API_KEY was provided, create an initial API key
if [ -n "${INITIAL_API_KEY}" ]; then
flags="${flags} --initial-api-key=${INITIAL_API_KEY}"
fi
php vendor/bin/shlink-installer init ${flags}
# Periodically run visit:locate every hour, if ENABLE_PERIODIC_VISIT_LOCATE=true was provided and running as root
# FIXME: ENABLE_PERIODIC_VISIT_LOCATE is deprecated. Remove cron support in Shlink 4.0.0
if [ "${ENABLE_PERIODIC_VISIT_LOCATE}" = "true" ] && [ "${SHLINK_USER_ID}" = "root" ]; then
echo "Configuring periodic visit location..."
echo "0 * * * * php /etc/shlink/bin/cli visit:locate -q" > /etc/crontabs/root
/usr/sbin/crond &
fi
# RoadRunner config needs these to have been set, so falling back to default values if not set yet
if [ "$SHLINK_RUNTIME" == 'rr' ]; then
export PORT="${PORT:-"8080"}"
# Default to 0 so that RoadRunner decides the number of workers based on the amount of logical CPUs
export WEB_WORKER_NUM="${WEB_WORKER_NUM:-"0"}"
export TASK_WORKER_NUM="${TASK_WORKER_NUM:-"0"}"
fi
if [ "$SHLINK_RUNTIME" == 'openswoole' ]; then
# When restarting the container, openswoole might think it is already in execution
# This forces the app to be started every second until the exit code is 0

View File

@@ -0,0 +1,9 @@
{
"name": "shortCode",
"in": "path",
"description": "The short code for the short URL.",
"required": true,
"schema": {
"type": "string"
}
}

View File

@@ -11,13 +11,7 @@
"$ref": "../parameters/version.json"
},
{
"name": "shortCode",
"in": "path",
"description": "The short code to resolve.",
"required": true,
"schema": {
"type": "string"
}
"$ref": "../parameters/shortCode.json"
},
{
"$ref": "../parameters/domain.json"
@@ -127,13 +121,7 @@
"$ref": "../parameters/version.json"
},
{
"name": "shortCode",
"in": "path",
"description": "The short code to edit.",
"required": true,
"schema": {
"type": "string"
}
"$ref": "../parameters/shortCode.json"
},
{
"$ref": "../parameters/domain.json"
@@ -295,13 +283,7 @@
"$ref": "../parameters/version.json"
},
{
"name": "shortCode",
"in": "path",
"description": "The short code to edit.",
"required": true,
"schema": {
"type": "string"
}
"$ref": "../parameters/shortCode.json"
},
{
"$ref": "../parameters/domain.json"

View File

@@ -11,13 +11,7 @@
"$ref": "../parameters/version.json"
},
{
"name": "shortCode",
"in": "path",
"description": "The short code for the short URL from which we want to get the visits.",
"required": true,
"schema": {
"type": "string"
}
"$ref": "../parameters/shortCode.json"
},
{
"$ref": "../parameters/domain.json"
@@ -172,5 +166,79 @@
}
}
}
},
"delete": {
"operationId": "deleteShortUrlVisits",
"tags": [
"Visits"
],
"summary": "Delete visits for short URL",
"description": "Delete all existing visits on the short URL behind provided short code.",
"parameters": [
{
"$ref": "../parameters/version.json"
},
{
"$ref": "../parameters/shortCode.json"
},
{
"$ref": "../parameters/domain.json"
}
],
"security": [
{
"ApiKey": []
}
],
"responses": {
"200": {
"description": "Deleted visits",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"deletedVisits": {
"description": "Amount of affected visits",
"type": "number"
}
}
},
"example": {
"deletedVisits": 536
}
}
}
},
"404": {
"description": "The short code does not belong to any short URL.",
"content": {
"application/problem+json": {
"schema": {
"$ref": "../definitions/Error.json"
},
"examples": {
"Short URL not found with API v3 and newer": {
"$ref": "../examples/short-url-not-found-v3.json"
},
"Short URL not found previous to API v3": {
"$ref": "../examples/short-url-not-found-v2.json"
}
}
}
}
},
"default": {
"description": "Unexpected error.",
"content": {
"application/problem+json": {
"schema": {
"$ref": "../definitions/Error.json"
}
}
}
}
}
}
}

View File

@@ -148,5 +148,55 @@
}
}
}
},
"delete": {
"operationId": "deleteOrphanVisits",
"tags": [
"Visits"
],
"summary": "Delete orphan visits",
"description": "Delete all visits to invalid short URLs, the base URL or any other 404.",
"parameters": [
{
"$ref": "../parameters/version.json"
}
],
"security": [
{
"ApiKey": []
}
],
"responses": {
"200": {
"description": "Deleted visits",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"deletedVisits": {
"description": "Amount of affected visits",
"type": "number"
}
}
},
"example": {
"deletedVisits": 536
}
}
}
},
"default": {
"description": "Unexpected error.",
"content": {
"application/problem+json": {
"schema": {
"$ref": "../definitions/Error.json"
}
}
}
}
}
}
}

View File

@@ -8,13 +8,7 @@
"description": "Represents a short URL. Tracks the visit and redirects tio the corresponding long URL",
"parameters": [
{
"name": "shortCode",
"in": "path",
"description": "The short code to resolve.",
"required": true,
"schema": {
"type": "string"
}
"$ref": "../parameters/shortCode.json"
}
],
"responses": {

View File

@@ -8,13 +8,7 @@
"description": "Generates a QR code image pointing to a short URL.<br />Since this is not an API endpoint but an image one, when an invalid value is provided for any of the query params, they will fall to their default values instead of throwing an error.",
"parameters": [
{
"name": "shortCode",
"in": "path",
"description": "The short code to resolve.",
"required": true,
"schema": {
"type": "string"
}
"$ref": "../parameters/shortCode.json"
},
{
"name": "size",

View File

@@ -8,13 +8,7 @@
"description": "Generates a 1px transparent image which can be used to track emails with a short URL",
"parameters": [
{
"name": "shortCode",
"in": "path",
"description": "The short code to resolve.",
"required": true,
"schema": {
"type": "string"
}
"$ref": "../parameters/shortCode.json"
}
],
"responses": {

View File

@@ -2,7 +2,7 @@
# Run docker containers if they are not up yet
if ! [[ $(docker ps | grep shlink_swoole) ]]; then
docker-compose up -d
docker compose up -d
fi
docker exec -it shlink_swoole /bin/sh -c "$*"

View File

@@ -13,15 +13,18 @@ return [
Command\ShortUrl\ListShortUrlsCommand::NAME => Command\ShortUrl\ListShortUrlsCommand::class,
Command\ShortUrl\GetShortUrlVisitsCommand::NAME => Command\ShortUrl\GetShortUrlVisitsCommand::class,
Command\ShortUrl\DeleteShortUrlCommand::NAME => Command\ShortUrl\DeleteShortUrlCommand::class,
Command\ShortUrl\DeleteShortUrlVisitsCommand::NAME => Command\ShortUrl\DeleteShortUrlVisitsCommand::class,
Command\Visit\LocateVisitsCommand::NAME => Command\Visit\LocateVisitsCommand::class,
Command\Visit\DownloadGeoLiteDbCommand::NAME => Command\Visit\DownloadGeoLiteDbCommand::class,
Command\Visit\GetOrphanVisitsCommand::NAME => Command\Visit\GetOrphanVisitsCommand::class,
Command\Visit\DeleteOrphanVisitsCommand::NAME => Command\Visit\DeleteOrphanVisitsCommand::class,
Command\Visit\GetNonOrphanVisitsCommand::NAME => Command\Visit\GetNonOrphanVisitsCommand::class,
Command\Api\GenerateKeyCommand::NAME => Command\Api\GenerateKeyCommand::class,
Command\Api\DisableKeyCommand::NAME => Command\Api\DisableKeyCommand::class,
Command\Api\ListKeysCommand::NAME => Command\Api\ListKeysCommand::class,
Command\Api\InitialApiKeyCommand::NAME => Command\Api\InitialApiKeyCommand::class,
Command\Tag\ListTagsCommand::NAME => Command\Tag\ListTagsCommand::class,
Command\Tag\RenameTagCommand::NAME => Command\Tag\RenameTagCommand::class,

View File

@@ -42,15 +42,18 @@ return [
Command\ShortUrl\ListShortUrlsCommand::class => ConfigAbstractFactory::class,
Command\ShortUrl\GetShortUrlVisitsCommand::class => ConfigAbstractFactory::class,
Command\ShortUrl\DeleteShortUrlCommand::class => ConfigAbstractFactory::class,
Command\ShortUrl\DeleteShortUrlVisitsCommand::class => ConfigAbstractFactory::class,
Command\Visit\DownloadGeoLiteDbCommand::class => ConfigAbstractFactory::class,
Command\Visit\LocateVisitsCommand::class => ConfigAbstractFactory::class,
Command\Visit\GetOrphanVisitsCommand::class => ConfigAbstractFactory::class,
Command\Visit\DeleteOrphanVisitsCommand::class => ConfigAbstractFactory::class,
Command\Visit\GetNonOrphanVisitsCommand::class => ConfigAbstractFactory::class,
Command\Api\GenerateKeyCommand::class => ConfigAbstractFactory::class,
Command\Api\DisableKeyCommand::class => ConfigAbstractFactory::class,
Command\Api\ListKeysCommand::class => ConfigAbstractFactory::class,
Command\Api\InitialApiKeyCommand::class => ConfigAbstractFactory::class,
Command\Tag\ListTagsCommand::class => ConfigAbstractFactory::class,
Command\Tag\RenameTagCommand::class => ConfigAbstractFactory::class,
@@ -88,6 +91,7 @@ return [
],
Command\ShortUrl\GetShortUrlVisitsCommand::class => [Visit\VisitsStatsHelper::class],
Command\ShortUrl\DeleteShortUrlCommand::class => [ShortUrl\DeleteShortUrlService::class],
Command\ShortUrl\DeleteShortUrlVisitsCommand::class => [ShortUrl\ShortUrlVisitsDeleter::class],
Command\Visit\DownloadGeoLiteDbCommand::class => [GeoLite\GeolocationDbUpdater::class],
Command\Visit\LocateVisitsCommand::class => [
@@ -96,11 +100,13 @@ return [
LockFactory::class,
],
Command\Visit\GetOrphanVisitsCommand::class => [Visit\VisitsStatsHelper::class],
Command\Visit\DeleteOrphanVisitsCommand::class => [Visit\VisitsDeleter::class],
Command\Visit\GetNonOrphanVisitsCommand::class => [Visit\VisitsStatsHelper::class, ShortUrlStringifier::class],
Command\Api\GenerateKeyCommand::class => [ApiKeyService::class, ApiKey\RoleResolver::class],
Command\Api\DisableKeyCommand::class => [ApiKeyService::class],
Command\Api\ListKeysCommand::class => [ApiKeyService::class],
Command\Api\InitialApiKeyCommand::class => [ApiKeyService::class],
Command\Tag\ListTagsCommand::class => [TagService::class],
Command\Tag\RenameTagCommand::class => [TagService::class],

View File

@@ -14,24 +14,23 @@ use function is_string;
class RoleResolver implements RoleResolverInterface
{
public function __construct(private DomainServiceInterface $domainService, private string $defaultDomain)
{
public function __construct(
private readonly DomainServiceInterface $domainService,
private readonly string $defaultDomain,
) {
}
public function determineRoles(InputInterface $input): array
public function determineRoles(InputInterface $input): iterable
{
$domainAuthority = $input->getOption(Role::DOMAIN_SPECIFIC->paramName());
$author = $input->getOption(Role::AUTHORED_SHORT_URLS->paramName());
$roleDefinitions = [];
if ($author) {
$roleDefinitions[] = RoleDefinition::forAuthoredShortUrls();
yield RoleDefinition::forAuthoredShortUrls();
}
if (is_string($domainAuthority)) {
$roleDefinitions[] = $this->resolveRoleForAuthority($domainAuthority);
yield $this->resolveRoleForAuthority($domainAuthority);
}
return $roleDefinitions;
}
private function resolveRoleForAuthority(string $domainAuthority): RoleDefinition

View File

@@ -10,7 +10,7 @@ use Symfony\Component\Console\Input\InputInterface;
interface RoleResolverInterface
{
/**
* @return RoleDefinition[]
* @return iterable<RoleDefinition>
*/
public function determineRoles(InputInterface $input): array;
public function determineRoles(InputInterface $input): iterable;
}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Api;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use Symfony\Component\Console\Command\Command;
@@ -39,10 +39,10 @@ class DisableKeyCommand extends Command
try {
$this->apiKeyService->disable($apiKey);
$io->success(sprintf('API key "%s" properly disabled', $apiKey));
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
} catch (InvalidArgumentException $e) {
$io->error($e->getMessage());
return ExitCodes::EXIT_FAILURE;
return ExitCode::EXIT_FAILURE;
}
}
}

View File

@@ -6,8 +6,9 @@ namespace Shlinkio\Shlink\CLI\Command\Api;
use Cake\Chronos\Chronos;
use Shlinkio\Shlink\CLI\ApiKey\RoleResolverInterface;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\CLI\Util\ShlinkTable;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
use Shlinkio\Shlink\Rest\ApiKey\Role;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
@@ -25,8 +26,8 @@ class GenerateKeyCommand extends Command
public const NAME = 'api-key:generate';
public function __construct(
private ApiKeyServiceInterface $apiKeyService,
private RoleResolverInterface $roleResolver,
private readonly ApiKeyServiceInterface $apiKeyService,
private readonly RoleResolverInterface $roleResolver,
) {
parent::__construct();
}
@@ -57,7 +58,7 @@ class GenerateKeyCommand extends Command
$this
->setName(self::NAME)
->setDescription('Generates a new valid API key.')
->setDescription('Generate a new valid API key.')
->addOption(
'name',
'm',
@@ -91,11 +92,12 @@ class GenerateKeyCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$expirationDate = $input->getOption('expiration-date');
$apiKey = $this->apiKeyService->create(
isset($expirationDate) ? Chronos::parse($expirationDate) : null,
$input->getOption('name'),
...$this->roleResolver->determineRoles($input),
);
$apiKey = $this->apiKeyService->create(ApiKeyMeta::fromParams(
name: $input->getOption('name'),
expirationDate: isset($expirationDate) ? Chronos::parse($expirationDate) : null,
roleDefinitions: $this->roleResolver->determineRoles($input),
));
$io = new SymfonyStyle($input, $output);
$io->success(sprintf('Generated API key: "%s"', $apiKey->toString()));
@@ -109,6 +111,6 @@ class GenerateKeyCommand extends Command
);
}
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
}

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Api;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class InitialApiKeyCommand extends Command
{
public const NAME = 'api-key:initial';
public function __construct(private readonly ApiKeyServiceInterface $apiKeyService)
{
parent::__construct();
}
protected function configure(): void
{
$this
->setHidden()
->setName(self::NAME)
->setDescription('Tries to create initial API key')
->addArgument('apiKey', InputArgument::REQUIRED, 'The initial API to create');
}
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$key = $input->getArgument('apiKey');
$result = $this->apiKeyService->createInitial($key);
if ($result === null && $output->isVerbose()) {
$output->writeln('<comment>Other API keys already exist. Initial API key creation skipped.</comment>');
}
return ExitCode::EXIT_SUCCESS;
}
}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Api;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\CLI\Util\ShlinkTable;
use Shlinkio\Shlink\Rest\ApiKey\Role;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
@@ -77,7 +77,7 @@ class ListKeysCommand extends Command
'Roles',
]), $rows);
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
private function determineMessagePattern(ApiKey $apiKey): string

View File

@@ -5,16 +5,16 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Db;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Process\PhpExecutableFinder;
use Throwable;
use function Functional\contains;
use function Functional\map;
@@ -53,11 +53,9 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
{
$io = new SymfonyStyle($input, $output);
$this->checkDbExists();
if ($this->schemaExists()) {
if ($this->databaseTablesExist()) {
$io->success('Database already exists. Run "db:migrate" command to make sure it is up to date.');
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
// Create database
@@ -65,33 +63,12 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
$this->runPhpCommand($output, [self::DOCTRINE_SCRIPT, self::DOCTRINE_CREATE_SCHEMA_COMMAND]);
$io->success('Database properly created!');
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
private function checkDbExists(): void
private function databaseTablesExist(): bool
{
if ($this->regularConn->getDriver()->getDatabasePlatform() instanceof SqlitePlatform) {
return;
}
// In order to create the new database, we have to use a connection where the dbname was not set.
// Otherwise, it will fail to connect and will not be able to create the new database
$schemaManager = $this->noDbNameConn->createSchemaManager();
$databases = $schemaManager->listDatabases();
// We cannot use getDatabase() to get the database name here, because then the driver will try to connect, and
// it does not exist yet. We need to read from the raw params instead.
$shlinkDatabase = $this->regularConn->getParams()['dbname'] ?? null;
if ($shlinkDatabase !== null && ! contains($databases, $shlinkDatabase)) {
$schemaManager->createDatabase($shlinkDatabase);
}
}
private function schemaExists(): bool
{
$schemaManager = $this->regularConn->createSchemaManager();
$existingTables = $schemaManager->listTableNames();
$existingTables = $this->ensureDatabaseExistsAndGetTables();
$allMetadata = $this->em->getMetadataFactory()->getAllMetadata();
$shlinkTables = map($allMetadata, static fn (ClassMetadata $metadata) => $metadata->getTableName());
@@ -99,4 +76,21 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
// Any other inconsistency will be taken care of by the migrations.
return some($shlinkTables, static fn (string $shlinkTable) => contains($existingTables, $shlinkTable));
}
private function ensureDatabaseExistsAndGetTables(): array
{
try {
// Trying to list tables requires opening a connection to configured database.
// If it fails, it means it does not exist yet.
return $this->regularConn->createSchemaManager()->listTableNames();
} catch (Throwable) {
// We cannot use getDatabase() to get the database name here, because then the driver will try to connect.
// Instead, we read from the raw params.
$shlinkDatabase = $this->regularConn->getParams()['dbname'] ?? '';
// Create the database using a connection where the dbname was not set.
$this->noDbNameConn->createSchemaManager()->createDatabase($shlinkDatabase);
return [];
}
}
}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Db;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
@@ -31,6 +31,6 @@ class MigrateDatabaseCommand extends AbstractDatabaseCommand
$this->runPhpCommand($output, [self::DOCTRINE_MIGRATIONS_SCRIPT, self::DOCTRINE_MIGRATE_COMMAND]);
$io->success('Database properly migrated!');
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Domain;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
@@ -109,6 +109,6 @@ class DomainRedirectsCommand extends Command
$io->success(sprintf('"Not found" redirects properly set for "%s"', $domainAuthority));
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Domain;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\CLI\Util\ShlinkTable;
use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
@@ -59,7 +59,7 @@ class ListDomainsCommand extends Command
}),
);
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
private function notFoundRedirectsToString(NotFoundRedirectConfigInterface $config): string

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
@@ -31,7 +31,6 @@ class CreateShortUrlCommand extends Command
public const NAME = 'short-url:create';
private ?SymfonyStyle $io;
private string $defaultDomain;
public function __construct(
private readonly UrlShortenerInterface $urlShortener,
@@ -39,7 +38,6 @@ class CreateShortUrlCommand extends Command
private readonly UrlShortenerOptions $options,
) {
parent::__construct();
$this->defaultDomain = $this->options->domain['hostname'] ?? '';
}
protected function configure(): void
@@ -121,7 +119,6 @@ class CreateShortUrlCommand extends Command
protected function interact(InputInterface $input, OutputInterface $output): void
{
$this->verifyLongUrlArgument($input, $output);
$this->verifyDomainArgument($input);
}
private function verifyLongUrlArgument(InputInterface $input, OutputInterface $output): void
@@ -138,19 +135,13 @@ class CreateShortUrlCommand extends Command
}
}
private function verifyDomainArgument(InputInterface $input): void
{
$domain = $input->getOption('domain');
$input->setOption('domain', $domain === $this->defaultDomain ? null : $domain);
}
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = $this->getIO($input, $output);
$longUrl = $input->getArgument('longUrl');
if (empty($longUrl)) {
$io->error('A URL was not provided!');
return ExitCodes::EXIT_FAILURE;
return ExitCode::EXIT_FAILURE;
}
$explodeWithComma = curry(explode(...))(',');
@@ -185,10 +176,10 @@ class CreateShortUrlCommand extends Command
sprintf('Processed long URL: <info>%s</info>', $longUrl),
sprintf('Generated short URL: <info>%s</info>', $this->stringifier->stringify($result->shortUrl)),
]);
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
} catch (InvalidUrlException | NonUniqueSlugException $e) {
$io->error($e->getMessage());
return ExitCodes::EXIT_FAILURE;
return ExitCode::EXIT_FAILURE;
}
}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Exception;
use Shlinkio\Shlink\Core\ShortUrl\DeleteShortUrlServiceInterface;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
@@ -55,10 +55,10 @@ class DeleteShortUrlCommand extends Command
try {
$this->runDelete($io, $identifier, $ignoreThreshold);
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
} catch (Exception\ShortUrlNotFoundException $e) {
$io->error($e->getMessage());
return ExitCodes::EXIT_FAILURE;
return ExitCode::EXIT_FAILURE;
} catch (Exception\DeleteShortUrlException $e) {
return $this->retry($io, $identifier, $e->getMessage());
}
@@ -75,7 +75,7 @@ class DeleteShortUrlCommand extends Command
$io->warning('Short URL was not deleted.');
}
return $forceDelete ? ExitCodes::EXIT_SUCCESS : ExitCodes::EXIT_WARNING;
return $forceDelete ? ExitCode::EXIT_SUCCESS : ExitCode::EXIT_WARNING;
}
private function runDelete(SymfonyStyle $io, ShortUrlIdentifier $identifier, bool $ignoreThreshold): void

View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
use Shlinkio\Shlink\CLI\Command\Visit\AbstractDeleteVisitsCommand;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlVisitsDeleterInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Style\SymfonyStyle;
use function sprintf;
class DeleteShortUrlVisitsCommand extends AbstractDeleteVisitsCommand
{
public const NAME = 'short-url:visits-delete';
public function __construct(private readonly ShortUrlVisitsDeleterInterface $deleter)
{
parent::__construct();
}
protected function configure(): void
{
$this
->setName(self::NAME)
->setDescription('Deletes visits from a short URL')
->addArgument(
'shortCode',
InputArgument::REQUIRED,
'The short code for the short URL which visits will be deleted',
)
->addOption(
'domain',
'd',
InputOption::VALUE_REQUIRED,
'The domain if the short code does not belong to the default one',
);
}
protected function doExecute(InputInterface $input, SymfonyStyle $io): ?int
{
$identifier = ShortUrlIdentifier::fromCli($input);
try {
$result = $this->deleter->deleteShortUrlVisits($identifier);
$io->success(sprintf('Successfully deleted %s visits', $result->affectedItems));
return ExitCode::EXIT_SUCCESS;
} catch (ShortUrlNotFoundException) {
$io->warning(sprintf('Short URL not found for "%s"', $identifier->__toString()));
return ExitCode::EXIT_WARNING;
}
}
protected function getWarningMessage(): string
{
return 'You are about to delete all visits for a short URL. This operation cannot be undone.';
}
}

View File

@@ -4,9 +4,9 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
use Shlinkio\Shlink\CLI\Option\EndDateOption;
use Shlinkio\Shlink\CLI\Option\StartDateOption;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Input\EndDateOption;
use Shlinkio\Shlink\CLI\Input\StartDateOption;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\CLI\Util\ShlinkTable;
use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Common\Paginator\Util\PagerfantaUtilsTrait;
@@ -102,6 +102,12 @@ class ListShortUrlsCommand extends Command
InputOption::VALUE_NONE,
'Whether to display the tags or not.',
)
->addOption(
'show-domain',
null,
InputOption::VALUE_NONE,
'Whether to display the domain or not. Those belonging to default domain will have value "DEFAULT".',
)
->addOption(
'show-api-key',
'k',
@@ -167,7 +173,7 @@ class ListShortUrlsCommand extends Command
$io->newLine();
$io->success('Short URLs properly listed');
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
private function renderPage(
@@ -217,6 +223,10 @@ class ListShortUrlsCommand extends Command
if ($input->getOption('show-tags')) {
$columnsMap['Tags'] = static fn (array $shortUrl): string => implode(', ', $shortUrl['tags']);
}
if ($input->getOption('show-domain')) {
$columnsMap['Domain'] = static fn (array $_, ShortUrl $shortUrl): string =>
$shortUrl->getDomain()?->authority ?? 'DEFAULT';
}
if ($input->getOption('show-api-key')) {
$columnsMap['API Key'] = static fn (array $_, ShortUrl $shortUrl): string =>
$shortUrl->authorApiKey()?->__toString() ?? '';

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
@@ -56,10 +56,10 @@ class ResolveUrlCommand extends Command
try {
$url = $this->urlResolver->resolveShortUrl(ShortUrlIdentifier::fromCli($input));
$output->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl()));
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
} catch (ShortUrlNotFoundException $e) {
$io->error($e->getMessage());
return ExitCodes::EXIT_FAILURE;
return ExitCode::EXIT_FAILURE;
}
}
}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Tag;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
@@ -41,11 +41,11 @@ class DeleteTagsCommand extends Command
if (empty($tagNames)) {
$io->warning('You have to provide at least one tag name');
return ExitCodes::EXIT_WARNING;
return ExitCode::EXIT_WARNING;
}
$this->tagService->deleteTags($tagNames);
$io->success('Tags properly deleted');
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Tag;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\CLI\Util\ShlinkTable;
use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
use Shlinkio\Shlink\Core\Tag\Model\TagsParams;
@@ -34,7 +34,7 @@ class ListTagsCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
ShlinkTable::default($output)->render(['Name', 'URLs amount', 'Visits amount'], $this->getTagsRows());
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
private function getTagsRows(): array

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Tag;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Exception\TagConflictException;
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
use Shlinkio\Shlink\Core\Tag\Model\TagRenaming;
@@ -42,10 +42,10 @@ class RenameTagCommand extends Command
try {
$this->tagService->renameTag(TagRenaming::fromNames($oldName, $newName));
$io->success('Tag properly renamed.');
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
} catch (TagNotFoundException | TagConflictException $e) {
$io->error($e->getMessage());
return ExitCodes::EXIT_FAILURE;
return ExitCode::EXIT_FAILURE;
}
}
}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Util;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@@ -28,7 +28,7 @@ abstract class AbstractLockedCommand extends Command
$output->writeln(
sprintf('<comment>Command "%s" is already in progress. Skipping.</comment>', $lockConfig->lockName),
);
return ExitCodes::EXIT_WARNING;
return ExitCode::EXIT_WARNING;
}
try {

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Visit;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
abstract class AbstractDeleteVisitsCommand extends Command
{
final protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
if (! $this->confirm($io)) {
$io->info('Operation aborted');
return ExitCode::EXIT_SUCCESS;
}
return $this->doExecute($input, $io);
}
private function confirm(SymfonyStyle $io): bool
{
$io->warning($this->getWarningMessage());
return $io->confirm('<comment>Continue deleting visits?</comment>', false);
}
abstract protected function doExecute(InputInterface $input, SymfonyStyle $io): ?int;
abstract protected function getWarningMessage(): string;
}

View File

@@ -4,9 +4,9 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Visit;
use Shlinkio\Shlink\CLI\Option\EndDateOption;
use Shlinkio\Shlink\CLI\Option\StartDateOption;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Input\EndDateOption;
use Shlinkio\Shlink\CLI\Input\StartDateOption;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\CLI\Util\ShlinkTable;
use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Common\Util\DateRange;
@@ -43,7 +43,7 @@ abstract class AbstractVisitsListCommand extends Command
ShlinkTable::default($output)->render($headers, $rows);
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
}
private function resolveRowsAndHeaders(Paginator $paginator): array

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Command\Visit;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Visit\VisitsDeleterInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function sprintf;
class DeleteOrphanVisitsCommand extends AbstractDeleteVisitsCommand
{
public const NAME = 'visit:orphan-delete';
public function __construct(private readonly VisitsDeleterInterface $deleter)
{
parent::__construct();
}
protected function configure(): void
{
$this
->setName(self::NAME)
->setDescription('Deletes all orphan visits');
}
protected function doExecute(InputInterface $input, SymfonyStyle $io): ?int
{
$result = $this->deleter->deleteOrphanVisits();
$io->success(sprintf('Successfully deleted %s visits', $result->affectedItems));
return ExitCode::EXIT_SUCCESS;
}
protected function getWarningMessage(): string
{
return 'You are about to delete all orphan visits. This operation cannot be undone.';
}
}

View File

@@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\CLI\Command\Visit;
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
use Shlinkio\Shlink\CLI\GeoLite\GeolocationDbUpdaterInterface;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
@@ -56,7 +56,7 @@ class DownloadGeoLiteDbCommand extends Command
$io->success('GeoLite2 db file properly downloaded.');
}
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
} catch (GeolocationDbUpdateFailedException $e) {
$olderDbExists = $e->olderDbExists();
@@ -72,7 +72,7 @@ class DownloadGeoLiteDbCommand extends Command
$this->getApplication()?->renderThrowable($e, $io);
}
return $olderDbExists ? ExitCodes::EXIT_WARNING : ExitCodes::EXIT_FAILURE;
return $olderDbExists ? ExitCode::EXIT_WARNING : ExitCode::EXIT_FAILURE;
}
}
}

View File

@@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\CLI\Command\Visit;
use Shlinkio\Shlink\CLI\Command\Util\AbstractLockedCommand;
use Shlinkio\Shlink\CLI\Command\Util\LockedCommandConfig;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
@@ -116,14 +116,14 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat
}
$this->io->success('Finished locating visits');
return ExitCodes::EXIT_SUCCESS;
return ExitCode::EXIT_SUCCESS;
} catch (Throwable $e) {
$this->io->error($e->getMessage());
if ($this->io->isVerbose()) {
$this->getApplication()?->renderThrowable($e, $this->io);
}
return ExitCodes::EXIT_FAILURE;
return ExitCode::EXIT_FAILURE;
}
}
@@ -171,7 +171,7 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat
$downloadDbCommand = $cliApp->find(DownloadGeoLiteDbCommand::NAME);
$exitCode = $downloadDbCommand->run(new ArrayInput([]), $this->io);
if ($exitCode === ExitCodes::EXIT_FAILURE) {
if ($exitCode === ExitCode::EXIT_FAILURE) {
throw new RuntimeException('It is not possible to locate visits without a GeoLite2 db file.');
}
}

View File

@@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Option;
namespace Shlinkio\Shlink\CLI\Input;
use Cake\Chronos\Chronos;
use Symfony\Component\Console\Command\Command;

View File

@@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Option;
namespace Shlinkio\Shlink\CLI\Input;
use Cake\Chronos\Chronos;
use Symfony\Component\Console\Command\Command;

View File

@@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Option;
namespace Shlinkio\Shlink\CLI\Input;
use Cake\Chronos\Chronos;
use Symfony\Component\Console\Command\Command;

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\CLI\Util;
final class ExitCodes
final class ExitCode
{
public const EXIT_SUCCESS = 0;
public const EXIT_FAILURE = -1;

View File

@@ -23,8 +23,8 @@ class ProcessRunner implements ProcessRunnerInterface
public function __construct(private ProcessHelper $helper, ?callable $createProcess = null)
{
$this->createProcess = $createProcess !== null
? Closure::fromCallable($createProcess)
: static fn (array $cmd) => new Process($cmd, null, null, null, LockedCommandConfig::DEFAULT_TTL);
? $createProcess(...)
: static fn (array $cmd) => new Process($cmd, timeout: LockedCommandConfig::DEFAULT_TTL);
}
public function run(OutputInterface $output, array $cmd): void

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace ShlinkioCliTest\Shlink\CLI\Command;
use PHPUnit\Framework\Attributes\Test;
use Shlinkio\Shlink\CLI\Command\ShortUrl\CreateShortUrlCommand;
use Shlinkio\Shlink\CLI\Command\ShortUrl\ListShortUrlsCommand;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\TestUtils\CliTest\CliTestCase;
class CreateShortUrlTest extends CliTestCase
{
#[Test]
public function defaultDomainIsIgnoredWhenExplicitlyProvided(): void
{
$slug = 'testing-default-domain';
$defaultDomain = 's.test';
[$output, $exitCode] = $this->exec(
[CreateShortUrlCommand::NAME, 'https://example.com', '--domain', $defaultDomain, '--custom-slug', $slug],
);
self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode);
self::assertStringContainsString('Generated short URL: http://' . $defaultDomain . '/' . $slug, $output);
[$listOutput] = $this->exec([ListShortUrlsCommand::NAME, '--show-domain', '--search-term', $slug]);
self::assertStringContainsString('DEFAULT', $listOutput);
}
}

View File

@@ -6,7 +6,7 @@ namespace ShlinkioCliTest\Shlink\CLI\Command;
use PHPUnit\Framework\Attributes\Test;
use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\TestUtils\CliTest\CliTestCase;
class GenerateApiKeyTest extends CliTestCase
@@ -17,6 +17,6 @@ class GenerateApiKeyTest extends CliTestCase
[$output, $exitCode] = $this->exec([GenerateKeyCommand::NAME]);
self::assertStringContainsString('[OK] Generated API key', $output);
self::assertEquals(ExitCodes::EXIT_SUCCESS, $exitCode);
self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode);
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace ShlinkioCliTest\Shlink\CLI\Command;
use PHPUnit\Framework\Attributes\Test;
use Shlinkio\Shlink\CLI\Command\ShortUrl\ListShortUrlsCommand;
use Shlinkio\Shlink\Importer\Command\ImportCommand;
use Shlinkio\Shlink\TestUtils\CliTest\CliTestCase;
use function fclose;
use function fopen;
use function fwrite;
use function is_string;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;
class ImportShortUrlsTest extends CliTestCase
{
/**
* @var false|string|null
* @todo Use native type once PHP 8.1 support is dropped
*/
private mixed $tempCsvFile = null;
protected function setUp(): void
{
$this->tempCsvFile = tempnam(sys_get_temp_dir(), 'shlink_csv');
if (! $this->tempCsvFile) {
return;
}
$handle = fopen($this->tempCsvFile, 'w+');
if (! $handle) {
$this->fail('It was not possible to open the temporary file to write CSV on it');
}
fwrite(
$handle,
<<<CSV
longURL;tags;domain;short code;Title
https://shlink.io;foo,baz;s.test;testing-default-domain-import-1;
https://example.com;foo;s.test;testing-default-domain-import-2;
CSV,
);
fclose($handle);
}
protected function tearDown(): void
{
if (is_string($this->tempCsvFile)) {
unlink($this->tempCsvFile);
}
}
#[Test]
public function defaultDomainIsIgnoredWhenExplicitlyProvided(): void
{
if (! $this->tempCsvFile) {
$this->fail('It was not possible to create a temporary CSV file');
}
[$output] = $this->exec([ImportCommand::NAME, 'csv'], [$this->tempCsvFile, ';']);
self::assertStringContainsString('https://shlink.io: Imported', $output);
self::assertStringContainsString('https://example.com: Imported', $output);
[$listOutput1] = $this->exec(
[ListShortUrlsCommand::NAME, '--show-domain', '--search-term', 'testing-default-domain-import-1'],
);
self::assertStringContainsString('DEFAULT', $listOutput1);
[$listOutput1] = $this->exec(
[ListShortUrlsCommand::NAME, '--show-domain', '--search-term', 'testing-default-domain-import-2'],
);
self::assertStringContainsString('DEFAULT', $listOutput1);
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace ShlinkioCliTest\Shlink\CLI\Command;
use PHPUnit\Framework\Attributes\Test;
use Shlinkio\Shlink\CLI\Command\Api\InitialApiKeyCommand;
use Shlinkio\Shlink\TestUtils\CliTest\CliTestCase;
class InitialApiKeyTest extends CliTestCase
{
#[Test]
public function createsNoKeyWhenOtherApiKeysAlreadyExist(): void
{
[$output] = $this->exec([InitialApiKeyCommand::NAME, 'new_api_key', '-v']);
self::assertEquals(
<<<OUT
Other API keys already exist. Initial API key creation skipped.
OUT,
$output,
);
}
}

View File

@@ -8,7 +8,7 @@ use Cake\Chronos\Chronos;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Shlinkio\Shlink\CLI\Command\Api\ListKeysCommand;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\TestUtils\CliTest\CliTestCase;
class ListApiKeysTest extends CliTestCase
@@ -19,7 +19,7 @@ class ListApiKeysTest extends CliTestCase
[$output, $exitCode] = $this->exec([ListKeysCommand::NAME, ...$flags]);
self::assertEquals($expectedOutput, $output);
self::assertEquals(ExitCodes::EXIT_SUCCESS, $exitCode);
self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode);
}
public static function provideFlags(): iterable

View File

@@ -40,7 +40,7 @@ class RoleResolverTest extends TestCase
'example.com',
)->willReturn(self::domainWithId(Domain::withAuthority('example.com')));
$result = $this->resolver->determineRoles($input);
$result = [...$this->resolver->determineRoles($input)];
self::assertEquals($expectedRoles, $result);
}
@@ -111,7 +111,7 @@ class RoleResolverTest extends TestCase
$this->expectException(InvalidRoleConfigException::class);
$this->resolver->determineRoles($input);
[...$this->resolver->determineRoles($input)];
}
private static function domainWithId(Domain $domain): Domain

View File

@@ -10,6 +10,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\ApiKey\RoleResolverInterface;
use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
@@ -37,8 +38,7 @@ class GenerateKeyCommandTest extends TestCase
public function noExpirationDateIsDefinedIfNotProvided(): void
{
$this->apiKeyService->expects($this->once())->method('create')->with(
$this->isNull(),
$this->isNull(),
$this->callback(fn (ApiKeyMeta $meta) => $meta->name === null && $meta->expirationDate === null),
)->willReturn(ApiKey::create());
$this->commandTester->execute([]);
@@ -51,8 +51,7 @@ class GenerateKeyCommandTest extends TestCase
public function expirationDateIsDefinedIfProvided(): void
{
$this->apiKeyService->expects($this->once())->method('create')->with(
$this->isInstanceOf(Chronos::class),
$this->isNull(),
$this->callback(fn (ApiKeyMeta $meta) => $meta->expirationDate instanceof Chronos),
)->willReturn(ApiKey::create());
$this->commandTester->execute([
@@ -64,8 +63,7 @@ class GenerateKeyCommandTest extends TestCase
public function nameIsDefinedIfProvided(): void
{
$this->apiKeyService->expects($this->once())->method('create')->with(
$this->isNull(),
$this->isType('string'),
$this->callback(fn (ApiKeyMeta $meta) => $meta->name === 'Alice'),
)->willReturn(ApiKey::create());
$this->commandTester->execute([

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\Api;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\Api\InitialApiKeyCommand;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
class InitialApiKeyCommandTest extends TestCase
{
use CliTestUtilsTrait;
private CommandTester $commandTester;
private MockObject & ApiKeyServiceInterface $apiKeyService;
public function setUp(): void
{
$this->apiKeyService = $this->createMock(ApiKeyServiceInterface::class);
$this->commandTester = $this->testerForCommand(new InitialApiKeyCommand($this->apiKeyService));
}
#[Test, DataProvider('provideParams')]
public function initialKeyIsCreatedWithProvidedValue(?ApiKey $result, bool $verbose, string $expectedOutput): void
{
$this->apiKeyService->expects($this->once())->method('createInitial')->with('the_key')->willReturn($result);
$this->commandTester->execute(
['apiKey' => 'the_key'],
['verbosity' => $verbose ? OutputInterface::VERBOSITY_VERBOSE : OutputInterface::VERBOSITY_NORMAL],
);
$output = $this->commandTester->getDisplay();
self::assertEquals($expectedOutput, $output);
}
public static function provideParams(): iterable
{
yield 'api key created, no verbose' => [ApiKey::create(), false, ''];
yield 'api key created, verbose' => [ApiKey::create(), true, ''];
yield 'no api key created, no verbose' => [null, false, ''];
yield 'no api key created, verbose' => [null, true, <<<OUT
Other API keys already exist. Initial API key creation skipped.
OUT,
];
}
}

View File

@@ -49,7 +49,7 @@ class ListKeysCommandTest extends TestCase
yield 'all keys' => [
[
$apiKey1 = ApiKey::create()->disable(),
$apiKey2 = ApiKey::fromMeta(ApiKeyMeta::withExpirationDate($dateInThePast)),
$apiKey2 = ApiKey::fromMeta(ApiKeyMeta::fromParams(expirationDate: $dateInThePast)),
$apiKey3 = ApiKey::create(),
],
false,
@@ -117,9 +117,9 @@ class ListKeysCommandTest extends TestCase
];
yield 'with names' => [
[
$apiKey1 = ApiKey::fromMeta(ApiKeyMeta::withName('Alice')),
$apiKey2 = ApiKey::fromMeta(ApiKeyMeta::withName('Alice and Bob')),
$apiKey3 = ApiKey::fromMeta(ApiKeyMeta::withName('')),
$apiKey1 = ApiKey::fromMeta(ApiKeyMeta::fromParams(name: 'Alice')),
$apiKey2 = ApiKey::fromMeta(ApiKeyMeta::fromParams(name: 'Alice and Bob')),
$apiKey3 = ApiKey::fromMeta(ApiKeyMeta::fromParams(name: '')),
$apiKey4 = ApiKey::create(),
],
true,

View File

@@ -7,11 +7,11 @@ namespace ShlinkioTest\Shlink\CLI\Command\Db;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
use Exception;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
@@ -69,17 +69,14 @@ class CreateDatabaseCommandTest extends TestCase
#[Test]
public function successMessageIsPrintedIfDatabaseAlreadyExists(): void
{
$shlinkDatabase = 'shlink_database';
$this->regularConn->expects($this->once())->method('getParams')->willReturn(['dbname' => $shlinkDatabase]);
$this->regularConn->expects($this->never())->method('getParams');
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
$metadataMock = $this->createMock(ClassMetadata::class);
$metadataMock->expects($this->once())->method('getTableName')->willReturn('foo_table');
$this->metadataFactory->method('getAllMetadata')->willReturn([$metadataMock]);
$this->schemaManager->expects($this->once())->method('listDatabases')->willReturn(
['foo', $shlinkDatabase, 'bar'],
);
$this->schemaManager->expects($this->never())->method('createDatabase');
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn(['foo_table', 'bar_table']);
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
@@ -90,15 +87,13 @@ class CreateDatabaseCommandTest extends TestCase
#[Test]
public function databaseIsCreatedIfItDoesNotExist(): void
{
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
$shlinkDatabase = 'shlink_database';
$this->regularConn->expects($this->once())->method('getParams')->willReturn(['dbname' => $shlinkDatabase]);
$this->metadataFactory->method('getAllMetadata')->willReturn([]);
$this->schemaManager->expects($this->once())->method('listDatabases')->willReturn(['foo', 'bar']);
$this->schemaManager->expects($this->once())->method('createDatabase')->with($shlinkDatabase);
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn(
['foo_table', 'bar_table'],
);
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
$this->schemaManager->expects($this->once())->method('listTableNames')->willThrowException(new Exception(''));
$this->commandTester->execute([]);
}
@@ -106,14 +101,12 @@ class CreateDatabaseCommandTest extends TestCase
#[Test, DataProvider('provideEmptyDatabase')]
public function tablesAreCreatedIfDatabaseIsEmpty(array $tables): void
{
$shlinkDatabase = 'shlink_database';
$this->regularConn->expects($this->once())->method('getParams')->willReturn(['dbname' => $shlinkDatabase]);
$this->regularConn->expects($this->never())->method('getParams');
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
$metadata = $this->createMock(ClassMetadata::class);
$metadata->method('getTableName')->willReturn('shlink_table');
$this->metadataFactory->method('getAllMetadata')->willReturn([$metadata]);
$this->schemaManager->expects($this->once())->method('listDatabases')->willReturn(
['foo', $shlinkDatabase, 'bar'],
);
$this->schemaManager->expects($this->never())->method('createDatabase');
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn($tables);
$this->processHelper->expects($this->once())->method('run')->with($this->isInstanceOf(OutputInterface::class), [
@@ -122,7 +115,6 @@ class CreateDatabaseCommandTest extends TestCase
CreateDatabaseCommand::DOCTRINE_CREATE_SCHEMA_COMMAND,
'--no-interaction',
]);
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
@@ -136,18 +128,4 @@ class CreateDatabaseCommandTest extends TestCase
yield 'no tables' => [[]];
yield 'migrations table' => [['non_shlink_table']];
}
#[Test]
public function databaseCheckIsSkippedForSqlite(): void
{
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(SqlitePlatform::class));
$this->regularConn->expects($this->never())->method('getParams');
$this->metadataFactory->expects($this->once())->method('getAllMetadata')->willReturn([]);
$this->schemaManager->expects($this->never())->method('listDatabases');
$this->schemaManager->expects($this->never())->method('createDatabase');
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn(['foo_table', 'bar_table']);
$this->commandTester->execute([]);
}
}

View File

@@ -9,7 +9,7 @@ use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\Domain\ListDomainsCommand;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
@@ -53,7 +53,7 @@ class ListDomainsCommandTest extends TestCase
$this->commandTester->execute($input);
self::assertEquals($expectedOutput, $this->commandTester->getDisplay());
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
self::assertEquals(ExitCode::EXIT_SUCCESS, $this->commandTester->getStatusCode());
}
public static function provideInputsAndOutputs(): iterable

View File

@@ -11,7 +11,7 @@ use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\ShortUrl\CreateShortUrlCommand;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
@@ -28,8 +28,6 @@ class CreateShortUrlCommandTest extends TestCase
{
use CliTestUtilsTrait;
private const DEFAULT_DOMAIN = 'default.com';
private CommandTester $commandTester;
private MockObject & UrlShortenerInterface $urlShortener;
private MockObject & ShortUrlStringifierInterface $stringifier;
@@ -43,7 +41,7 @@ class CreateShortUrlCommandTest extends TestCase
$this->urlShortener,
$this->stringifier,
new UrlShortenerOptions(
domain: ['hostname' => self::DEFAULT_DOMAIN, 'schema' => ''],
domain: ['hostname' => 'example.com', 'schema' => ''],
defaultShortCodesLength: 5,
),
);
@@ -67,7 +65,7 @@ class CreateShortUrlCommandTest extends TestCase
], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$output = $this->commandTester->getDisplay();
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
self::assertEquals(ExitCode::EXIT_SUCCESS, $this->commandTester->getStatusCode());
self::assertStringContainsString('stringified_short_url', $output);
self::assertStringNotContainsString('but the real-time updates cannot', $output);
}
@@ -84,7 +82,7 @@ class CreateShortUrlCommandTest extends TestCase
$this->commandTester->execute(['longUrl' => $url]);
$output = $this->commandTester->getDisplay();
self::assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode());
self::assertEquals(ExitCode::EXIT_FAILURE, $this->commandTester->getStatusCode());
self::assertStringContainsString('Provided URL http://domain.com/invalid is invalid.', $output);
}
@@ -99,7 +97,7 @@ class CreateShortUrlCommandTest extends TestCase
$this->commandTester->execute(['longUrl' => 'http://domain.com/invalid', '--custom-slug' => 'my-slug']);
$output = $this->commandTester->getDisplay();
self::assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode());
self::assertEquals(ExitCode::EXIT_FAILURE, $this->commandTester->getStatusCode());
self::assertStringContainsString('Provided slug "my-slug" is already in use', $output);
}
@@ -123,7 +121,7 @@ class CreateShortUrlCommandTest extends TestCase
]);
$output = $this->commandTester->getDisplay();
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
self::assertEquals(ExitCode::EXIT_SUCCESS, $this->commandTester->getStatusCode());
self::assertStringContainsString('stringified_short_url', $output);
}
@@ -141,15 +139,14 @@ class CreateShortUrlCommandTest extends TestCase
$input['longUrl'] = 'http://domain.com/foo/bar';
$this->commandTester->execute($input);
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
self::assertEquals(ExitCode::EXIT_SUCCESS, $this->commandTester->getStatusCode());
}
public static function provideDomains(): iterable
{
yield 'no domain' => [[], null];
yield 'non-default domain foo' => [['--domain' => 'foo.com'], 'foo.com'];
yield 'non-default domain bar' => [['-d' => 'bar.com'], 'bar.com'];
yield 'default domain' => [['--domain' => self::DEFAULT_DOMAIN], null];
yield 'domain foo' => [['--domain' => 'foo.com'], 'foo.com'];
yield 'domain bar' => [['-d' => 'bar.com'], 'bar.com'];
}
#[Test, DataProvider('provideFlags')]

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\ShortUrl\DeleteShortUrlVisitsCommand;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlVisitsDeleterInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Tester\CommandTester;
class DeleteShortUrlVisitsCommandTest extends TestCase
{
use CliTestUtilsTrait;
private CommandTester $commandTester;
private MockObject & ShortUrlVisitsDeleterInterface $deleter;
protected function setUp(): void
{
$this->deleter = $this->createMock(ShortUrlVisitsDeleterInterface::class);
$this->commandTester = $this->testerForCommand(new DeleteShortUrlVisitsCommand($this->deleter));
}
#[Test, DataProvider('provideCancellingInputs')]
public function executionIsAbortedIfManuallyCancelled(array $input): void
{
$this->deleter->expects($this->never())->method('deleteShortUrlVisits');
$this->commandTester->setInputs($input);
$exitCode = $this->commandTester->execute(['shortCode' => 'foo']);
$output = $this->commandTester->getDisplay();
self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode);
self::assertStringContainsString('Operation aborted', $output);
}
public static function provideCancellingInputs(): iterable
{
yield 'default input' => [[]];
yield 'no' => [['no']];
yield 'n' => [['n']];
}
#[Test, DataProvider('provideErrorArgs')]
public function warningIsPrintedInCaseOfNotFoundShortUrl(array $args, string $expectedError): void
{
$this->deleter->expects($this->once())->method('deleteShortUrlVisits')->willThrowException(
new ShortUrlNotFoundException(),
);
$this->commandTester->setInputs(['yes']);
$exitCode = $this->commandTester->execute($args);
$output = $this->commandTester->getDisplay();
self::assertEquals(ExitCode::EXIT_WARNING, $exitCode);
self::assertStringContainsString($expectedError, $output);
}
public static function provideErrorArgs(): iterable
{
yield 'domain' => [['shortCode' => 'foo'], 'Short URL not found for "foo"'];
yield 'no domain' => [['shortCode' => 'foo', '--domain' => 's.test'], 'Short URL not found for "s.test/foo"'];
}
#[Test]
public function successMessageIsPrintedForValidShortUrls(): void
{
$this->deleter->expects($this->once())->method('deleteShortUrlVisits')->willReturn(new BulkDeleteResult(5));
$this->commandTester->setInputs(['yes']);
$exitCode = $this->commandTester->execute(['shortCode' => 'foo']);
$output = $this->commandTester->getDisplay();
self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode);
self::assertStringContainsString('Successfully deleted 5 visits', $output);
}
}

View File

@@ -138,19 +138,25 @@ class ListShortUrlsCommandTest extends TestCase
public static function provideOptionalFlags(): iterable
{
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withName('my api key'));
$apiKey = ApiKey::fromMeta(ApiKeyMeta::fromParams(name: 'my api key'));
$key = $apiKey->toString();
yield 'tags only' => [
['--show-tags' => true],
['| Tags ', '| foo, bar, baz'],
['| API Key ', '| API Key Name |', $key, '| my api key'],
['| API Key ', '| API Key Name |', $key, '| my api key', '| Domain', '| DEFAULT'],
$apiKey,
];
yield 'domain only' => [
['--show-domain' => true],
['| Domain', '| DEFAULT'],
['| Tags ', '| foo, bar, baz', '| API Key ', '| API Key Name |', $key, '| my api key'],
$apiKey,
];
yield 'api key only' => [
['--show-api-key' => true],
['| API Key ', $key],
['| Tags ', '| foo, bar, baz', '| API Key Name |', '| my api key'],
['| Tags ', '| foo, bar, baz', '| API Key Name |', '| my api key', '| Domain', '| DEFAULT'],
$apiKey,
];
yield 'api key name only' => [
@@ -165,9 +171,24 @@ class ListShortUrlsCommandTest extends TestCase
['| API Key Name |', '| my api key'],
$apiKey,
];
yield 'tags and domain' => [
['--show-tags' => true, '--show-domain' => true],
['| Tags ', '| foo, bar, baz', '| Domain', '| DEFAULT'],
['| API Key Name |', '| my api key'],
$apiKey,
];
yield 'all' => [
['--show-tags' => true, '--show-api-key' => true, '--show-api-key-name' => true],
['| API Key ', '| Tags ', '| API Key Name |', '| foo, bar, baz', $key, '| my api key'],
['--show-tags' => true, '--show-domain' => true, '--show-api-key' => true, '--show-api-key-name' => true],
[
'| API Key ',
'| Tags ',
'| API Key Name |',
'| foo, bar, baz',
$key,
'| my api key',
'| Domain',
'| DEFAULT',
],
[],
$apiKey,
];

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\Visit;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\Visit\DeleteOrphanVisitsCommand;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Core\Visit\VisitsDeleterInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Tester\CommandTester;
class DeleteOrphanVisitsCommandTest extends TestCase
{
use CliTestUtilsTrait;
private CommandTester $commandTester;
private MockObject & VisitsDeleterInterface $deleter;
protected function setUp(): void
{
$this->deleter = $this->createMock(VisitsDeleterInterface::class);
$this->commandTester = $this->testerForCommand(new DeleteOrphanVisitsCommand($this->deleter));
}
#[Test]
public function successMessageIsPrintedAfterDeletion(): void
{
$this->deleter->expects($this->once())->method('deleteOrphanVisits')->willReturn(new BulkDeleteResult(5));
$this->commandTester->setInputs(['yes']);
$exitCode = $this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode);
self::assertStringContainsString('You are about to delete all orphan visits.', $output);
self::assertStringContainsString('Successfully deleted 5 visits', $output);
}
}

View File

@@ -12,7 +12,7 @@ use Shlinkio\Shlink\CLI\Command\Visit\DownloadGeoLiteDbCommand;
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
use Shlinkio\Shlink\CLI\GeoLite\GeolocationDbUpdaterInterface;
use Shlinkio\Shlink\CLI\GeoLite\GeolocationResult;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Tester\CommandTester;
@@ -65,12 +65,12 @@ class DownloadGeoLiteDbCommandTest extends TestCase
yield 'existing db' => [
true,
'[WARNING] GeoLite2 db file update failed. Visits will continue to be located',
ExitCodes::EXIT_WARNING,
ExitCode::EXIT_WARNING,
];
yield 'not existing db' => [
false,
'[ERROR] GeoLite2 db file download failed. It will not be possible to locate',
ExitCodes::EXIT_FAILURE,
ExitCode::EXIT_FAILURE,
];
}
@@ -86,7 +86,7 @@ class DownloadGeoLiteDbCommandTest extends TestCase
$exitCode = $this->commandTester->getStatusCode();
self::assertStringContainsString($expectedMessage, $output);
self::assertSame(ExitCodes::EXIT_SUCCESS, $exitCode);
self::assertSame(ExitCode::EXIT_SUCCESS, $exitCode);
}
public static function provideSuccessParams(): iterable

View File

@@ -10,7 +10,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\Visit\DownloadGeoLiteDbCommand;
use Shlinkio\Shlink\CLI\Command\Visit\LocateVisitsCommand;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
@@ -85,7 +85,7 @@ class LocateVisitsCommandTest extends TestCase
$this->visitToLocation->expects(
$this->exactly($expectedUnlocatedCalls + $expectedEmptyCalls + $expectedAllCalls),
)->method('resolveVisitLocation')->withAnyParameters()->willReturn(Location::emptyInstance());
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->commandTester->setInputs(['y']);
$this->commandTester->execute($args);
@@ -118,7 +118,7 @@ class LocateVisitsCommandTest extends TestCase
->withAnyParameters()
->willReturnCallback($this->invokeHelperMethods($visit, $location));
$this->visitToLocation->expects($this->once())->method('resolveVisitLocation')->willThrowException($e);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
@@ -147,7 +147,7 @@ class LocateVisitsCommandTest extends TestCase
$this->visitToLocation->expects($this->once())->method('resolveVisitLocation')->willThrowException(
IpCannotBeLocatedException::forError(WrongIpException::fromIpAddress('1.2.3.4')),
);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
@@ -171,7 +171,7 @@ class LocateVisitsCommandTest extends TestCase
$this->visitService->expects($this->never())->method('locateUnlocatedVisits');
$this->visitToLocation->expects($this->never())->method('resolveVisitLocation');
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$output = $this->commandTester->getDisplay();
@@ -186,7 +186,7 @@ class LocateVisitsCommandTest extends TestCase
public function showsProperMessageWhenGeoLiteUpdateFails(): void
{
$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_FAILURE);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_FAILURE);
$this->visitService->expects($this->never())->method('locateUnlocatedVisits');
$this->commandTester->execute([]);
@@ -199,7 +199,7 @@ class LocateVisitsCommandTest extends TestCase
public function providingAllFlagOnItsOwnDisplaysNotice(): void
{
$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->commandTester->execute(['--all' => true]);
$output = $this->commandTester->getDisplay();
@@ -210,7 +210,7 @@ class LocateVisitsCommandTest extends TestCase
#[Test, DataProvider('provideAbortInputs')]
public function processingAllCancelsCommandIfUserDoesNotActivelyAgreeToConfirmation(array $inputs): void
{
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCode::EXIT_SUCCESS);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Execution aborted');

View File

@@ -13,6 +13,7 @@ use Shlinkio\Shlink\Core\ErrorHandler;
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface;
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
use Symfony\Component\Lock;
return [
@@ -38,6 +39,7 @@ return [
ShortUrl\ShortUrlListService::class => ConfigAbstractFactory::class,
ShortUrl\DeleteShortUrlService::class => ConfigAbstractFactory::class,
ShortUrl\ShortUrlResolver::class => ConfigAbstractFactory::class,
ShortUrl\ShortUrlVisitsDeleter::class => ConfigAbstractFactory::class,
ShortUrl\Helper\ShortCodeUniquenessHelper::class => ConfigAbstractFactory::class,
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class => ConfigAbstractFactory::class,
ShortUrl\Helper\ShortUrlStringifier::class => ConfigAbstractFactory::class,
@@ -61,6 +63,7 @@ return [
Visit\VisitsTracker::class => ConfigAbstractFactory::class,
Visit\RequestTracker::class => ConfigAbstractFactory::class,
Visit\VisitsDeleter::class => ConfigAbstractFactory::class,
Visit\Geolocation\VisitLocator::class => ConfigAbstractFactory::class,
Visit\Geolocation\VisitToLocationHelper::class => ConfigAbstractFactory::class,
Visit\VisitsStatsHelper::class => ConfigAbstractFactory::class,
@@ -69,6 +72,10 @@ return [
EntityRepositoryFactory::class,
Visit\Entity\Visit::class,
],
Visit\Repository\VisitDeleterRepository::class => [
EntityRepositoryFactory::class,
Visit\Entity\Visit::class,
],
Util\UrlValidator::class => ConfigAbstractFactory::class,
Util\DoctrineBatchHelper::class => ConfigAbstractFactory::class,
@@ -117,6 +124,7 @@ return [
Options\TrackingOptions::class,
],
Visit\RequestTracker::class => [Visit\VisitsTracker::class, Options\TrackingOptions::class],
Visit\VisitsDeleter::class => [Visit\Repository\VisitDeleterRepository::class],
ShortUrl\ShortUrlService::class => [
'em',
ShortUrl\ShortUrlResolver::class,
@@ -137,6 +145,10 @@ return [
ShortUrl\ShortUrlResolver::class,
],
ShortUrl\ShortUrlResolver::class => ['em', Options\UrlShortenerOptions::class],
ShortUrl\ShortUrlVisitsDeleter::class => [
Visit\Repository\VisitDeleterRepository::class,
ShortUrl\ShortUrlResolver::class,
],
ShortUrl\Helper\ShortCodeUniquenessHelper::class => ['em', Options\UrlShortenerOptions::class],
Domain\DomainService::class => ['em', 'config.url_shortener.domain.hostname'],
@@ -161,7 +173,11 @@ return [
],
Action\RobotsAction::class => [Crawling\CrawlingHelper::class],
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class => ['em'],
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class => [
'em',
Options\UrlShortenerOptions::class,
Lock\LockFactory::class,
],
ShortUrl\Helper\ShortUrlStringifier::class => ['config.url_shortener.domain', 'config.router.base_path'],
ShortUrl\Helper\ShortUrlTitleResolutionHelper::class => [Util\UrlValidator::class],
ShortUrl\Helper\ShortUrlRedirectionBuilder::class => [Options\TrackingOptions::class],

View File

@@ -47,7 +47,6 @@ enum EnvVars: string
case PORT = 'PORT';
case TASK_WORKER_NUM = 'TASK_WORKER_NUM';
case WEB_WORKER_NUM = 'WEB_WORKER_NUM';
case INITIAL_API_KEY = 'INITIAL_API_KEY';
case ANONYMIZE_REMOTE_ADDR = 'ANONYMIZE_REMOTE_ADDR';
case TRACK_ORPHAN_VISITS = 'TRACK_ORPHAN_VISITS';
case DISABLE_TRACK_PARAM = 'DISABLE_TRACK_PARAM';

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Model;
final class BulkDeleteResult
{
public function __construct(public readonly int $affectedItems)
{
}
public function toArray(string $fieldName): array
{
return [$fieldName => $this->affectedItems];
}
}

View File

@@ -12,7 +12,7 @@ enum DeviceType: string
public static function matchFromUserAgent(string $userAgent): ?self
{
$detect = new MobileDetect(null, $userAgent); // @phpstan-ignore-line
$detect = new MobileDetect(userAgent: $userAgent); // @phpstan-ignore-line
return match (true) {
// $detect->is('iOS') && $detect->isTablet() => self::IOS, // TODO To detect iPad only

View File

@@ -26,4 +26,9 @@ final class UrlShortenerOptions
{
return $this->mode === ShortUrlMode::LOOSE;
}
public function defaultDomain(): string
{
return $this->domain['hostname'] ?? '';
}
}

View File

@@ -8,6 +8,8 @@ use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Symfony\Component\Console\Input\InputInterface;
use function sprintf;
final class ShortUrlIdentifier
{
private function __construct(public readonly string $shortCode, public readonly ?string $domain = null)
@@ -54,4 +56,13 @@ final class ShortUrlIdentifier
{
return new self($shortCode, $domain);
}
public function __toString(): string
{
if ($this->domain === null) {
return $this->shortCode;
}
return sprintf('%s/%s', $this->domain, $this->shortCode);
}
}

View File

@@ -9,8 +9,13 @@ use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\Tag\Entity\Tag;
use Symfony\Component\Lock\Lock;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\InMemoryStore;
use function Functional\invoke;
use function Functional\map;
use function Functional\unique;
@@ -20,31 +25,43 @@ class PersistenceShortUrlRelationResolver implements ShortUrlRelationResolverInt
private array $memoizedNewDomains = [];
/** @var array<string, Tag> */
private array $memoizedNewTags = [];
/** @var array<string, Lock> */
private array $tagLocks = [];
/** @var array<string, Lock> */
private array $domainLocks = [];
public function __construct(private readonly EntityManagerInterface $em)
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly UrlShortenerOptions $options = new UrlShortenerOptions(),
private readonly LockFactory $locker = new LockFactory(new InMemoryStore()),
) {
// Registering this as an event listener will make the postFlush method to be called automatically
$this->em->getEventManager()->addEventListener(Events::postFlush, $this);
}
public function resolveDomain(?string $domain): ?Domain
{
if ($domain === null) {
if ($domain === null || $domain === $this->options->defaultDomain()) {
return null;
}
$this->lock($this->domainLocks, 'domain_' . $domain);
/** @var Domain|null $existingDomain */
$existingDomain = $this->em->getRepository(Domain::class)->findOneBy(['authority' => $domain]);
if ($existingDomain) {
// The lock can be released immediately of the domain is not new
$this->releaseLock($this->domainLocks, 'domain_' . $domain);
return $existingDomain;
}
// Memoize only new domains, and let doctrine handle objects hydrated from persistence
return $existingDomain ?? $this->memoizeNewDomain($domain);
return $this->memoizeNewDomain($domain);
}
private function memoizeNewDomain(string $domain): Domain
{
return $this->memoizedNewDomains[$domain] = $this->memoizedNewDomains[$domain] ?? Domain::withAuthority(
$domain,
);
return $this->memoizedNewDomains[$domain] ??= Domain::withAuthority($domain);
}
/**
@@ -61,8 +78,16 @@ class PersistenceShortUrlRelationResolver implements ShortUrlRelationResolverInt
$repo = $this->em->getRepository(Tag::class);
return new Collections\ArrayCollection(map($tags, function (string $tagName) use ($repo): Tag {
$this->lock($this->tagLocks, 'tag_' . $tagName);
$existingTag = $repo->findOneBy(['name' => $tagName]);
if ($existingTag) {
$this->releaseLock($this->tagLocks, 'tag_' . $tagName);
return $existingTag;
}
// Memoize only new tags, and let doctrine handle objects hydrated from persistence
$tag = $repo->findOneBy(['name' => $tagName]) ?? $this->memoizeNewTag($tagName);
$tag = $this->memoizeNewTag($tagName);
$this->em->persist($tag);
return $tag;
@@ -71,12 +96,39 @@ class PersistenceShortUrlRelationResolver implements ShortUrlRelationResolverInt
private function memoizeNewTag(string $tagName): Tag
{
return $this->memoizedNewTags[$tagName] = $this->memoizedNewTags[$tagName] ?? new Tag($tagName);
return $this->memoizedNewTags[$tagName] ??= new Tag($tagName);
}
/**
* @param array<string, Lock> $locks
*/
private function lock(array &$locks, string $name): void
{
// Lock dependency creation for up to 5 seconds. This will prevent errors when trying to create the same one
// more than once in parallel.
$locks[$name] = $lock = $this->locker->createLock($name, 5);
$lock->acquire(true);
}
/**
* @param array<string, Lock> $locks
*/
private function releaseLock(array &$locks, string $name): void
{
$locks[$name]->release();
unset($locks[$name]);
}
public function postFlush(): void
{
// Reset memoized domains and tags
$this->memoizedNewDomains = [];
$this->memoizedNewTags = [];
// Release all locks
invoke($this->tagLocks, 'release');
invoke($this->domainLocks, 'release');
$this->tagLocks = [];
$this->domainLocks = [];
}
}

View File

@@ -25,7 +25,7 @@ class ShortUrlListService implements ShortUrlListServiceInterface
*/
public function listShortUrls(ShortUrlsParams $params, ?ApiKey $apiKey = null): Paginator
{
$defaultDomain = $this->urlShortenerOptions->domain['hostname'] ?? '';
$defaultDomain = $this->urlShortenerOptions->defaultDomain();
$paginator = new Paginator(new ShortUrlRepositoryAdapter($this->repo, $params, $apiKey, $defaultDomain));
$paginator->setMaxPerPage($params->itemsPerPage)
->setCurrentPage($params->page);

View File

@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Visit\Repository\VisitDeleterRepositoryInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ShortUrlVisitsDeleter implements ShortUrlVisitsDeleterInterface
{
public function __construct(
private readonly VisitDeleterRepositoryInterface $repository,
private readonly ShortUrlResolverInterface $resolver,
) {
}
/**
* @throws ShortUrlNotFoundException
*/
public function deleteShortUrlVisits(ShortUrlIdentifier $identifier, ?ApiKey $apiKey = null): BulkDeleteResult
{
$shortUrl = $this->resolver->resolveShortUrl($identifier, $apiKey);
return new BulkDeleteResult($this->repository->deleteShortUrlVisits($shortUrl));
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
interface ShortUrlVisitsDeleterInterface
{
/**
* @throws ShortUrlNotFoundException
*/
public function deleteShortUrlVisits(ShortUrlIdentifier $identifier, ?ApiKey $apiKey = null): BulkDeleteResult;
}

View File

@@ -27,7 +27,7 @@ class UrlValidator implements UrlValidatorInterface, RequestMethodInterface
{
private const MAX_REDIRECTS = 15;
private const CHROME_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '
. 'Chrome/108.0.0.0 Safari/537.36';
. 'Chrome/112.0.0.0 Safari/537.36';
public function __construct(private ClientInterface $httpClient, private UrlShortenerOptions $options)
{

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Repository;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
class VisitDeleterRepository extends EntitySpecificationRepository implements VisitDeleterRepositoryInterface
{
public function deleteShortUrlVisits(ShortUrl $shortUrl): int
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->delete(Visit::class, 'v')
->where($qb->expr()->eq('v.shortUrl', ':shortUrl'))
->setParameter('shortUrl', $shortUrl);
return $qb->getQuery()->execute();
}
public function deleteOrphanVisits(): int
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->delete(Visit::class, 'v')
->where($qb->expr()->isNull('v.shortUrl'));
return $qb->getQuery()->execute();
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit\Repository;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
interface VisitDeleterRepositoryInterface
{
public function deleteShortUrlVisits(ShortUrl $shortUrl): int;
public function deleteOrphanVisits(): int;
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Core\Visit\Repository\VisitDeleterRepositoryInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class VisitsDeleter implements VisitsDeleterInterface
{
public function __construct(private readonly VisitDeleterRepositoryInterface $repository)
{
}
public function deleteOrphanVisits(?ApiKey $apiKey = null): BulkDeleteResult
{
// TODO Check API key has permissions for orphan visits
return new BulkDeleteResult($this->repository->deleteOrphanVisits());
}
}

View File

@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Visit;
use Shlinkio\Shlink\Core\Model\BulkDeleteResult;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
interface VisitsDeleterInterface
{
public function deleteOrphanVisits(?ApiKey $apiKey = null): BulkDeleteResult;
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace ShlinkioDbTest\Shlink\Core\Visit\Repository;
use PHPUnit\Framework\Attributes\Test;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Repository\VisitDeleterRepository;
use Shlinkio\Shlink\TestUtils\DbTest\DatabaseTestCase;
class VisitDeleterRepositoryTest extends DatabaseTestCase
{
private VisitDeleterRepository $repo;
protected function setUp(): void
{
$em = $this->getEntityManager();
$this->repo = new VisitDeleterRepository($em, $em->getClassMetadata(Visit::class));
}
#[Test]
public function deletesExpectedShortUrlVisits(): void
{
$shortUrl1 = ShortUrl::withLongUrl('https://foo.com');
$this->getEntityManager()->persist($shortUrl1);
$this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl1, Visitor::emptyInstance()));
$this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl1, Visitor::emptyInstance()));
$shortUrl2 = ShortUrl::create(ShortUrlCreation::fromRawData([
ShortUrlInputFilter::LONG_URL => 'https://foo.com',
ShortUrlInputFilter::DOMAIN => 's.test',
ShortUrlInputFilter::CUSTOM_SLUG => 'foo',
]), new PersistenceShortUrlRelationResolver($this->getEntityManager()));
$this->getEntityManager()->persist($shortUrl2);
$this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl2, Visitor::emptyInstance()));
$this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl2, Visitor::emptyInstance()));
$this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl2, Visitor::emptyInstance()));
$this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl2, Visitor::emptyInstance()));
$shortUrl3 = ShortUrl::create(ShortUrlCreation::fromRawData([
ShortUrlInputFilter::LONG_URL => 'https://foo.com',
ShortUrlInputFilter::CUSTOM_SLUG => 'foo',
]), new PersistenceShortUrlRelationResolver($this->getEntityManager()));
$this->getEntityManager()->persist($shortUrl3);
$this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl3, Visitor::emptyInstance()));
$this->getEntityManager()->flush();
self::assertEquals(2, $this->repo->deleteShortUrlVisits($shortUrl1));
self::assertEquals(0, $this->repo->deleteShortUrlVisits($shortUrl1));
self::assertEquals(4, $this->repo->deleteShortUrlVisits($shortUrl2));
self::assertEquals(0, $this->repo->deleteShortUrlVisits($shortUrl2));
self::assertEquals(1, $this->repo->deleteShortUrlVisits($shortUrl3));
self::assertEquals(0, $this->repo->deleteShortUrlVisits($shortUrl3));
}
#[Test]
public function deletesExpectedOrphanVisits(): void
{
$visitor = Visitor::emptyInstance();
$this->getEntityManager()->persist(Visit::forBasePath($visitor));
$this->getEntityManager()->persist(Visit::forInvalidShortUrl($visitor));
$this->getEntityManager()->persist(Visit::forRegularNotFound($visitor));
$this->getEntityManager()->persist(Visit::forBasePath($visitor));
$this->getEntityManager()->persist(Visit::forInvalidShortUrl($visitor));
$this->getEntityManager()->persist(Visit::forRegularNotFound($visitor));
$this->getEntityManager()->flush();
self::assertEquals(6, $this->repo->deleteOrphanVisits());
self::assertEquals(0, $this->repo->deleteOrphanVisits());
}
}

Some files were not shown because too many files have changed in this diff Show More