Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

193 linhas
6.3KB

  1. <?php
  2. /*
  3. * This file is part of the Fxp Composer Asset Plugin package.
  4. *
  5. * (c) François Pluchino <francois.pluchino@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Fxp\Composer\AssetPlugin\Converter;
  11. use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface;
  12. /**
  13. * Abstract class for converter for asset package to composer package.
  14. *
  15. * @author François Pluchino <francois.pluchino@gmail.com>
  16. */
  17. abstract class AbstractPackageConverter implements PackageConverterInterface
  18. {
  19. /**
  20. * @var AssetTypeInterface
  21. */
  22. protected $assetType;
  23. /**
  24. * Constructor.
  25. *
  26. * @param AssetTypeInterface $assetType
  27. */
  28. public function __construct(AssetTypeInterface $assetType)
  29. {
  30. $this->assetType = $assetType;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function convert(array $data, array &$vcsRepos = array())
  36. {
  37. $keys = $this->getMapKeys();
  38. $dependencies = $this->getMapDependencies();
  39. $extras = $this->getMapExtras();
  40. return $this->convertData($data, $keys, $dependencies, $extras, $vcsRepos);
  41. }
  42. /**
  43. * Converts the all keys (keys, dependencies and extra keys).
  44. *
  45. * @param array $asset The asset data
  46. * @param array $keys The map of asset key and composer key
  47. * @param array $dependencies The map of asset dependency key and composer dependency key
  48. * @param array $extras The map of asset key and composer extra key
  49. * @param array $vcsRepos The list of new vcs configs
  50. *
  51. * @return array The composer package converted
  52. */
  53. protected function convertData(array $asset, array $keys, array $dependencies, array $extras, array &$vcsRepos = array())
  54. {
  55. $composer = array();
  56. foreach ($keys as $assetKey => $composerKey) {
  57. $this->convertKey($asset, $assetKey, $composer, $composerKey);
  58. }
  59. foreach ($dependencies as $assetKey => $composerKey) {
  60. $this->convertDependencies($asset, $assetKey, $composer, $composerKey, $vcsRepos);
  61. }
  62. foreach ($extras as $assetKey => $composerKey) {
  63. $this->convertExtraKey($asset, $assetKey, $composer, $composerKey);
  64. }
  65. return $composer;
  66. }
  67. /**
  68. * Converts the simple key of package.
  69. *
  70. * @param array $asset The asset data
  71. * @param string $assetKey The asset key
  72. * @param array $composer The composer data
  73. * @param string|array $composerKey The composer key or array with composer key name and closure
  74. *
  75. * @throws \InvalidArgumentException When the 'composerKey' argument of asset packager converter is not an string or an array with the composer key and closure
  76. */
  77. protected function convertKey(array $asset, $assetKey, array &$composer, $composerKey)
  78. {
  79. if (is_array($composerKey)) {
  80. PackageUtil::convertArrayKey($asset, $assetKey, $composer, $composerKey);
  81. } else {
  82. PackageUtil::convertStringKey($asset, $assetKey, $composer, $composerKey);
  83. }
  84. }
  85. /**
  86. * Converts the extra key of package.
  87. *
  88. * @param array $asset The asset data
  89. * @param string $assetKey The asset extra key
  90. * @param array $composer The composer data
  91. * @param string|array $composerKey The composer extra key or array with composer extra key name and closure
  92. * @param string $extraKey The extra key name
  93. */
  94. protected function convertExtraKey(array $asset, $assetKey, array &$composer, $composerKey, $extraKey = 'extra')
  95. {
  96. $extra = isset($composer[$extraKey]) ? $composer[$extraKey] : array();
  97. $this->convertKey($asset, $assetKey, $extra, $composerKey);
  98. if (count($extra) > 0) {
  99. $composer[$extraKey] = $extra;
  100. }
  101. }
  102. /**
  103. * Converts simple key of package.
  104. *
  105. * @param array $asset The asset data
  106. * @param string $assetKey The asset key of dependencies
  107. * @param array $composer The composer data
  108. * @param string $composerKey The composer key of dependencies
  109. * @param array $vcsRepos The list of new vcs configs
  110. */
  111. protected function convertDependencies(array $asset, $assetKey, array &$composer, $composerKey, array &$vcsRepos = array())
  112. {
  113. if (isset($asset[$assetKey]) && is_array($asset[$assetKey])) {
  114. $newDependencies = array();
  115. foreach ($asset[$assetKey] as $dependency => $version) {
  116. list($dependency, $version) = $this->convertDependency($dependency, $version, $vcsRepos, $composer);
  117. $version = $this->assetType->getVersionConverter()->convertRange($version);
  118. if (0 !== strpos($version, $dependency)) {
  119. $newDependencies[$this->assetType->getComposerVendorName().'/'.$dependency] = $version;
  120. }
  121. }
  122. $composer[$composerKey] = $newDependencies;
  123. }
  124. }
  125. /**
  126. * Convert the .
  127. *
  128. * @param string $dependency The dependency
  129. * @param string $version The version
  130. * @param array $vcsRepos The list of new vcs configs
  131. * @param array $composer The partial composer data
  132. *
  133. * @return string[] The new dependency and the new version
  134. */
  135. protected function convertDependency($dependency, $version, array &$vcsRepos = array(), array $composer)
  136. {
  137. list($dependency, $version) = PackageUtil::checkUrlVersion($this->assetType, $dependency, $version, $vcsRepos, $composer);
  138. list($dependency, $version) = PackageUtil::checkAliasVersion($this->assetType, $dependency, $version);
  139. list($dependency, $version) = PackageUtil::convertDependencyVersion($this->assetType, $dependency, $version);
  140. return array($dependency, $version);
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. protected function getMapKeys()
  146. {
  147. return array();
  148. }
  149. /**
  150. * Get the map conversion of dependencies.
  151. *
  152. * @return array
  153. */
  154. protected function getMapDependencies()
  155. {
  156. return array(
  157. 'dependencies' => 'require',
  158. 'devDependencies' => 'require-dev',
  159. );
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. protected function getMapExtras()
  165. {
  166. return array();
  167. }
  168. }