From 2dd74f4a23131adfbe014e8260446850ce9a3c0c Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 6 Nov 2024 12:09:25 -0500 Subject: [PATCH 1/4] Adding WordPress URL generator --- docs/url-generators.md | 1 + src/Url/WordPressGenerator.php | 74 ++++++++++++++++++++++++++++ tests/Url/WordPressGeneratorTest.php | 61 +++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/Url/WordPressGenerator.php create mode 100644 tests/Url/WordPressGeneratorTest.php diff --git a/docs/url-generators.md b/docs/url-generators.md index 7987117..bddf08a 100644 --- a/docs/url-generators.md +++ b/docs/url-generators.md @@ -6,6 +6,7 @@ URL generators are used to generate URLs for the packages listed in a diff: - `GitLabGenerator`: Generates URLs for GitLab repositories. Supports custom domains. - `BitbucketGenerator`: Generates URLs for Bitbucket repositories. - `DrupalGenerator`: Generates URLs for Drupal packages. +- `WordPress`: Generates URLs for WordPress plugins and themes (via [WordPress Packagist](https://wpackagist.org/)). They are chosen automatically based on the package URL or other conditions specified in `supportsPackage()` method. diff --git a/src/Url/WordPressGenerator.php b/src/Url/WordPressGenerator.php new file mode 100644 index 0000000..975338e --- /dev/null +++ b/src/Url/WordPressGenerator.php @@ -0,0 +1,74 @@ +getName(), 'wpackagist-plugin/') || 0 === strpos($package->getName(), 'wpackagist-theme/'); + } + + /** + * Generates a compare URL for two versions of the same package. + * + * @return string|null + */ + public function getCompareUrl(PackageInterface $initialPackage, PackageInterface $targetPackage) + { + return null; + } + + /** + * Generates URL for viewing a release or commit of a package. + * + * @return string|null + */ + public function getReleaseUrl(PackageInterface $package) + { + return null; + } + + /** + * Generates URL for viewing the project page of a package (usually repository root). + * + * @return string|null + */ + public function getProjectUrl(PackageInterface $package) + { + $type = $this->getPackageType($package); + + if (null === $type) { + return null; + } + + return sprintf('https://wordpress.org/%ss/%s', $type, $this->getPackageSlug($package)); + } + + /** + * @return string|null + */ + protected function getPackageType(PackageInterface $package) + { + [$type] = explode('/', $package->getName(), 2); + + return 0 === strpos($type, 'wpackagist-') ? substr($type, 11) : null; + } + + /** + * @return string + */ + protected function getPackageSlug(PackageInterface $package) + { + [, $slug] = explode('/', $package->getName(), 2); + + return $slug; + } +} diff --git a/tests/Url/WordPressGeneratorTest.php b/tests/Url/WordPressGeneratorTest.php new file mode 100644 index 0000000..cc4809d --- /dev/null +++ b/tests/Url/WordPressGeneratorTest.php @@ -0,0 +1,61 @@ + array( + $this->getPackageWithSource('wpackagist-plugin/jetpack', '13.1', 'https://plugins.svn.wordpress.org/jetpack/', '13.1'), + null, + ), + 'theme' => array( + $this->getPackageWithSource('wpackagist-theme/twentytwenty', '1.7', 'https://themes.svn.wordpress.org/twentytwenty/', '1.7'), + null, + ), + ); + } + + public function projectUrlProvider() + { + return array( + 'plugin' => array( + $this->getPackageWithSource('wpackagist-plugin/jetpack', '13.1', 'https://plugins.svn.wordpress.org/jetpack/', '13.1'), + 'https://wordpress.org/plugins/jetpack', + ), + 'theme' => array( + $this->getPackageWithSource('wpackagist-theme/twentytwenty', '1.7', 'https://themes.svn.wordpress.org/twentytwenty/', '1.7'), + 'https://wordpress.org/themes/twentytwenty', + ), + ); + } + + public function compareUrlProvider() + { + return array( + 'plugin' => array( + $this->getPackageWithSource('wpackagist-plugin/jetpack', '13.1', 'https://plugins.svn.wordpress.org/jetpack/', '13.1'), + $this->getPackageWithSource('wpackagist-plugin/jetpack', '13.2', 'https://plugins.svn.wordpress.org/jetpack/', '13.2'), + null, + ), + 'theme' => array( + $this->getPackageWithSource('wpackagist-theme/twentytwenty', '1.7', 'https://themes.svn.wordpress.org/twentytwenty/', '1.7'), + $this->getPackageWithSource('wpackagist-theme/twentytwenty', '1.8', 'https://themes.svn.wordpress.org/twentytwenty/', '1.8'), + null, + ), + ); + } + + /** + * {@inheritdoc} + */ + protected function getGenerator() + { + return new WordPressGenerator(); + } +} From 85e28c37423d944368c47361e8796b0160ae1002 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 6 Nov 2024 12:12:57 -0500 Subject: [PATCH 2/4] Include generator --- src/Url/GeneratorContainer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Url/GeneratorContainer.php b/src/Url/GeneratorContainer.php index 0d204c0..c5b610e 100644 --- a/src/Url/GeneratorContainer.php +++ b/src/Url/GeneratorContainer.php @@ -21,6 +21,7 @@ public function __construct(array $gitlabDomains = array()) new GithubGenerator(), new BitBucketGenerator(), new GitlabGenerator(), + new WordPressGenerator(), ); foreach ($gitlabDomains as $domain) { From 6aca3b0698be858325a67fb16d758b05b498e8e1 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 6 Nov 2024 12:15:51 -0500 Subject: [PATCH 3/4] Style CI --- tests/Url/WordPressGeneratorTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Url/WordPressGeneratorTest.php b/tests/Url/WordPressGeneratorTest.php index cc4809d..8d352a7 100644 --- a/tests/Url/WordPressGeneratorTest.php +++ b/tests/Url/WordPressGeneratorTest.php @@ -2,7 +2,6 @@ namespace IonBazan\ComposerDiff\Tests\Url; -use Composer\Package\PackageInterface; use IonBazan\ComposerDiff\Url\WordPressGenerator; class WordPressGeneratorTest extends GeneratorTest From 844bd110a84340adcf28cd107e359ae3161cac19 Mon Sep 17 00:00:00 2001 From: Ion Bazan Date: Thu, 7 Nov 2024 11:42:02 +0800 Subject: [PATCH 4/4] Fix compatibility issues and add test --- src/Url/WordPressGenerator.php | 44 ++++++---------------------- tests/Url/WordPressGeneratorTest.php | 10 +++++++ 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/Url/WordPressGenerator.php b/src/Url/WordPressGenerator.php index 975338e..8fc1691 100644 --- a/src/Url/WordPressGenerator.php +++ b/src/Url/WordPressGenerator.php @@ -7,19 +7,15 @@ class WordPressGenerator implements UrlGenerator { /** - * Determines if the generator supports the given package. - * - * @return bool + * {@inheritdoc} */ public function supportsPackage(PackageInterface $package) { - return 0 === strpos($package->getName(), 'wpackagist-plugin/') || 0 === strpos($package->getName(), 'wpackagist-theme/'); + return (bool) preg_match('#^wpackagist-(plugin|theme)/#', $package->getName()); } /** - * Generates a compare URL for two versions of the same package. - * - * @return string|null + * {@inheritdoc} */ public function getCompareUrl(PackageInterface $initialPackage, PackageInterface $targetPackage) { @@ -27,9 +23,7 @@ public function getCompareUrl(PackageInterface $initialPackage, PackageInterface } /** - * Generates URL for viewing a release or commit of a package. - * - * @return string|null + * {@inheritdoc} */ public function getReleaseUrl(PackageInterface $package) { @@ -37,38 +31,18 @@ public function getReleaseUrl(PackageInterface $package) } /** - * Generates URL for viewing the project page of a package (usually repository root). - * - * @return string|null + * {@inheritdoc} */ public function getProjectUrl(PackageInterface $package) { - $type = $this->getPackageType($package); + preg_match('#wpackagist-(plugin|theme)/(.+)#', $package->getName(), $matches); - if (null === $type) { + if (empty($matches)) { return null; } - return sprintf('https://wordpress.org/%ss/%s', $type, $this->getPackageSlug($package)); - } - - /** - * @return string|null - */ - protected function getPackageType(PackageInterface $package) - { - [$type] = explode('/', $package->getName(), 2); - - return 0 === strpos($type, 'wpackagist-') ? substr($type, 11) : null; - } - - /** - * @return string - */ - protected function getPackageSlug(PackageInterface $package) - { - [, $slug] = explode('/', $package->getName(), 2); + list (, $type, $slug) = $matches; - return $slug; + return sprintf('https://wordpress.org/%ss/%s', $type, $slug); } } diff --git a/tests/Url/WordPressGeneratorTest.php b/tests/Url/WordPressGeneratorTest.php index 8d352a7..5d31f06 100644 --- a/tests/Url/WordPressGeneratorTest.php +++ b/tests/Url/WordPressGeneratorTest.php @@ -6,6 +6,16 @@ class WordPressGeneratorTest extends GeneratorTest { + public function testItSupportsOnlyWpackagistPackages() + { + $generator = $this->getGenerator(); + + $this->assertFalse($generator->supportsPackage($this->getPackage('acme/package', '3.12.1'))); + $this->assertTrue($generator->supportsPackage($this->getPackage('wpackagist-plugin/my-plugin', '3.12.1'))); + $this->assertTrue($generator->supportsPackage($this->getPackage('wpackagist-theme/my-theme', '3.12.1'))); + $this->assertFalse($generator->supportsPackage($this->getPackage('acme-wpackagist-theme/my-theme', '3.12.1'))); + } + public function releaseUrlProvider() { return array(