PluginManager.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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\Package;
  16. use Composer\Package\Version\VersionParser;
  17. use Composer\Repository\RepositoryInterface;
  18. use Composer\Package\AliasPackage;
  19. use Composer\Package\PackageInterface;
  20. use Composer\Package\Link;
  21. use Composer\Semver\Constraint\Constraint;
  22. use Composer\DependencyResolver\Pool;
  23. use Composer\Plugin\Capability\Capability;
  24. /**
  25. * Plugin manager
  26. *
  27. * @author Nils Adermann <naderman@naderman.de>
  28. * @author Jordi Boggiano <j.boggiano@seld.be>
  29. */
  30. class PluginManager
  31. {
  32. protected $composer;
  33. protected $io;
  34. protected $globalComposer;
  35. protected $versionParser;
  36. protected $disablePlugins = false;
  37. protected $plugins = array();
  38. protected $registeredPlugins = array();
  39. private static $classCounter = 0;
  40. /**
  41. * Initializes plugin manager
  42. *
  43. * @param IOInterface $io
  44. * @param Composer $composer
  45. * @param Composer $globalComposer
  46. * @param bool $disablePlugins
  47. */
  48. public function __construct(IOInterface $io, Composer $composer, Composer $globalComposer = null, $disablePlugins = false)
  49. {
  50. $this->io = $io;
  51. $this->composer = $composer;
  52. $this->globalComposer = $globalComposer;
  53. $this->versionParser = new VersionParser();
  54. $this->disablePlugins = $disablePlugins;
  55. }
  56. /**
  57. * Loads all plugins from currently installed plugin packages
  58. */
  59. public function loadInstalledPlugins()
  60. {
  61. if ($this->disablePlugins) {
  62. return;
  63. }
  64. $repo = $this->composer->getRepositoryManager()->getLocalRepository();
  65. $globalRepo = $this->globalComposer ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null;
  66. if ($repo) {
  67. $this->loadRepository($repo);
  68. }
  69. if ($globalRepo) {
  70. $this->loadRepository($globalRepo);
  71. }
  72. }
  73. /**
  74. * Gets all currently active plugin instances
  75. *
  76. * @return array plugins
  77. */
  78. public function getPlugins()
  79. {
  80. return $this->plugins;
  81. }
  82. /**
  83. * Gets global composer or null when main composer is not fully loaded
  84. *
  85. * @return Composer|null
  86. */
  87. public function getGlobalComposer()
  88. {
  89. return $this->globalComposer;
  90. }
  91. /**
  92. * Register a plugin package, activate it etc.
  93. *
  94. * If it's of type composer-installer it is registered as an installer
  95. * instead for BC
  96. *
  97. * @param PackageInterface $package
  98. * @param bool $failOnMissingClasses By default this silently skips plugins that can not be found, but if set to true it fails with an exception
  99. *
  100. * @throws \UnexpectedValueException
  101. */
  102. public function registerPackage(PackageInterface $package, $failOnMissingClasses = false)
  103. {
  104. if ($this->disablePlugins) {
  105. return;
  106. }
  107. if ($package->getType() === 'composer-plugin') {
  108. $requiresComposer = null;
  109. foreach ($package->getRequires() as $link) { /** @var Link $link */
  110. if ('composer-plugin-api' === $link->getTarget()) {
  111. $requiresComposer = $link->getConstraint();
  112. break;
  113. }
  114. }
  115. if (!$requiresComposer) {
  116. throw new \RuntimeException("Plugin ".$package->getName()." is missing a require statement for a version of the composer-plugin-api package.");
  117. }
  118. $currentPluginApiVersion = $this->getPluginApiVersion();
  119. $currentPluginApiConstraint = new Constraint('==', $this->versionParser->normalize($currentPluginApiVersion));
  120. if ($requiresComposer->getPrettyString() === '1.0.0' && $this->getPluginApiVersion() === '1.0.0') {
  121. $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>');
  122. } elseif (!$requiresComposer->matches($currentPluginApiConstraint)) {
  123. $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>');
  124. return;
  125. }
  126. }
  127. $oldInstallerPlugin = ($package->getType() === 'composer-installer');
  128. if (in_array($package->getName(), $this->registeredPlugins)) {
  129. return;
  130. }
  131. $extra = $package->getExtra();
  132. if (empty($extra['class'])) {
  133. throw new \UnexpectedValueException('Error while installing '.$package->getPrettyName().', composer-plugin packages should have a class defined in their extra key to be usable.');
  134. }
  135. $classes = is_array($extra['class']) ? $extra['class'] : array($extra['class']);
  136. $localRepo = $this->composer->getRepositoryManager()->getLocalRepository();
  137. $globalRepo = $this->globalComposer ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null;
  138. $pool = new Pool('dev');
  139. $pool->addRepository($localRepo);
  140. if ($globalRepo) {
  141. $pool->addRepository($globalRepo);
  142. }
  143. $autoloadPackages = array($package->getName() => $package);
  144. $autoloadPackages = $this->collectDependencies($pool, $autoloadPackages, $package);
  145. $generator = $this->composer->getAutoloadGenerator();
  146. $autoloads = array();
  147. foreach ($autoloadPackages as $autoloadPackage) {
  148. $downloadPath = $this->getInstallPath($autoloadPackage, $globalRepo && $globalRepo->hasPackage($autoloadPackage));
  149. $autoloads[] = array($autoloadPackage, $downloadPath);
  150. }
  151. $map = $generator->parseAutoloads($autoloads, new Package('dummy', '1.0.0.0', '1.0.0'));
  152. $classLoader = $generator->createLoader($map);
  153. $classLoader->register();
  154. foreach ($classes as $class) {
  155. if (class_exists($class, false)) {
  156. $class = trim($class, '\\');
  157. $path = $classLoader->findFile($class);
  158. $code = file_get_contents($path);
  159. $separatorPos = strrpos($class, '\\');
  160. $className = $class;
  161. if ($separatorPos) {
  162. $className = substr($class, $separatorPos + 1);
  163. }
  164. $code = preg_replace('{^((?:final\s+)?(?:\s*))class\s+('.preg_quote($className).')}mi', '$1class $2_composer_tmp'.self::$classCounter, $code, 1);
  165. $code = str_replace('__FILE__', var_export($path, true), $code);
  166. $code = str_replace('__DIR__', var_export(dirname($path), true), $code);
  167. $code = str_replace('__CLASS__', var_export($class, true), $code);
  168. $code = preg_replace('/^\s*<\?(php)?/i', '', $code, 1);
  169. eval($code);
  170. $class .= '_composer_tmp'.self::$classCounter;
  171. self::$classCounter++;
  172. }
  173. if ($oldInstallerPlugin) {
  174. $installer = new $class($this->io, $this->composer);
  175. $this->composer->getInstallationManager()->addInstaller($installer);
  176. } elseif (class_exists($class)) {
  177. $plugin = new $class();
  178. $this->addPlugin($plugin);
  179. $this->registeredPlugins[] = $package->getName();
  180. } elseif ($failOnMissingClasses) {
  181. throw new \UnexpectedValueException('Plugin '.$package->getName().' could not be initialized, class not found: '.$class);
  182. }
  183. }
  184. }
  185. /**
  186. * Returns the version of the internal composer-plugin-api package.
  187. *
  188. * @return string
  189. */
  190. protected function getPluginApiVersion()
  191. {
  192. return PluginInterface::PLUGIN_API_VERSION;
  193. }
  194. /**
  195. * Adds a plugin, activates it and registers it with the event dispatcher
  196. *
  197. * Ideally plugin packages should be registered via registerPackage, but if you use Composer
  198. * programmatically and want to register a plugin class directly this is a valid way
  199. * to do it.
  200. *
  201. * @param PluginInterface $plugin plugin instance
  202. */
  203. public function addPlugin(PluginInterface $plugin)
  204. {
  205. $this->io->writeError('Loading plugin '.get_class($plugin), true, IOInterface::DEBUG);
  206. $this->plugins[] = $plugin;
  207. $plugin->activate($this->composer, $this->io);
  208. if ($plugin instanceof EventSubscriberInterface) {
  209. $this->composer->getEventDispatcher()->addSubscriber($plugin);
  210. }
  211. }
  212. /**
  213. * Load all plugins and installers from a repository
  214. *
  215. * Note that plugins in the specified repository that rely on events that
  216. * have fired prior to loading will be missed. This means you likely want to
  217. * call this method as early as possible.
  218. *
  219. * @param RepositoryInterface $repo Repository to scan for plugins to install
  220. *
  221. * @throws \RuntimeException
  222. */
  223. private function loadRepository(RepositoryInterface $repo)
  224. {
  225. foreach ($repo->getPackages() as $package) { /** @var PackageInterface $package */
  226. if ($package instanceof AliasPackage) {
  227. continue;
  228. }
  229. if ('composer-plugin' === $package->getType()) {
  230. $this->registerPackage($package);
  231. // Backward compatibility
  232. } elseif ('composer-installer' === $package->getType()) {
  233. $this->registerPackage($package);
  234. }
  235. }
  236. }
  237. /**
  238. * Recursively generates a map of package names to packages for all deps
  239. *
  240. * @param Pool $pool Package pool of installed packages
  241. * @param array $collected Current state of the map for recursion
  242. * @param PackageInterface $package The package to analyze
  243. *
  244. * @return array Map of package names to packages
  245. */
  246. private function collectDependencies(Pool $pool, array $collected, PackageInterface $package)
  247. {
  248. $requires = array_merge(
  249. $package->getRequires(),
  250. $package->getDevRequires()
  251. );
  252. foreach ($requires as $requireLink) {
  253. $requiredPackage = $this->lookupInstalledPackage($pool, $requireLink);
  254. if ($requiredPackage && !isset($collected[$requiredPackage->getName()])) {
  255. $collected[$requiredPackage->getName()] = $requiredPackage;
  256. $collected = $this->collectDependencies($pool, $collected, $requiredPackage);
  257. }
  258. }
  259. return $collected;
  260. }
  261. /**
  262. * Resolves a package link to a package in the installed pool
  263. *
  264. * Since dependencies are already installed this should always find one.
  265. *
  266. * @param Pool $pool Pool of installed packages only
  267. * @param Link $link Package link to look up
  268. *
  269. * @return PackageInterface|null The found package
  270. */
  271. private function lookupInstalledPackage(Pool $pool, Link $link)
  272. {
  273. $packages = $pool->whatProvides($link->getTarget(), $link->getConstraint());
  274. return !empty($packages) ? $packages[0] : null;
  275. }
  276. /**
  277. * Retrieves the path a package is installed to.
  278. *
  279. * @param PackageInterface $package
  280. * @param bool $global Whether this is a global package
  281. *
  282. * @return string Install path
  283. */
  284. private function getInstallPath(PackageInterface $package, $global = false)
  285. {
  286. if (!$global) {
  287. return $this->composer->getInstallationManager()->getInstallPath($package);
  288. }
  289. return $this->globalComposer->getInstallationManager()->getInstallPath($package);
  290. }
  291. /**
  292. * @param PluginInterface $plugin
  293. * @param string $capability
  294. * @throws \RuntimeException On empty or non-string implementation class name value
  295. * @return null|string The fully qualified class of the implementation or null if Plugin is not of Capable type or does not provide it
  296. */
  297. protected function getCapabilityImplementationClassName(PluginInterface $plugin, $capability)
  298. {
  299. if (!($plugin instanceof Capable)) {
  300. return null;
  301. }
  302. $capabilities = (array) $plugin->getCapabilities();
  303. if (!empty($capabilities[$capability]) && is_string($capabilities[$capability]) && trim($capabilities[$capability])) {
  304. return trim($capabilities[$capability]);
  305. }
  306. if (
  307. array_key_exists($capability, $capabilities)
  308. && (empty($capabilities[$capability]) || !is_string($capabilities[$capability]) || !trim($capabilities[$capability]))
  309. ) {
  310. throw new \UnexpectedValueException('Plugin '.get_class($plugin).' provided invalid capability class name(s), got '.var_export($capabilities[$capability], 1));
  311. }
  312. }
  313. /**
  314. * @param PluginInterface $plugin
  315. * @param string $capabilityClassName The fully qualified name of the API interface which the plugin may provide
  316. * an implementation of.
  317. * @param array $ctorArgs Arguments passed to Capability's constructor.
  318. * Keeping it an array will allow future values to be passed w\o changing the signature.
  319. * @return null|Capability
  320. */
  321. public function getPluginCapability(PluginInterface $plugin, $capabilityClassName, array $ctorArgs = array())
  322. {
  323. if ($capabilityClass = $this->getCapabilityImplementationClassName($plugin, $capabilityClassName)) {
  324. if (!class_exists($capabilityClass)) {
  325. throw new \RuntimeException("Cannot instantiate Capability, as class $capabilityClass from plugin ".get_class($plugin)." does not exist.");
  326. }
  327. $ctorArgs['plugin'] = $plugin;
  328. $capabilityObj = new $capabilityClass($ctorArgs);
  329. // FIXME these could use is_a and do the check *before* instantiating once drop support for php<5.3.9
  330. if (!$capabilityObj instanceof Capability || !$capabilityObj instanceof $capabilityClassName) {
  331. throw new \RuntimeException(
  332. 'Class ' . $capabilityClass . ' must implement both Composer\Plugin\Capability\Capability and '. $capabilityClassName . '.'
  333. );
  334. }
  335. return $capabilityObj;
  336. }
  337. }
  338. /**
  339. * @param string $capabilityClassName The fully qualified name of the API interface which the plugin may provide
  340. * an implementation of.
  341. * @param array $ctorArgs Arguments passed to Capability's constructor.
  342. * Keeping it an array will allow future values to be passed w\o changing the signature.
  343. * @return Capability[]
  344. */
  345. public function getPluginCapabilities($capabilityClassName, array $ctorArgs = array())
  346. {
  347. $capabilities = array();
  348. foreach ($this->getPlugins() as $plugin) {
  349. if ($capability = $this->getPluginCapability($plugin, $capabilityClassName, $ctorArgs)) {
  350. $capabilities[] = $capability;
  351. }
  352. }
  353. return $capabilities;
  354. }
  355. }