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