/vendor/composer/composer/doc/articles/plugins.md

https://gitlab.com/yousafsyed/easternglamor · Markdown · 151 lines · 116 code · 35 blank · 0 comment · 0 complexity · 1a78b2da027b33f9996582be9a80e228 MD5 · raw file

  1. <!--
  2. tagline: Modify and extend Composer's functionality
  3. -->
  4. # Setting up and using plugins
  5. ## Synopsis
  6. You may wish to alter or expand Composer's functionality with your own. For
  7. example if your environment poses special requirements on the behaviour of
  8. Composer which do not apply to the majority of its users or if you wish to
  9. accomplish something with composer in a way that is not desired by most users.
  10. In these cases you could consider creating a plugin to handle your
  11. specific logic.
  12. ## Creating a Plugin
  13. A plugin is a regular composer package which ships its code as part of the
  14. package and may also depend on further packages.
  15. ### Plugin Package
  16. The package file is the same as any other package file but with the following
  17. requirements:
  18. 1. the [type][1] attribute must be `composer-plugin`.
  19. 2. the [extra][2] attribute must contain an element `class` defining the
  20. class name of the plugin (including namespace). If a package contains
  21. multiple plugins this can be array of class names.
  22. Additionally you must require the special package called `composer-plugin-api`
  23. to define which composer API versions your plugin is compatible with. The
  24. current composer plugin API version is 1.0.0.
  25. For example
  26. ```json
  27. {
  28. "name": "my/plugin-package",
  29. "type": "composer-plugin",
  30. "require": {
  31. "composer-plugin-api": "1.0.0"
  32. }
  33. }
  34. ```
  35. ### Plugin Class
  36. Every plugin has to supply a class which implements the
  37. [`Composer\Plugin\PluginInterface`][3]. The `activate()` method of the plugin
  38. is called after the plugin is loaded and receives an instance of
  39. [`Composer\Composer`][4] as well as an instance of
  40. [`Composer\IO\IOInterface`][5]. Using these two objects all configuration can
  41. be read and all internal objects and state can be manipulated as desired.
  42. Example:
  43. ```php
  44. <?php
  45. namespace phpDocumentor\Composer;
  46. use Composer\Composer;
  47. use Composer\IO\IOInterface;
  48. use Composer\Plugin\PluginInterface;
  49. class TemplateInstallerPlugin implements PluginInterface
  50. {
  51. public function activate(Composer $composer, IOInterface $io)
  52. {
  53. $installer = new TemplateInstaller($io, $composer);
  54. $composer->getInstallationManager()->addInstaller($installer);
  55. }
  56. }
  57. ```
  58. ## Event Handler
  59. Furthermore plugins may implement the
  60. [`Composer\EventDispatcher\EventSubscriberInterface`][6] in order to have its
  61. event handlers automatically registered with the `EventDispatcher` when the
  62. plugin is loaded.
  63. Plugin can subscribe to any of the available [script events](scripts.md#event-names).
  64. Example:
  65. ```php
  66. <?php
  67. namespace Naderman\Composer\AWS;
  68. use Composer\Composer;
  69. use Composer\EventDispatcher\EventSubscriberInterface;
  70. use Composer\IO\IOInterface;
  71. use Composer\Plugin\PluginInterface;
  72. use Composer\Plugin\PluginEvents;
  73. use Composer\Plugin\PreFileDownloadEvent;
  74. class AwsPlugin implements PluginInterface, EventSubscriberInterface
  75. {
  76. protected $composer;
  77. protected $io;
  78. public function activate(Composer $composer, IOInterface $io)
  79. {
  80. $this->composer = $composer;
  81. $this->io = $io;
  82. }
  83. public static function getSubscribedEvents()
  84. {
  85. return array(
  86. PluginEvents::PRE_FILE_DOWNLOAD => array(
  87. array('onPreFileDownload', 0)
  88. ),
  89. );
  90. }
  91. public function onPreFileDownload(PreFileDownloadEvent $event)
  92. {
  93. $protocol = parse_url($event->getProcessedUrl(), PHP_URL_SCHEME);
  94. if ($protocol === 's3') {
  95. $awsClient = new AwsClient($this->io, $this->composer->getConfig());
  96. $s3RemoteFilesystem = new S3RemoteFilesystem($this->io, $event->getRemoteFilesystem()->getOptions(), $awsClient);
  97. $event->setRemoteFilesystem($s3RemoteFilesystem);
  98. }
  99. }
  100. }
  101. ```
  102. ## Using Plugins
  103. Plugin packages are automatically loaded as soon as they are installed and will
  104. be loaded when composer starts up if they are found in the current project's
  105. list of installed packages. Additionally all plugin packages installed in the
  106. `COMPOSER_HOME` directory using the composer global command are loaded before
  107. local project plugins are loaded.
  108. > You may pass the `--no-plugins` option to composer commands to disable all
  109. > installed plugins. This may be particularly helpful if any of the plugins
  110. > causes errors and you wish to update or uninstall it.
  111. [1]: ../04-schema.md#type
  112. [2]: ../04-schema.md#extra
  113. [3]: https://github.com/composer/composer/blob/master/src/Composer/Plugin/PluginInterface.php
  114. [4]: https://github.com/composer/composer/blob/master/src/Composer/Composer.php
  115. [5]: https://github.com/composer/composer/blob/master/src/Composer/IO/IOInterface.php
  116. [6]: https://github.com/composer/composer/blob/master/src/Composer/EventDispatcher/EventSubscriberInterface.php