24fc68a0679aa059125b128e7e4fb52452b25a13
[yaffs-website] / vendor / drupal / console-core / src / Utils / ChainDiscovery.php
1 <?php
2 /**
3  * @file
4  * Contains Drupal\Console\Core\Utils\Site.
5  */
6
7 namespace Drupal\Console\Core\Utils;
8
9 use Symfony\Component\Finder\Finder;
10 use Symfony\Component\Yaml\Yaml;
11
12 /**
13  * Class ChainDiscovery
14  * @package Drupal\Console\Core\Utils
15  */
16 class ChainDiscovery
17 {
18     /**
19      * @var ConfigurationManager
20      */
21     protected $configurationManager;
22
23     /**
24      * @var string
25      */
26     protected $appRoot;
27
28     /**
29      * @var array
30      */
31     private $directories = [];
32
33     /**
34      * ChainDiscovery constructor.
35      *
36      * @param string               $appRoot
37      * @param ConfigurationManager $configurationManager
38      */
39     public function __construct(
40         $appRoot,
41         ConfigurationManager $configurationManager
42     ) {
43         $this->appRoot = $appRoot;
44         $this->configurationManager = $configurationManager;
45
46         $this->addDirectories(
47             [
48             $this->configurationManager->getHomeDirectory() . DIRECTORY_SEPARATOR . '.console'. DIRECTORY_SEPARATOR .'chain',
49             $this->appRoot . DIRECTORY_SEPARATOR . 'console'. DIRECTORY_SEPARATOR .'chain',
50             $this->appRoot . DIRECTORY_SEPARATOR . '.console'. DIRECTORY_SEPARATOR .'chain',
51             ]
52         );
53     }
54
55     /**
56      * @param array $directories
57      */
58     public function addDirectories(array $directories)
59     {
60         $this->directories = array_merge($this->directories, $directories);
61     }
62
63     /**
64      * @param bool $onlyFiles
65      * @return array
66      */
67     public function getChainFiles($onlyFiles = false)
68     {
69         $chainFiles = [];
70         foreach ($this->directories as $directory) {
71             if (!is_dir($directory)) {
72                 continue;
73             }
74             $finder = new Finder();
75             $finder->files()
76                 ->name('*.yml')
77                 ->in($directory);
78             foreach ($finder as $file) {
79                 $chainFiles[$file->getPath()][] = sprintf(
80                     '%s/%s',
81                     $directory,
82                     $file->getBasename()
83                 );
84             }
85         }
86
87         if ($onlyFiles) {
88             $files = [];
89             foreach ($chainFiles as $chainDirectory => $chainFileList) {
90                 $files = array_merge($files, $chainFileList);
91             }
92             return $files;
93         }
94
95         return $chainFiles;
96     }
97
98     /**
99      * @return array
100      */
101     public function getChainCommands()
102     {
103         $chainCommands = [];
104         $files = $this->getChainFiles(true);
105         foreach ($files as $file) {
106             $chain = Yaml::parse(file_get_contents($file));
107             if (!array_key_exists('command', $chain)) {
108                 continue;
109             }
110             if (!array_key_exists('name', $chain['command'])) {
111                 continue;
112             }
113             $name = $chain['command']['name'];
114             $description = '';
115             if (array_key_exists('description', $chain['command'])) {
116                 $description = $chain['command']['description'];
117             }
118             $chainCommands[$name] = [
119                 'description' => $description,
120                 'file' => $file,
121             ];
122         }
123
124         return $chainCommands;
125     }
126 }