[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Part of the Joomla Framework Console Package 4 * 5 * @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 7 */ 8 9 namespace Joomla\Console\Descriptor; 10 11 use Joomla\Console\Application; 12 use Joomla\Console\Command\AbstractCommand; 13 use Symfony\Component\Console\Exception\CommandNotFoundException; 14 15 /** 16 * Describes an application. 17 * 18 * @since 2.0.0 19 */ 20 final class ApplicationDescription 21 { 22 /** 23 * Placeholder for commands in the global namespace. 24 * 25 * @var string 26 * @since 2.0.0 27 */ 28 public const GLOBAL_NAMESPACE = '_global'; 29 30 /** 31 * The application's aliased commands. 32 * 33 * @var AbstractCommand[] 34 * @since 2.0.0 35 */ 36 private $aliases; 37 38 /** 39 * The application being described. 40 * 41 * @var Application 42 * @since 2.0.0 43 */ 44 private $application; 45 46 /** 47 * The application's commands. 48 * 49 * @var AbstractCommand[] 50 * @since 2.0.0 51 */ 52 private $commands; 53 54 /** 55 * The command namespace to process. 56 * 57 * @var string 58 * @since 2.0.0 59 */ 60 private $namespace = ''; 61 62 /** 63 * The application's command namespaces. 64 * 65 * @var array[] 66 * @since 2.0.0 67 */ 68 private $namespaces; 69 70 /** 71 * Flag indicating hidden commands should be displayed. 72 * 73 * @var boolean 74 * @since 2.0.0 75 */ 76 private $showHidden; 77 78 /** 79 * Constructor. 80 * 81 * @param Application $application The application being described. 82 * @param string $namespace The command namespace to process. 83 * @param boolean $showHidden Flag indicating hidden commands should be displayed. 84 * 85 * @since 2.0.0 86 */ 87 public function __construct(Application $application, string $namespace = '', bool $showHidden = false) 88 { 89 $this->application = $application; 90 $this->namespace = $namespace; 91 $this->showHidden = $showHidden; 92 } 93 94 /** 95 * Get the application's command namespaces. 96 * 97 * @return array[] 98 * 99 * @since 2.0.0 100 */ 101 public function getNamespaces(): array 102 { 103 if ($this->namespaces === null) 104 { 105 $this->inspectApplication(); 106 } 107 108 return $this->namespaces; 109 } 110 111 /** 112 * Get the application's commands. 113 * 114 * @return AbstractCommand[] 115 * 116 * @since 2.0.0 117 */ 118 public function getCommands(): array 119 { 120 if ($this->commands === null) 121 { 122 $this->inspectApplication(); 123 } 124 125 return $this->commands; 126 } 127 128 /** 129 * Get a command by name. 130 * 131 * @param string $name The name of the command to retrieve. 132 * 133 * @return AbstractCommand 134 * 135 * @since 2.0.0 136 * @throws CommandNotFoundException 137 */ 138 public function getCommand(string $name): AbstractCommand 139 { 140 if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) 141 { 142 throw new CommandNotFoundException(sprintf('Command %s does not exist.', $name)); 143 } 144 145 return $this->commands[$name] ?? $this->aliases[$name]; 146 } 147 148 /** 149 * Returns the namespace part of the command name. 150 * 151 * @param string $name The command name to process 152 * @param integer $limit The maximum number of parts of the namespace 153 * 154 * @return string 155 * 156 * @since 2.0.0 157 */ 158 private function extractNamespace(string $name, ?int $limit = null): string 159 { 160 $parts = explode(':', $name); 161 array_pop($parts); 162 163 return implode(':', $limit === null ? $parts : \array_slice($parts, 0, $limit)); 164 } 165 166 /** 167 * Inspects the application. 168 * 169 * @return void 170 * 171 * @since 2.0.0 172 */ 173 private function inspectApplication(): void 174 { 175 $this->commands = []; 176 $this->namespaces = []; 177 178 $all = $this->application->getAllCommands($this->namespace ? $this->application->findNamespace($this->namespace) : ''); 179 180 foreach ($this->sortCommands($all) as $namespace => $commands) 181 { 182 $names = []; 183 184 foreach ($commands as $name => $command) 185 { 186 if (!$command->getName() || (!$this->showHidden && $command->isHidden())) 187 { 188 continue; 189 } 190 191 if ($command->getName() === $name) 192 { 193 $this->commands[$name] = $command; 194 } 195 else 196 { 197 $this->aliases[$name] = $command; 198 } 199 200 $names[] = $name; 201 } 202 203 $this->namespaces[$namespace] = ['id' => $namespace, 'commands' => $names]; 204 } 205 } 206 207 /** 208 * Sort a set of commands. 209 * 210 * @param AbstractCommand[] $commands The commands to sort. 211 * 212 * @return AbstractCommand[][] 213 * 214 * @since 2.0.0 215 */ 216 private function sortCommands(array $commands): array 217 { 218 $namespacedCommands = []; 219 $globalCommands = []; 220 221 foreach ($commands as $name => $command) 222 { 223 $key = $this->extractNamespace($name, 1); 224 225 if (!$key) 226 { 227 $globalCommands[self::GLOBAL_NAMESPACE][$name] = $command; 228 } 229 else 230 { 231 $namespacedCommands[$key][$name] = $command; 232 } 233 } 234 235 ksort($namespacedCommands); 236 $namespacedCommands = array_merge($globalCommands, $namespacedCommands); 237 238 foreach ($namespacedCommands as &$commandsSet) 239 { 240 ksort($commandsSet); 241 } 242 243 // Unset reference to keep scope clear 244 unset($commandsSet); 245 246 return $namespacedCommands; 247 } 248 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |