Skip to content

Commit

Permalink
Add notification API
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Apr 13, 2012
1 parent 2198ebe commit 505e0eb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
52 changes: 50 additions & 2 deletions src/Packagist/WebBundle/Controller/ApiController.php
Expand Up @@ -15,6 +15,7 @@
use Composer\IO\NullIO;
use Composer\Repository\VcsRepository;
use Packagist\WebBundle\Package\Updater;
use Packagist\WebBundle\Entity\Package;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -40,14 +41,19 @@ public function packagesAction()
$packages = $em->getRepository('Packagist\WebBundle\Entity\Package')
->getFullPackages();

$data = array();
$notifyUrl = $this->generateUrl('track_download', array('name' => 'VND/PKG'));

$data = array(
'notify' => str_replace('VND/PKG', '%package%', $notifyUrl),
'packages' => array(),
);
foreach ($packages as $package) {
$versions = array();
foreach ($package->getVersions() as $version) {
$versions[$version->getVersion()] = $version->toArray();
$em->detach($version);
}
$data[$package->getName()] = array('versions' => $versions);
$data['packages'][$package->getName()] = array($versions);
$em->detach($package);
}
unset($versions, $package, $packages);
Expand Down Expand Up @@ -106,4 +112,46 @@ public function githubPostReceive(Request $request)

return new Response(json_encode(array('status' => 'error', 'message' => 'Could not find a package that matches this request (does user maintain the package?)',)), 404);
}

/**
* @Route("/downloads/{name}", name="track_download", requirements={"name"="[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+"}, defaults={"_format" = "json"})
* @Method({"POST"})
*/
public function trackDownloadAction(Request $request, $name)
{
$result = $this->getDoctrine()->getConnection()->fetchAssoc(
'SELECT p.id, v.id vid
FROM package p
LEFT JOIN package_version v ON p.id = v.package_id
WHERE p.name = ?
AND v.normalizedVersion = ?
LIMIT 1',
array($name, $request->request->get('version_normalized'))
);

if (!$result) {
return new Response('{"status": "error", "message": "Package not found"}', 200);
}

$redis = $this->get('snc_redis.default');
$id = $result['id'];
$version = $result['vid'];

$throttleKey = 'dl:'.$id.':'.$request->getClientIp().':'.date('Ymd');
$requests = $redis->incr($throttleKey);
if (1 === $requests) {
$redis->expire($throttleKey, 86400);
}
if ($requests <= 10) {
$redis->incr('dl:'.$id.':'.date('Ymd'));
$redis->incr('dl:'.$id.':'.date('Ym'));
$redis->incr('dl:'.$id);

$redis->incr('dl:'.$id.'-'.$version.':'.date('Ymd'));
$redis->incr('dl:'.$id.'-'.$version.':'.date('Ym'));
$redis->incr('dl:'.$id.'-'.$version);
}

return new Response('{"status": "success"}', 201);
}
}
11 changes: 10 additions & 1 deletion src/Packagist/WebBundle/Package/Dumper.php
Expand Up @@ -14,6 +14,7 @@

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Routing\RouterInterface;
use Packagist\WebBundle\Entity\Version;

/**
Expand Down Expand Up @@ -42,6 +43,11 @@ class Dumper
*/
protected $buildDir;

/**
* @var RouterInterface
*/
protected $router;

/**
* Data cache
* @var array
Expand All @@ -55,10 +61,11 @@ class Dumper
* @param string $webDir web root
* @param string $cacheDir cache dir
*/
public function __construct(RegistryInterface $doctrine, Filesystem $filesystem, $webDir, $cacheDir)
public function __construct(RegistryInterface $doctrine, Filesystem $filesystem, RouterInterface $router, $webDir, $cacheDir)
{
$this->doctrine = $doctrine;
$this->fs = $filesystem;
$this->router = $router;
$this->webDir = realpath($webDir);
$this->buildDir = $cacheDir . '/composer-packages-build';
}
Expand Down Expand Up @@ -112,6 +119,8 @@ public function dump(array $packages, $force = false)
if (!isset($this->files['packages.json']['packages'])) {
$this->files['packages.json']['packages'] = array();
}
$url = $this->router->generate('track_download', array('name' => 'VND/PKG'));
$this->files['packages.json']['notify'] = str_replace('VND/PKG', '%package%', $url);

// dump files to build dir
foreach ($modifiedFiles as $file => $dummy) {
Expand Down
2 changes: 1 addition & 1 deletion src/Packagist/WebBundle/Resources/config/services.yml
Expand Up @@ -7,4 +7,4 @@ services:

packagist.package_dumper:
class: Packagist\WebBundle\Package\Dumper
arguments: [ @doctrine, @filesystem, %kernel.root_dir%/../web/, %kernel.cache_dir% ]
arguments: [ @doctrine, @filesystem, @router, %kernel.root_dir%/../web/, %kernel.cache_dir% ]

0 comments on commit 505e0eb

Please sign in to comment.