forked from PUGX/badge-poser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BadgeController.php
145 lines (126 loc) · 4.84 KB
/
BadgeController.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
<?php
/*
* This file is part of the badge-poser package
*
* (c) Giulio De Donato <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PUGX\BadgeBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use PUGX\BadgeBundle\Service\ImageCreator;
use PUGX\BadgeBundle\Exception\InvalidArgumentException;
class BadgeController extends ContainerAware
{
CONST ERROR_TEXT_GENERIC = 'ERR 1 ';
CONST ERROR_TEXT_NOT_A_NUMBER = 'ERR 2 ';
CONST ERROR_TEXT_CLIENT_EXCEPTION = 'ERR 3 ';
/**
* @Route("/{repository}/downloads.png",
* name = "pugx_badge",
* requirements = {"repository" = "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?"}
* )
* @Route("/{repository}/d/{type}.png",
* name = "pugx_badge_stat",
* defaults = {"type" = "total"},
* requirements = {
* "type" = "total|daily|monthly",
* "repository" = "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?"
* }
* )
* @Method({"GET"})
*
* @param string $repository
* @param string $type
*
* @return StreamedResponse
*/
public function downloadsAction($repository, $type = 'total')
{
$imageCreator = $this->container->get('image_creator');
$outputFilename = sprintf('%s.png', $type);
$status = 500;
try {
$package = $this->container->get('package_manager')->fetchPackage($repository);
$text = $this->container->get('package_manager')->getPackageDownloads($package, $type);
$status = 200;
} catch (UnexpectedValueException $e) {
$text = self::ERROR_TEXT_CLIENT_EXCEPTION;
} catch (\Exception $e){
$text = self::ERROR_TEXT_GENERIC;
}
$image = $imageCreator->createDownloadsImage($text);
return $this->streamImage($status, $image, $outputFilename);
}
/**
* @Route("/{repository}/version.png",
* name="pugx_badge_version",
* requirements={"repository" = "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?"}
* )
* @Route("/{repository}/v/{latest}.png",
* name = "pugx_badge_version_latest",
* defaults = {"latest" = "stable"},
* requirements = {
* "type" = "stable|unstable",
* "repository" = "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?"
* }
* )
* @Method({"GET"})
*
* @param string $repository
* @param string $latest
*
* @return StreamedResponse
*/
public function versionAction($repository, $latest = 'stable')
{
$image = null;
$outputFilename = sprintf('%s.png', $latest);
$error = self::ERROR_TEXT_GENERIC;
$status = 500;
try {
$package = $this->container->get('package_manager')->fetchPackage($repository);
$package = $this->container->get('package_manager')->calculateLatestVersions($package);
if ('stable' == $latest && $package->hasStableVersion()) {
$image = $this->container->get('image_creator')->createStableImage($package->getLatestStableVersion());
} else if ($package->hasUnstableVersion()) {
$image = $this->container->get('image_creator')->createUnstableImage($package->getLatestUnstableVersion());
}
$status = 200;
} catch (UnexpectedValueException $e) {
$error = self::ERROR_TEXT_CLIENT_EXCEPTION;
} catch (\Exception $e){
$error = self::ERROR_TEXT_GENERIC;
}
if (null == $image) {
$image = $this->container->get('image_creator')->createStableImage($error);
}
return $this->streamImage($status, $image, $outputFilename);
}
/**
* @param resource $image
* @param string $outputFilename
*
* @return StreamedResponse
*/
protected function streamImage($status, $image, $outputFilename)
{
$imageCreator = $this->container->get('image_creator');
//generating the streamed response
$response = new StreamedResponse(null, $status);
$response->setCallback(function () use ($imageCreator, $image) {
$imageCreator->streamRawImageData($image);
});
$response->headers->set('Content-Type', 'image/png');
$response->headers->set('Content-Disposition', 'inline; filename="'.$outputFilename.'"');
$response->send();
$imageCreator->destroyImage($image);
return $response;
}
}