-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb96a97
commit 28b3952
Showing
9 changed files
with
369 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.