Controller.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\console;
  8. use Yii;
  9. use yii\base\Action;
  10. use yii\base\InlineAction;
  11. use yii\base\InvalidRouteException;
  12. use yii\helpers\Console;
  13. use yii\helpers\Inflector;
  14. /**
  15. * Controller is the base class of console command classes.
  16. *
  17. * A console controller consists of one or several actions known as sub-commands.
  18. * Users call a console command by specifying the corresponding route which identifies a controller action.
  19. * The `yii` program is used when calling a console command, like the following:
  20. *
  21. * ```
  22. * yii <route> [--param1=value1 --param2 ...]
  23. * ```
  24. *
  25. * where `<route>` is a route to a controller action and the params will be populated as properties of a command.
  26. * See [[options()]] for details.
  27. *
  28. * @property string $help This property is read-only.
  29. * @property string $helpSummary This property is read-only.
  30. * @property array $passedOptionValues The properties corresponding to the passed options. This property is
  31. * read-only.
  32. * @property array $passedOptions The names of the options passed during execution. This property is
  33. * read-only.
  34. *
  35. * @author Qiang Xue <qiang.xue@gmail.com>
  36. * @since 2.0
  37. */
  38. class Controller extends \yii\base\Controller
  39. {
  40. /**
  41. * @deprecated since 2.0.13. Use [[ExitCode::OK]] instead.
  42. */
  43. const EXIT_CODE_NORMAL = 0;
  44. /**
  45. * @deprecated since 2.0.13. Use [[ExitCode::UNSPECIFIED_ERROR]] instead.
  46. */
  47. const EXIT_CODE_ERROR = 1;
  48. /**
  49. * @var bool whether to run the command interactively.
  50. */
  51. public $interactive = true;
  52. /**
  53. * @var bool whether to enable ANSI color in the output.
  54. * If not set, ANSI color will only be enabled for terminals that support it.
  55. */
  56. public $color;
  57. /**
  58. * @var bool whether to display help information about current command.
  59. * @since 2.0.10
  60. */
  61. public $help;
  62. /**
  63. * @var bool if true - script finish with `ExitCode::OK` in case of exception.
  64. * false - `ExitCode::UNSPECIFIED_ERROR`.
  65. * Default: `YII_ENV_TEST`
  66. * @since 2.0.36
  67. */
  68. public $silentExitOnException;
  69. /**
  70. * @var array the options passed during execution.
  71. */
  72. private $_passedOptions = [];
  73. public function beforeAction($action)
  74. {
  75. $silentExit = $this->silentExitOnException !== null ? $this->silentExitOnException : YII_ENV_TEST;
  76. Yii::$app->errorHandler->silentExitOnException = $silentExit;
  77. return parent::beforeAction($action);
  78. }
  79. /**
  80. * Returns a value indicating whether ANSI color is enabled.
  81. *
  82. * ANSI color is enabled only if [[color]] is set true or is not set
  83. * and the terminal supports ANSI color.
  84. *
  85. * @param resource $stream the stream to check.
  86. * @return bool Whether to enable ANSI style in output.
  87. */
  88. public function isColorEnabled($stream = \STDOUT)
  89. {
  90. return $this->color === null ? Console::streamSupportsAnsiColors($stream) : $this->color;
  91. }
  92. /**
  93. * Runs an action with the specified action ID and parameters.
  94. * If the action ID is empty, the method will use [[defaultAction]].
  95. * @param string $id the ID of the action to be executed.
  96. * @param array $params the parameters (name-value pairs) to be passed to the action.
  97. * @return int the status of the action execution. 0 means normal, other values mean abnormal.
  98. * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully.
  99. * @throws Exception if there are unknown options or missing arguments
  100. * @see createAction
  101. */
  102. public function runAction($id, $params = [])
  103. {
  104. if (!empty($params)) {
  105. // populate options here so that they are available in beforeAction().
  106. $options = $this->options($id === '' ? $this->defaultAction : $id);
  107. if (isset($params['_aliases'])) {
  108. $optionAliases = $this->optionAliases();
  109. foreach ($params['_aliases'] as $name => $value) {
  110. if (array_key_exists($name, $optionAliases)) {
  111. $params[$optionAliases[$name]] = $value;
  112. } else {
  113. $message = Yii::t('yii', 'Unknown alias: -{name}', ['name' => $name]);
  114. if (!empty($optionAliases)) {
  115. $aliasesAvailable = [];
  116. foreach ($optionAliases as $alias => $option) {
  117. $aliasesAvailable[] = '-' . $alias . ' (--' . $option . ')';
  118. }
  119. $message .= '. ' . Yii::t('yii', 'Aliases available: {aliases}', [
  120. 'aliases' => implode(', ', $aliasesAvailable)
  121. ]);
  122. }
  123. throw new Exception($message);
  124. }
  125. }
  126. unset($params['_aliases']);
  127. }
  128. foreach ($params as $name => $value) {
  129. // Allow camelCase options to be entered in kebab-case
  130. if (!in_array($name, $options, true) && strpos($name, '-') !== false) {
  131. $kebabName = $name;
  132. $altName = lcfirst(Inflector::id2camel($kebabName));
  133. if (in_array($altName, $options, true)) {
  134. $name = $altName;
  135. }
  136. }
  137. if (in_array($name, $options, true)) {
  138. $default = $this->$name;
  139. if (is_array($default)) {
  140. $this->$name = preg_split('/\s*,\s*(?![^()]*\))/', $value);
  141. } elseif ($default !== null) {
  142. settype($value, gettype($default));
  143. $this->$name = $value;
  144. } else {
  145. $this->$name = $value;
  146. }
  147. $this->_passedOptions[] = $name;
  148. unset($params[$name]);
  149. if (isset($kebabName)) {
  150. unset($params[$kebabName]);
  151. }
  152. } elseif (!is_int($name)) {
  153. $message = Yii::t('yii', 'Unknown option: --{name}', ['name' => $name]);
  154. if (!empty($options)) {
  155. $message .= '. ' . Yii::t('yii', 'Options available: {options}', ['options' => '--' . implode(', --', $options)]);
  156. }
  157. throw new Exception($message);
  158. }
  159. }
  160. }
  161. if ($this->help) {
  162. $route = $this->getUniqueId() . '/' . $id;
  163. return Yii::$app->runAction('help', [$route]);
  164. }
  165. return parent::runAction($id, $params);
  166. }
  167. /**
  168. * Binds the parameters to the action.
  169. * This method is invoked by [[Action]] when it begins to run with the given parameters.
  170. * This method will first bind the parameters with the [[options()|options]]
  171. * available to the action. It then validates the given arguments.
  172. * @param Action $action the action to be bound with parameters
  173. * @param array $params the parameters to be bound to the action
  174. * @return array the valid parameters that the action can run with.
  175. * @throws Exception if there are unknown options or missing arguments
  176. */
  177. public function bindActionParams($action, $params)
  178. {
  179. if ($action instanceof InlineAction) {
  180. $method = new \ReflectionMethod($this, $action->actionMethod);
  181. } else {
  182. $method = new \ReflectionMethod($action, 'run');
  183. }
  184. $args = [];
  185. $missing = [];
  186. $actionParams = [];
  187. $requestedParams = [];
  188. foreach ($method->getParameters() as $i => $param) {
  189. $name = $param->getName();
  190. $key = null;
  191. if (array_key_exists($i, $params)) {
  192. $key = $i;
  193. } elseif (array_key_exists($name, $params)) {
  194. $key = $name;
  195. }
  196. if ($key !== null) {
  197. if ($param->isArray()) {
  198. $params[$key] = $params[$key] === '' ? [] : preg_split('/\s*,\s*/', $params[$key]);
  199. }
  200. $args[] = $actionParams[$key] = $params[$key];
  201. unset($params[$key]);
  202. } elseif (PHP_VERSION_ID >= 70100 && ($type = $param->getType()) !== null && !$type->isBuiltin()) {
  203. try {
  204. $this->bindInjectedParams($type, $name, $args, $requestedParams);
  205. } catch (\yii\base\Exception $e) {
  206. throw new Exception($e->getMessage());
  207. }
  208. } elseif ($param->isDefaultValueAvailable()) {
  209. $args[] = $actionParams[$i] = $param->getDefaultValue();
  210. } else {
  211. $missing[] = $name;
  212. }
  213. }
  214. if (!empty($missing)) {
  215. throw new Exception(Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', $missing)]));
  216. }
  217. // We use a different array here, specifically one that doesn't contain service instances but descriptions instead.
  218. if (\Yii::$app->requestedParams === null) {
  219. \Yii::$app->requestedParams = array_merge($actionParams, $requestedParams);
  220. }
  221. return array_merge($args, $params);
  222. }
  223. /**
  224. * Formats a string with ANSI codes.
  225. *
  226. * You may pass additional parameters using the constants defined in [[\yii\helpers\Console]].
  227. *
  228. * Example:
  229. *
  230. * ```
  231. * echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
  232. * ```
  233. *
  234. * @param string $string the string to be formatted
  235. * @return string
  236. */
  237. public function ansiFormat($string)
  238. {
  239. if ($this->isColorEnabled()) {
  240. $args = func_get_args();
  241. array_shift($args);
  242. $string = Console::ansiFormat($string, $args);
  243. }
  244. return $string;
  245. }
  246. /**
  247. * Prints a string to STDOUT.
  248. *
  249. * You may optionally format the string with ANSI codes by
  250. * passing additional parameters using the constants defined in [[\yii\helpers\Console]].
  251. *
  252. * Example:
  253. *
  254. * ```
  255. * $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
  256. * ```
  257. *
  258. * @param string $string the string to print
  259. * @param int ...$args additional parameters to decorate the output
  260. * @return int|bool Number of bytes printed or false on error
  261. */
  262. public function stdout($string)
  263. {
  264. if ($this->isColorEnabled()) {
  265. $args = func_get_args();
  266. array_shift($args);
  267. $string = Console::ansiFormat($string, $args);
  268. }
  269. return Console::stdout($string);
  270. }
  271. /**
  272. * Prints a string to STDERR.
  273. *
  274. * You may optionally format the string with ANSI codes by
  275. * passing additional parameters using the constants defined in [[\yii\helpers\Console]].
  276. *
  277. * Example:
  278. *
  279. * ```
  280. * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
  281. * ```
  282. *
  283. * @param string $string the string to print
  284. * @return int|bool Number of bytes printed or false on error
  285. */
  286. public function stderr($string)
  287. {
  288. if ($this->isColorEnabled(\STDERR)) {
  289. $args = func_get_args();
  290. array_shift($args);
  291. $string = Console::ansiFormat($string, $args);
  292. }
  293. return fwrite(\STDERR, $string);
  294. }
  295. /**
  296. * Prompts the user for input and validates it.
  297. *
  298. * @param string $text prompt string
  299. * @param array $options the options to validate the input:
  300. *
  301. * - required: whether it is required or not
  302. * - default: default value if no input is inserted by the user
  303. * - pattern: regular expression pattern to validate user input
  304. * - validator: a callable function to validate input. The function must accept two parameters:
  305. * - $input: the user input to validate
  306. * - $error: the error value passed by reference if validation failed.
  307. *
  308. * An example of how to use the prompt method with a validator function.
  309. *
  310. * ```php
  311. * $code = $this->prompt('Enter 4-Chars-Pin', ['required' => true, 'validator' => function($input, &$error) {
  312. * if (strlen($input) !== 4) {
  313. * $error = 'The Pin must be exactly 4 chars!';
  314. * return false;
  315. * }
  316. * return true;
  317. * }]);
  318. * ```
  319. *
  320. * @return string the user input
  321. */
  322. public function prompt($text, $options = [])
  323. {
  324. if ($this->interactive) {
  325. return Console::prompt($text, $options);
  326. }
  327. return isset($options['default']) ? $options['default'] : '';
  328. }
  329. /**
  330. * Asks user to confirm by typing y or n.
  331. *
  332. * A typical usage looks like the following:
  333. *
  334. * ```php
  335. * if ($this->confirm("Are you sure?")) {
  336. * echo "user typed yes\n";
  337. * } else {
  338. * echo "user typed no\n";
  339. * }
  340. * ```
  341. *
  342. * @param string $message to echo out before waiting for user input
  343. * @param bool $default this value is returned if no selection is made.
  344. * @return bool whether user confirmed.
  345. * Will return true if [[interactive]] is false.
  346. */
  347. public function confirm($message, $default = false)
  348. {
  349. if ($this->interactive) {
  350. return Console::confirm($message, $default);
  351. }
  352. return true;
  353. }
  354. /**
  355. * Gives the user an option to choose from. Giving '?' as an input will show
  356. * a list of options to choose from and their explanations.
  357. *
  358. * @param string $prompt the prompt message
  359. * @param array $options Key-value array of options to choose from
  360. *
  361. * @return string An option character the user chose
  362. */
  363. public function select($prompt, $options = [])
  364. {
  365. return Console::select($prompt, $options);
  366. }
  367. /**
  368. * Returns the names of valid options for the action (id)
  369. * An option requires the existence of a public member variable whose
  370. * name is the option name.
  371. * Child classes may override this method to specify possible options.
  372. *
  373. * Note that the values setting via options are not available
  374. * until [[beforeAction()]] is being called.
  375. *
  376. * @param string $actionID the action id of the current request
  377. * @return string[] the names of the options valid for the action
  378. */
  379. public function options($actionID)
  380. {
  381. // $actionId might be used in subclasses to provide options specific to action id
  382. return ['color', 'interactive', 'help', 'silentExitOnException'];
  383. }
  384. /**
  385. * Returns option alias names.
  386. * Child classes may override this method to specify alias options.
  387. *
  388. * @return array the options alias names valid for the action
  389. * where the keys is alias name for option and value is option name.
  390. *
  391. * @since 2.0.8
  392. * @see options()
  393. */
  394. public function optionAliases()
  395. {
  396. return [
  397. 'h' => 'help',
  398. ];
  399. }
  400. /**
  401. * Returns properties corresponding to the options for the action id
  402. * Child classes may override this method to specify possible properties.
  403. *
  404. * @param string $actionID the action id of the current request
  405. * @return array properties corresponding to the options for the action
  406. */
  407. public function getOptionValues($actionID)
  408. {
  409. // $actionId might be used in subclasses to provide properties specific to action id
  410. $properties = [];
  411. foreach ($this->options($this->action->id) as $property) {
  412. $properties[$property] = $this->$property;
  413. }
  414. return $properties;
  415. }
  416. /**
  417. * Returns the names of valid options passed during execution.
  418. *
  419. * @return array the names of the options passed during execution
  420. */
  421. public function getPassedOptions()
  422. {
  423. return $this->_passedOptions;
  424. }
  425. /**
  426. * Returns the properties corresponding to the passed options.
  427. *
  428. * @return array the properties corresponding to the passed options
  429. */
  430. public function getPassedOptionValues()
  431. {
  432. $properties = [];
  433. foreach ($this->_passedOptions as $property) {
  434. $properties[$property] = $this->$property;
  435. }
  436. return $properties;
  437. }
  438. /**
  439. * Returns one-line short summary describing this controller.
  440. *
  441. * You may override this method to return customized summary.
  442. * The default implementation returns first line from the PHPDoc comment.
  443. *
  444. * @return string
  445. */
  446. public function getHelpSummary()
  447. {
  448. return $this->parseDocCommentSummary(new \ReflectionClass($this));
  449. }
  450. /**
  451. * Returns help information for this controller.
  452. *
  453. * You may override this method to return customized help.
  454. * The default implementation returns help information retrieved from the PHPDoc comment.
  455. * @return string
  456. */
  457. public function getHelp()
  458. {
  459. return $this->parseDocCommentDetail(new \ReflectionClass($this));
  460. }
  461. /**
  462. * Returns a one-line short summary describing the specified action.
  463. * @param Action $action action to get summary for
  464. * @return string a one-line short summary describing the specified action.
  465. */
  466. public function getActionHelpSummary($action)
  467. {
  468. if ($action === null) {
  469. return $this->ansiFormat(Yii::t('yii', 'Action not found.'), Console::FG_RED);
  470. }
  471. return $this->parseDocCommentSummary($this->getActionMethodReflection($action));
  472. }
  473. /**
  474. * Returns the detailed help information for the specified action.
  475. * @param Action $action action to get help for
  476. * @return string the detailed help information for the specified action.
  477. */
  478. public function getActionHelp($action)
  479. {
  480. return $this->parseDocCommentDetail($this->getActionMethodReflection($action));
  481. }
  482. /**
  483. * Returns the help information for the anonymous arguments for the action.
  484. *
  485. * The returned value should be an array. The keys are the argument names, and the values are
  486. * the corresponding help information. Each value must be an array of the following structure:
  487. *
  488. * - required: boolean, whether this argument is required.
  489. * - type: string, the PHP type of this argument.
  490. * - default: string, the default value of this argument
  491. * - comment: string, the comment of this argument
  492. *
  493. * The default implementation will return the help information extracted from the doc-comment of
  494. * the parameters corresponding to the action method.
  495. *
  496. * @param Action $action
  497. * @return array the help information of the action arguments
  498. */
  499. public function getActionArgsHelp($action)
  500. {
  501. $method = $this->getActionMethodReflection($action);
  502. $tags = $this->parseDocCommentTags($method);
  503. $params = isset($tags['param']) ? (array) $tags['param'] : [];
  504. $args = [];
  505. /** @var \ReflectionParameter $reflection */
  506. foreach ($method->getParameters() as $i => $reflection) {
  507. if ($reflection->getClass() !== null) {
  508. continue;
  509. }
  510. $name = $reflection->getName();
  511. $tag = isset($params[$i]) ? $params[$i] : '';
  512. if (preg_match('/^(\S+)\s+(\$\w+\s+)?(.*)/s', $tag, $matches)) {
  513. $type = $matches[1];
  514. $comment = $matches[3];
  515. } else {
  516. $type = null;
  517. $comment = $tag;
  518. }
  519. if ($reflection->isDefaultValueAvailable()) {
  520. $args[$name] = [
  521. 'required' => false,
  522. 'type' => $type,
  523. 'default' => $reflection->getDefaultValue(),
  524. 'comment' => $comment,
  525. ];
  526. } else {
  527. $args[$name] = [
  528. 'required' => true,
  529. 'type' => $type,
  530. 'default' => null,
  531. 'comment' => $comment,
  532. ];
  533. }
  534. }
  535. return $args;
  536. }
  537. /**
  538. * Returns the help information for the options for the action.
  539. *
  540. * The returned value should be an array. The keys are the option names, and the values are
  541. * the corresponding help information. Each value must be an array of the following structure:
  542. *
  543. * - type: string, the PHP type of this argument.
  544. * - default: string, the default value of this argument
  545. * - comment: string, the comment of this argument
  546. *
  547. * The default implementation will return the help information extracted from the doc-comment of
  548. * the properties corresponding to the action options.
  549. *
  550. * @param Action $action
  551. * @return array the help information of the action options
  552. */
  553. public function getActionOptionsHelp($action)
  554. {
  555. $optionNames = $this->options($action->id);
  556. if (empty($optionNames)) {
  557. return [];
  558. }
  559. $class = new \ReflectionClass($this);
  560. $options = [];
  561. foreach ($class->getProperties() as $property) {
  562. $name = $property->getName();
  563. if (!in_array($name, $optionNames, true)) {
  564. continue;
  565. }
  566. $defaultValue = $property->getValue($this);
  567. $tags = $this->parseDocCommentTags($property);
  568. // Display camelCase options in kebab-case
  569. $name = Inflector::camel2id($name, '-', true);
  570. if (isset($tags['var']) || isset($tags['property'])) {
  571. $doc = isset($tags['var']) ? $tags['var'] : $tags['property'];
  572. if (is_array($doc)) {
  573. $doc = reset($doc);
  574. }
  575. if (preg_match('/^(\S+)(.*)/s', $doc, $matches)) {
  576. $type = $matches[1];
  577. $comment = $matches[2];
  578. } else {
  579. $type = null;
  580. $comment = $doc;
  581. }
  582. $options[$name] = [
  583. 'type' => $type,
  584. 'default' => $defaultValue,
  585. 'comment' => $comment,
  586. ];
  587. } else {
  588. $options[$name] = [
  589. 'type' => null,
  590. 'default' => $defaultValue,
  591. 'comment' => '',
  592. ];
  593. }
  594. }
  595. return $options;
  596. }
  597. private $_reflections = [];
  598. /**
  599. * @param Action $action
  600. * @return \ReflectionMethod
  601. */
  602. protected function getActionMethodReflection($action)
  603. {
  604. if (!isset($this->_reflections[$action->id])) {
  605. if ($action instanceof InlineAction) {
  606. $this->_reflections[$action->id] = new \ReflectionMethod($this, $action->actionMethod);
  607. } else {
  608. $this->_reflections[$action->id] = new \ReflectionMethod($action, 'run');
  609. }
  610. }
  611. return $this->_reflections[$action->id];
  612. }
  613. /**
  614. * Parses the comment block into tags.
  615. * @param \Reflector $reflection the comment block
  616. * @return array the parsed tags
  617. */
  618. protected function parseDocCommentTags($reflection)
  619. {
  620. $comment = $reflection->getDocComment();
  621. $comment = "@description \n" . strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($comment, '/'))), "\r", '');
  622. $parts = preg_split('/^\s*@/m', $comment, -1, PREG_SPLIT_NO_EMPTY);
  623. $tags = [];
  624. foreach ($parts as $part) {
  625. if (preg_match('/^(\w+)(.*)/ms', trim($part), $matches)) {
  626. $name = $matches[1];
  627. if (!isset($tags[$name])) {
  628. $tags[$name] = trim($matches[2]);
  629. } elseif (is_array($tags[$name])) {
  630. $tags[$name][] = trim($matches[2]);
  631. } else {
  632. $tags[$name] = [$tags[$name], trim($matches[2])];
  633. }
  634. }
  635. }
  636. return $tags;
  637. }
  638. /**
  639. * Returns the first line of docblock.
  640. *
  641. * @param \Reflector $reflection
  642. * @return string
  643. */
  644. protected function parseDocCommentSummary($reflection)
  645. {
  646. $docLines = preg_split('~\R~u', $reflection->getDocComment());
  647. if (isset($docLines[1])) {
  648. return trim($docLines[1], "\t *");
  649. }
  650. return '';
  651. }
  652. /**
  653. * Returns full description from the docblock.
  654. *
  655. * @param \Reflector $reflection
  656. * @return string
  657. */
  658. protected function parseDocCommentDetail($reflection)
  659. {
  660. $comment = strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($reflection->getDocComment(), '/'))), "\r", '');
  661. if (preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) {
  662. $comment = trim(substr($comment, 0, $matches[0][1]));
  663. }
  664. if ($comment !== '') {
  665. return rtrim(Console::renderColoredString(Console::markdownToAnsi($comment)));
  666. }
  667. return '';
  668. }
  669. }