-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
ProviderController.php
149 lines (133 loc) · 4.91 KB
/
ProviderController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Packagist\WebBundle\Controller;
use Packagist\WebBundle\Entity\Package;
use Packagist\WebBundle\Entity\Version;
use Packagist\WebBundle\Service\DistManager;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class ProviderController extends Controller
{
/**
* @Route("/packages.json", name="root_packages", defaults={"_format" = "json"})
* @Method({"GET"})
*/
public function packagesAction()
{
$manager = $this->container->get('packagist.package_manager');
$rootPackages = $manager->getRootPackagesJson($this->getUser());
return new Response(\json_encode($rootPackages), 200, ['Content-Type' => 'application/json']);
}
/**
* @Route(
* "/p/providers${hash}.json",
* requirements={"hash"="[a-f0-9]+"},
* name="root_providers", defaults={"_format" = "json"}
* )
*
* @param string $hash
* @return Response
*
* @Method({"GET"})
*/
public function providersAction($hash)
{
$manager = $this->container->get('packagist.package_manager');
$providers = $manager->getProvidersJson($this->getUser(), $hash);
if (!$providers) {
return $this->createNotFound();
}
return new Response(\json_encode($providers), 200, ['Content-Type' => 'application/json']);
}
/**
* @Route(
* "/p/{package}.json",
* requirements={"package"="[\w+\/\-\$]+"},
* name="root_package", defaults={"_format" = "json"}
* )
*
* @param string $package
* @return Response
*
* @Method({"GET"})
*/
public function packageAction($package)
{
$package = \explode('$', $package);
$manager = $this->container->get('packagist.package_manager');
if (\count($package) !== 2) {
$package = $manager->getPackageJson($this->getUser(), $package[0]);
if ($package) {
return new Response(\json_encode($package), 200, ['Content-Type' => 'application/json']);
}
return $this->createNotFound();
}
$manager = $this->container->get('packagist.package_manager');
$package = $manager->getCachedPackageJson($this->getUser(), $package[0], $package[1]);
if (!$package) {
return $this->createNotFound();
}
return new Response(\json_encode($package), 200, ['Content-Type' => 'application/json']);
}
/**
* @Route(
* "/zipball/{package}/{hash}",
* name="download_dist_package",
* requirements={"package"="[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?", "hash"="[a-f0-9]{40}\.[a-z]+?"}
* )
* @Method({"GET"})
* @ParamConverter("package", options={"mapping": {"package": "name"}})
*
* @param Package $package
* @param string $hash
* @return Response
*/
public function zipballAction(Package $package, $hash)
{
$distManager = $this->container->get(DistManager::class);
if (false === \preg_match('{[a-f0-9]{40}}i', $hash, $match)) {
return new JsonResponse(['status' => 'error', 'message' => 'Not Found'], 404);
}
$versions = $package->getVersions()->filter(
function (Version $version) use ($match) {
if ($dist = $version->getDist()) {
return isset($dist['reference']) && $match[0] === $dist['reference'];
}
return false;
}
);
// Try to download from cache
if ($versions->count() === 0) {
list($path, $versionName) = $distManager->lookupInCache($match[0], $package->getName());
if (null !== $versionName) {
$version = $package->getVersions()
->filter(
function (Version $version) use ($versionName) {
return $versionName === $version->getVersion();
}
)->first();
if ($version && $this->isGranted('ROLE_MAINTAINER', $version)) {
return new BinaryFileResponse($path);
}
}
}
/** @var Version $version */
foreach ($versions as $version) {
if (!$this->isGranted('ROLE_MAINTAINER', $version)) {
continue;
}
if ($path = $distManager->getDistPath($version)) {
return new BinaryFileResponse($path);
}
break;
}
return $this->createNotFound();
}
protected function createNotFound()
{
return new JsonResponse(['status' => 'error', 'message' => 'Not Found'], 404);
}
}