actions.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Yoast SEO Plugin File.
  4. *
  5. * @package Yoast\YoastSEO\Composer
  6. */
  7. namespace Yoast\WP\Free\Composer;
  8. use Composer\Script\Event;
  9. use Yoast\WP\Free\Dependency_Injection\Container_Compiler;
  10. /**
  11. * Class to handle Composer actions and events.
  12. */
  13. class Actions {
  14. /**
  15. * Prefixes dependencies if composer install is ran with dev mode.
  16. *
  17. * Used in composer in the post-install script hook.
  18. *
  19. * @codeCoverageIgnore
  20. *
  21. * @param \Composer\Script\Event $event Composer event that triggered this script.
  22. *
  23. * @return void
  24. */
  25. public static function prefix_dependencies( Event $event ) {
  26. $io = $event->getIO();
  27. if ( ! $event->isDevMode() ) {
  28. $io->write( 'Not prefixing dependencies.' );
  29. return;
  30. }
  31. $io->write( 'Prefixing dependencies...' );
  32. $event_dispatcher = $event->getComposer()->getEventDispatcher();
  33. $event_dispatcher->dispatchScript( 'prefix-dependencies', $event->isDevMode() );
  34. }
  35. /**
  36. * Compiles the dependency injection container.
  37. *
  38. * Used the composer compile-dependency-injection-container command.
  39. *
  40. * @codeCoverageIgnore
  41. *
  42. * @param \Composer\Script\Event $event Composer event that triggered this script.
  43. *
  44. * @return void
  45. */
  46. public static function compile_dependency_injection_container( Event $event ) {
  47. $io = $event->getIO();
  48. $io->write( 'Compiling the dependency injection container...' );
  49. // Pas true as debug to force a recheck of the container.
  50. Container_Compiler::compile( true );
  51. $io->write( 'The dependency injection container has been compiled.' );
  52. }
  53. /**
  54. * Runs PHPCS on the staged files.
  55. *
  56. * Used the composer check-staged-cs command.
  57. *
  58. * @codeCoverageIgnore
  59. *
  60. * @return void
  61. */
  62. public static function check_staged_cs() {
  63. self::check_cs_for_changed_files( '--staged' );
  64. }
  65. /**
  66. * Runs PHPCS on the staged files.
  67. *
  68. * Used the composer check-staged-cs command.
  69. *
  70. * @codeCoverageIgnore
  71. *
  72. * @param \Composer\Script\Event $event Composer event that triggered this script.
  73. *
  74. * @return void
  75. */
  76. public static function check_branch_cs( Event $event ) {
  77. $args = $event->getArguments();
  78. if ( empty( $args ) ) {
  79. self::check_cs_for_changed_files( 'trunk' );
  80. return;
  81. }
  82. self::check_cs_for_changed_files( $args[0] );
  83. }
  84. /**
  85. * Runs PHPCS on changed files compared to some git reference.
  86. *
  87. * @param string $compare The git reference.
  88. *
  89. * @codeCoverageIgnore
  90. *
  91. * @return void
  92. */
  93. private static function check_cs_for_changed_files( $compare ) {
  94. \exec( 'git diff --name-only --diff-filter=d ' . \escapeshellarg( $compare ), $files );
  95. $files = \array_filter(
  96. $files,
  97. function ( $file ) {
  98. return \substr( $file, -4 ) === '.php';
  99. }
  100. );
  101. if ( empty( $files ) ) {
  102. echo 'No files to compare! Exiting.';
  103. return;
  104. }
  105. \system( 'composer check-cs -- ' . \implode( ' ', \array_map( 'escapeshellarg', $files ) ) );
  106. }
  107. }