Skip to content

Commit 3b61309

Browse files
committed
Wrap plugin functionality.
1 parent 81e5107 commit 3b61309

File tree

2 files changed

+199
-142
lines changed

2 files changed

+199
-142
lines changed

src/Plugin.php

Lines changed: 5 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,15 @@
2424
class Plugin implements PluginInterface, EventSubscriberInterface {
2525

2626
/**
27-
* @var \Composer\IO\IOInterface
27+
* @var \derhasi\Composer\PluginWrapper
2828
*/
29-
protected $io;
30-
31-
/**
32-
* @var \Composer\Composer
33-
*/
34-
protected $composer;
35-
36-
/**
37-
* @var \Composer\Util\Filesystem
38-
*/
39-
protected $filesystem;
40-
41-
/**
42-
* @var \derhasi\Composer\PathPreserver[string]
43-
*/
44-
protected $preservers;
29+
protected $wrapper;
4530

4631
/**
4732
* {@inheritdoc}
4833
*/
4934
public function activate(Composer $composer, IOInterface $io) {
50-
$this->io = $io;
51-
$this->composer = $composer;
52-
$this->filesystem = new Filesystem();
35+
$this->wrapper = new PluginWrapper($composer, $io);
5336
}
5437

5538
/**
@@ -73,21 +56,7 @@ public static function getSubscribedEvents() {
7356
*/
7457
public function prePackage(PackageEvent $event) {
7558

76-
$packages = $this->getPackagesFromEvent($event);
77-
$paths = $this->getInstallPathsFromPackages($packages);
78-
79-
$preserver = new PathPreserver(
80-
$paths,
81-
$this->getPreservePaths(),
82-
$this->composer->getConfig()->get('cache-dir'),
83-
$this->filesystem,
84-
$this->io
85-
);
86-
87-
// Store preserver for reuse in post package.
88-
$this->preservers[$this->getUniqueNameFromPackages($packages)] = $preserver;
89-
90-
$preserver->preserve();
59+
$this->wrapper->prePackage($event);
9160
}
9261

9362
/**
@@ -96,113 +65,7 @@ public function prePackage(PackageEvent $event) {
9665
* @param \Composer\Script\PackageEvent $event
9766
*/
9867
public function postPackage(PackageEvent $event) {
99-
$packages = $this->getPackagesFromEvent($event);
100-
$key = $this->getUniqueNameFromPackages($packages);
101-
if ($this->preservers[$key]) {
102-
$this->preservers[$key]->rollback();
103-
unset($this->preservers[$key]);
104-
}
105-
}
106-
107-
/**
108-
* Retrieves relevant package from the event.
109-
*
110-
* In the case of update, the target package is retrieved, as that will
111-
* provide the path the package will be installed to.
112-
*
113-
* @param \Composer\Script\PackageEvent $event
114-
* @return \Composer\Package\PackageInterface[]
115-
* @throws \Exception
116-
*/
117-
protected function getPackagesFromEvent(PackageEvent $event) {
118-
119-
$operation = $event->getOperation();
120-
if ($operation instanceof InstallOperation) {
121-
$packages = array($operation->getPackage());
122-
}
123-
elseif ($operation instanceof UpdateOperation) {
124-
$packages = array(
125-
$operation->getInitialPackage(),
126-
$operation->getTargetPackage(),
127-
);
128-
}
129-
elseif ($operation instanceof UninstallOperation) {
130-
$packages = array($operation->getPackage());
131-
}
132-
133-
return $packages;
68+
$this->wrapper->postPackage($event);
13469
}
13570

136-
/**
137-
* Retrieve install paths from package installers.
138-
*
139-
* @param \Composer\Package\PackageInterface[] $packages
140-
*
141-
* @return string[]
142-
*/
143-
protected function getInstallPathsFromPackages(array $packages) {
144-
/** @var \Composer\Installer\InstallationManager $installationManager */
145-
$installationManager = $this->composer->getInstallationManager();
146-
147-
$paths = array();
148-
foreach ($packages as $package) {
149-
$paths[] = $installationManager->getInstallPath($package);
150-
}
151-
return $this->absolutePaths($paths);
152-
}
153-
154-
/**
155-
* Provides a unique string for a package combination.
156-
*
157-
* @param \Composer\Package\PackageInterface[] $packages
158-
*
159-
* @return string
160-
*/
161-
protected function getUniqueNameFromPackages(array $packages) {
162-
$return = array();
163-
foreach ($packages as $package) {
164-
$return[] = $package->getUniqueName();
165-
}
166-
sort($return);
167-
return implode(', ', $return);
168-
}
169-
170-
/**
171-
* Get preserve paths from root configuration.
172-
*
173-
* @return string[]
174-
*/
175-
protected function getPreservePaths() {
176-
$extra = $this->composer->getPackage()->getExtra();
177-
178-
if (!isset($extra['preserve-paths'])) {
179-
$paths = $extra['preserve-paths'];
180-
}
181-
elseif (!is_array($extra['preserve-paths']) && !is_object($extra['preserve-paths'])) {
182-
$paths = array($extra['preserve-paths']);
183-
}
184-
else {
185-
$paths = array_values((array) $extra['preserve-paths']);
186-
}
187-
188-
return $this->absolutePaths($paths);
189-
}
190-
191-
/**
192-
* Helper to convert relative paths to absolute ones.
193-
*
194-
* @param string[] $paths
195-
* @return string[]
196-
*/
197-
protected function absolutePaths($paths) {
198-
$return = array();
199-
foreach ($paths as $path) {
200-
201-
if (!$this->filesystem->isAbsolutePath($path)) {
202-
$path = getcwd() . '/' . $path;
203-
}
204-
$return[] = $path;
205-
}
206-
return $return;
207-
}
20871
}

src/PluginWrapper.php

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains derhasi\Composer\Plugin.
6+
*/
7+
8+
namespace derhasi\Composer;
9+
10+
use Composer\Composer;
11+
use Composer\DependencyResolver\Operation\InstallOperation;
12+
use Composer\DependencyResolver\Operation\UninstallOperation;
13+
use Composer\DependencyResolver\Operation\UpdateOperation;
14+
use Composer\EventDispatcher\EventSubscriberInterface;
15+
use Composer\IO\IOInterface;
16+
use Composer\Plugin\PluginInterface;
17+
use Composer\Script\PackageEvent;
18+
use Composer\Script\ScriptEvents;
19+
use Composer\Util\Filesystem;
20+
21+
/**
22+
* Wrapper for making Plugin debuggable.
23+
*/
24+
class PluginWrapper implements {
25+
26+
/**
27+
* @var \Composer\IO\IOInterface
28+
*/
29+
protected $io;
30+
31+
/**
32+
* @var \Composer\Composer
33+
*/
34+
protected $composer;
35+
36+
/**
37+
* @var \Composer\Util\Filesystem
38+
*/
39+
protected $filesystem;
40+
41+
/**
42+
* @var \derhasi\Composer\PathPreserver[string]
43+
*/
44+
protected $preservers;
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function __construct(Composer $composer, IOInterface $io) {
50+
$this->io = $io;
51+
$this->composer = $composer;
52+
$this->filesystem = new Filesystem();
53+
}
54+
55+
/**
56+
* Pre Package event behaviour for backing up preserved paths.
57+
*
58+
* @param \Composer\Script\PackageEvent $event
59+
*/
60+
public function prePackage(PackageEvent $event) {
61+
62+
$packages = $this->getPackagesFromEvent($event);
63+
$paths = $this->getInstallPathsFromPackages($packages);
64+
65+
$preserver = new PathPreserver(
66+
$paths,
67+
$this->getPreservePaths(),
68+
$this->composer->getConfig()->get('cache-dir'),
69+
$this->filesystem,
70+
$this->io
71+
);
72+
73+
// Store preserver for reuse in post package.
74+
$this->preservers[$this->getUniqueNameFromPackages($packages)] = $preserver;
75+
76+
$preserver->preserve();
77+
}
78+
79+
/**
80+
* Pre Package event behaviour for backing up preserved paths.
81+
*
82+
* @param \Composer\Script\PackageEvent $event
83+
*/
84+
public function postPackage(PackageEvent $event) {
85+
$packages = $this->getPackagesFromEvent($event);
86+
$key = $this->getUniqueNameFromPackages($packages);
87+
if ($this->preservers[$key]) {
88+
$this->preservers[$key]->rollback();
89+
unset($this->preservers[$key]);
90+
}
91+
}
92+
93+
/**
94+
* Retrieves relevant package from the event.
95+
*
96+
* In the case of update, the target package is retrieved, as that will
97+
* provide the path the package will be installed to.
98+
*
99+
* @param \Composer\Script\PackageEvent $event
100+
* @return \Composer\Package\PackageInterface[]
101+
* @throws \Exception
102+
*/
103+
protected function getPackagesFromEvent(PackageEvent $event) {
104+
105+
$operation = $event->getOperation();
106+
if ($operation instanceof InstallOperation) {
107+
$packages = array($operation->getPackage());
108+
}
109+
elseif ($operation instanceof UpdateOperation) {
110+
$packages = array(
111+
$operation->getInitialPackage(),
112+
$operation->getTargetPackage(),
113+
);
114+
}
115+
elseif ($operation instanceof UninstallOperation) {
116+
$packages = array($operation->getPackage());
117+
}
118+
119+
return $packages;
120+
}
121+
122+
/**
123+
* Retrieve install paths from package installers.
124+
*
125+
* @param \Composer\Package\PackageInterface[] $packages
126+
*
127+
* @return string[]
128+
*/
129+
protected function getInstallPathsFromPackages(array $packages) {
130+
/** @var \Composer\Installer\InstallationManager $installationManager */
131+
$installationManager = $this->composer->getInstallationManager();
132+
133+
$paths = array();
134+
foreach ($packages as $package) {
135+
$paths[] = $installationManager->getInstallPath($package);
136+
}
137+
return $this->absolutePaths($paths);
138+
}
139+
140+
/**
141+
* Provides a unique string for a package combination.
142+
*
143+
* @param \Composer\Package\PackageInterface[] $packages
144+
*
145+
* @return string
146+
*/
147+
protected function getUniqueNameFromPackages(array $packages) {
148+
$return = array();
149+
foreach ($packages as $package) {
150+
$return[] = $package->getUniqueName();
151+
}
152+
sort($return);
153+
return implode(', ', $return);
154+
}
155+
156+
/**
157+
* Get preserve paths from root configuration.
158+
*
159+
* @return string[]
160+
*/
161+
protected function getPreservePaths() {
162+
$extra = $this->composer->getPackage()->getExtra();
163+
164+
if (!isset($extra['preserve-paths'])) {
165+
$paths = $extra['preserve-paths'];
166+
}
167+
elseif (!is_array($extra['preserve-paths']) && !is_object($extra['preserve-paths'])) {
168+
$paths = array($extra['preserve-paths']);
169+
}
170+
else {
171+
$paths = array_values((array) $extra['preserve-paths']);
172+
}
173+
174+
return $this->absolutePaths($paths);
175+
}
176+
177+
/**
178+
* Helper to convert relative paths to absolute ones.
179+
*
180+
* @param string[] $paths
181+
* @return string[]
182+
*/
183+
protected function absolutePaths($paths) {
184+
$return = array();
185+
foreach ($paths as $path) {
186+
187+
if (!$this->filesystem->isAbsolutePath($path)) {
188+
$path = getcwd() . '/' . $path;
189+
}
190+
$return[] = $path;
191+
}
192+
return $return;
193+
}
194+
}

0 commit comments

Comments
 (0)