Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding WordPress Package Link Generator #36

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/url-generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions src/Url/GeneratorContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(array $gitlabDomains = array())
new GithubGenerator(),
new BitBucketGenerator(),
new GitlabGenerator(),
new WordPressGenerator(),
);

foreach ($gitlabDomains as $domain) {
Expand Down
48 changes: 48 additions & 0 deletions src/Url/WordPressGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace IonBazan\ComposerDiff\Url;

use Composer\Package\PackageInterface;

class WordPressGenerator implements UrlGenerator
{
/**
* {@inheritdoc}
*/
public function supportsPackage(PackageInterface $package)
{
return (bool) preg_match('#^wpackagist-(plugin|theme)/#', $package->getName());
}

/**
* {@inheritdoc}
*/
public function getCompareUrl(PackageInterface $initialPackage, PackageInterface $targetPackage)
{
return null;
}

/**
* {@inheritdoc}
*/
public function getReleaseUrl(PackageInterface $package)
{
return null;
}

/**
* {@inheritdoc}
*/
public function getProjectUrl(PackageInterface $package)
{
preg_match('#wpackagist-(plugin|theme)/(.+)#', $package->getName(), $matches);

if (empty($matches)) {
return null;

Check warning on line 41 in src/Url/WordPressGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/Url/WordPressGenerator.php#L41

Added line #L41 was not covered by tests
}

list (, $type, $slug) = $matches;

return sprintf('https://wordpress.org/%ss/%s', $type, $slug);
}
}
70 changes: 70 additions & 0 deletions tests/Url/WordPressGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace IonBazan\ComposerDiff\Tests\Url;

use IonBazan\ComposerDiff\Url\WordPressGenerator;

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(
'plugin' => 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();
}
}