Skip to content

Commit

Permalink
Refactories the package converter
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Jul 3, 2014
1 parent eb96a97 commit 28b3952
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 212 deletions.
142 changes: 142 additions & 0 deletions Converter/AbstractPackageConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

/*
* This file is part of the Fxp Composer Asset Plugin package.
*
* (c) François Pluchino <francois.pluchino@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fxp\Composer\AssetPlugin\Converter;

use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface;

/**
* Abstract class for converter for asset package to composer package.
*
* @author François Pluchino <francois.pluchino@gmail.com>
*/
abstract class AbstractPackageConverter implements PackageConverterInterface
{
/**
* @var AssetTypeInterface
*/
protected $assetType;

/**
* Constructor.
*
* @param AssetTypeInterface $assetType
*/
public function __construct(AssetTypeInterface $assetType)
{
$this->assetType = $assetType;
}

/**
* Converts the all keys (keys, dependencies and extra keys).
*
* @param array $asset The asset data
* @param array $keys The map of asset key and composer key
* @param array $dependencies The map of asset dependency key and composer dependency key
* @param array $extras The map of asset key and composer extra key
*
* @return array The composer package converted
*/
protected function convertData(array $asset, array $keys, array $dependencies, array $extras)
{
$composer = array();

foreach ($keys as $assetKey => $composerKey) {
$this->convertKey($asset, $assetKey, $composer, $composerKey);
}

foreach ($dependencies as $assetKey => $composerKey) {
$this->convertDependencies($asset, $assetKey, $composer, $composerKey);
}

foreach ($extras as $assetKey => $composerKey) {
$this->convertExtraKey($asset, $assetKey, $composer, $composerKey);
}

return $composer;
}

/**
* Converts the simple key of package.
*
* @param array $asset The asset data
* @param string $assetKey The asset key
* @param array &$composer The composer data
* @param string|array $composerKey The composer key or array with composer key name and closure
*
* @throws \InvalidArgumentException When the 'composerKey' argument of asset packager converter is not an string or an array with the composer key and closure
*/
protected function convertKey(array $asset, $assetKey, array &$composer, $composerKey)
{
if (is_string($composerKey)) {
if (isset($asset[$assetKey])) {
$composer[$composerKey] = $asset[$assetKey];
}

} elseif (is_array($composerKey) && 2 === count($composerKey)
&& is_string($composerKey[0]) && $composerKey[1] instanceof \Closure) {
$closure = $composerKey[1];
$composerKey = $composerKey[0];
$data = isset($asset[$assetKey]) ? $asset[$assetKey] : null;
$previousData = isset($composer[$composerKey]) ? $composer[$composerKey] : null;
$data = $closure($data, $previousData);

if (null !== $data) {
$composer[$composerKey] = $data;
}

} else {
throw new \InvalidArgumentException('The "composerKey" argument of asset packager converter must be an string or an array with the composer key and closure');
}
}

/**
* Converts the extra key of package.
*
* @param array $asset The asset data
* @param string $assetKey The asset extra key
* @param array &$composer The composer data
* @param string|array $composerKey The composer extra key or array with composer extra key name and closure
* @param string $extraKey The extra key name
*/
protected function convertExtraKey(array $asset, $assetKey, array &$composer, $composerKey, $extraKey = 'extra')
{
$extra = isset($composer[$extraKey]) ? $composer[$extraKey] : array();

$this->convertKey($asset, $assetKey, $extra, $composerKey);

if (count($extra) > 0) {
$composer[$extraKey] = $extra;
}
}

/**
* Converts simple key of package.
*
* @param array $asset The asset data
* @param string $assetKey The asset key of dependencies
* @param array &$composer The composer data
* @param string $composerKey The composer key of dependencies
*/
protected function convertDependencies(array $asset, $assetKey, array &$composer, $composerKey)
{
if (isset($asset[$assetKey]) && is_array($asset[$assetKey])) {
$newDependencies = array();

foreach ($asset[$assetKey] as $dependency => $version) {
$version = $this->assetType->getVersionConverter()->convertRange($version);
$newDependencies[$this->assetType->getComposerVendorName() . '/' . $dependency] = $version;
}

$composer[$composerKey] = $newDependencies;
}
}
}
54 changes: 54 additions & 0 deletions Converter/BowerPackageConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Fxp Composer Asset Plugin package.
*
* (c) François Pluchino <francois.pluchino@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fxp\Composer\AssetPlugin\Converter;

/**
* Converter for bower package to composer package.
*
* @author François Pluchino <francois.pluchino@gmail.com>
*/
class BowerPackageConverter extends AbstractPackageConverter
{
/**
* {@inheritdoc}
*/
public function convert(array $data)
{
$assetType = $this->assetType;
$keys = array(
'name' => array('name', function ($value) use ($assetType) {
return $assetType->getComposerVendorName() . '/' . $value;
}),
'type' => array('type', function () {
return 'bower-asset-library';
}),
'version' => array('version', function ($value) use ($assetType) {
return $assetType->getVersionConverter()->convertVersion($value);
}),
'description' => 'description',
'keywords' => 'keywords',
'license' => 'license',
'bin' => 'bin',
);
$dependencies = array(
'dependencies' => 'require',
'devDependencies' => 'require-dev',
);
$extras = array(
'main' => 'bower-asset-main',
'ignore' => 'bower-asset-ignore',
'private' => 'bower-asset-private',
);

return $this->convertData($data, $keys, $dependencies, $extras);
}
}
86 changes: 86 additions & 0 deletions Converter/NpmPackageConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the Fxp Composer Asset Plugin package.
*
* (c) François Pluchino <francois.pluchino@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fxp\Composer\AssetPlugin\Converter;

/**
* Converter for NPM package to composer package.
*
* @author François Pluchino <francois.pluchino@gmail.com>
*/
class NpmPackageConverter extends AbstractPackageConverter
{
/**
* {@inheritdoc}
*/
public function convert(array $data)
{
$assetType = $this->assetType;
$keys = array(
'name' => array('name', function ($value) use ($assetType) {
return $assetType->getComposerVendorName() . '/' . $value;
}),
'type' => array('type', function () {
return 'npm-asset-library';
}),
'version' => array('version', function ($value) use ($assetType) {
return $assetType->getVersionConverter()->convertVersion($value);
}),
'description' => 'description',
'keywords' => 'keywords',
'homepage' => 'homepage',
'license' => 'license',
'author' => array('authors', function ($value) use ($assetType) {
if (null !== $value) {
$value = array($value);
}

return $value;
}),
'contributors' => array('authors', function ($value, $prevValue) use ($assetType) {
$mergeValue = is_array($prevValue) ? $prevValue : array();
$mergeValue = array_merge($mergeValue, is_array($value) ? $value : array());

if (count($mergeValue) > 0) {
$value = $mergeValue;
}

return $value;
}),
'bin' => 'bin',
);
$dependencies = array(
'dependencies' => 'require',
'devDependencies' => 'require-dev',
);
$extras = array(
'bugs' => 'npm-asset-bugs',
'files' => 'npm-asset-files',
'main' => 'npm-asset-main',
'man' => 'npm-asset-man',
'directories' => 'npm-asset-directories',
'repository' => 'npm-asset-repository',
'scripts' => 'npm-asset-scripts',
'config' => 'npm-asset-config',
'bundledDependencies' => 'npm-asset-bundled-dependencies',
'optionalDependencies' => 'npm-asset-optional-dependencies',
'engines' => 'npm-asset-engines',
'engineStrict' => 'npm-asset-engine-strict',
'os' => 'npm-asset-os',
'cpu' => 'npm-asset-cpu',
'preferGlobal' => 'npm-asset-prefer-global',
'private' => 'npm-asset-private',
'publishConfig' => 'npm-asset-publish-config',
);

return $this->convertData($data, $keys, $dependencies, $extras);
}
}
29 changes: 29 additions & 0 deletions Converter/PackageConverterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Fxp Composer Asset Plugin package.
*
* (c) François Pluchino <francois.pluchino@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fxp\Composer\AssetPlugin\Converter;

/**
* Interface for the converter for asset package to composer package.
*
* @author François Pluchino <francois.pluchino@gmail.com>
*/
interface PackageConverterInterface
{
/**
* Converts the asset data package to composer data package.
*
* @param array $data The asset data package
*
* @return array The composer data package
*/
public function convert(array $data);
}
2 changes: 1 addition & 1 deletion Repository/AssetVcsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private function preProcess(VcsDriverInterface $driver, array $data, $identifier
{
// keep the name of the main identifier for all packages
$data['name'] = $this->packageName ?: $data['name'];
$data = $this->assetType->convert($data);
$data = $this->assetType->getPackageConverter()->convert($data);

if (!isset($data['dist'])) {
$data['dist'] = $driver->getDist($identifier);
Expand Down
25 changes: 23 additions & 2 deletions Type/AbstractAssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Fxp\Composer\AssetPlugin\Type;

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

Expand All @@ -21,14 +22,26 @@
*/
abstract class AbstractAssetType implements AssetTypeInterface
{
/**
* @var PackageConverterInterface
*/
protected $packageConverter;

/**
* @var VersionConverterInterface
*/
protected $versionConverter;

public function __construct()
/**
* Constructor.
*
* @param PackageConverterInterface $packageConverter
* @param VersionConverterInterface $versionConverter
*/
public function __construct(PackageConverterInterface $packageConverter, VersionConverterInterface $versionConverter = null)
{
$this->versionConverter = new SemverConverter();
$this->packageConverter = $packageConverter;
$this->versionConverter = !$versionConverter ? new SemverConverter() : $versionConverter;
}

/**
Expand All @@ -47,6 +60,14 @@ public function getFilename()
return $this->getName() . '.json';
}

/**
* {@inheritdoc}
*/
public function getPackageConverter()
{
return $this->packageConverter;
}

/**
* {@inheritdoc}
*/
Expand Down
9 changes: 4 additions & 5 deletions Type/AssetTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Fxp\Composer\AssetPlugin\Type;

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

/**
Expand Down Expand Up @@ -49,11 +50,9 @@ public function getFilename();
public function getVersionConverter();

/**
* Converts the asset data package to composer data package.
* Gets the package converter.
*
* @param array $data The asset data package
*
* @return array The composer data package
* @return PackageConverterInterface
*/
public function convert(array $data);
public function getPackageConverter();
}
Loading

0 comments on commit 28b3952

Please sign in to comment.