Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Libraries / DebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Libraries\DebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Libraries;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Core\Extension\ModuleHandlerInterface;
15 use Drupal\Core\Extension\ThemeHandlerInterface;
16 use Drupal\Core\Asset\LibraryDiscoveryInterface;
17 use Drupal\Component\Serialization\Yaml;
18 use Drupal\Console\Core\Command\Shared\CommandTrait;
19 use Drupal\Console\Core\Style\DrupalStyle;
20
21 class DebugCommand extends Command
22 {
23     use CommandTrait;
24
25     /**
26  * @var  ModuleHandlerInterface
27 */
28     protected $moduleHandler;
29
30     /**
31  * @var  ThemeHandlerInterface;
32 */
33     protected $themeHandler;
34
35     /**
36  * @var  LibraryDiscoveryInterface
37 */
38     protected $libraryDiscovery;
39
40     /**
41  * @var string
42 */
43     protected $appRoot;
44
45     /**
46      * DebugCommand constructor.
47      *
48      * @param ModuleHandlerInterface    $moduleHandler
49      * @param ThemeHandlerInterface     $themeHandler
50      * @param LibraryDiscoveryInterface $libraryDiscovery
51      * @param string                    $appRoot
52      */
53     public function __construct(
54         ModuleHandlerInterface $moduleHandler,
55         ThemeHandlerInterface $themeHandler,
56         LibraryDiscoveryInterface $libraryDiscovery,
57         $appRoot
58     ) {
59         $this->moduleHandler = $moduleHandler;
60         $this->themeHandler = $themeHandler;
61         $this->libraryDiscovery = $libraryDiscovery;
62         $this->appRoot = $appRoot;
63         parent::__construct();
64     }
65
66     /**
67      * {@inheritdoc}
68      */
69     protected function configure()
70     {
71         $this
72             ->setName('libraries:debug')
73             ->setDescription($this->trans('commands.libraries.debug.description'))
74             ->addArgument(
75                 'group',
76                 InputArgument::OPTIONAL,
77                 $this->trans('commands.libraries.debug.options.name')
78             );
79     }
80
81     /**
82      * {@inheritdoc}
83      */
84     protected function execute(InputInterface $input, OutputInterface $output)
85     {
86         $io = new DrupalStyle($input, $output);
87         $group = $input->getArgument('group');
88
89         if (!$group) {
90             $groups = $this->getAllLibraries();
91
92             $tableHeader = [
93                 $this->trans('commands.libraries.debug.messages.name'),
94             ];
95
96             $io->table($tableHeader, $groups, 'compact');
97         } else {
98             $librariesData = $this->libraryDiscovery
99                 ->getLibrariesByExtension($group);
100
101             foreach ($librariesData as $key => $libraries) {
102                 $io->comment($key);
103                 $io->writeln(Yaml::encode($libraries));
104             }
105         }
106     }
107
108     private function getAllLibraries()
109     {
110         $modules = $this->moduleHandler->getModuleList();
111         $themes = $this->themeHandler->rebuildThemeData();
112         $extensions = array_merge($modules, $themes);
113         $libraries = [];
114
115         foreach ($extensions as $extensionName => $extension) {
116             $libraryFile = $extension->getPath() . '/' . $extensionName . '.libraries.yml';
117             if (is_file($this->appRoot . '/' . $libraryFile)) {
118                 $libraries[$extensionName] = $this->libraryDiscovery->getLibrariesByExtension($extensionName);
119             }
120         }
121
122         return array_keys($libraries);
123     }
124 }