Skip to content

Commit

Permalink
Composer private proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed Jan 28, 2023
1 parent 98cba67 commit 657fffb
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 16 deletions.
18 changes: 18 additions & 0 deletions src/Cron/MirrorCronLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Packeton\Cron;

use Okvpn\Bundle\CronBundle\Loader\ScheduleLoaderInterface;

class MirrorCronLoader implements ScheduleLoaderInterface
{
/**
* {@inheritdoc}
*/
public function getSchedules(array $options = []): iterable
{
return [];
}
}
2 changes: 1 addition & 1 deletion src/Cron/WorkerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(JobScheduler $jobScheduler)
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function handle(ScheduleEnvelope $envelope, StackInterface $stack): ScheduleEnvelope
{
Expand Down
34 changes: 21 additions & 13 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,30 @@ private function addMirrorsRepositoriesConfiguration(ArrayNodeDefinition|NodeDef
->useAttributeAsKey('name')
->prototype('array');

$jsonNormalizer = static function ($json) {
if (\is_string($json) && \is_array($opt = @json_decode($json, true))) {
return $opt;
}
if (!\is_array($json)) {
throw new \InvalidArgumentException('This node must be array or JSON string');
}

return $json;
};

$mirrorNodeBuilder
->children()
->scalarNode('url')->end()
->variableNode('repository_json')
->beforeNormalization()
->always()
->then(static function ($repository) {
if (\is_string($repository) && \is_array($repo = @json_decode($repository, true))) {
return $repo;
}
if (\is_string($repository) && \is_file($repository)) {
$repository = \file_get_contents($repository);
$repository = $repository ? \json_decode($repository, true) : null;
}
return $repository;
})
->variableNode('options')
->beforeNormalization()->always()->then($jsonNormalizer)->end()
->end()
->variableNode('composer_auth')
->beforeNormalization()->always()->then($jsonNormalizer)->end()
->end()
->arrayNode('http_basic')
->children()
->scalarNode('username')->cannotBeEmpty()->end()
->scalarNode('password')->cannotBeEmpty()->end()
->end()
->end()
->scalarNode('sync_interval')->defaultValue(86400)->end()
Expand Down
5 changes: 5 additions & 0 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\ErrorHandler\DebugClassLoader;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

Expand Down Expand Up @@ -81,5 +82,9 @@ public function boot()

EncryptedTextType::setCrypter($crypter);
EncryptedArrayType::setCrypter($crypter);

if (class_exists(DebugClassLoader::class)) {
DebugClassLoader::disable();;
}
}
}
18 changes: 17 additions & 1 deletion src/Mirror/Model/ProxyOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ public function logo(): ?string
*/
public function http(): array
{
return $this->config['options'] ?? [];
return $this->config['options']['http'] ?? [];
}

/**
* @return array|null
*/
public function getAuthBasic(): ?array
{
return $this->config['http_basic'] ?? null;
}

/**
* @return array|null
*/
public function getComposerAuth(): ?array
{
return $this->config['composer_auth'] ?? null;
}
}
11 changes: 10 additions & 1 deletion src/Mirror/Service/ProxyHttpDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ public function getHttpClient(ProxyOptions $options, ?IOInterface &$io = null):
{
$config = $this->packagistFactory->createConfig();
$io ??= new NullIO();

if ($composer = $options->getComposerAuth()) {
$config->merge(['config' => $composer]);
}

$http = new HttpDownloader($io, $config);
$origin = \parse_url($options->getUrl(), \PHP_URL_HOST);

if ($basic = $options->getAuthBasic()) {
$io->setAuthentication($origin, $basic['username'], $basic['password']);
}

// capture username/password from URL if there is one
if (Preg::isMatchStrictGroups('{^https?://([^:/]+):([^@/]+)@([^/]+)}i', $options->getUrl(), $match)) {
$origin = \parse_url($options->getUrl(), \PHP_URL_HOST);
$io->setAuthentication($origin, \rawurldecode($match[1]), \rawurldecode($match[2]));
}

Expand Down
2 changes: 2 additions & 0 deletions src/Mirror/Service/RemotePackagesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public function markEnable(string $name): void

public function allEnabled(): array
{
return $this->redis->zRange("repo:{$this->repo}:enabled", 0, -1) ?: [];
}

public function allApproved(): array
{
return $this->redis->zRange("repo:{$this->repo}:approve", 0, -1) ?: [];
}

public function markApprove(string $name): void
Expand Down
12 changes: 12 additions & 0 deletions src/Mirror/Service/SyncMirrorWorker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Packeton\Mirror\Service;

class SyncMirrorWorker
{
public function __invoke(array $arguments = [])
{
}
}

0 comments on commit 657fffb

Please sign in to comment.