Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

209 Zeilen
7.0KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\composer;
  8. use Composer\Composer;
  9. use Composer\DependencyResolver\Operation\UpdateOperation;
  10. use Composer\EventDispatcher\EventSubscriberInterface;
  11. use Composer\Installer\PackageEvent;
  12. use Composer\Installer\PackageEvents;
  13. use Composer\IO\IOInterface;
  14. use Composer\Plugin\PluginInterface;
  15. use Composer\Script;
  16. use Composer\Script\ScriptEvents;
  17. /**
  18. * Plugin is the composer plugin that registers the Yii composer installer.
  19. *
  20. * @author Qiang Xue <qiang.xue@gmail.com>
  21. * @since 2.0
  22. */
  23. class Plugin implements PluginInterface, EventSubscriberInterface
  24. {
  25. /**
  26. * @var array noted package updates.
  27. */
  28. private $_packageUpdates = [];
  29. /**
  30. * @var string path to the vendor directory.
  31. */
  32. private $_vendorDir;
  33. /**
  34. * @inheritdoc
  35. */
  36. public function activate(Composer $composer, IOInterface $io)
  37. {
  38. $installer = new Installer($io, $composer);
  39. $composer->getInstallationManager()->addInstaller($installer);
  40. $this->_vendorDir = rtrim($composer->getConfig()->get('vendor-dir'), '/');
  41. $file = $this->_vendorDir . '/yiisoft/extensions.php';
  42. if (!is_file($file)) {
  43. @mkdir(dirname($file), 0777, true);
  44. file_put_contents($file, "<?php\n\nreturn [];\n");
  45. }
  46. }
  47. /**
  48. * @inheritdoc
  49. * @return array The event names to listen to.
  50. */
  51. public static function getSubscribedEvents()
  52. {
  53. return [
  54. PackageEvents::POST_PACKAGE_UPDATE => 'checkPackageUpdates',
  55. ScriptEvents::POST_UPDATE_CMD => 'showUpgradeNotes',
  56. ];
  57. }
  58. /**
  59. * Listen to POST_PACKAGE_UPDATE event and take note of the package updates.
  60. * @param PackageEvent $event
  61. */
  62. public function checkPackageUpdates(PackageEvent $event)
  63. {
  64. $operation = $event->getOperation();
  65. if ($operation instanceof UpdateOperation) {
  66. $this->_packageUpdates[$operation->getInitialPackage()->getName()] = [
  67. 'from' => $operation->getInitialPackage()->getVersion(),
  68. 'fromPretty' => $operation->getInitialPackage()->getPrettyVersion(),
  69. 'to' => $operation->getTargetPackage()->getVersion(),
  70. 'toPretty' => $operation->getTargetPackage()->getPrettyVersion(),
  71. 'direction' => $event->getPolicy()->versionCompare(
  72. $operation->getInitialPackage(),
  73. $operation->getTargetPackage(),
  74. '<'
  75. ) ? 'up' : 'down',
  76. ];
  77. }
  78. }
  79. /**
  80. * Listen to POST_UPDATE_CMD event to display information about upgrade notes if appropriate.
  81. * @param Script\Event $event
  82. */
  83. public function showUpgradeNotes(Script\Event $event)
  84. {
  85. $packageName = 'yiisoft/yii2';
  86. if (!isset($this->_packageUpdates[$packageName])) {
  87. return;
  88. }
  89. $package = $this->_packageUpdates['yiisoft/yii2'];
  90. // do not show a notice on up/downgrades between dev versions
  91. // avoid messages like from version dev-master to dev-master
  92. if ($package['fromPretty'] == $package['toPretty']) {
  93. return;
  94. }
  95. $io = $event->getIO();
  96. // print the relevant upgrade notes for the upgrade
  97. // - only on upgrade, not on downgrade
  98. // - only if the "from" version is non-dev, otherwise we have no idea which notes to show
  99. if ($package['direction'] === 'up' && $this->isNumericVersion($package['fromPretty'])) {
  100. $notes = $this->findUpgradeNotes($packageName, $package['fromPretty']);
  101. if ($notes !== false && empty($notes)) {
  102. // no relevent upgrade notes, do not show anything.
  103. return;
  104. }
  105. $this->printUpgradeIntro($io, $package);
  106. if ($notes) {
  107. // safety check: do not display notes if they are too many
  108. if (count($notes) > 250) {
  109. $io->write("\n <fg=yellow;options=bold>The relevant notes for your upgrade are too long to be displayed here.</>");
  110. } else {
  111. $io->write("\n " . trim(implode("\n ", $notes)));
  112. }
  113. }
  114. $io->write("\n You can find the upgrade notes for all versions online at:");
  115. } else {
  116. $this->printUpgradeIntro($io, $package);
  117. $io->write("\n You can find the upgrade notes online at:");
  118. }
  119. $this->printUpgradeLink($io, $package);
  120. }
  121. /**
  122. * Print link to upgrade notes
  123. * @param IOInterface $io
  124. * @param array $package
  125. */
  126. private function printUpgradeLink($io, $package)
  127. {
  128. $maxVersion = $package['direction'] === 'up' ? $package['toPretty'] : $package['fromPretty'];
  129. // make sure to always show a valid link, even if $maxVersion is something like dev-master
  130. if (!$this->isNumericVersion($maxVersion)) {
  131. $maxVersion = 'master';
  132. }
  133. $io->write(" https://github.com/yiisoft/yii2/blob/$maxVersion/framework/UPGRADE.md\n");
  134. }
  135. /**
  136. * Print upgrade intro
  137. * @param IOInterface $io
  138. * @param array $package
  139. */
  140. private function printUpgradeIntro($io, $package)
  141. {
  142. $io->write("\n <fg=yellow;options=bold>Seems you have "
  143. . ($package['direction'] === 'up' ? 'upgraded' : 'downgraded')
  144. . ' Yii Framework from version '
  145. . $package['fromPretty'] . ' to ' . $package['toPretty'] . '.</>'
  146. );
  147. $io->write("\n <options=bold>Please check the upgrade notes for possible incompatible changes");
  148. $io->write(' and adjust your application code accordingly.</>');
  149. }
  150. /**
  151. * Read upgrade notes from a files and returns an array of lines
  152. * @param string $packageName
  153. * @param string $fromVersion until which version to read the notes
  154. * @return array|false
  155. */
  156. private function findUpgradeNotes($packageName, $fromVersion)
  157. {
  158. $upgradeFile = $this->_vendorDir . '/' . $packageName . '/UPGRADE.md';
  159. if (!is_file($upgradeFile) || !is_readable($upgradeFile)) {
  160. return false;
  161. }
  162. $lines = preg_split('~\R~', file_get_contents($upgradeFile));
  163. $relevantLines = [];
  164. $consuming = false;
  165. foreach($lines as $line) {
  166. if (preg_match('/^Upgrade from Yii ([0-9]\.[0-9]+\.?[0-9]*)/i', $line, $matches)) {
  167. if (version_compare($matches[1], $fromVersion, '<')) {
  168. break;
  169. }
  170. $consuming = true;
  171. }
  172. if ($consuming) {
  173. $relevantLines[] = $line;
  174. }
  175. }
  176. return $relevantLines;
  177. }
  178. /**
  179. * Check whether a version is numeric, e.g. 2.0.10.
  180. * @param string $version
  181. * @return int|false
  182. */
  183. private function isNumericVersion($version)
  184. {
  185. return preg_match('~^([0-9]\.[0-9]+\.?[0-9]*)~', $version);
  186. }
  187. }