This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ComposerPresenter.php
91 lines (70 loc) · 2.16 KB
/
ComposerPresenter.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
<?php
namespace NetteAddons\Api;
use NetteAddons\Model\Addon;
use NetteAddons\Model\Addons;
use NetteAddons\Model\AddonVersion;
use NetteAddons\Model\AddonVersions;
use NetteAddons\Model\AddonDownloads;
use NetteAddons\Model\Utils\Composer;
final class ComposerPresenter extends \NetteAddons\BasePresenter
{
/** @var \NetteAddons\Model\Addons */
public $addons;
/** @var \NetteAddons\Model\AddonDownloads */
public $addonDownloads;
/** @var \NetteAddons\Model\AddonVersions */
public $addonVersions;
public function __construct(Addons $addons, AddonDownloads $addonDownloads, AddonVersions $addonVersions)
{
parent::__construct();
$this->addons = $addons;
$this->addonDownloads = $addonDownloads;
$this->addonVersions = $addonVersions;
}
public function renderPackages()
{
$addons = $this->addons->findAll();
$addons = array_map('NetteAddons\Model\Addon::fromActiveRow', iterator_to_array($addons));
$packagesJson = Composer::createPackagesJson($addons);
$packagesJson->notify = str_replace(
'placeholder',
'%package%',
$this->link('//downloadNotify', array('package' => 'placeholder'))
);
$this->sendJson($packagesJson);
}
/**
* Called when composer installs a package to increase counters.
*
* @link http://getcomposer.org/doc/05-repositories.md#notify
*
* @param string
*/
public function actionDownloadNotify($package)
{
$post = $this->getRequest()->getPost();
if (!isset($post['version'])) {
$this->error('Invalid request.');
}
$version = (string) $post['version'];
if (!$addonRow = $this->addons->findOneByComposerFullName($package)) {
$this->error('Package not found.');
}
$addon = Addon::fromActiveRow($addonRow);
$versionRow = $this->addonVersions->findOneBy(array(
'addonId' => $addon->id,
'version' => $version,
));
if (!$versionRow) {
$this->error('Version of package not found.');
}
$version = AddonVersion::fromActiveRow($versionRow);
$this->addonDownloads->saveDownload(
AddonDownloads::TYPE_INSTALL,
$version->id,
$this->getHttpRequest()->getRemoteAddress(),
$this->getHttpRequest()->getHeader('user-agent')
);
$this->sendJson(array('status' => 'success'));
}
}