-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #281 from jg-development/feature/280_add_submit_pr…
…oxy_package Add Feature to submit packages from proxy
- Loading branch information
Showing
10 changed files
with
271 additions
and
8 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
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Packeton\Composer\Repository; | ||
|
||
use Composer\Config; | ||
use Composer\IO\IOInterface; | ||
use Composer\Repository\ComposerRepository; | ||
use Composer\Util\HttpDownloader; | ||
use Composer\Util\ProcessExecutor; | ||
|
||
class ComposerProxyRepository extends ComposerRepository implements PacketonRepositoryInterface | ||
{ | ||
public function __construct( | ||
protected array $repoConfig, | ||
protected IOInterface $io, | ||
protected Config $config, | ||
protected HttpDownloader $httpDownloader, | ||
protected ?ProcessExecutor $process = null | ||
) { | ||
parent::__construct($repoConfig, $io, $config, $httpDownloader); | ||
|
||
$this->process ??= new ProcessExecutor($this->io); | ||
} | ||
|
||
public function getHttpDownloader(): HttpDownloader | ||
{ | ||
return $this->httpDownloader; | ||
} | ||
|
||
public function getProcessExecutor(): ProcessExecutor | ||
{ | ||
return $this->process; | ||
} | ||
|
||
public function getConfig(): Config | ||
{ | ||
return $this->config; | ||
} | ||
|
||
public function getIO(): IOInterface | ||
{ | ||
return $this->io; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Packeton\Filter; | ||
|
||
use Composer\Package\PackageInterface; | ||
|
||
class VersionFilter | ||
{ | ||
/** | ||
* @param PackageInterface[] $versions | ||
* @return PackageInterface[] | ||
*/ | ||
public function filterVersionsForOnlyMatchingRepoName(string $repoName, array $versions): array | ||
{ | ||
$result = []; | ||
foreach ($versions as $version) { | ||
if ($version->getName() === $repoName) { | ||
$result[] = $version; | ||
} | ||
} | ||
return $result; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Packeton\Form\Type\Package; | ||
|
||
use Packeton\Entity\Package; | ||
use Packeton\Form\Type\CredentialType; | ||
use Packeton\Model\PackageManager; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
|
||
class ProxyPackageType extends AbstractType | ||
{ | ||
/** | ||
* @var PackageManager | ||
*/ | ||
protected $packageManager; | ||
|
||
/** | ||
* @param PackageManager $packageManager | ||
*/ | ||
public function __construct(PackageManager $packageManager) | ||
{ | ||
$this->packageManager = $packageManager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add('credentials', CredentialType::class) | ||
->add('name', TextType::class, [ | ||
'required' => true, | ||
'constraints' => [new NotBlank()], | ||
'attr' => ['class' => 'package-repo-info', 'placeholder' => 'acme/package-name'], | ||
'disabled' => false === $options['is_created'], | ||
]) | ||
->add('repository', TextType::class, [ | ||
'label' => 'Packages.json', | ||
'attr' => [ | ||
'class' => 'package-repo-info', | ||
'placeholder' => 'e.g.: https://repo.magento.com/packages.json', | ||
], | ||
]); | ||
|
||
$builder->addEventListener(FormEvents::POST_SUBMIT, [$this, 'updateRepository'], 255); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParent(): string | ||
{ | ||
return BasePackageType::class; | ||
} | ||
|
||
/** | ||
* @param FormEvent $event | ||
*/ | ||
public function updateRepository(FormEvent $event): void | ||
{ | ||
$package = $event->getData(); | ||
if ($package instanceof Package) { | ||
$this->packageManager->updatePackageUrl($package); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults([ | ||
'data_class' => Package::class, | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getBlockPrefix(): string | ||
{ | ||
return 'proxy'; | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Packeton\Model; | ||
|
||
use League\Flysystem\FilesystemOperator; | ||
use Packeton\Composer\Repository\PacketonRepositoryInterface; | ||
use Packeton\Entity\Package; | ||
use Packeton\Service\DistConfig; | ||
|
||
class ComposerProxyPackageManager | ||
{ | ||
public function __construct( | ||
private readonly DistConfig $config, | ||
private readonly FilesystemOperator $baseStorage, | ||
) { | ||
} | ||
|
||
public function buildArchive(Package $package, PacketonRepositoryInterface $repository, ?string $reference = null): ?string | ||
{ | ||
$version = $package->getVersionByReference($reference) ?: $package->getVersions()->first(); | ||
if (null === $version) { | ||
throw new \RuntimeException("Not found any versions for reference '$reference' of package '{$package->getName()}'"); | ||
} | ||
|
||
$keyName = $this->config->buildName($package->getName(), $version->getReference(), $version->getVersion()); | ||
$cachedName = $this->config->resolvePath($keyName); | ||
if (file_exists($cachedName)) { | ||
return $cachedName; | ||
} | ||
|
||
$selected = []; | ||
$serialized = $package->getCustomVersions(); | ||
foreach ($serialized as $data) { | ||
$verName = $data['version'] ?? null; | ||
if ($verName === $version->getVersion() || $verName === $version->getNormalizedVersion()) { | ||
$selected = $data['definition'] ?? []; | ||
$selected['version'] = $version->getVersion(); | ||
} | ||
} | ||
|
||
$selected['name'] = $package->getName(); | ||
$dir = dirname($cachedName); | ||
if (!is_dir($dir)) { | ||
@mkdir($dir, 0777, true); | ||
} | ||
|
||
$url = $package->getVersionByReference($reference)->getDist()['proxy_url']; | ||
$response = $repository->getHttpDownloader()->get($url); | ||
$body = (string) $response->getBody(); | ||
|
||
$this->baseStorage->write($keyName, $body); | ||
|
||
return $cachedName; | ||
} | ||
|
||
} |
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.