More tidying.
[yaffs-website] / vendor / drupal / console / src / Command / Shared / ServicesTrait.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\Console\Command\Shared\ServicesTrait.
6  */
7
8 namespace Drupal\Console\Command\Shared;
9
10 use Drupal\Console\Core\Style\DrupalStyle;
11
12 trait ServicesTrait
13 {
14     /**
15      * @param DrupalStyle $io
16      *
17      * @return mixed
18      */
19     public function servicesQuestion(DrupalStyle $io)
20     {
21         if ($io->confirm(
22             $this->trans('commands.common.questions.services.confirm'),
23             false
24         )
25         ) {
26             $service_collection = [];
27             $io->writeln($this->trans('commands.common.questions.services.message'));
28             $services = $this->container->getServiceIds();
29             while (true) {
30                 $service = $io->choiceNoList(
31                     $this->trans('commands.common.questions.services.name'),
32                     $services,
33                     null,
34                     true
35                 );
36
37                 $service = trim($service);
38                 if (empty($service)) {
39                     break;
40                 }
41
42                 array_push($service_collection, $service);
43                 $service_key = array_search($service, $services, true);
44
45                 if ($service_key >= 0) {
46                     unset($services[$service_key]);
47                 }
48             }
49
50             return $service_collection;
51         }
52     }
53
54     /**
55      * @param array $services
56      *
57      * @return array
58      */
59     public function buildServices($services)
60     {
61         if (!empty($services)) {
62             $buildServices = [];
63             foreach ($services as $service) {
64                 $class = get_class($this->container->get($service));
65                 $shortClass = explode('\\', $class);
66                 $machineName = str_replace('.', '_', $service);
67                 $buildServices[$service] = [
68                   'name' => $service,
69                   'machine_name' => $machineName,
70                   'camel_case_name' => $this->stringConverter->underscoreToCamelCase($machineName),
71                   'class' => $class,
72                   'short' => end($shortClass),
73                 ];
74             }
75
76             return $buildServices;
77         }
78
79         return [];
80     }
81 }