1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\AppBundle\Event\Listener; |
10
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\ApiClientBundle\Service\Client; |
12
|
|
|
use AnimeDb\Bundle\AppBundle\Service\Downloader; |
13
|
|
|
use AnimeDb\Bundle\AnimeDbBundle\Event\Package\Installed as InstalledEvent; |
14
|
|
|
use AnimeDb\Bundle\AnimeDbBundle\Event\Package\Removed as RemovedEvent; |
15
|
|
|
use AnimeDb\Bundle\AnimeDbBundle\Event\Package\Updated as UpdatedEvent; |
16
|
|
|
use AnimeDb\Bundle\AppBundle\Entity\Plugin; |
17
|
|
|
use Composer\Package\Package as ComposerPackage; |
18
|
|
|
use AnimeDb\Bundle\AnimeDbBundle\Manipulator\Parameters; |
19
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
20
|
|
|
use Doctrine\ORM\EntityRepository; |
21
|
|
|
|
22
|
|
|
class Package |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const PLUGIN_TYPE = 'anime-db-plugin'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const PACKAGE_SHMOP = 'anime-db/shmop'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var EntityManagerInterface |
36
|
|
|
*/ |
37
|
|
|
protected $em; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var EntityRepository |
41
|
|
|
*/ |
42
|
|
|
protected $rep; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* API client. |
46
|
|
|
* |
47
|
|
|
* @var Client |
48
|
|
|
*/ |
49
|
|
|
protected $client; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Parameters |
53
|
|
|
*/ |
54
|
|
|
protected $parameters; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Downloader |
58
|
|
|
*/ |
59
|
|
|
protected $downloader; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param EntityManagerInterface $em |
63
|
|
|
* @param Client $client |
64
|
|
|
* @param Downloader $downloader |
65
|
|
|
* @param Parameters $parameters |
66
|
|
|
*/ |
67
|
17 |
|
public function __construct( |
68
|
|
|
EntityManagerInterface $em, |
69
|
|
|
Client $client, |
70
|
|
|
Downloader $downloader, |
71
|
|
|
Parameters $parameters |
72
|
|
|
) { |
73
|
17 |
|
$this->client = $client; |
74
|
17 |
|
$this->downloader = $downloader; |
75
|
17 |
|
$this->em = $em; |
76
|
17 |
|
$this->parameters = $parameters; |
77
|
17 |
|
$this->rep = $em->getRepository('AnimeDbAppBundle:Plugin'); |
78
|
17 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Update plugin data. |
82
|
|
|
* |
83
|
|
|
* @param UpdatedEvent $event |
84
|
|
|
*/ |
85
|
5 |
|
public function onUpdated(UpdatedEvent $event) |
86
|
|
|
{ |
87
|
5 |
|
if ($event->getPackage()->getType() == self::PLUGIN_TYPE) { |
88
|
4 |
|
$this->addPackage($event->getPackage()); |
89
|
|
|
} |
90
|
5 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Registr plugin. |
94
|
|
|
* |
95
|
|
|
* @param InstalledEvent $event |
96
|
|
|
*/ |
97
|
5 |
|
public function onInstalled(InstalledEvent $event) |
98
|
|
|
{ |
99
|
5 |
|
if ($event->getPackage()->getType() == self::PLUGIN_TYPE) { |
100
|
4 |
|
$this->addPackage($event->getPackage()); |
101
|
|
|
} |
102
|
5 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Add plugin from package. |
106
|
|
|
* |
107
|
|
|
* @param ComposerPackage $package |
108
|
|
|
*/ |
109
|
8 |
|
protected function addPackage(ComposerPackage $package) |
110
|
|
|
{ |
111
|
8 |
|
$plugin = $this->rep->find($package->getName()); |
112
|
|
|
|
113
|
|
|
// create new plugin if not exists |
114
|
8 |
|
if (!$plugin) { |
115
|
4 |
|
$plugin = new Plugin(); |
116
|
4 |
|
$plugin->setName($package->getName()); |
117
|
|
|
} |
118
|
|
|
|
119
|
8 |
|
list($vendor, $package) = explode('/', $plugin->getName()); |
120
|
|
|
|
121
|
|
|
try { |
122
|
8 |
|
$data = $this->client->getPlugin($vendor, $package); |
123
|
8 |
|
$plugin->setTitle($data['title'])->setDescription($data['description']); |
124
|
8 |
|
if ($data['logo']) { |
125
|
8 |
|
$this->downloader->entity($data['logo'], $plugin, true); |
126
|
|
|
} |
127
|
|
|
} catch (\Exception $e) { |
128
|
|
|
// is not a critical error |
129
|
|
|
} |
130
|
|
|
|
131
|
8 |
|
$this->em->persist($plugin); |
132
|
8 |
|
$this->em->flush(); |
133
|
8 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Unregistr plugin. |
137
|
|
|
* |
138
|
|
|
* @param RemovedEvent $event |
139
|
|
|
*/ |
140
|
3 |
|
public function onRemoved(RemovedEvent $event) |
141
|
|
|
{ |
142
|
3 |
|
if ($event->getPackage()->getType() == self::PLUGIN_TYPE) { |
143
|
2 |
|
$plugin = $this->rep->find($event->getPackage()->getName()); |
144
|
|
|
|
145
|
2 |
|
if ($plugin) { |
146
|
1 |
|
$this->em->remove($plugin); |
147
|
1 |
|
$this->em->flush(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
3 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Configure shmop. |
154
|
|
|
* |
155
|
|
|
* @param InstalledEvent $event |
156
|
|
|
*/ |
157
|
2 |
|
public function onInstalledConfigureShmop(InstalledEvent $event) |
158
|
|
|
{ |
159
|
|
|
// use Shmop as driver for Cache Time Keeper |
160
|
2 |
|
if ($event->getPackage()->getName() == self::PACKAGE_SHMOP) { |
161
|
1 |
|
$this->parameters->set('cache_time_keeper.driver', 'cache_time_keeper.driver.multi'); |
162
|
1 |
|
$this->parameters->set('cache_time_keeper.driver.multi.fast', 'cache_time_keeper.driver.shmop'); |
163
|
|
|
} |
164
|
2 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Restore config on removed shmop. |
168
|
|
|
* |
169
|
|
|
* @param RemovedEvent $event |
170
|
|
|
*/ |
171
|
2 |
|
public function onRemovedShmop(RemovedEvent $event) |
172
|
|
|
{ |
173
|
2 |
|
if ($event->getPackage()->getName() == self::PACKAGE_SHMOP) { |
174
|
1 |
|
$this->parameters->set('cache_time_keeper.driver', 'cache_time_keeper.driver.file'); |
175
|
|
|
} |
176
|
2 |
|
} |
177
|
|
|
} |
178
|
|
|
|