Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

TextDescriptor.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 10.04 KiB


 001  <?php
 002   
 003  /*
 004   * This file is part of the Symfony package.
 005   *
 006   * (c) Fabien Potencier <fabien@symfony.com>
 007   *
 008   * For the full copyright and license information, please view the LICENSE
 009   * file that was distributed with this source code.
 010   */
 011   
 012  namespace Symfony\Component\Console\Descriptor;
 013   
 014  use Symfony\Component\Console\Application;
 015  use Symfony\Component\Console\Command\Command;
 016  use Symfony\Component\Console\Input\InputArgument;
 017  use Symfony\Component\Console\Input\InputDefinition;
 018  use Symfony\Component\Console\Input\InputOption;
 019   
 020  /**
 021   * Text descriptor.
 022   *
 023   * @author Jean-François Simon <contact@jfsimon.fr>
 024   *
 025   * @internal
 026   */
 027  class TextDescriptor extends Descriptor
 028  {
 029      /**
 030       * {@inheritdoc}
 031       */
 032      protected function describeInputArgument(InputArgument $argument, array $options = array())
 033      {
 034          if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
 035              $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($argument->getDefault()));
 036          } else {
 037              $default = '';
 038          }
 039   
 040          $totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument->getName());
 041          $spacingWidth = $totalWidth - strlen($argument->getName()) + 2;
 042   
 043          $this->writeText(sprintf('  <info>%s</info>%s%s%s',
 044              $argument->getName(),
 045              str_repeat(' ', $spacingWidth),
 046              // + 17 = 2 spaces + <info> + </info> + 2 spaces
 047              preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 17), $argument->getDescription()),
 048              $default
 049          ), $options);
 050      }
 051   
 052      /**
 053       * {@inheritdoc}
 054       */
 055      protected function describeInputOption(InputOption $option, array $options = array())
 056      {
 057          if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
 058              $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault()));
 059          } else {
 060              $default = '';
 061          }
 062   
 063          $value = '';
 064          if ($option->acceptValue()) {
 065              $value = '='.strtoupper($option->getName());
 066   
 067              if ($option->isValueOptional()) {
 068                  $value = '['.$value.']';
 069              }
 070          }
 071   
 072          $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions(array($option));
 073          $synopsis = sprintf('%s%s',
 074              $option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : '    ',
 075              sprintf('--%s%s', $option->getName(), $value)
 076          );
 077   
 078          $spacingWidth = $totalWidth - strlen($synopsis) + 2;
 079   
 080          $this->writeText(sprintf('  <info>%s</info>%s%s%s%s',
 081              $synopsis,
 082              str_repeat(' ', $spacingWidth),
 083              // + 17 = 2 spaces + <info> + </info> + 2 spaces
 084              preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 17), $option->getDescription()),
 085              $default,
 086              $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''
 087          ), $options);
 088      }
 089   
 090      /**
 091       * {@inheritdoc}
 092       */
 093      protected function describeInputDefinition(InputDefinition $definition, array $options = array())
 094      {
 095          $totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
 096          foreach ($definition->getArguments() as $argument) {
 097              $totalWidth = max($totalWidth, strlen($argument->getName()));
 098          }
 099   
 100          if ($definition->getArguments()) {
 101              $this->writeText('<comment>Arguments:</comment>', $options);
 102              $this->writeText("\n");
 103              foreach ($definition->getArguments() as $argument) {
 104                  $this->describeInputArgument($argument, array_merge($options, array('total_width' => $totalWidth)));
 105                  $this->writeText("\n");
 106              }
 107          }
 108   
 109          if ($definition->getArguments() && $definition->getOptions()) {
 110              $this->writeText("\n");
 111          }
 112   
 113          if ($definition->getOptions()) {
 114              $laterOptions = array();
 115   
 116              $this->writeText('<comment>Options:</comment>', $options);
 117              foreach ($definition->getOptions() as $option) {
 118                  if (strlen($option->getShortcut()) > 1) {
 119                      $laterOptions[] = $option;
 120                      continue;
 121                  }
 122                  $this->writeText("\n");
 123                  $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
 124              }
 125              foreach ($laterOptions as $option) {
 126                  $this->writeText("\n");
 127                  $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
 128              }
 129          }
 130      }
 131   
 132      /**
 133       * {@inheritdoc}
 134       */
 135      protected function describeCommand(Command $command, array $options = array())
 136      {
 137          $command->getSynopsis(true);
 138          $command->getSynopsis(false);
 139          $command->mergeApplicationDefinition(false);
 140   
 141          $this->writeText('<comment>Usage:</comment>', $options);
 142          foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) {
 143              $this->writeText("\n");
 144              $this->writeText('  '.$usage, $options);
 145          }
 146          $this->writeText("\n");
 147   
 148          $definition = $command->getNativeDefinition();
 149          if ($definition->getOptions() || $definition->getArguments()) {
 150              $this->writeText("\n");
 151              $this->describeInputDefinition($definition, $options);
 152              $this->writeText("\n");
 153          }
 154   
 155          if ($help = $command->getProcessedHelp()) {
 156              $this->writeText("\n");
 157              $this->writeText('<comment>Help:</comment>', $options);
 158              $this->writeText("\n");
 159              $this->writeText('  '.str_replace("\n", "\n  ", $help), $options);
 160              $this->writeText("\n");
 161          }
 162      }
 163   
 164      /**
 165       * {@inheritdoc}
 166       */
 167      protected function describeApplication(Application $application, array $options = array())
 168      {
 169          $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
 170          $description = new ApplicationDescription($application, $describedNamespace);
 171   
 172          if (isset($options['raw_text']) && $options['raw_text']) {
 173              $width = $this->getColumnWidth($description->getCommands());
 174   
 175              foreach ($description->getCommands() as $command) {
 176                  $this->writeText(sprintf("%-{$width}s %s", $command->getName(), $command->getDescription()), $options);
 177                  $this->writeText("\n");
 178              }
 179          } else {
 180              if ('' != $help = $application->getHelp()) {
 181                  $this->writeText("$help\n\n", $options);
 182              }
 183   
 184              $this->writeText("<comment>Usage:</comment>\n", $options);
 185              $this->writeText("  command [options] [arguments]\n\n", $options);
 186   
 187              $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options);
 188   
 189              $this->writeText("\n");
 190              $this->writeText("\n");
 191   
 192              $width = $this->getColumnWidth($description->getCommands());
 193   
 194              if ($describedNamespace) {
 195                  $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
 196              } else {
 197                  $this->writeText('<comment>Available commands:</comment>', $options);
 198              }
 199   
 200              // add commands by namespace
 201              foreach ($description->getNamespaces() as $namespace) {
 202                  if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
 203                      $this->writeText("\n");
 204                      $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options);
 205                  }
 206   
 207                  foreach ($namespace['commands'] as $name) {
 208                      $this->writeText("\n");
 209                      $spacingWidth = $width - strlen($name);
 210                      $this->writeText(sprintf('  <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $description->getCommand($name)->getDescription()), $options);
 211                  }
 212              }
 213   
 214              $this->writeText("\n");
 215          }
 216      }
 217   
 218      /**
 219       * {@inheritdoc}
 220       */
 221      private function writeText($content, array $options = array())
 222      {
 223          $this->write(
 224              isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
 225              isset($options['raw_output']) ? !$options['raw_output'] : true
 226          );
 227      }
 228   
 229      /**
 230       * Formats input option/argument default value.
 231       *
 232       * @param mixed $default
 233       *
 234       * @return string
 235       */
 236      private function formatDefaultValue($default)
 237      {
 238          if (PHP_VERSION_ID < 50400) {
 239              return str_replace(array('\/', '\\\\'), array('/', '\\'), json_encode($default));
 240          }
 241   
 242          return str_replace('\\\\', '\\', json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
 243      }
 244   
 245      /**
 246       * @param Command[] $commands
 247       *
 248       * @return int
 249       */
 250      private function getColumnWidth(array $commands)
 251      {
 252          $widths = array();
 253   
 254          foreach ($commands as $command) {
 255              $widths[] = strlen($command->getName());
 256              foreach ($command->getAliases() as $alias) {
 257                  $widths[] = strlen($alias);
 258              }
 259          }
 260   
 261          return max($widths) + 2;
 262      }
 263   
 264      /**
 265       * @param InputOption[] $options
 266       *
 267       * @return int
 268       */
 269      private function calculateTotalWidthForOptions($options)
 270      {
 271          $totalWidth = 0;
 272          foreach ($options as $option) {
 273              // "-" + shortcut + ", --" + name
 274              $nameLength = 1 + max(strlen($option->getShortcut()), 1) + 4 + strlen($option->getName());
 275   
 276              if ($option->acceptValue()) {
 277                  $valueLength = 1 + strlen($option->getName()); // = + value
 278                  $valueLength += $option->isValueOptional() ? 2 : 0; // [ + ]
 279   
 280                  $nameLength += $valueLength;
 281              }
 282              $totalWidth = max($totalWidth, $nameLength);
 283          }
 284   
 285          return $totalWidth;
 286      }
 287  }
 288