Skip to content

Commit ba93574

Browse files
committed
Merge branch 'cs-fixes'
2 parents c499b14 + 84bd167 commit ba93574

32 files changed

Lines changed: 302 additions & 166 deletions

.symfony.local.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ http:
33
port: 7015
44
use_gzip: true
55

6-
workers:
7-
messenger_consume:
8-
cmd: ['symfony', 'console', 'messenger:consume', 'async', 'scheduler_packages', '--sleep', '10']
9-
watch: ['config', 'src', 'templates']
6+
# todo the worker isn't doing anything for me at the moment
7+
#workers:
8+
# messenger_consume:
9+
# cmd: ['symfony', 'console', 'messenger:consume', '--all', '--sleep', '10']
10+
# watch: ['config', 'src', 'templates']

src/Command/PackagesUpdateCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5050
return Command::FAILURE;
5151
}
5252

53+
$io->writeln("Scheduling package $packageName for update...");
54+
5355
$packages = [['id' => $package->getId()]];
5456

5557
$randomTimes = false;
5658
$reschedule = true;
5759
} elseif ($force) {
60+
$io->writeln('Scheduling all packages for update...');
5861
$packages = $this->packageRepository->getAllPackageIds();
5962

6063
$reschedule = true;
6164
} else {
62-
$packages = $this->packageRepository->getStalePackages();
65+
$io->writeln('Scheduling stale packages for update...');
66+
$packages = $this->packageRepository->getStalePackageIds();
6367
}
6468

6569
foreach ($packages as $package) {
6670
$this->messenger->dispatch(new SchedulePackageUpdate($package['id'], randomTime: $randomTimes, reschedule: $reschedule, forceRefresh: $force));
6771
}
6872

73+
$packageCount = count($packages);
74+
$io->success("Scheduled $packageCount package(s) for update.");
75+
6976
return Command::SUCCESS;
7077
}
7178
}

src/Controller/ApiController.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\HttpFoundation\JsonResponse;
1818
use Symfony\Component\HttpFoundation\Request;
1919
use Symfony\Component\HttpFoundation\Response;
20-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2120
use Symfony\Component\Messenger\Envelope;
2221
use Symfony\Component\Messenger\MessageBusInterface;
2322
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;
@@ -79,13 +78,13 @@ public function packageMetadata(string $packageName): Response
7978
$basePackageName = u($packageName)->trimSuffix('~dev')->toString();
8079

8180
if (null === $package = $this->findPackage($basePackageName)) {
82-
throw new NotFoundHttpException();
81+
throw $this->createNotFoundException();
8382
}
8483

8584
$this->messenger->dispatch(new UpdatePackage($package->getId()));
8685

8786
if (!$this->providerManager->exists($packageName)) {
88-
throw new NotFoundHttpException();
87+
throw $this->createNotFoundException();
8988
}
9089

9190
return new BinaryFileResponse($this->providerManager->path($packageName), headers: ['Content-Type' => 'application/json']);
@@ -105,26 +104,26 @@ public function packageMetadata(string $packageName): Response
105104
public function packageDistribution(string $packageName, string $packageVersion, string $reference, string $type): Response
106105
{
107106
if (!$this->getParameter('dirigent.dist_mirroring.enabled')) {
108-
throw new NotFoundHttpException();
107+
throw $this->createNotFoundException();
109108
}
110109

111110
if (!$this->distributionResolver->exists($packageName, $packageVersion, $reference, $type)) {
112111
if (null === $package = $this->packageRepository->findOneBy(['name' => $packageName])) {
113-
throw new NotFoundHttpException();
112+
throw $this->createNotFoundException();
114113
}
115114

116115
if (null === $version = $this->versionRepository->findOneBy(['package' => $package, 'normalizedVersion' => $packageVersion])) {
117-
throw new NotFoundHttpException();
116+
throw $this->createNotFoundException();
118117
}
119118

120119
if ($version->isDevelopment() && !$this->getParameter('dirigent.dist_mirroring.dev_packages')) {
121-
throw new NotFoundHttpException();
120+
throw $this->createNotFoundException();
122121
}
123122

124123
$this->messenger->dispatch(new UpdatePackage($package->getId()));
125124

126125
if (!$this->distributionResolver->resolve($version, $reference, $type)) {
127-
throw new NotFoundHttpException();
126+
throw $this->createNotFoundException();
128127
}
129128
}
130129

src/Controller/Dashboard/DashboardPackagesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function edit(Request $request, string $packageName): Response
174174
return $this->redirectToRoute('dashboard_packages_info', ['packageName' => $package->getName()]);
175175
}
176176

177-
return $this->render('dashboard/packages/package_edit.html.twig', [
177+
return $this->render('dashboard/packages/edit.html.twig', [
178178
'package' => $package,
179179
'form' => $form,
180180
]);

src/Controller/Dashboard/DashboardPackagesInfoController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function info(string $packageName): Response
3535
return $this->versionInfo($packageName, $version->getNormalizedVersion());
3636
}
3737

38-
#[Route('/packages/{packageName}/v/{packageVersion}', name: 'dashboard_packages_version_info', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
38+
#[Route('/packages/{packageName}/v/{packageVersion}', name: 'dashboard_packages_version_info', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+', 'packageVersion' => '.*'])]
3939
#[IsGrantedAccess]
4040
public function versionInfo(string $packageName, string $packageVersion): Response
4141
{

src/Controller/Dashboard/DashboardRegistryController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function configureFields(string $pageName): iterable
7171
->setRequired(true)
7272
->setChoices(RegistryPackageMirroring::cases())
7373
->setFormTypeOption('choice_label', function (RegistryPackageMirroring $choice): string {
74-
return "registry.package_mirroring.{$choice->value}";
74+
return "registry.package-mirroring.{$choice->value}";
7575
})
7676
->renderExpanded();
7777

src/Doctrine/Entity/Package.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Package
5959
#[ORM\Column(nullable: true)]
6060
private ?string $remoteId = null;
6161

62-
#[ORM\Column(type: Types::STRING, enumType: PackageFetchStrategy::class, nullable: true)]
62+
#[ORM\Column(nullable: true, enumType: PackageFetchStrategy::class)]
6363
private PackageFetchStrategy|string|null $fetchStrategy = null;
6464

6565
#[ORM\ManyToOne]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Doctrine\EventListener;
4+
5+
use CodedMonkey\Dirigent\Doctrine\Entity\Package;
6+
use CodedMonkey\Dirigent\Doctrine\Repository\PackageRepository;
7+
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
8+
use Doctrine\ORM\Event\PreRemoveEventArgs;
9+
use Doctrine\ORM\Events;
10+
11+
#[AsEntityListener(Events::preRemove, entity: Package::class)]
12+
class PackageListener
13+
{
14+
public function preRemove(Package $package, PreRemoveEventArgs $event): void
15+
{
16+
/** @var PackageRepository $repository */
17+
$repository = $event->getObjectManager()->getRepository(Package::class);
18+
19+
// Delete existing package links
20+
$repository->deletePackageLinks($package);
21+
}
22+
}

src/Doctrine/Repository/PackageRepository.php

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace CodedMonkey\Dirigent\Doctrine\Repository;
44

55
use CodedMonkey\Dirigent\Doctrine\Entity\Package;
6+
use CodedMonkey\Dirigent\Doctrine\Entity\Version;
67
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8+
use Doctrine\DBAL\Connection;
79
use Doctrine\Persistence\ManagerRegistry;
810
use Symfony\Component\DependencyInjection\Attribute\Autowire;
911

@@ -40,8 +42,6 @@ public function save(Package $entity, bool $flush = false): void
4042

4143
public function remove(Package $entity, bool $flush = false): void
4244
{
43-
$this->deletePackageLinks($entity->getId());
44-
4545
$this->getEntityManager()->remove($entity);
4646

4747
if ($flush) {
@@ -57,7 +57,17 @@ public function findOneByName(string $name): ?Package
5757
/**
5858
* @return list<array{id: int}>
5959
*/
60-
public function getStalePackages(): array
60+
public function getAllPackageIds(): array
61+
{
62+
$connection = $this->getEntityManager()->getConnection();
63+
64+
return $connection->fetchAllAssociative('SELECT id FROM package ORDER BY id');
65+
}
66+
67+
/**
68+
* @return list<array{id: int}>
69+
*/
70+
public function getStalePackageIds(): array
6171
{
6272
$connection = $this->getEntityManager()->getConnection();
6373

@@ -75,65 +85,64 @@ public function getStalePackages(): array
7585
);
7686
}
7787

78-
/**
79-
* @return list<array{id: int}>
80-
*/
81-
public function getAllPackageIds(): array
82-
{
83-
$connection = $this->getEntityManager()->getConnection();
84-
85-
return $connection->fetchAllAssociative('SELECT id FROM package ORDER BY id');
86-
}
87-
88-
public function updatePackageLinks(int $packageId, int $versionId): void
88+
public function updatePackageLinks(Package $package, Version $version): void
8989
{
9090
$connection = $this->getEntityManager()->getConnection();
91-
$connection->beginTransaction();
91+
$connection->transactional(function (Connection $connection) use ($package, $version) {
92+
$this->deletePackageLinks($package);
93+
$queryParameters = ['id' => $package->getId(), 'version' => $version->getId()];
9294

93-
try {
94-
$this->deletePackageLinks($packageId);
95-
96-
$connection->executeStatement(<<<'SQL'
95+
$connection->executeStatement(
96+
<<<'SQL'
9797
INSERT INTO package_provide_link (linked_package_name, implementation, package_id)
9898
SELECT linked_package_name, FALSE, :id
9999
FROM version_provide_link
100100
WHERE version_id = :version AND linked_package_name NOT LIKE '%-implementation'
101101
SQL,
102-
['id' => $packageId, 'version' => $versionId],
102+
$queryParameters,
103103
);
104-
$connection->executeStatement(<<<'SQL'
104+
$connection->executeStatement(
105+
<<<'SQL'
105106
INSERT INTO package_provide_link (linked_package_name, implementation, package_id)
106107
SELECT SUBSTRING(linked_package_name, 1, LENGTH(linked_package_name) - 15), TRUE, :id
107108
FROM version_provide_link
108109
WHERE version_id = :version AND linked_package_name LIKE '%-implementation'
109110
SQL,
110-
['id' => $packageId, 'version' => $versionId],
111+
$queryParameters,
111112
);
112113
$connection->executeStatement(
113-
'INSERT INTO package_require_link (linked_package_name, dev_dependency, package_id) SELECT linked_package_name, FALSE, :id FROM version_require_link WHERE version_id = :version',
114-
['id' => $packageId, 'version' => $versionId],
114+
<<<'SQL'
115+
INSERT INTO package_require_link (linked_package_name, dev_dependency, package_id)
116+
SELECT linked_package_name, FALSE, :id FROM version_require_link WHERE version_id = :version
117+
SQL,
118+
$queryParameters,
115119
);
116120
$connection->executeStatement(
117-
'INSERT INTO package_require_link (linked_package_name, dev_dependency, package_id) SELECT linked_package_name, TRUE, :id FROM version_dev_require_link WHERE version_id = :version',
118-
['id' => $packageId, 'version' => $versionId],
121+
<<<'SQL'
122+
INSERT INTO package_require_link (linked_package_name, dev_dependency, package_id)
123+
SELECT linked_package_name, TRUE, :id FROM version_dev_require_link WHERE version_id = :version
124+
SQL,
125+
$queryParameters,
119126
);
120127
$connection->executeStatement(
121-
'INSERT INTO package_suggest_link (linked_package_name, package_id) SELECT linked_package_name, :id FROM version_suggest_link WHERE version_id = :version',
122-
['id' => $packageId, 'version' => $versionId],
128+
<<<'SQL'
129+
INSERT INTO package_suggest_link (linked_package_name, package_id)
130+
SELECT linked_package_name, :id FROM version_suggest_link WHERE version_id = :version
131+
SQL,
132+
$queryParameters,
123133
);
124-
} catch (\Throwable $exception) {
125-
$connection->rollBack();
126-
throw $exception;
127-
}
128-
129-
$connection->commit();
134+
});
130135
}
131136

132-
public function deletePackageLinks(int $packageId): void
137+
public function deletePackageLinks(Package $package): void
133138
{
134139
$connection = $this->getEntityManager()->getConnection();
135-
$connection->executeStatement('DELETE FROM package_provide_link WHERE package_id = :id', ['id' => $packageId]);
136-
$connection->executeStatement('DELETE FROM package_require_link WHERE package_id = :id', ['id' => $packageId]);
137-
$connection->executeStatement('DELETE FROM package_suggest_link WHERE package_id = :id', ['id' => $packageId]);
140+
$connection->transactional(function (Connection $connection) use ($package) {
141+
$queryParameters = ['id' => $package->getId()];
142+
143+
$connection->executeStatement('DELETE FROM package_provide_link WHERE package_id = :id', $queryParameters);
144+
$connection->executeStatement('DELETE FROM package_require_link WHERE package_id = :id', $queryParameters);
145+
$connection->executeStatement('DELETE FROM package_suggest_link WHERE package_id = :id', $queryParameters);
146+
});
138147
}
139148
}

src/EasyAdmin/PackagePaginator.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010

1111
class PackagePaginator implements EntityPaginatorInterface
1212
{
13-
private ?int $currentPage = null;
14-
private ?int $pageSize = null;
15-
private ?int $rangeSize = null;
16-
private ?int $rangeEdgeSize = null;
17-
private $results;
18-
private $numResults;
19-
private ?int $rangeFirstResultNumber = null;
20-
private ?int $rangeLastResultNumber = null;
13+
private int $currentPage;
14+
private int $pageSize;
15+
private int $rangeSize;
16+
private int $rangeEdgeSize;
17+
private array $results;
18+
private int $numResults;
2119

2220
public function __construct(
2321
private readonly UrlGeneratorInterface $router,
@@ -33,18 +31,16 @@ public function paginate(PaginatorDto $paginatorDto, QueryBuilder $queryBuilder)
3331
$this->rangeEdgeSize = $paginatorDto->getRangeEdgeSize();
3432
$this->currentPage = max(1, $paginatorDto->getPageNumber());
3533
$firstResult = ($this->currentPage - 1) * $this->pageSize;
36-
$this->rangeFirstResultNumber = $this->pageSize * ($this->currentPage - 1) + 1;
37-
$this->rangeLastResultNumber = $this->rangeFirstResultNumber + $this->pageSize - 1;
34+
$rangeFirstResultNumber = $this->pageSize * ($this->currentPage - 1) + 1;
3835

3936
$countQueryBuilder = clone $queryBuilder;
4037
$this->numResults = $countQueryBuilder
4138
->select('COUNT(package.id)')
4239
->getQuery()
4340
->getSingleScalarResult();
4441

45-
if ($this->rangeFirstResultNumber > $this->numResults) {
42+
if ($rangeFirstResultNumber > $this->numResults) {
4643
$this->results = [];
47-
$this->rangeLastResultNumber = $this->numResults;
4844

4945
return $this;
5046
}
@@ -57,9 +53,6 @@ public function paginate(PaginatorDto $paginatorDto, QueryBuilder $queryBuilder)
5753
->getResult();
5854

5955
$this->results = $results;
60-
if ($this->rangeLastResultNumber > $this->numResults) {
61-
$this->rangeLastResultNumber = $this->numResults;
62-
}
6356

6457
return $this;
6558
}

0 commit comments

Comments
 (0)