|
- <?php
-
-
-
- namespace Fxp\Composer\AssetPlugin\Converter;
-
- use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface;
-
-
- abstract class AbstractPackageConverter implements PackageConverterInterface
- {
-
-
- protected $assetType;
-
-
-
- public function __construct(AssetTypeInterface $assetType)
- {
- $this->assetType = $assetType;
- }
-
-
-
- public function convert(array $data, array &$vcsRepos = array())
- {
- $keys = $this->getMapKeys();
- $dependencies = $this->getMapDependencies();
- $extras = $this->getMapExtras();
-
- return $this->convertData($data, $keys, $dependencies, $extras, $vcsRepos);
- }
-
-
-
- protected function convertData(array $asset, array $keys, array $dependencies, array $extras, array &$vcsRepos = array())
- {
- $composer = array();
-
- foreach ($keys as $assetKey => $composerKey) {
- $this->convertKey($asset, $assetKey, $composer, $composerKey);
- }
-
- foreach ($dependencies as $assetKey => $composerKey) {
- $this->convertDependencies($asset, $assetKey, $composer, $composerKey, $vcsRepos);
- }
-
- foreach ($extras as $assetKey => $composerKey) {
- $this->convertExtraKey($asset, $assetKey, $composer, $composerKey);
- }
-
- return $composer;
- }
-
-
-
- protected function convertKey(array $asset, $assetKey, array &$composer, $composerKey)
- {
- if (is_array($composerKey)) {
- PackageUtil::convertArrayKey($asset, $assetKey, $composer, $composerKey);
- } else {
- PackageUtil::convertStringKey($asset, $assetKey, $composer, $composerKey);
- }
- }
-
-
-
- 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;
- }
- }
-
-
-
- protected function convertDependencies(array $asset, $assetKey, array &$composer, $composerKey, array &$vcsRepos = array())
- {
- if (isset($asset[$assetKey]) && is_array($asset[$assetKey])) {
- $newDependencies = array();
-
- foreach ($asset[$assetKey] as $dependency => $version) {
- list($dependency, $version) = $this->convertDependency($dependency, $version, $vcsRepos, $composer);
- $version = $this->assetType->getVersionConverter()->convertRange($version);
- if (0 !== strpos($version, $dependency)) {
- $newDependencies[$this->assetType->getComposerVendorName().'/'.$dependency] = $version;
- }
- }
-
- $composer[$composerKey] = $newDependencies;
- }
- }
-
-
-
- protected function convertDependency($dependency, $version, array &$vcsRepos = array(), array $composer)
- {
- list($dependency, $version) = PackageUtil::checkUrlVersion($this->assetType, $dependency, $version, $vcsRepos, $composer);
- list($dependency, $version) = PackageUtil::checkAliasVersion($this->assetType, $dependency, $version);
- list($dependency, $version) = PackageUtil::convertDependencyVersion($this->assetType, $dependency, $version);
-
- return array($dependency, $version);
- }
-
-
-
- protected function getMapKeys()
- {
- return array();
- }
-
-
-
- protected function getMapDependencies()
- {
- return array(
- 'dependencies' => 'require',
- 'devDependencies' => 'require-dev',
- );
- }
-
-
-
- protected function getMapExtras()
- {
- return array();
- }
- }
|