当前位置: 首页>>代码示例>>PHP>>正文


PHP Helper::substr方法代码示例

本文整理汇总了PHP中Symfony\Component\Console\Helper\Helper::substr方法的典型用法代码示例。如果您正苦于以下问题:PHP Helper::substr方法的具体用法?PHP Helper::substr怎么用?PHP Helper::substr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Symfony\Component\Console\Helper\Helper的用法示例。


在下文中一共展示了Helper::substr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: find

 /**
  * Finds a command by name or alias.
  *
  * Contrary to get, this command tries to find the best
  * match if you give it an abbreviation of a name or alias.
  *
  * @param string $name A command name or a command alias
  *
  * @return Command A Command instance
  *
  * @throws CommandNotFoundException When command name is incorrect or ambiguous
  */
 public function find($name)
 {
     $allCommands = array_keys($this->commands);
     $expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
         return preg_quote($matches[1]) . '[^:]*';
     }, $name);
     $commands = preg_grep('{^' . $expr . '}', $allCommands);
     if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) {
         if (false !== ($pos = strrpos($name, ':'))) {
             // check if a namespace exists and contains commands
             $this->findNamespace(substr($name, 0, $pos));
         }
         $message = sprintf('Command "%s" is not defined.', $name);
         if ($alternatives = $this->findAlternatives($name, $allCommands)) {
             if (1 == count($alternatives)) {
                 $message .= "\n\nDid you mean this?\n    ";
             } else {
                 $message .= "\n\nDid you mean one of these?\n    ";
             }
             $message .= implode("\n    ", $alternatives);
         }
         throw new CommandNotFoundException($message, $alternatives);
     }
     // filter out aliases for commands which are already on the list
     if (count($commands) > 1) {
         $commandList = $this->commands;
         $commands = array_filter($commands, function ($nameOrAlias) use($commandList, $commands) {
             $commandName = $commandList[$nameOrAlias]->getName();
             return $commandName === $nameOrAlias || !in_array($commandName, $commands);
         });
     }
     $exact = in_array($name, $commands, true);
     if (count($commands) > 1 && !$exact) {
         $usableWidth = $this->terminal->getWidth() - 10;
         $abbrevs = array_values($commands);
         $maxLen = 0;
         foreach ($abbrevs as $abbrev) {
             $maxLen = max(Helper::strlen($abbrev), $maxLen);
         }
         $abbrevs = array_map(function ($cmd) use($commandList, $usableWidth, $maxLen) {
             $abbrev = str_pad($cmd, $maxLen, ' ') . ' ' . $commandList[$cmd]->getDescription();
             return Helper::strlen($abbrev) > $usableWidth ? Helper::substr($abbrev, 0, $usableWidth - 3) . '...' : $abbrev;
         }, array_values($commands));
         $suggestions = $this->getAbbreviationSuggestions($abbrevs);
         throw new CommandNotFoundException(sprintf("Command \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $name, $suggestions), array_values($commands));
     }
     return $this->get($exact ? $name : reset($commands));
 }
开发者ID:symfony,项目名称:symfony,代码行数:60,代码来源:Application.php


注:本文中的Symfony\Component\Console\Helper\Helper::substr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。