|
| 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