diff --git a/composer.json b/composer.json index a9f15652..a73a704b 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "ext-gd": "*", "ext-iconv": "*", "ext-json": "*", - "badges/poser": "^2.2", + "badges/poser": "^2.3.1", "bitbucket/client": "^4.0", "cache/predis-adapter": "^1.1", "knplabs/github-api": "^3.3", diff --git a/composer.lock b/composer.lock index b31ed3cd..4e9903c8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,32 +4,33 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7cf8622f839ff4c64a6985a5227f7b90", + "content-hash": "452e794b5520af6ecd085aa72c1f84d2", "packages": [ { "name": "badges/poser", - "version": "v2.2.0", + "version": "v2.3.1", "source": { "type": "git", "url": "https://github.com/badges/poser.git", - "reference": "49e91ea72d514ee431c5c185855a16e50a2d2030" + "reference": "13f4ae7998700568fe1d07c8964492ea845e675c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/badges/poser/zipball/49e91ea72d514ee431c5c185855a16e50a2d2030", - "reference": "49e91ea72d514ee431c5c185855a16e50a2d2030", + "url": "https://api.github.com/repos/badges/poser/zipball/13f4ae7998700568fe1d07c8964492ea845e675c", + "reference": "13f4ae7998700568fe1d07c8964492ea845e675c", "shasum": "" }, "require": { "ext-gd": "*", "ext-simplexml": "*", + "kartsims/easysvg": "^2.4", "php": ">=7.4", "symfony/console": "^4.0|^5.0|^6.0" }, "require-dev": { "behat/behat": "^3.8", "friends-of-phpspec/phpspec-code-coverage": "^6.0", - "friendsofphp/php-cs-fixer": "^2.17", + "friendsofphp/php-cs-fixer": "^3.5", "moave/phpspec-data-provider-extension": "dev-feat/add-compatibility-to-php80", "phpspec/phpspec": "^7.0", "vimeo/psalm": "^4.3" @@ -76,7 +77,7 @@ ], "support": { "issues": "https://github.com/badges/poser/issues", - "source": "https://github.com/badges/poser/tree/v2.2.0" + "source": "https://github.com/badges/poser/tree/v2.3.1" }, "funding": [ { @@ -92,7 +93,7 @@ "type": "github" } ], - "time": "2022-01-10T08:41:38+00:00" + "time": "2022-02-21T09:52:41+00:00" }, { "name": "bitbucket/client", @@ -1041,18 +1042,64 @@ }, "time": "2021-10-08T21:21:46+00:00" }, + { + "name": "kartsims/easysvg", + "version": "2.4", + "source": { + "type": "git", + "url": "https://github.com/kartsims/easysvg.git", + "reference": "c105955d9da6226a681120bcce7cdcc0e6c1d8e3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/kartsims/easysvg/zipball/c105955d9da6226a681120bcce7cdcc0e6c1d8e3", + "reference": "c105955d9da6226a681120bcce7cdcc0e6c1d8e3", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-xmlreader": "*", + "php": ">=7.4.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.5" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Simon Tarchichi", + "email": "kartsims@gmail.com" + } + ], + "description": "PHP library to generate SVG XML", + "homepage": "https://github.com/kartsims/easysvg", + "support": { + "issues": "https://github.com/kartsims/easysvg/issues", + "source": "https://github.com/kartsims/easysvg/tree/master" + }, + "time": "2022-02-21T09:22:32+00:00" + }, { "name": "knplabs/github-api", - "version": "v3.5.0", + "version": "v3.5.1", "source": { "type": "git", "url": "https://github.com/KnpLabs/php-github-api.git", - "reference": "9906308d0c0e0b551e5fd461e026592de66fe311" + "reference": "37b167998e8e1f318b3d99633675cfa007540565" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/KnpLabs/php-github-api/zipball/9906308d0c0e0b551e5fd461e026592de66fe311", - "reference": "9906308d0c0e0b551e5fd461e026592de66fe311", + "url": "https://api.github.com/repos/KnpLabs/php-github-api/zipball/37b167998e8e1f318b3d99633675cfa007540565", + "reference": "37b167998e8e1f318b3d99633675cfa007540565", "shasum": "" }, "require": { @@ -1119,7 +1166,7 @@ ], "support": { "issues": "https://github.com/KnpLabs/php-github-api/issues", - "source": "https://github.com/KnpLabs/php-github-api/tree/v3.5.0" + "source": "https://github.com/KnpLabs/php-github-api/tree/v3.5.1" }, "funding": [ { @@ -1127,7 +1174,7 @@ "type": "github" } ], - "time": "2022-01-23T11:32:47+00:00" + "time": "2022-02-19T11:15:46+00:00" }, { "name": "knplabs/packagist-api", @@ -3585,16 +3632,16 @@ }, { "name": "symfony/flex", - "version": "v2.1.4", + "version": "v2.1.6", "source": { "type": "git", "url": "https://github.com/symfony/flex.git", - "reference": "110e8928b6e719e9b9f146c8fd92c41a4b74a3ac" + "reference": "dd7dafe3bfebf8441e167b76d8a18974585467f3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/flex/zipball/110e8928b6e719e9b9f146c8fd92c41a4b74a3ac", - "reference": "110e8928b6e719e9b9f146c8fd92c41a4b74a3ac", + "url": "https://api.github.com/repos/symfony/flex/zipball/dd7dafe3bfebf8441e167b76d8a18974585467f3", + "reference": "dd7dafe3bfebf8441e167b76d8a18974585467f3", "shasum": "" }, "require": { @@ -3630,7 +3677,7 @@ "description": "Composer plugin for Symfony", "support": { "issues": "https://github.com/symfony/flex/issues", - "source": "https://github.com/symfony/flex/tree/v2.1.4" + "source": "https://github.com/symfony/flex/tree/v2.1.6" }, "funding": [ { @@ -3646,7 +3693,7 @@ "type": "tidelift" } ], - "time": "2022-01-27T10:25:12+00:00" + "time": "2022-02-16T17:31:43+00:00" }, { "name": "symfony/framework-bundle", @@ -5613,16 +5660,16 @@ }, { "name": "symfony/webpack-encore-bundle", - "version": "v1.13.2", + "version": "v1.14.0", "source": { "type": "git", "url": "https://github.com/symfony/webpack-encore-bundle.git", - "reference": "509cad50878e838c879743225e0e921b3b64a3f2" + "reference": "1729c314574f32e49f9660a0cee870f7e603864f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/webpack-encore-bundle/zipball/509cad50878e838c879743225e0e921b3b64a3f2", - "reference": "509cad50878e838c879743225e0e921b3b64a3f2", + "url": "https://api.github.com/repos/symfony/webpack-encore-bundle/zipball/1729c314574f32e49f9660a0cee870f7e603864f", + "reference": "1729c314574f32e49f9660a0cee870f7e603864f", "shasum": "" }, "require": { @@ -5664,7 +5711,7 @@ "description": "Integration with your Symfony app & Webpack Encore!", "support": { "issues": "https://github.com/symfony/webpack-encore-bundle/issues", - "source": "https://github.com/symfony/webpack-encore-bundle/tree/v1.13.2" + "source": "https://github.com/symfony/webpack-encore-bundle/tree/v1.14.0" }, "funding": [ { @@ -5680,7 +5727,7 @@ "type": "tidelift" } ], - "time": "2021-12-02T19:45:55+00:00" + "time": "2022-02-14T15:06:55+00:00" }, { "name": "symfony/yaml", @@ -7290,16 +7337,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "9.2.10", + "version": "9.2.11", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "d5850aaf931743067f4bfc1ae4cbd06468400687" + "reference": "665a1ac0a763c51afc30d6d130dac0813092b17f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/d5850aaf931743067f4bfc1ae4cbd06468400687", - "reference": "d5850aaf931743067f4bfc1ae4cbd06468400687", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/665a1ac0a763c51afc30d6d130dac0813092b17f", + "reference": "665a1ac0a763c51afc30d6d130dac0813092b17f", "shasum": "" }, "require": { @@ -7355,7 +7402,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.10" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.11" }, "funding": [ { @@ -7363,7 +7410,7 @@ "type": "github" } ], - "time": "2021-12-05T09:12:13+00:00" + "time": "2022-02-18T12:46:09+00:00" }, { "name": "phpunit/php-file-iterator", @@ -7608,16 +7655,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.13", + "version": "9.5.14", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "597cb647654ede35e43b137926dfdfef0fb11743" + "reference": "1883687169c017d6ae37c58883ca3994cfc34189" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/597cb647654ede35e43b137926dfdfef0fb11743", - "reference": "597cb647654ede35e43b137926dfdfef0fb11743", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1883687169c017d6ae37c58883ca3994cfc34189", + "reference": "1883687169c017d6ae37c58883ca3994cfc34189", "shasum": "" }, "require": { @@ -7695,7 +7742,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.13" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.14" }, "funding": [ { @@ -7707,7 +7754,7 @@ "type": "github" } ], - "time": "2022-01-24T07:33:35+00:00" + "time": "2022-02-18T12:54:07+00:00" }, { "name": "sebastian/cli-parser", diff --git a/config/routes/badge.yaml b/config/routes/badge.yaml index 4f28955e..835213a4 100644 --- a/config/routes/badge.yaml +++ b/config/routes/badge.yaml @@ -4,9 +4,11 @@ pugx_badge_version: methods: GET defaults: latest: "stable" + style: "flat" _ext: "svg" requirements: repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" pugx_badge_version_latest: @@ -14,10 +16,12 @@ pugx_badge_version_latest: controller: App\Controller\Badge\VersionController::version defaults: latest: "stable" + style: "flat" _ext: "svg" requirements: latest: "stable|unstable" repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" pugx_badge_license: @@ -25,9 +29,11 @@ pugx_badge_license: controller: App\Controller\Badge\LicenseController::license methods: GET defaults: + style: "flat" _ext: "svg" requirements: repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" pugx_badge_download: @@ -36,19 +42,23 @@ pugx_badge_download: methods: GET defaults: type: "total" + style: "flat" _ext: "svg" requirements: repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" pugx_badge_download_type: path: /{repository}/d/{type}.{_ext} controller: App\Controller\Badge\DownloadsController::downloads defaults: + style: "flat" _ext: "svg" requirements: type: "total|daily|monthly" repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" pugx_badge_composerlock: @@ -56,9 +66,11 @@ pugx_badge_composerlock: controller: App\Controller\Badge\ComposerLockController::composerLock methods: GET defaults: + style: "flat" _ext: "svg" requirements: repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" pugx_badge_gitattributes: @@ -66,9 +78,11 @@ pugx_badge_gitattributes: controller: App\Controller\Badge\GitAttributesController::gitAttributes methods: GET defaults: + style: "flat" _ext: "svg" requirements: repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" pugx_badge_dependents: @@ -76,9 +90,11 @@ pugx_badge_dependents: controller: App\Controller\Badge\DependentsController::dependents methods: GET defaults: + style: "flat" _ext: "svg" requirements: repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" pugx_badge_suggesters: @@ -86,9 +102,11 @@ pugx_badge_suggesters: controller: App\Controller\Badge\SuggestersController::suggesters methods: GET defaults: + style: "flat" _ext: "svg" requirements: repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" pugx_badge_circleci: @@ -97,10 +115,12 @@ pugx_badge_circleci: methods: GET defaults: branch: "master" + style: "flat" _ext: "svg" requirements: repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" branch: ".+" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" pugx_badge_require: @@ -108,8 +128,10 @@ pugx_badge_require: controller: App\Controller\Badge\RequireController::require methods: GET defaults: + style: "flat" _ext: "svg" requirements: repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?" type: ".+" + style: "flat|flat-square|for-the-badge|plastic" _ext: "svg" diff --git a/config/services.yaml b/config/services.yaml index 5f175d96..0f93c9fc 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -90,7 +90,7 @@ services: PUGX\Poser\Poser: class: ~ arguments: - $renders: [ '@PUGX\Poser\SvgPlasticRender', '@PUGX\Poser\SvgFlatRender', '@PUGX\Poser\SvgFlatSquareRender' ] + $renders: [ '@PUGX\Poser\SvgPlasticRender', '@PUGX\Poser\SvgFlatRender', '@PUGX\Poser\SvgFlatSquareRender', '@PUGX\Poser\SvgForTheBadgeRender' ] PUGX\Poser\SvgPlasticRender: class: PUGX\Poser\Render\SvgPlasticRender @@ -100,3 +100,6 @@ services: PUGX\Poser\SvgFlatSquareRender: class: PUGX\Poser\Render\SvgFlatSquareRender + + PUGX\Poser\SvgForTheBadgeRender: + class: PUGX\Poser\Render\SvgForTheBadgeRenderer diff --git a/src/Badge/Model/Badge.php b/src/Badge/Model/Badge.php index f7f1a160..ac3cb471 100644 --- a/src/Badge/Model/Badge.php +++ b/src/Badge/Model/Badge.php @@ -13,15 +13,19 @@ final class Badge implements BadgeInterface, \Stringable { - private const DEFAULT_FORMAT = 'svg'; + public const DEFAULT_FORMAT = 'svg'; + public const DEFAULT_STYLE = \PUGX\Poser\Badge::DEFAULT_STYLE; + private string $subject; private string $status; private string $format; + private string $style; - public function __construct(string $subject, string $status, private string $color, string $format = self::DEFAULT_FORMAT) + public function __construct(string $subject, string $status, private string $color, string $format = self::DEFAULT_FORMAT, string $style = self::DEFAULT_STYLE) { $this->subject = $this->escapeValue($subject); $this->status = $this->escapeValue($status); + $this->style = $this->escapeValue($style); $this->format = $this->escapeValue($format); if (!$this->isValidColorHex($this->color)) { @@ -45,6 +49,14 @@ public function getFormat(): string return $this->format; } + /** + * @return string the style of the image e.g. "flat" or "plastic". + */ + public function getStyle(): string + { + return $this->style; + } + public function getStatus(): string { return $this->status; @@ -58,11 +70,12 @@ public function getSubject(): string public function __toString(): string { return \sprintf( - '%s-%s-%s.%s', + '%s-%s-%s-%s.%s', $this->subject, $this->status, $this->color, - $this->format + $this->style, + $this->format, ); } diff --git a/src/Badge/Model/BadgeInterface.php b/src/Badge/Model/BadgeInterface.php index 4436f646..6f752418 100644 --- a/src/Badge/Model/BadgeInterface.php +++ b/src/Badge/Model/BadgeInterface.php @@ -19,6 +19,8 @@ public function getStatus(): string; public function getHexColor(): string; + public function getStyle(): string; + public function getFormat(): string; public function __toString(): string; diff --git a/src/Badge/Model/CacheableBadge.php b/src/Badge/Model/CacheableBadge.php index 182ca1b3..f49479e1 100644 --- a/src/Badge/Model/CacheableBadge.php +++ b/src/Badge/Model/CacheableBadge.php @@ -71,4 +71,9 @@ public function __toString(): string { return $this->badge->__toString(); } + + public function getStyle(): string + { + return $this->badge->getStyle(); + } } diff --git a/src/Badge/Model/UseCase/BaseCreatePackagistImage.php b/src/Badge/Model/UseCase/BaseCreatePackagistImage.php index 78c751a8..8ae81d69 100644 --- a/src/Badge/Model/UseCase/BaseCreatePackagistImage.php +++ b/src/Badge/Model/UseCase/BaseCreatePackagistImage.php @@ -37,7 +37,8 @@ protected function createBadgeFromRepository( string $repository, string $subject, string $color, - string $format = 'svg', + string $format = Badge::DEFAULT_FORMAT, + string $style = Badge::DEFAULT_STYLE, ?string $context = null, int $maxage = self::TTL_DEFAULT_MAXAGE, int $smaxage = self::TTL_DEFAULT_SMAXAGE @@ -46,10 +47,10 @@ protected function createBadgeFromRepository( $package = $this->fetchPackage($repository); $text = $this->prepareText($package, $context); } catch (\Exception) { - return $this->createDefaultBadge($format); + return $this->createDefaultBadge($format, $style); } - return $this->createBadge($subject, $text, $color, $format, $maxage, $smaxage); + return $this->createBadge($subject, $text, $color, $format, $style, $maxage, $smaxage); } /** @@ -68,11 +69,12 @@ protected function createBadge( string $status, string $color, string $format, + string $style, int $maxage = self::TTL_DEFAULT_MAXAGE, int $smaxage = self::TTL_DEFAULT_SMAXAGE ): CacheableBadge { return new CacheableBadge( - new Badge($subject, $status, $color, $format), + new Badge($subject, $status, $color, $format, $style), $maxage, $smaxage ); @@ -81,13 +83,13 @@ protected function createBadge( /** * @throws InvalidArgumentException */ - protected function createDefaultBadge(string $format): CacheableBadge + protected function createDefaultBadge(string $format, string $style = Badge::DEFAULT_STYLE): CacheableBadge { $subject = ' - '; $text = ' - '; $color = '7A7A7A'; - return $this->createBadge($subject, $text, $color, $format, CacheableBadge::TTL_NO_CACHE, CacheableBadge::TTL_NO_CACHE); + return $this->createBadge($subject, $text, $color, $format, $style, CacheableBadge::TTL_NO_CACHE, CacheableBadge::TTL_NO_CACHE); } abstract protected function prepareText(Package $package, ?string $context): string; diff --git a/src/Badge/Model/UseCase/CreateCircleCiBadge.php b/src/Badge/Model/UseCase/CreateCircleCiBadge.php index 3047e367..a1ee5f1e 100644 --- a/src/Badge/Model/UseCase/CreateCircleCiBadge.php +++ b/src/Badge/Model/UseCase/CreateCircleCiBadge.php @@ -11,6 +11,7 @@ namespace App\Badge\Model\UseCase; +use App\Badge\Model\Badge; use App\Badge\Model\CacheableBadge; use App\Badge\Model\Package; use App\Badge\Model\PackageRepositoryInterface; @@ -45,7 +46,7 @@ public function __construct(PackageRepositoryInterface $packageRepository, prote * @throws InvalidArgumentException * @throws UnexpectedValueException */ - public function createCircleCiBadge(string $repository, string $branch = 'master', string $format = 'svg'): CacheableBadge + public function createCircleCiBadge(string $repository, string $branch = 'master', string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { try { //check if the repo exist @@ -60,16 +61,16 @@ public function createCircleCiBadge(string $repository, string $branch = 'master $response = $this->circleCiClient->getBuilds($repository, $branch); if (Response::HTTP_OK !== $response->getStatusCode()) { - return $this->createDefaultBadge($format); + return $this->createDefaultBadge($format, $style); } $builds = \json_decode($response->getContent(), true, 512, \JSON_THROW_ON_ERROR); } catch (Throwable) { - return $this->createDefaultBadge($format); + return $this->createDefaultBadge($format, $style); } if (\count($builds) < 1) { - return $this->createDefaultBadge($format); + return $this->createDefaultBadge($format, $style); } $build = \current($builds); @@ -87,6 +88,7 @@ public function createCircleCiBadge(string $repository, string $branch = 'master self::SUBJECT, $color, $format, + $style, null, self::TTL_DEFAULT_MAXAGE, self::TTL_DEFAULT_SMAXAGE diff --git a/src/Badge/Model/UseCase/CreateComposerLockBadge.php b/src/Badge/Model/UseCase/CreateComposerLockBadge.php index 6281b8e1..40eff2f3 100644 --- a/src/Badge/Model/UseCase/CreateComposerLockBadge.php +++ b/src/Badge/Model/UseCase/CreateComposerLockBadge.php @@ -11,6 +11,7 @@ namespace App\Badge\Model\UseCase; +use App\Badge\Model\Badge; use App\Badge\Model\CacheableBadge; use App\Badge\Model\Package; use App\Badge\Model\PackageRepositoryInterface; @@ -55,13 +56,13 @@ public function __construct( * @throws UnexpectedValueException * @throws \GuzzleHttp\Exception\GuzzleException */ - public function createComposerLockBadge(string $repository, string $format = 'svg'): CacheableBadge + public function createComposerLockBadge(string $repository, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { try { $package = $this->fetchPackage($repository); $repo = \str_replace('.git', '', $package->getRepository()); } catch (\Exception) { - return $this->createDefaultBadge($format); + return $this->createDefaultBadge($format, $style); } $repositoryInfo = Repository::createFromRepositoryUrl($repo); @@ -100,6 +101,7 @@ public function createComposerLockBadge(string $repository, string $format = 'sv $subject, $color, $format, + $style, null, self::TTL_DEFAULT_MAXAGE, self::TTL_DEFAULT_SMAXAGE diff --git a/src/Badge/Model/UseCase/CreateDependentsBadge.php b/src/Badge/Model/UseCase/CreateDependentsBadge.php index 9f45d17f..8ac39c98 100644 --- a/src/Badge/Model/UseCase/CreateDependentsBadge.php +++ b/src/Badge/Model/UseCase/CreateDependentsBadge.php @@ -11,6 +11,7 @@ namespace App\Badge\Model\UseCase; +use App\Badge\Model\Badge; use App\Badge\Model\CacheableBadge; use App\Badge\Model\Package; use App\Badge\Model\PackageRepositoryInterface; @@ -38,7 +39,7 @@ public function __construct( $this->normalizer = $textNormalizer ?? new TextNormalizer(); } - public function createDependentsBadge(string $repository, string $format = 'svg'): CacheableBadge + public function createDependentsBadge(string $repository, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { $maxage = self::TTL_DEFAULT_MAXAGE; $smaxage = self::TTL_DEFAULT_SMAXAGE; @@ -48,6 +49,7 @@ public function createDependentsBadge(string $repository, string $format = 'svg' self::SUBJECT, self::COLOR, $format, + $style, null, $maxage, $smaxage diff --git a/src/Badge/Model/UseCase/CreateDownloadsBadge.php b/src/Badge/Model/UseCase/CreateDownloadsBadge.php index a1bbde13..968dc816 100644 --- a/src/Badge/Model/UseCase/CreateDownloadsBadge.php +++ b/src/Badge/Model/UseCase/CreateDownloadsBadge.php @@ -11,6 +11,7 @@ namespace App\Badge\Model\UseCase; +use App\Badge\Model\Badge; use App\Badge\Model\CacheableBadge; use App\Badge\Model\Package; use App\Badge\Model\PackageRepositoryInterface; @@ -40,7 +41,7 @@ public function __construct(PackageRepositoryInterface $packageRepository, ?Norm /** * @throws InvalidArgumentException */ - public function createDownloadsBadge(string $repository, string $type, string $format): CacheableBadge + public function createDownloadsBadge(string $repository, string $type, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { $maxage = self::TTL_DEFAULT_MAXAGE; $smaxage = self::TTL_DEFAULT_SMAXAGE; @@ -50,6 +51,7 @@ public function createDownloadsBadge(string $repository, string $type, string $f self::SUBJECT, self::COLOR, $format, + $style, $type, $maxage, $smaxage diff --git a/src/Badge/Model/UseCase/CreateErrorBadge.php b/src/Badge/Model/UseCase/CreateErrorBadge.php index 61c24b36..c38b50b0 100644 --- a/src/Badge/Model/UseCase/CreateErrorBadge.php +++ b/src/Badge/Model/UseCase/CreateErrorBadge.php @@ -35,10 +35,10 @@ final class CreateErrorBadge /** * @throws InvalidArgumentException */ - public function createErrorBadge(Throwable $throwable, string $format): CacheableBadge + public function createErrorBadge(Throwable $throwable, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { return new CacheableBadge( - $this->createBadge($throwable, $format), + $this->createBadge($throwable, $format, $style), self::TTL_DEFAULT_MAXAGE, self::TTL_DEFAULT_SMAXAGE ); @@ -47,7 +47,7 @@ public function createErrorBadge(Throwable $throwable, string $format): Cacheabl /** * @throws InvalidArgumentException */ - private function createBadge(Throwable $throwable, string $format): BadgeInterface + private function createBadge(Throwable $throwable, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): BadgeInterface { $status = self::ERROR_TEXT_GENERIC; if ($throwable instanceof BadResponseException) { @@ -56,6 +56,6 @@ private function createBadge(Throwable $throwable, string $format): BadgeInterfa $status = self::ERROR_TEXT_CLIENT_EXCEPTION; } - return new Badge((string) $throwable, $status, self::COLOR, $format); + return new Badge((string) $throwable, $status, self::COLOR, $format, $style); } } diff --git a/src/Badge/Model/UseCase/CreateGitAttributesBadge.php b/src/Badge/Model/UseCase/CreateGitAttributesBadge.php index 6706b028..fa8ecfa9 100644 --- a/src/Badge/Model/UseCase/CreateGitAttributesBadge.php +++ b/src/Badge/Model/UseCase/CreateGitAttributesBadge.php @@ -11,6 +11,7 @@ namespace App\Badge\Model\UseCase; +use App\Badge\Model\Badge; use App\Badge\Model\CacheableBadge; use App\Badge\Model\Package; use App\Badge\Model\PackageRepositoryInterface; @@ -56,13 +57,13 @@ public function __construct( * @throws UnexpectedValueException * @throws GuzzleException */ - public function createGitAttributesBadge(string $repository, string $format = 'svg'): CacheableBadge + public function createGitAttributesBadge(string $repository, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { try { $package = $this->fetchPackage($repository); $repo = \str_replace('.git', '', $package->getRepository()); } catch (\Exception) { - return $this->createDefaultBadge($format); + return $this->createDefaultBadge($format, $style); } $repositoryInfo = Repository::createFromRepositoryUrl($repo); @@ -102,6 +103,7 @@ public function createGitAttributesBadge(string $repository, string $format = 's $subject, $color, $format, + $style, null, self::TTL_DEFAULT_MAXAGE, self::TTL_DEFAULT_SMAXAGE diff --git a/src/Badge/Model/UseCase/CreateLicenseBadge.php b/src/Badge/Model/UseCase/CreateLicenseBadge.php index 1319459f..f52e60f1 100644 --- a/src/Badge/Model/UseCase/CreateLicenseBadge.php +++ b/src/Badge/Model/UseCase/CreateLicenseBadge.php @@ -11,6 +11,7 @@ namespace App\Badge\Model\UseCase; +use App\Badge\Model\Badge; use App\Badge\Model\CacheableBadge; use App\Badge\Model\Package; use InvalidArgumentException; @@ -30,13 +31,14 @@ final class CreateLicenseBadge extends BaseCreatePackagistImage /** * @throws InvalidArgumentException */ - public function createLicenseBadge(string $repository, string $format = 'svg'): CacheableBadge + public function createLicenseBadge(string $repository, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { return $this->createBadgeFromRepository( $repository, self::SUBJECT, self::COLOR, $format, + $style, null, self::TTL_DEFAULT_MAXAGE, self::TTL_DEFAULT_SMAXAGE diff --git a/src/Badge/Model/UseCase/CreateRequireBadge.php b/src/Badge/Model/UseCase/CreateRequireBadge.php index a800c160..2877d5c6 100644 --- a/src/Badge/Model/UseCase/CreateRequireBadge.php +++ b/src/Badge/Model/UseCase/CreateRequireBadge.php @@ -11,6 +11,7 @@ namespace App\Badge\Model\UseCase; +use App\Badge\Model\Badge; use App\Badge\Model\CacheableBadge; use App\Badge\Model\Package; use App\Badge\Model\PackageRepositoryInterface; @@ -32,13 +33,14 @@ public function __construct( parent::__construct($packageRepository); } - public function createRequireBadge(string $repository, string $type, string $format = 'svg'): CacheableBadge + public function createRequireBadge(string $repository, string $type, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { return $this->createBadgeFromRepository( $repository, $type, self::COLOR, $format, + $style, $type, self::TTL_DEFAULT_MAXAGE, self::TTL_DEFAULT_SMAXAGE diff --git a/src/Badge/Model/UseCase/CreateSuggestersBadge.php b/src/Badge/Model/UseCase/CreateSuggestersBadge.php index 70003261..84f0beb8 100644 --- a/src/Badge/Model/UseCase/CreateSuggestersBadge.php +++ b/src/Badge/Model/UseCase/CreateSuggestersBadge.php @@ -11,6 +11,7 @@ namespace App\Badge\Model\UseCase; +use App\Badge\Model\Badge; use App\Badge\Model\CacheableBadge; use App\Badge\Model\Package; use App\Badge\Model\PackageRepositoryInterface; @@ -38,13 +39,14 @@ public function __construct( $this->normalizer = $textNormalizer ?? new TextNormalizer(); } - public function createSuggestersBadge(string $repository, string $format = 'svg'): CacheableBadge + public function createSuggestersBadge(string $repository, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { return $this->createBadgeFromRepository( $repository, self::SUBJECT, self::COLOR, $format, + $style, null, self::TTL_DEFAULT_MAXAGE, self::TTL_DEFAULT_SMAXAGE diff --git a/src/Badge/Model/UseCase/CreateVersionBadge.php b/src/Badge/Model/UseCase/CreateVersionBadge.php index de1b53f0..58bfcd9c 100644 --- a/src/Badge/Model/UseCase/CreateVersionBadge.php +++ b/src/Badge/Model/UseCase/CreateVersionBadge.php @@ -11,6 +11,7 @@ namespace App\Badge\Model\UseCase; +use App\Badge\Model\Badge; use App\Badge\Model\CacheableBadge; use App\Badge\Model\Package; use InvalidArgumentException; @@ -32,13 +33,14 @@ final class CreateVersionBadge extends BaseCreatePackagistImage /** * @throws InvalidArgumentException */ - public function createStableBadge(string $repository, string $format = 'svg'): CacheableBadge + public function createStableBadge(string $repository, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { return $this->createBadgeFromRepository( $repository, self::SUBJECT_STABLE, self::COLOR_STABLE, $format, + $style, 'stable', self::TTL_DEFAULT_MAXAGE, self::TTL_DEFAULT_SMAXAGE @@ -48,13 +50,14 @@ public function createStableBadge(string $repository, string $format = 'svg'): C /** * @throws InvalidArgumentException */ - public function createUnstableBadge(string $repository, string $format = 'svg'): CacheableBadge + public function createUnstableBadge(string $repository, string $format = Badge::DEFAULT_FORMAT, string $style = Badge::DEFAULT_STYLE): CacheableBadge { return $this->createBadgeFromRepository( $repository, self::SUBJECT_UNSTABLE, self::COLOR_UNSTABLE, $format, + $style, 'unstable', self::TTL_DEFAULT_MAXAGE, self::TTL_DEFAULT_SMAXAGE diff --git a/src/Badge/Service/ImageFactory.php b/src/Badge/Service/ImageFactory.php index ba421d10..613dd383 100644 --- a/src/Badge/Service/ImageFactory.php +++ b/src/Badge/Service/ImageFactory.php @@ -26,7 +26,7 @@ public function createFromBadge(BadgeInterface $badge): Image $badge->getSubject(), $badge->getStatus(), \trim($badge->getHexColor(), '#'), - \PUGX\Poser\Badge::DEFAULT_STYLE, + $badge->getStyle(), $badge->getFormat() ); diff --git a/src/Controller/Badge/AbstractBadgeController.php b/src/Controller/Badge/AbstractBadgeController.php index 381fac38..41f69023 100644 --- a/src/Controller/Badge/AbstractBadgeController.php +++ b/src/Controller/Badge/AbstractBadgeController.php @@ -14,7 +14,10 @@ use App\Badge\Infrastructure\ResponseFactory; use App\Badge\Model\CacheableBadge; use App\Badge\Service\ImageFactory; +use PUGX\Poser\Badge; +use PUGX\Poser\Poser; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; abstract class AbstractBadgeController extends AbstractController @@ -28,4 +31,14 @@ public function serveBadge(ImageFactory $imageFactory, CacheableBadge $cacheable $cacheableBadge->getSMaxAge() ); } + + public function checkStyle(Request $request, Poser $poser, string $style = Badge::DEFAULT_STYLE): string + { + $style = $request->query->get('style', $style); + if (!\in_array($style, $poser->validStyles(), true)) { + $style = Badge::DEFAULT_STYLE; + } + + return $style; + } } diff --git a/src/Controller/Badge/CircleCiController.php b/src/Controller/Badge/CircleCiController.php index b896ad22..662a9331 100644 --- a/src/Controller/Badge/CircleCiController.php +++ b/src/Controller/Badge/CircleCiController.php @@ -11,8 +11,10 @@ namespace App\Controller\Badge; +use App\Badge\Model\Badge; use App\Badge\Model\UseCase\CreateCircleCiBadge; use App\Badge\Service\ImageFactory; +use PUGX\Poser\Poser; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -23,26 +25,22 @@ final class CircleCiController extends AbstractBadgeController { /** * CircleCi action. - * - * @param string $repository - * @param string $branch - * @param string $format */ public function status( Request $request, + Poser $poser, ImageFactory $imageFactory, CreateCircleCiBadge $circleCiBadge, - $repository, - $branch = 'master', - $format = 'svg' + string $repository, + string $branch = 'master', + string $format = Badge::DEFAULT_FORMAT, + string $style = Badge::DEFAULT_STYLE, ): Response { - if ('plastic' === $request->query->get('format')) { - $format = 'plastic'; - } + $style = $this->checkStyle($request, $poser, $style); return $this->serveBadge( $imageFactory, - $circleCiBadge->createCircleCiBadge($repository, $branch, $format) + $circleCiBadge->createCircleCiBadge($repository, $branch, $format, $style) ); } } diff --git a/src/Controller/Badge/ComposerLockController.php b/src/Controller/Badge/ComposerLockController.php index f548c8eb..15de354f 100644 --- a/src/Controller/Badge/ComposerLockController.php +++ b/src/Controller/Badge/ComposerLockController.php @@ -11,8 +11,10 @@ namespace App\Controller\Badge; +use App\Badge\Model\Badge; use App\Badge\Model\UseCase\CreateComposerLockBadge; use App\Badge\Service\ImageFactory; +use PUGX\Poser\Poser; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use UnexpectedValueException; @@ -26,7 +28,6 @@ final class ComposerLockController extends AbstractBadgeController * ComposerLock action. * * @param string $repository repository - * @param string $format * * @throws \InvalidArgumentException * @throws UnexpectedValueException @@ -34,18 +35,18 @@ final class ComposerLockController extends AbstractBadgeController */ public function composerLock( Request $request, + Poser $poser, ImageFactory $imageFactory, CreateComposerLockBadge $composerLockBadge, - $repository, - $format = 'svg' + string $repository, + string $format = Badge::DEFAULT_FORMAT, + string $style = Badge::DEFAULT_STYLE, ): Response { - if ('plastic' === $request->query->get('format')) { - $format = 'plastic'; - } + $style = $this->checkStyle($request, $poser, $style); return $this->serveBadge( $imageFactory, - $composerLockBadge->createComposerLockBadge($repository, $format) + $composerLockBadge->createComposerLockBadge($repository, $format, $style) ); } } diff --git a/src/Controller/Badge/DependentsController.php b/src/Controller/Badge/DependentsController.php index 6135a6bd..f52e0daa 100644 --- a/src/Controller/Badge/DependentsController.php +++ b/src/Controller/Badge/DependentsController.php @@ -2,6 +2,7 @@ namespace App\Controller\Badge; +use App\Badge\Model\Badge; use App\Badge\Model\UseCase\CreateDependentsBadge; use App\Badge\Service\ImageFactory; use PUGX\Poser\Poser; @@ -16,15 +17,14 @@ public function dependents( CreateDependentsBadge $createDependentsBadge, ImageFactory $imageFactory, string $repository, - string $format = 'svg' + string $format = Badge::DEFAULT_FORMAT, + string $style = Badge::DEFAULT_STYLE, ): Response { - if (\in_array($request->query->get('format'), $poser->validStyles(), true)) { - $format = (string) $request->query->get('format'); - } + $style = $this->checkStyle($request, $poser, $style); return $this->serveBadge( $imageFactory, - $createDependentsBadge->createDependentsBadge($repository, $format) + $createDependentsBadge->createDependentsBadge($repository, $format, $style) ); } } diff --git a/src/Controller/Badge/DownloadsController.php b/src/Controller/Badge/DownloadsController.php index f96437d5..186195bd 100644 --- a/src/Controller/Badge/DownloadsController.php +++ b/src/Controller/Badge/DownloadsController.php @@ -11,6 +11,7 @@ namespace App\Controller\Badge; +use App\Badge\Model\Badge; use App\Badge\Model\UseCase\CreateDownloadsBadge; use App\Badge\Service\ImageFactory; use PUGX\Poser\Poser; @@ -32,15 +33,14 @@ public function downloads( ImageFactory $imageFactory, string $repository, string $type, - string $format = 'svg' + string $format = Badge::DEFAULT_FORMAT, + string $style = Badge::DEFAULT_STYLE, ): Response { - if (\in_array($request->query->get('format'), $poser->validStyles(), true)) { - $format = (string) $request->query->get('format'); - } + $style = $this->checkStyle($request, $poser, $style); return $this->serveBadge( $imageFactory, - $createDownloadsBadge->createDownloadsBadge($repository, $type, $format) + $createDownloadsBadge->createDownloadsBadge($repository, $type, $format, $style), ); } } diff --git a/src/Controller/Badge/GitAttributesController.php b/src/Controller/Badge/GitAttributesController.php index 95514464..cffa7141 100644 --- a/src/Controller/Badge/GitAttributesController.php +++ b/src/Controller/Badge/GitAttributesController.php @@ -11,8 +11,10 @@ namespace App\Controller\Badge; +use App\Badge\Model\Badge; use App\Badge\Model\UseCase\CreateGitAttributesBadge; use App\Badge\Service\ImageFactory; +use PUGX\Poser\Poser; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -24,18 +26,18 @@ final class GitAttributesController extends AbstractBadgeController */ public function gitAttributes( Request $request, + Poser $poser, CreateGitAttributesBadge $createGitAttributesBadge, ImageFactory $imageFactory, string $repository, - string $format = 'svg' + string $format = Badge::DEFAULT_FORMAT, + string $style = Badge::DEFAULT_STYLE, ): Response { - if ('plastic' === $request->query->get('format')) { - $format = 'plastic'; - } + $style = $this->checkStyle($request, $poser, $style); return $this->serveBadge( $imageFactory, - $createGitAttributesBadge->createGitAttributesBadge($repository, $format) + $createGitAttributesBadge->createGitAttributesBadge($repository, $format, $style) ); } } diff --git a/src/Controller/Badge/LicenseController.php b/src/Controller/Badge/LicenseController.php index b85f2216..1278c03a 100644 --- a/src/Controller/Badge/LicenseController.php +++ b/src/Controller/Badge/LicenseController.php @@ -11,6 +11,7 @@ namespace App\Controller\Badge; +use App\Badge\Model\Badge; use App\Badge\Model\UseCase\CreateLicenseBadge; use App\Badge\Service\ImageFactory; use PUGX\Poser\Poser; @@ -31,15 +32,14 @@ public function license( CreateLicenseBadge $createLicenseBadge, ImageFactory $imageFactory, string $repository, - string $format = 'svg' + string $format = Badge::DEFAULT_FORMAT, + string $style = Badge::DEFAULT_STYLE, ): Response { - if (\in_array($request->query->get('format'), $poser->validStyles(), true)) { - $format = (string) $request->query->get('format'); - } + $style = $this->checkStyle($request, $poser, $style); return $this->serveBadge( $imageFactory, - $createLicenseBadge->createLicenseBadge($repository, $format) + $createLicenseBadge->createLicenseBadge($repository, $format, $style), ); } } diff --git a/src/Controller/Badge/RequireController.php b/src/Controller/Badge/RequireController.php index a6e6433a..0d1bad76 100644 --- a/src/Controller/Badge/RequireController.php +++ b/src/Controller/Badge/RequireController.php @@ -2,6 +2,7 @@ namespace App\Controller\Badge; +use App\Badge\Model\Badge; use App\Badge\Model\UseCase\CreateRequireBadge; use App\Badge\Service\ImageFactory; use PUGX\Poser\Poser; @@ -17,15 +18,14 @@ public function require( ImageFactory $imageFactory, string $repository, string $type, - string $format = 'svg' + string $format = Badge::DEFAULT_FORMAT, + string $style = Badge::DEFAULT_STYLE, ): Response { - if (\in_array($request->query->get('format'), $poser->validStyles(), true)) { - $format = (string) $request->query->get('format'); - } + $style = $this->checkStyle($request, $poser, $style); return $this->serveBadge( $imageFactory, - $createRequireBadge->createRequireBadge($repository, $type, $format) + $createRequireBadge->createRequireBadge($repository, $type, $format, $style), ); } } diff --git a/src/Controller/Badge/SuggestersController.php b/src/Controller/Badge/SuggestersController.php index f447d5d6..b5f82600 100644 --- a/src/Controller/Badge/SuggestersController.php +++ b/src/Controller/Badge/SuggestersController.php @@ -2,6 +2,7 @@ namespace App\Controller\Badge; +use App\Badge\Model\Badge; use App\Badge\Model\UseCase\CreateSuggestersBadge; use App\Badge\Service\ImageFactory; use PUGX\Poser\Poser; @@ -16,15 +17,14 @@ public function suggesters( CreateSuggestersBadge $createSuggestersBadge, ImageFactory $imageFactory, string $repository, - string $format = 'svg' + string $format = Badge::DEFAULT_FORMAT, + string $style = Badge::DEFAULT_STYLE, ): Response { - if (\in_array($request->query->get('format'), $poser->validStyles(), true)) { - $format = (string) $request->query->get('format'); - } + $style = $this->checkStyle($request, $poser, $style); return $this->serveBadge( $imageFactory, - $createSuggestersBadge->createSuggestersBadge($repository, $format) + $createSuggestersBadge->createSuggestersBadge($repository, $format, $style), ); } } diff --git a/src/Controller/Badge/VersionController.php b/src/Controller/Badge/VersionController.php index 4fa450c4..6ae667c1 100644 --- a/src/Controller/Badge/VersionController.php +++ b/src/Controller/Badge/VersionController.php @@ -11,6 +11,7 @@ namespace App\Controller\Badge; +use App\Badge\Model\Badge; use App\Badge\Model\UseCase\CreateVersionBadge; use App\Badge\Service\ImageFactory; use PUGX\Poser\Poser; @@ -32,17 +33,15 @@ public function version( CreateVersionBadge $createVersionBadge, string $repository, string $latest, - string $format = 'svg' + string $format = Badge::DEFAULT_FORMAT, + string $style = Badge::DEFAULT_STYLE, ): Response { - if (\in_array($request->query->get('format'), $poser->validStyles(), true)) { - $format = $request->query->get('format'); - } - + $style = $this->checkStyle($request, $poser, $style); $function = 'create'.\ucfirst($latest).'Badge'; return $this->serveBadge( $imageFactory, - $createVersionBadge->{$function}($repository, $format) + $createVersionBadge->{$function}($repository, $format, $style) ); } } diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 83999480..796659c8 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -30,9 +30,8 @@ public function index( 'home/index.html.twig', [ 'repository' => $repository, - 'badges' => $generator->generateAllSnippets($repository), + 'badges' => $generator->generateAllSnippets($poser, $repository), 'badges_served_svg' => $poser->generate($prefix, $text, 'CC0066', 'flat'), - 'formats' => $formats, 'contributors' => $contributors, ] ); diff --git a/src/Controller/SnippetController.php b/src/Controller/SnippetController.php index c7989403..231d3602 100644 --- a/src/Controller/SnippetController.php +++ b/src/Controller/SnippetController.php @@ -4,6 +4,7 @@ use App\Service\SnippetGeneratorInterface; use GuzzleHttp\Exception\BadResponseException; +use PUGX\Poser\Poser; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -15,7 +16,7 @@ final class SnippetController extends AbstractController * * @throws \Exception */ - public function all(Request $request, SnippetGeneratorInterface $generator): JsonResponse + public function all(Request $request, Poser $poser, SnippetGeneratorInterface $generator): JsonResponse { $repository = $request->get('repository'); $response = new JsonResponse(); @@ -28,7 +29,7 @@ public function all(Request $request, SnippetGeneratorInterface $generator): Jso } try { - $badges = $generator->generateAllSnippets($repository); + $badges = $generator->generateAllSnippets($poser, $repository); $response->setData($badges); } catch (BadResponseException) { $response->setData(['msg' => 'Package not found. Please check the package name. e.g. (symfony/symfony)']); diff --git a/src/Service/SnippetGenerator.php b/src/Service/SnippetGenerator.php index 2dc22cbf..8dfd2d40 100644 --- a/src/Service/SnippetGenerator.php +++ b/src/Service/SnippetGenerator.php @@ -2,6 +2,7 @@ namespace App\Service; +use PUGX\Poser\Poser; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouterInterface; @@ -25,7 +26,7 @@ public function __construct( /** * @throws \Exception */ - public function generateAllSnippets(string $repository): array + public function generateAllSnippets(Poser $poser, string $repository): array { $repoLink = $this->generateRepositoryLink($repository); @@ -51,6 +52,27 @@ public function generateAllSnippets(string $repository): array $snippets['all']['markdown'] = \trim($snippets['all']['markdown']); + $badge = [ + 'name' => 'latest_stable_version', + 'route' => 'pugx_badge_version_latest', + ]; + $validStyles = $poser->validStyles(); + \sort($validStyles); + foreach ($validStyles as $style) { + $badge['style'] = $style; + $badge['label'] = $style; + $img = $this->generateImg($badge, $repository); + $markdown = $this->generateMarkdown($badge, $img, $repoLink); + $snippets['badge_styles'][] = [ + 'name' => $badge['name'], + 'label' => $badge['label'], + 'markdown' => $markdown, + 'img' => $img, + 'imgLink' => $repoLink, + 'featured' => false, + ]; + } + return $snippets; } diff --git a/src/Service/SnippetGeneratorInterface.php b/src/Service/SnippetGeneratorInterface.php index 1882662e..f9fb15a9 100644 --- a/src/Service/SnippetGeneratorInterface.php +++ b/src/Service/SnippetGeneratorInterface.php @@ -2,10 +2,12 @@ namespace App\Service; +use PUGX\Poser\Poser; + interface SnippetGeneratorInterface { /** * @return array */ - public function generateAllSnippets(string $repository): array; + public function generateAllSnippets(Poser $poser, string $repository): array; } diff --git a/symfony.lock b/symfony.lock index 52c07bca..03a4e4a3 100644 --- a/symfony.lock +++ b/symfony.lock @@ -86,6 +86,9 @@ "jean85/pretty-package-versions": { "version": "1.2" }, + "kartsims/easysvg": { + "version": "2.4" + }, "knplabs/github-api": { "version": "2.6", "recipe": { @@ -119,18 +122,12 @@ "nikic/php-parser": { "version": "v4.13.1" }, - "ocramius/package-versions": { - "version": "1.8.0" - }, "phar-io/manifest": { "version": "2.0.3" }, "phar-io/version": { "version": "3.1.0" }, - "php": { - "version": "8.0" - }, "php-cs-fixer/diff": { "version": "v1.3.0" }, diff --git a/templates/home/index.html.twig b/templates/home/index.html.twig index e76750e8..c34c5b52 100644 --- a/templates/home/index.html.twig +++ b/templates/home/index.html.twig @@ -91,12 +91,33 @@ + + + + {% endfor %} + + + + +
+
+

Styles

+
+ {% for badge in badges.badge_styles %} +
+

{{ badge.label }}

+ ?style={{ badge.label }} + + + + + class="badge-input" + data-badge="{{ badge.name }}" + readonly + type="text" + value="{{ badge.markdown }}" title="{{ badge.label }}">
{% endfor %} diff --git a/tests/Badge/Model/BadgeTest.php b/tests/Badge/Model/BadgeTest.php index 2293faa4..05393f4a 100644 --- a/tests/Badge/Model/BadgeTest.php +++ b/tests/Badge/Model/BadgeTest.php @@ -10,16 +10,16 @@ final class BadgeTest extends TestCase { public function testCreation(): void { - $badge = new Badge('sub', 'status', 'FFFFFF', 'svg'); + $badge = new Badge('sub', 'status', 'FFFFFF', 'svg', 'flat'); - self::assertEquals('sub-status-FFFFFF.svg', (string) $badge); + self::assertEquals('sub-status-FFFFFF-flat.svg', (string) $badge); } public function testDashesAndUnderscores(): void { - $badge = new Badge('su--b', 'st-a_tu__s', 'FFFFFF', 'svg'); + $badge = new Badge('su--b', 'st-a_tu__s', 'FFFFFF', 'svg', 'flat'); - self::assertEquals('su-b-st-a tu_s-FFFFFF.svg', (string) $badge); + self::assertEquals('su-b-st-a tu_s-FFFFFF-flat.svg', (string) $badge); } public function testShouldUseDefaultImageFormat(): void @@ -29,6 +29,13 @@ public function testShouldUseDefaultImageFormat(): void self::assertEquals('svg', $badge->getFormat()); } + public function testShouldUseDefaultImageStyle(): void + { + $badge = new Badge('sub', 'status', 'FFFFFF'); + + self::assertEquals('flat', $badge->getStyle()); + } + /** @dataProvider invalidColorProvider */ public function testShouldThrowExceptionForInvalidColors(string $color): void { diff --git a/tests/Badge/Model/UseCase/CreateDownloadsBadgeTest.php b/tests/Badge/Model/UseCase/CreateDownloadsBadgeTest.php index 609fc6ac..593e73f9 100644 --- a/tests/Badge/Model/UseCase/CreateDownloadsBadgeTest.php +++ b/tests/Badge/Model/UseCase/CreateDownloadsBadgeTest.php @@ -46,7 +46,7 @@ public function testShouldCreateDownloadsBadge(): void $repository = 'PUGX/badge-poser'; - $badge = $this->useCase->createDownloadsBadge($repository, 'daily', 'svg'); + $badge = $this->useCase->createDownloadsBadge($repository, 'daily', 'svg', 'flat'); self::assertEquals('102 today', $badge->getStatus()); } @@ -58,7 +58,7 @@ public function testShouldCreateDefaultBadgeOnError(): void ->will(self::throwException(new \RuntimeException())); $repository = 'PUGX/badge-poser'; - $badge = $this->useCase->createDownloadsBadge($repository, 'daily', 'svg'); + $badge = $this->useCase->createDownloadsBadge($repository, 'daily', 'svg', 'flat'); self::assertEquals(' - ', $badge->getSubject()); self::assertEquals(' - ', $badge->getStatus()); diff --git a/tests/Service/SnippetGeneratorStub.php b/tests/Service/SnippetGeneratorStub.php index 646cbb11..82cc80bc 100644 --- a/tests/Service/SnippetGeneratorStub.php +++ b/tests/Service/SnippetGeneratorStub.php @@ -3,10 +3,11 @@ namespace App\Tests\Service; use App\Service\SnippetGeneratorInterface; +use PUGX\Poser\Poser; final class SnippetGeneratorStub implements SnippetGeneratorInterface { - public function generateAllSnippets(string $repository): array + public function generateAllSnippets(Poser $poser, string $repository): array { return [ 'all' => [ @@ -30,6 +31,24 @@ public function generateAllSnippets(string $repository): array 'featured' => false, ], ], + 'badge_styles' => [ + [ + 'name' => 'latest_stable_version', + 'label' => 'flat', + 'markdown' => '[![flat](http://poser.local:8001/phpunit/phpunit/v)](https://packagist.org/packages/phpunit/phpunit)', + 'img' => 'http://poser.local:8001/phpunit/phpunit/v', + 'imgLink' => 'https://packagist.org/packages/phpunit/phpunit', + 'featured' => false, + ], + [ + 'name' => 'latest_stable_version', + 'label' => 'for-the-badge', + 'markdown' => '[![for-the-badge](http://poser.local:8001/phpunit/phpunit/v?style=for-the-badge)](https://packagist.org/packages/phpunit/phpunit)', + 'img' => 'http://poser.local:8001/phpunit/phpunit/v?style=for-the-badge', + 'imgLink' => 'https://packagist.org/packages/phpunit/phpunit', + 'featured' => false, + ], + ], ]; } } diff --git a/tests/Service/SnippetGeneratorTest.php b/tests/Service/SnippetGeneratorTest.php index a7d254c2..6ddb7965 100644 --- a/tests/Service/SnippetGeneratorTest.php +++ b/tests/Service/SnippetGeneratorTest.php @@ -4,6 +4,7 @@ use App\Service\SnippetGenerator; use PHPUnit\Framework\TestCase; +use PUGX\Poser\Poser; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouterInterface; @@ -85,6 +86,10 @@ public function testGenerateAllSnippetsWithFeaturedBadges(): void ) ->willReturnOnConsecutiveCalls('repo_url', 'img_url', 'img_url2'); + $poser = $this->getMockBuilder(Poser::class) + ->disableOriginalConstructor() + ->getMock(); + $generator = new SnippetGenerator($router, ['badge_1_name', 'badge_2_name'], $badges); $expected = [ @@ -111,7 +116,7 @@ public function testGenerateAllSnippetsWithFeaturedBadges(): void ], ]; - self::assertEquals($expected, $generator->generateAllSnippets('vendor/package')); + self::assertEquals($expected, $generator->generateAllSnippets($poser, 'vendor/package')); } public function testGenerateAllSnippetsWithoutFeaturedBadges(): void @@ -164,6 +169,10 @@ public function testGenerateAllSnippetsWithoutFeaturedBadges(): void ) ->willReturnOnConsecutiveCalls('repo_url', 'img_url'); + $poser = $this->getMockBuilder(Poser::class) + ->disableOriginalConstructor() + ->getMock(); + $generator = new SnippetGenerator($router, [], $badges); $expected = [ @@ -182,7 +191,7 @@ public function testGenerateAllSnippetsWithoutFeaturedBadges(): void ], ]; - self::assertEquals($expected, $generator->generateAllSnippets('vendor/package')); + self::assertEquals($expected, $generator->generateAllSnippets($poser, 'vendor/package')); } public function testWontGenerateAllSnippetsIfBadgeRouteDoesNotExists(): void @@ -216,9 +225,13 @@ public function testWontGenerateAllSnippetsIfBadgeRouteDoesNotExists(): void ) ->willReturn('repo_url'); + $poser = $this->getMockBuilder(Poser::class) + ->disableOriginalConstructor() + ->getMock(); + $this->expectException(\RuntimeException::class); $generator = new SnippetGenerator($router, [], $badges); - $generator->generateAllSnippets('vendor/package'); + $generator->generateAllSnippets($poser, 'vendor/package'); } }