Controller.php 25 KB

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