phpstorm.drush.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @file
  4. * Generate PhpStorm metadata file.
  5. */
  6. /**
  7. * Implements of hook_drush_command().
  8. */
  9. function phpstorm_drush_command() {
  10. $items = array();
  11. $items['phpstorm-metadata'] = array(
  12. 'description' => 'Save the PhpStorm Metadata file to Drupal root.',
  13. 'core' => array('8+'),
  14. 'aliases' => array('phpm'),
  15. 'category' => 'devel',
  16. );
  17. return $items;
  18. }
  19. /**
  20. * Implements hook_drush_help_alter().
  21. */
  22. function phpstorm_drush_help_alter(&$command) {
  23. if ($command['command'] == 'cache-rebuild') {
  24. $command['options']['storm'] = 'Write a new PHPstorm metadata file to Drupal root.';
  25. }
  26. }
  27. /*
  28. * Implements drush_hook_post_COMMAND().
  29. */
  30. function drush_phpstorm_post_cache_rebuild() {
  31. if (drush_get_option('storm')) {
  32. drush_invoke_process('@self', 'phpstorm-metadata');
  33. }
  34. }
  35. /**
  36. * Generate PhpStorm Metadata file.
  37. *
  38. * @see http://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata
  39. */
  40. function drush_phpstorm_metadata() {
  41. $container = \Drupal::getContainer();
  42. $reflectedClass = new ReflectionClass($container);
  43. $map = array();
  44. // Map for all services of the container.
  45. // @see \Symfony\Component\DependencyInjection\Container::getServiceIds().
  46. foreach ($reflectedClass->getMethods() as $method) {
  47. if (preg_match('/^get(.+)Service$/', $method->name, $match)) {
  48. $id = strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), strtr($match[1], '_', '.')));
  49. $service = \Drupal::service($id);
  50. if (is_object($service)) {
  51. $map["\\Drupal::service('')"][$id] = '\\' . get_class($service);
  52. }
  53. }
  54. }
  55. // Entity Manager - getStorage
  56. foreach (\Drupal::entityTypeManager()->getDefinitions() as $type => $definition) {
  57. $class = Drupal::entityTypeManager()->getStorage($type);
  58. $map["\\Drupal::entityManager()->getStorage('')"][$type] = '\\' . get_class($class);
  59. $map["\\Drupal::entityTypeManager()->getStorage('')"][$type] = '\\' . get_class($class);
  60. }
  61. $content = _drush_phpstorm_metadata_phpstorm_metadata_template($map);
  62. file_put_contents(DRUPAL_ROOT . '/.phpstorm.meta.php', $content);
  63. }
  64. function _drush_phpstorm_metadata_phpstorm_metadata_template($data) {
  65. $file = '<?php
  66. namespace PHPSTORM_META {
  67. /** @noinspection PhpUnusedLocalVariableInspection */
  68. /** @noinspection PhpIllegalArrayKeyTypeInspection */
  69. $STATIC_METHOD_TYPES = [
  70. ';
  71. foreach ($data as $method => $map) {
  72. $file .= "\n";
  73. $file .= " {$method} => [\n";
  74. foreach ($map as $argument => $class) {
  75. $file .= " '{$argument}' instanceof {$class},\n";
  76. }
  77. $file .= " ],";
  78. $file .= "\n";
  79. }
  80. $file .= '
  81. ];
  82. }
  83. ';
  84. return $file;
  85. }