Skip to content

Commit

Permalink
Replace the deprecated Composer Metadata v1 with v2
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed Mar 23, 2022
1 parent d698f19 commit 0934171
Show file tree
Hide file tree
Showing 31 changed files with 853 additions and 256 deletions.
4 changes: 2 additions & 2 deletions app/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Application\Console\Packagist\GetListCommand;
use App\Application\Console\Packagist\GetUpdatesCommand;
use App\Application\Console\Packagist\MassImportCommand;
use App\Application\Console\Queue\QueueConsumerCommand;
use App\Application\Console\Queue\QueueConsumeCommand;
use DI\ContainerBuilder;
use function DI\autowire;

Expand All @@ -16,7 +16,7 @@
GetListCommand::class => autowire(GetListCommand::class),
GetUpdatesCommand::class => autowire(GetUpdatesCommand::class),
MassImportCommand::class => autowire(MassImportCommand::class),
QueueConsumerCommand::class => autowire(QueueConsumerCommand::class)
QueueConsumeCommand::class => autowire(QueueConsumeCommand::class)
]
);
};
17 changes: 17 additions & 0 deletions app/repositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
use App\Application\Settings\SettingsInterface;
use App\Domain\Dependency\DependencyRepositoryInterface;
use App\Domain\Package\PackageRepositoryInterface;
use App\Domain\Preference\PreferenceRepositoryInterface;
use App\Domain\Stats\StatsRepositoryInterface;
use App\Domain\Version\VersionRepositoryInterface;
use App\Infrastructure\Persistence\Dependency\CachedDependencyRepository;
use App\Infrastructure\Persistence\Dependency\PdoDependencyRepository;
use App\Infrastructure\Persistence\Package\CachedPackageRepository;
use App\Infrastructure\Persistence\Package\PdoPackageRepository;
use App\Infrastructure\Persistence\Preference\CachedPreferenceRepository;
use App\Infrastructure\Persistence\Preference\PdoPreferenceRepository;
use App\Infrastructure\Persistence\Stats\CachedStatsRepository;
use App\Infrastructure\Persistence\Stats\PdoStatsRepository;
use App\Infrastructure\Persistence\Version\CachedVersionRepository;
Expand Down Expand Up @@ -50,6 +53,20 @@
$container->get(CacheItemPoolInterface::class)
);
},
// Preference
PdoPreferenceRepository::class => autowire(PdoPreferenceRepository::class),
PreferenceRepositoryInterface::class => static function (ContainerInterface $container): PreferenceRepositoryInterface {
$settings = $container->get(SettingsInterface::class);

if ($settings->has('cache') === false || $settings->getBool('cache.enabled', false) === false) {
return $container->get(PdoPreferenceRepository::class);
}

return new CachedPreferenceRepository(
$container->get(PdoPreferenceRepository::class),
$container->get(CacheItemPoolInterface::class)
);
},
// Stats
PdoStatsRepository::class => autowire(PdoStatsRepository::class),
StatsRepositoryInterface::class => static function (ContainerInterface $container): StatsRepositoryInterface {
Expand Down
6 changes: 3 additions & 3 deletions bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use App\Application\Console\Packagist\GetListCommand;
use App\Application\Console\Packagist\GetUpdatesCommand;
use App\Application\Console\Packagist\MassImportCommand;
use App\Application\Console\Queue\QueueConsumerCommand;
use App\Application\Console\Queue\QueueConsumeCommand;
use DI\ContainerBuilder;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
Expand Down Expand Up @@ -81,8 +81,8 @@
MassImportCommand::getDefaultName() => static function () use ($container): MassImportCommand {
return $container->get(MassImportCommand::class);
},
QueueConsumerCommand::getDefaultName() => static function () use ($container): QueueConsumerCommand {
return $container->get(QueueConsumerCommand::class);
QueueConsumeCommand::getDefaultName() => static function () use ($container): QueueConsumeCommand {
return $container->get(QueueConsumeCommand::class);
}
]
)
Expand Down
19 changes: 19 additions & 0 deletions db/migrations/20220322224934_preferences.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types = 1);

use Phinx\Migration\AbstractMigration;

final class Preferences extends AbstractMigration {
public function change(): void {
$preferences = $this->table('preferences');
$preferences
->addColumn('category', 'text', ['null' => false])
->addColumn('property', 'text', ['null' => false])
->addColumn('value', 'text', ['null' => false])
->addColumn('type', 'text', ['null' => false])
->addTimestampsWithTimezone()
->addIndex(['category', 'property'], ['unique' => true])
->addIndex('created_at')
->create();
}
}
95 changes: 55 additions & 40 deletions src/Application/Console/Packagist/GetUpdatesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

namespace App\Application\Console\Packagist;

use App\Domain\Package\Package;
use App\Application\Message\Command\PackageDiscoveryCommand;
use App\Application\Service\Packagist;
use App\Domain\Package\PackageRepositoryInterface;
use Buzz\Browser;
use App\Domain\Preference\PreferenceRepositoryInterface;
use App\Domain\Preference\PreferenceTypeEnum;
use Courier\Client\Producer;
use Exception;
use InvalidArgumentException;
use RuntimeException;
Expand All @@ -22,8 +25,10 @@ final class GetUpdatesCommand extends Command {
private const FILE_TIMEOUT = 43200;

protected static $defaultName = 'packagist:get-updates';
private PreferenceRepositoryInterface $preferenceRepository;
private PackageRepositoryInterface $packageRepository;
private Browser $browser;
private Packagist $packagist;
private Producer $producer;

/**
* Command configuration.
Expand Down Expand Up @@ -67,56 +72,62 @@ protected function execute(InputInterface $input, OutputInterface $output): int
throw new InvalidArgumentException('Invalid mirror option');
}

$dataPath = sys_get_temp_dir() . '/changes.json';
$preferenceCol = $this->preferenceRepository->find(
[
'category' => 'packagist',
'property' => 'timestamp'
]
);

$modTime = false;
if (file_exists($dataPath)) {
$modTime = filemtime($dataPath);
$preference = $preferenceCol[0] ?? null;
if ($preference === null) {
$since = $this->packagist->getChangesTimestamp();
$preference = $this->preferenceRepository->create(
'packagist',
'timestamp',
(string)$since,
PreferenceTypeEnum::isInteger
);
}

if ($modTime === false || (time() - $modTime) > self::FILE_TIMEOUT) {
$url = "${mirror}/metadata/changes.json?since=${since}";
if ($output->isVerbose()) {
$io->text(
sprintf(
"[%s] Downloading a fresh copy of <options=bold;fg=cyan>${url}</>",
date('H:i:s'),
)
);
}
$changes = $this->packagist->getPackageUpdates($preference->getValueAsInteger());
foreach ($changes['changes'] as $action) {
switch ($action['type']) {
case 'update':
$packageName = $action['package'];
if (str_ends_with($packageName, '~dev')) {
$packageName = substr($packageName, 0, strlen($packageName) - 4);
}

$response = $this->browser->get($url, ['User-Agent' => 'php.package.health (twitter.com/flavioheleno)']);
if ($response->getStatusCode() >= 400) {
throw new RuntimeException(
sprintf(
'Request to "%s" returned status code %d',
$url,
$response->getStatusCode()
)
);
}
$packageCol = $this->packageRepository->find(['name' => $packageName]);

file_put_contents($dataPath, (string)$response->getBody());
}
$package = $packageCol[0] ?? null;
if ($package === null) {
$package = $this->packageRepository->create($packageName);
}

$json = json_decode(file_get_contents($dataPath), true, 512, JSON_THROW_ON_ERROR);
$this->producer->sendCommand(
new PackageDiscoveryCommand($package)
);


foreach ($json['actions'] as $action) {
switch ($action['type']) {
case 'update':
break;
case 'delete':
// TODO
// $this->producer->sendCommand(
// new PackageRemoveCommand($package);
// );

break;
case 'resync':
break;
default:
//
}
}

$package = new Package($packageName, '', '', '');

$this->packageRepository->save($package);
$preference = $preference->withIntegerValue($changes['timestamp']);
if ($preference->isDirty()) {
$this->preferenceRepository->update($preference);
}

$io->text(
Expand Down Expand Up @@ -146,11 +157,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

public function __construct(
PreferenceRepositoryInterface $preferenceRepository,
PackageRepositoryInterface $packageRepository,
Browser $browser
Packagist $packagist,
Producer $producer
) {
$this->packageRepository = $packageRepository;
$this->browser = $browser;
$this->preferenceRepository = $preferenceRepository;
$this->packageRepository = $packageRepository;
$this->packagist = $packagist;
$this->producer = $producer;

parent::__construct();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class QueueConsumerCommand extends Command {
protected static $defaultName = 'queue:consumer';
final class QueueConsumeCommand extends Command {
protected static $defaultName = 'queue:consume';
private Consumer $consumer;

/**
Expand All @@ -25,7 +25,7 @@ final class QueueConsumerCommand extends Command {
*/
protected function configure(): void {
$this
->setDescription('Consume all message bus queues');
->setDescription('Consume message bus queues');
}

/**
Expand Down
Loading

0 comments on commit 0934171

Please sign in to comment.