PluginManager.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Plugin;
  12. use Composer\Composer;
  13. use Composer\EventDispatcher\EventSubscriberInterface;
  14. use Composer\IO\IOInterface;
  15. use Composer\Package\CompletePackage;
  16. use Composer\Package\Package;
  17. use Composer\Package\Version\VersionParser;
  18. use Composer\Repository\RepositoryInterface;
  19. use Composer\Package\PackageInterface;
  20. use Composer\Package\Link;
  21. use Composer\Repository\RepositorySet;
  22. use Composer\Semver\Constraint\Constraint;
  23. use Composer\Plugin\Capability\Capability;
  24. use Composer\Util\PackageSorter;
  25. /**
  26. * Plugin manager
  27. *
  28. * @author Nils Adermann <naderman@naderman.de>
  29. * @author Jordi Boggiano <j.boggiano@seld.be>
  30. */
  31. class PluginManager
  32. {
  33. protected $composer;
  34. protected $io;
  35. protected $globalComposer;
  36. protected $versionParser;
  37. protected $disablePlugins = false;
  38. protected $plugins = array();
  39. protected $registeredPlugins = array();
  40. private static $classCounter = 0;
  41. /**
  42. * Initializes plugin manager
  43. *
  44. * @param IOInterface $io
  45. * @param Composer $composer
  46. * @param Composer $globalComposer
  47. * @param bool $disablePlugins
  48. */
  49. public function __construct(IOInterface $io, Composer $composer, Composer $globalComposer = null, $disablePlugins = false)
  50. {
  51. $this->io = $io;
  52. $this->composer = $composer;
  53. $this->globalComposer = $globalComposer;
  54. $this->versionParser = new VersionParser();
  55. $this->disablePlugins = $disablePlugins;
  56. }
  57. /**
  58. * Loads all plugins from currently installed plugin packages
  59. */
  60. public function loadInstalledPlugins()
  61. {
  62. if ($this->disablePlugins) {
  63. return;
  64. }
  65. $repo = $this->composer->getRepositoryManager()->getLocalRepository();
  66. $globalRepo = $this->globalComposer ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null;
  67. if ($repo) {
  68. $this->loadRepository($repo);
  69. }
  70. if ($globalRepo) {
  71. $this->loadRepository($globalRepo);
  72. }
  73. }
  74. /**
  75. * Gets all currently active plugin instances
  76. *
  77. * @return array plugins
  78. */
  79. public function getPlugins()
  80. {
  81. return $this->plugins;
  82. }
  83. /**
  84. * Gets global composer or null when main composer is not fully loaded
  85. *
  86. * @return Composer|null
  87. */
  88. public function getGlobalComposer()
  89. {
  90. return $this->globalComposer;
  91. }
  92. /**
  93. * Register a plugin package, activate it etc.
  94. *
  95. * If it's of type composer-installer it is registered as an installer
  96. * instead for BC
  97. *
  98. * @param PackageInterface $package
  99. * @param bool $failOnMissingClasses By default this silently skips plugins that can not be found, but if set to true it fails with an exception
  100. *
  101. * @throws \UnexpectedValueException
  102. */
  103. public function registerPackage(PackageInterface $package, $failOnMissingClasses = false)
  104. {
  105. if ($this->disablePlugins) {
  106. return;
  107. }
  108. if ($package->getType() === 'composer-plugin') {
  109. $requiresComposer = null;
  110. foreach ($package->getRequires() as $link) { /** @var Link $link */
  111. if ('composer-plugin-api' === $link->getTarget()) {
  112. $requiresComposer = $link->getConstraint();
  113. break;
  114. }
  115. }
  116. if (!$requiresComposer) {
  117. throw new \RuntimeException("Plugin ".$package->getName()." is missing a require statement for a version of the composer-plugin-api package.");
  118. }
  119. $currentPluginApiVersion = $this->getPluginApiVersion();
  120. $currentPluginApiConstraint = new Constraint('==', $this->versionParser->normalize($currentPluginApiVersion));
  121. if ($requiresComposer->getPrettyString() === '1.0.0' && $this->getPluginApiVersion() === '1.0.0') {
  122. $this->io->writeError('<warning>The "' . $package->getName() . '" plugin requires composer-plugin-api 1.0.0, this *WILL* break in the future and it should be fixed ASAP (require ^1.0 for example).</warning>');
  123. } elseif (!$requiresComposer->matches($currentPluginApiConstraint)) {
  124. $this->io->writeError('<warning>The "' . $package->getName() . '" plugin was skipped because it requires a Plugin API version ("' . $requiresComposer->getPrettyString() . '") that does not match your Composer installation ("' . $currentPluginApiVersion . '"). You may need to run composer update with the "--no-plugins" option.</warning>');
  125. return;
  126. }
  127. }
  128. $oldInstallerPlugin = ($package->getType() === 'composer-installer');
  129. if (isset($this->registeredPlugins[$package->getName()])) {
  130. return;
  131. }
  132. $extra = $package->getExtra();
  133. if (empty($extra['class'])) {
  134. throw new \UnexpectedValueException('Error while installing '.$package->getPrettyName().', composer-plugin packages should have a class defined in their extra key to be usable.');
  135. }
  136. $classes = is_array($extra['class']) ? $extra['class'] : array($extra['class']);
  137. $localRepo = $this->composer->getRepositoryManager()->getLocalRepository();
  138. $globalRepo = $this->globalComposer ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null;
  139. $repositorySet = new RepositorySet(array(), 'dev');
  140. $repositorySet->addRepository($localRepo);
  141. if ($globalRepo) {
  142. $repositorySet->addRepository($globalRepo);
  143. }
  144. $autoloadPackages = array($package->getName() => $package);
  145. $autoloadPackages = $this->collectDependencies($repositorySet, $autoloadPackages, $package);
  146. $generator = $this->composer->getAutoloadGenerator();
  147. $autoloads = array();
  148. foreach ($autoloadPackages as $autoloadPackage) {
  149. $downloadPath = $this->getInstallPath($autoloadPackage, $globalRepo && $globalRepo->hasPackage($autoloadPackage));
  150. $autoloads[] = array($autoloadPackage, $downloadPath);
  151. }
  152. $map = $generator->parseAutoloads($autoloads, new Package('dummy', '1.0.0.0', '1.0.0'));
  153. $classLoader = $generator->createLoader($map);
  154. $classLoader->register();
  155. foreach ($classes as $class) {
  156. if (class_exists($class, false)) {
  157. $class = trim($class, '\\');
  158. $path = $classLoader->findFile($class);
  159. $code = file_get_contents($path);
  160. $separatorPos = strrpos($class, '\\');
  161. $className = $class;
  162. if ($separatorPos) {
  163. $className = substr($class, $separatorPos + 1);
  164. }
  165. $code = preg_replace('{^((?:final\s+)?(?:\s*))class\s+('.preg_quote($className).')}mi', '$1class $2_composer_tmp'.self::$classCounter, $code, 1);
  166. $code = str_replace('__FILE__', var_export($path, true), $code);
  167. $code = str_replace('__DIR__', var_export(dirname($path), true), $code);
  168. $code = str_replace('__CLASS__', var_export($class, true), $code);
  169. $code = preg_replace('/^\s*<\?(php)?/i', '', $code, 1);
  170. eval($code);
  171. $class .= '_composer_tmp'.self::$classCounter;
  172. self::$classCounter++;
  173. }
  174. if ($oldInstallerPlugin) {
  175. $installer = new $class($this->io, $this->composer);
  176. $this->composer->getInstallationManager()->addInstaller($installer);
  177. $this->registeredPlugins[$package->getName()] = $installer;
  178. } elseif (class_exists($class)) {
  179. $plugin = new $class();
  180. $this->addPlugin($plugin);
  181. $this->registeredPlugins[$package->getName()] = $plugin;
  182. } elseif ($failOnMissingClasses) {
  183. throw new \UnexpectedValueException('Plugin '.$package->getName().' could not be initialized, class not found: '.$class);
  184. }
  185. }
  186. }
  187. /**
  188. * Deactivates a plugin package
  189. *
  190. * If it's of type composer-installer it is unregistered from the installers
  191. * instead for BC
  192. *
  193. * @param PackageInterface $package
  194. *
  195. * @throws \UnexpectedValueException
  196. */
  197. public function deactivatePackage(PackageInterface $package)
  198. {
  199. if ($this->disablePlugins) {
  200. return;
  201. }
  202. $oldInstallerPlugin = ($package->getType() === 'composer-installer');
  203. if (!isset($this->registeredPlugins[$package->getName()])) {
  204. return;
  205. }
  206. if ($oldInstallerPlugin) {
  207. $installer = $this->registeredPlugins[$package->getName()];
  208. unset($this->registeredPlugins[$package->getName()]);
  209. $this->composer->getInstallationManager()->removeInstaller($installer);
  210. } else {
  211. $plugin = $this->registeredPlugins[$package->getName()];
  212. unset($this->registeredPlugins[$package->getName()]);
  213. $this->removePlugin($plugin);
  214. }
  215. }
  216. /**
  217. * Uninstall a plugin package
  218. *
  219. * If it's of type composer-installer it is unregistered from the installers
  220. * instead for BC
  221. *
  222. * @param PackageInterface $package
  223. *
  224. * @throws \UnexpectedValueException
  225. */
  226. public function uninstallPackage(PackageInterface $package)
  227. {
  228. if ($this->disablePlugins) {
  229. return;
  230. }
  231. $oldInstallerPlugin = ($package->getType() === 'composer-installer');
  232. if (!isset($this->registeredPlugins[$package->getName()])) {
  233. return;
  234. }
  235. if ($oldInstallerPlugin) {
  236. $this->deactivatePackage($package);
  237. } else {
  238. $plugin = $this->registeredPlugins[$package->getName()];
  239. unset($this->registeredPlugins[$package->getName()]);
  240. $this->removePlugin($plugin);
  241. $this->uninstallPlugin($plugin);
  242. }
  243. }
  244. /**
  245. * Returns the version of the internal composer-plugin-api package.
  246. *
  247. * @return string
  248. */
  249. protected function getPluginApiVersion()
  250. {
  251. return PluginInterface::PLUGIN_API_VERSION;
  252. }
  253. /**
  254. * Adds a plugin, activates it and registers it with the event dispatcher
  255. *
  256. * Ideally plugin packages should be registered via registerPackage, but if you use Composer
  257. * programmatically and want to register a plugin class directly this is a valid way
  258. * to do it.
  259. *
  260. * @param PluginInterface $plugin plugin instance
  261. */
  262. public function addPlugin(PluginInterface $plugin)
  263. {
  264. $this->io->writeError('Loading plugin '.get_class($plugin), true, IOInterface::DEBUG);
  265. $this->plugins[] = $plugin;
  266. $plugin->activate($this->composer, $this->io);
  267. if ($plugin instanceof EventSubscriberInterface) {
  268. $this->composer->getEventDispatcher()->addSubscriber($plugin);
  269. }
  270. }
  271. /**
  272. * Removes a plugin, deactivates it and removes any listener the plugin has set on the plugin instance
  273. *
  274. * Ideally plugin packages should be deactivated via deactivatePackage, but if you use Composer
  275. * programmatically and want to deregister a plugin class directly this is a valid way
  276. * to do it.
  277. *
  278. * @param PluginInterface $plugin plugin instance
  279. */
  280. public function removePlugin(PluginInterface $plugin)
  281. {
  282. $index = array_search($plugin, $this->plugins, true);
  283. if ($index === false) {
  284. return;
  285. }
  286. $this->io->writeError('Unloading plugin '.get_class($plugin), true, IOInterface::DEBUG);
  287. unset($this->plugins[$index]);
  288. $plugin->deactivate($this->composer, $this->io);
  289. $this->composer->getEventDispatcher()->removeListener($plugin);
  290. }
  291. /**
  292. * Notifies a plugin it is being uninstalled and should clean up
  293. *
  294. * Ideally plugin packages should be uninstalled via uninstallPackage, but if you use Composer
  295. * programmatically and want to deregister a plugin class directly this is a valid way
  296. * to do it.
  297. *
  298. * @param PluginInterface $plugin plugin instance
  299. */
  300. public function uninstallPlugin(PluginInterface $plugin)
  301. {
  302. $this->io->writeError('Uninstalling plugin '.get_class($plugin), true, IOInterface::DEBUG);
  303. $plugin->uninstall($this->composer, $this->io);
  304. }
  305. /**
  306. * Load all plugins and installers from a repository
  307. *
  308. * Note that plugins in the specified repository that rely on events that
  309. * have fired prior to loading will be missed. This means you likely want to
  310. * call this method as early as possible.
  311. *
  312. * @param RepositoryInterface $repo Repository to scan for plugins to install
  313. *
  314. * @throws \RuntimeException
  315. */
  316. private function loadRepository(RepositoryInterface $repo)
  317. {
  318. $packages = $repo->getPackages();
  319. $sortedPackages = array_reverse(PackageSorter::sortPackages($packages));
  320. foreach ($sortedPackages as $package) {
  321. if (!($package instanceof CompletePackage)) {
  322. continue;
  323. }
  324. if ('composer-plugin' === $package->getType()) {
  325. $this->registerPackage($package);
  326. // Backward compatibility
  327. } elseif ('composer-installer' === $package->getType()) {
  328. $this->registerPackage($package);
  329. }
  330. }
  331. }
  332. /**
  333. * Recursively generates a map of package names to packages for all deps
  334. *
  335. * @param RepositorySet $repositorySet Repository set of installed packages
  336. * @param array $collected Current state of the map for recursion
  337. * @param PackageInterface $package The package to analyze
  338. *
  339. * @return array Map of package names to packages
  340. */
  341. private function collectDependencies(RepositorySet $repositorySet, array $collected, PackageInterface $package)
  342. {
  343. $requires = array_merge(
  344. $package->getRequires(),
  345. $package->getDevRequires()
  346. );
  347. foreach ($requires as $requireLink) {
  348. $requiredPackage = $this->lookupInstalledPackage($repositorySet, $requireLink);
  349. if ($requiredPackage && !isset($collected[$requiredPackage->getName()])) {
  350. $collected[$requiredPackage->getName()] = $requiredPackage;
  351. $collected = $this->collectDependencies($repositorySet, $collected, $requiredPackage);
  352. }
  353. }
  354. return $collected;
  355. }
  356. /**
  357. * Resolves a package link to a package in the installed repo set
  358. *
  359. * Since dependencies are already installed this should always find one.
  360. *
  361. * @param RepositorySet $repositorySet Repository set of installed packages only
  362. * @param Link $link Package link to look up
  363. *
  364. * @return PackageInterface|null The found package
  365. */
  366. private function lookupInstalledPackage(RepositorySet $repositorySet, Link $link)
  367. {
  368. $packages = $repositorySet->findPackages($link->getTarget(), $link->getConstraint(), false);
  369. return !empty($packages) ? $packages[0] : null;
  370. }
  371. /**
  372. * Retrieves the path a package is installed to.
  373. *
  374. * @param PackageInterface $package
  375. * @param bool $global Whether this is a global package
  376. *
  377. * @return string Install path
  378. */
  379. private function getInstallPath(PackageInterface $package, $global = false)
  380. {
  381. if (!$global) {
  382. return $this->composer->getInstallationManager()->getInstallPath($package);
  383. }
  384. return $this->globalComposer->getInstallationManager()->getInstallPath($package);
  385. }
  386. /**
  387. * @param PluginInterface $plugin
  388. * @param string $capability
  389. * @throws \RuntimeException On empty or non-string implementation class name value
  390. * @return null|string The fully qualified class of the implementation or null if Plugin is not of Capable type or does not provide it
  391. */
  392. protected function getCapabilityImplementationClassName(PluginInterface $plugin, $capability)
  393. {
  394. if (!($plugin instanceof Capable)) {
  395. return null;
  396. }
  397. $capabilities = (array) $plugin->getCapabilities();
  398. if (!empty($capabilities[$capability]) && is_string($capabilities[$capability]) && trim($capabilities[$capability])) {
  399. return trim($capabilities[$capability]);
  400. }
  401. if (
  402. array_key_exists($capability, $capabilities)
  403. && (empty($capabilities[$capability]) || !is_string($capabilities[$capability]) || !trim($capabilities[$capability]))
  404. ) {
  405. throw new \UnexpectedValueException('Plugin '.get_class($plugin).' provided invalid capability class name(s), got '.var_export($capabilities[$capability], 1));
  406. }
  407. }
  408. /**
  409. * @param PluginInterface $plugin
  410. * @param string $capabilityClassName The fully qualified name of the API interface which the plugin may provide
  411. * an implementation of.
  412. * @param array $ctorArgs Arguments passed to Capability's constructor.
  413. * Keeping it an array will allow future values to be passed w\o changing the signature.
  414. * @return null|Capability
  415. */
  416. public function getPluginCapability(PluginInterface $plugin, $capabilityClassName, array $ctorArgs = array())
  417. {
  418. if ($capabilityClass = $this->getCapabilityImplementationClassName($plugin, $capabilityClassName)) {
  419. if (!class_exists($capabilityClass)) {
  420. throw new \RuntimeException("Cannot instantiate Capability, as class $capabilityClass from plugin ".get_class($plugin)." does not exist.");
  421. }
  422. $ctorArgs['plugin'] = $plugin;
  423. $capabilityObj = new $capabilityClass($ctorArgs);
  424. // FIXME these could use is_a and do the check *before* instantiating once drop support for php<5.3.9
  425. if (!$capabilityObj instanceof Capability || !$capabilityObj instanceof $capabilityClassName) {
  426. throw new \RuntimeException(
  427. 'Class ' . $capabilityClass . ' must implement both Composer\Plugin\Capability\Capability and '. $capabilityClassName . '.'
  428. );
  429. }
  430. return $capabilityObj;
  431. }
  432. }
  433. /**
  434. * @param string $capabilityClassName The fully qualified name of the API interface which the plugin may provide
  435. * an implementation of.
  436. * @param array $ctorArgs Arguments passed to Capability's constructor.
  437. * Keeping it an array will allow future values to be passed w\o changing the signature.
  438. * @return Capability[]
  439. */
  440. public function getPluginCapabilities($capabilityClassName, array $ctorArgs = array())
  441. {
  442. $capabilities = array();
  443. foreach ($this->getPlugins() as $plugin) {
  444. if ($capability = $this->getPluginCapability($plugin, $capabilityClassName, $ctorArgs)) {
  445. $capabilities[] = $capability;
  446. }
  447. }
  448. return $capabilities;
  449. }
  450. }