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