Skip to content

Commit

Permalink
Add extensions listing page, fixes #1440
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Mar 21, 2024
1 parent 14129df commit 184baa5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/Controller/ExtensionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php declare(strict_types=1);

/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Controller;

use App\Entity\Package;
use App\Model\DownloadManager;
use App\Model\FavoriteManager;
use Pagerfanta\Adapter\FixedAdapter;
use Pagerfanta\Pagerfanta;
use Predis\Client as RedisClient;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ExtensionController extends Controller
{
#[Route(path: '/extensions.{_format}', name: 'browse_extensions', defaults: ['_format' => 'html'])]
public function extensionsAction(Request $req, RedisClient $redis, FavoriteManager $favMgr, DownloadManager $dlMgr): Response
{
$packages = $this->getEM()->getRepository(Package::class)
->createQueryBuilder('p')
->where("(p.type = 'php-ext' OR p.type = 'php-ext-zend')")
->andWhere('p.frozen IS NULL')
->orderBy('p.name')
->getQuery()
->enableResultCache(900)
->getResult();

$packages = new Pagerfanta(new FixedAdapter(count($packages), $packages));

$data = [
'packages' => $packages,
];
$data['meta'] = $this->getPackagesMetadata($favMgr, $dlMgr, $data['packages']);

if ($req->getRequestFormat() === 'json') {
$result = [
'packages' => [],
];

foreach ($packages as $package) {
$url = $this->generateUrl('view_package', ['name' => $package->getName()], UrlGeneratorInterface::ABSOLUTE_URL);

$result['packages'][] = [
'name' => $package->getName(),
'description' => $package->getDescription() ?: '',
'url' => $url,
'downloads' => $data['meta']['downloads'][$package->getId()],
'favers' => $data['meta']['favers'][$package->getId()],
];
}

$response = new JsonResponse($result);
$response->setSharedMaxAge(900);
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');

return $response;
}

return $this->render('extensions/list.html.twig', $data);
}
}
1 change: 1 addition & 0 deletions src/Entity/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function isRemoteIdMismatch(): bool
#[ORM\Index(name: 'dumped2_crawled_frozen_idx', columns: ['dumpedAtV2', 'crawledAt', 'frozen'])]
#[ORM\Index(name: 'vendor_idx', columns: ['vendor'])]
#[ORM\Index(name: 'frozen_idx', columns: ['frozen'])]
#[ORM\Index(name: 'type_frozen_idx', columns: ['type', 'frozen'])]
#[UniquePackage(groups: ['Create'])]
#[VendorWritable(groups: ['Create'])]
#[ValidPackageRepository(groups: ['Update', 'Default'])]
Expand Down
5 changes: 5 additions & 0 deletions templates/extensions/list.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% embed "web/list.html.twig" %}
{% block content_title %}
<h2 class="title">PHP Extensions</h2>
{% endblock %}
{% endembed %}

0 comments on commit 184baa5

Please sign in to comment.