Skip to content

Commit

Permalink
Refactories the version converter
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Jul 3, 2014
1 parent 1f71599 commit eb96a97
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 25 deletions.
14 changes: 7 additions & 7 deletions Converter/SemverConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class SemverConverter implements VersionConverterInterface
/**
* {@inheritdoc}
*/
public static function convertVersion($version)
public function convertVersion($version)
{
if (preg_match_all(static::createPattern('([a-z]+|(\-|\+)[a-z]+|(\-|\+)[0-9]+)'),
if (preg_match_all($this->createPattern('([a-z]+|(\-|\+)[a-z]+|(\-|\+)[0-9]+)'),
$version, $matches, PREG_OFFSET_CAPTURE)) {
$end = substr($version, strlen($matches[1][0][0]));
$version = $matches[1][0][0] . '-';
Expand Down Expand Up @@ -82,7 +82,7 @@ public static function convertVersion($version)
/**
* {@inheritdoc}
*/
public static function convertRange($range)
public function convertRange($range)
{
foreach (array('<', '>', '=', '~', '^', '||') as $character) {
$range = str_replace($character . ' ', $character, $range);
Expand Down Expand Up @@ -116,7 +116,7 @@ public static function convertRange($range)
break;
default:
if ('~' === $special) {
$newMatch = '>='.static::convertVersion($match).',<';
$newMatch = '>='.$this->convertVersion($match).',<';
$exp = explode('.', $match);
$upVersion = isset($exp[0]) ? $exp[0] : '0';

Expand All @@ -126,10 +126,10 @@ public static function convertRange($range)
$upVersion .= '.1';
}

$newMatch .= static::convertVersion($upVersion);
$newMatch .= $this->convertVersion($upVersion);
$matches[$i] = $newMatch;
} else {
$matches[$i] = static::convertVersion($match);
$matches[$i] = $this->convertVersion($match);
}
$special = null;
break;
Expand All @@ -146,7 +146,7 @@ public static function convertRange($range)
*
* @return string The full pattern with '/'
*/
protected static function createPattern($pattern)
protected function createPattern($pattern)
{
$numVer = '([0-9]+|\x|\*)';
$numVer2 = '(' . $numVer . '\.' . $numVer . ')';
Expand Down
4 changes: 2 additions & 2 deletions Converter/VersionConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface VersionConverterInterface
*
* @return string The composer version
*/
public static function convertVersion($version);
public function convertVersion($version);

/**
* Converts the range asset version to range composer version.
Expand All @@ -34,5 +34,5 @@ public static function convertVersion($version);
*
* @return string The range composer version
*/
public static function convertRange($range);
public function convertRange($range);
}
7 changes: 3 additions & 4 deletions Repository/AssetVcsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Composer\Repository\Vcs\VcsDriverInterface;
use Composer\Repository\VcsRepository;
use Fxp\Composer\AssetPlugin\Assets;
use Fxp\Composer\AssetPlugin\Converter\SemverConverter;
use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface;

/**
Expand Down Expand Up @@ -129,11 +128,11 @@ protected function initialize()

// manually versioned package
if (isset($data['version'])) {
$data['version'] = SemverConverter::convertVersion($data['version']);
$data['version'] = $this->assetType->getVersionConverter()->convertVersion($data['version']);
$data['version_normalized'] = $this->versionParser->normalize($data['version']);
} else {
// auto-versioned package, read value from tag
$data['version'] = SemverConverter::convertVersion($tag);
$data['version'] = $this->assetType->getVersionConverter()->convertVersion($tag);
$data['version_normalized'] = $parsedTag;
}

Expand Down Expand Up @@ -288,7 +287,7 @@ private function validateBranch($branch)
private function validateTag($version)
{
try {
$version = SemverConverter::convertVersion($version);
$version = $this->assetType->getVersionConverter()->convertVersion($version);

return $this->versionParser->normalize($version);
} catch (\Exception $e) {
Expand Down
20 changes: 18 additions & 2 deletions Tests/Converter/SemverConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Fxp\Composer\AssetPlugin\Tests\Converter;

use Fxp\Composer\AssetPlugin\Converter\SemverConverter;
use Fxp\Composer\AssetPlugin\Converter\VersionConverterInterface;

/**
* Tests for the conversion of Semver syntax to composer syntax.
Expand All @@ -20,12 +21,27 @@
*/
class SemverConverterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var VersionConverterInterface
*/
protected $converter;

protected function setUp()
{
$this->converter = new SemverConverter();
}

protected function tearDown()
{
$this->converter = null;
}

/**
* @dataProvider getTestVersions
*/
public function testConverter($semver, $composer)
{
$this->assertEquals($composer, SemverConverter::convertVersion($semver));
$this->assertEquals($composer, $this->converter->convertVersion($semver));
}

public function getTestVersions()
Expand Down Expand Up @@ -70,7 +86,7 @@ public function getTestVersions()
*/
public function testRangeConverter($semver, $composer)
{
$this->assertEquals($composer, SemverConverter::convertRange($semver));
$this->assertEquals($composer, $this->converter->convertRange($semver));
}

public function getTestRanges()
Expand Down
21 changes: 21 additions & 0 deletions Type/AbstractAssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@

namespace Fxp\Composer\AssetPlugin\Type;

use Fxp\Composer\AssetPlugin\Converter\SemverConverter;
use Fxp\Composer\AssetPlugin\Converter\VersionConverterInterface;

/**
* Abstract asset type.
*
* @author François Pluchino <francois.pluchino@gmail.com>
*/
abstract class AbstractAssetType implements AssetTypeInterface
{
/**
* @var VersionConverterInterface
*/
protected $versionConverter;

public function __construct()
{
$this->versionConverter = new SemverConverter();
}

/**
* {@inheritdoc}
*/
Expand All @@ -33,4 +46,12 @@ public function getFilename()
{
return $this->getName() . '.json';
}

/**
* {@inheritdoc}
*/
public function getVersionConverter()
{
return $this->versionConverter;
}
}
9 changes: 9 additions & 0 deletions Type/AssetTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Fxp\Composer\AssetPlugin\Type;

use Fxp\Composer\AssetPlugin\Converter\VersionConverterInterface;

/**
* Asset type interface.
*
Expand Down Expand Up @@ -39,6 +41,13 @@ public function getComposerVendorName();
*/
public function getFilename();

/**
* Gets the version converter.
*
* @return VersionConverterInterface
*/
public function getVersionConverter();

/**
* Converts the asset data package to composer data package.
*
Expand Down
8 changes: 3 additions & 5 deletions Type/BowerAssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Fxp\Composer\AssetPlugin\Type;

use Fxp\Composer\AssetPlugin\Converter\SemverConverter;

/**
* Bower asset type.
*
Expand All @@ -36,7 +34,7 @@ public function convert(array $data)
$package = array(
'name' => $this->getComposerVendorName() . '/' . $data['name'],
'type' => "bower-asset-library",
'version' => SemverConverter::convertVersion($data['version']),
'version' => $this->getVersionConverter()->convertVersion($data['version']),
);

if (isset($data['description'])) {
Expand All @@ -55,7 +53,7 @@ public function convert(array $data)
$package['require'] = array();

foreach ($data['dependencies'] as $dependency => $version) {
$version = SemverConverter::convertRange($version);
$version = $this->getVersionConverter()->convertRange($version);
$package['require'][$this->getComposerVendorName() . '/' . $dependency] = $version;
}
}
Expand All @@ -64,7 +62,7 @@ public function convert(array $data)
$package['require-dev'] = array();

foreach ($data['devDependencies'] as $dependency => $version) {
$version = SemverConverter::convertRange($version);
$version = $this->getVersionConverter()->convertRange($version);
$package['require-dev'][$this->getComposerVendorName() . '/' . $dependency] = $version;
}
}
Expand Down
8 changes: 3 additions & 5 deletions Type/NpmAssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Fxp\Composer\AssetPlugin\Type;

use Fxp\Composer\AssetPlugin\Converter\SemverConverter;

/**
* NPM asset type.
*
Expand Down Expand Up @@ -44,7 +42,7 @@ public function convert(array $data)
$package = array(
'name' => $this->getComposerVendorName() . '/' . $data['name'],
'type' => "npm-asset-library",
'version' => SemverConverter::convertVersion($data['version']),
'version' => $this->getVersionConverter()->convertVersion($data['version']),
);

if (isset($data['description'])) {
Expand Down Expand Up @@ -83,7 +81,7 @@ public function convert(array $data)
$package['require'] = array();

foreach ($data['dependencies'] as $dependency => $version) {
$version = SemverConverter::convertRange($version);
$version = $this->getVersionConverter()->convertRange($version);
$package['require'][$this->getComposerVendorName() . '/' . $dependency] = $version;
}
}
Expand All @@ -92,7 +90,7 @@ public function convert(array $data)
$package['require-dev'] = array();

foreach ($data['devDependencies'] as $dependency => $version) {
$version = SemverConverter::convertRange($version);
$version = $this->getVersionConverter()->convertRange($version);
$package['require-dev'][$this->getComposerVendorName() . '/' . $dependency] = $version;
}
}
Expand Down

0 comments on commit eb96a97

Please sign in to comment.