Definition.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Argument\BoundArgument;
  12. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  13. use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
  14. /**
  15. * Definition represents a service definition.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class Definition
  20. {
  21. private $class;
  22. private $file;
  23. private $factory;
  24. private $shared = true;
  25. private $deprecated = false;
  26. private $deprecationTemplate;
  27. private $properties = [];
  28. private $calls = [];
  29. private $instanceof = [];
  30. private $autoconfigured = false;
  31. private $configurator;
  32. private $tags = [];
  33. private $public = true;
  34. private $private = true;
  35. private $synthetic = false;
  36. private $abstract = false;
  37. private $lazy = false;
  38. private $decoratedService;
  39. private $autowired = false;
  40. private $changes = [];
  41. private $bindings = [];
  42. private $errors = [];
  43. protected $arguments = [];
  44. private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.';
  45. /**
  46. * @internal
  47. *
  48. * Used to store the name of the inner id when using service decoration together with autowiring
  49. */
  50. public $innerServiceId;
  51. /**
  52. * @internal
  53. *
  54. * Used to store the behavior to follow when using service decoration and the decorated service is invalid
  55. */
  56. public $decorationOnInvalid;
  57. /**
  58. * @param string|null $class The service class
  59. * @param array $arguments An array of arguments to pass to the service constructor
  60. */
  61. public function __construct($class = null, array $arguments = [])
  62. {
  63. if (null !== $class) {
  64. $this->setClass($class);
  65. }
  66. $this->arguments = $arguments;
  67. }
  68. /**
  69. * Returns all changes tracked for the Definition object.
  70. *
  71. * @return array An array of changes for this Definition
  72. */
  73. public function getChanges()
  74. {
  75. return $this->changes;
  76. }
  77. /**
  78. * Sets the tracked changes for the Definition object.
  79. *
  80. * @param array $changes An array of changes for this Definition
  81. *
  82. * @return $this
  83. */
  84. public function setChanges(array $changes)
  85. {
  86. $this->changes = $changes;
  87. return $this;
  88. }
  89. /**
  90. * Sets a factory.
  91. *
  92. * @param string|array|Reference $factory A PHP function, reference or an array containing a class/Reference and a method to call
  93. *
  94. * @return $this
  95. */
  96. public function setFactory($factory)
  97. {
  98. $this->changes['factory'] = true;
  99. if (\is_string($factory) && false !== strpos($factory, '::')) {
  100. $factory = explode('::', $factory, 2);
  101. } elseif ($factory instanceof Reference) {
  102. $factory = [$factory, '__invoke'];
  103. }
  104. $this->factory = $factory;
  105. return $this;
  106. }
  107. /**
  108. * Gets the factory.
  109. *
  110. * @return string|array|null The PHP function or an array containing a class/Reference and a method to call
  111. */
  112. public function getFactory()
  113. {
  114. return $this->factory;
  115. }
  116. /**
  117. * Sets the service that this service is decorating.
  118. *
  119. * @param string|null $id The decorated service id, use null to remove decoration
  120. * @param string|null $renamedId The new decorated service id
  121. * @param int $priority The priority of decoration
  122. * @param int $invalidBehavior The behavior to adopt when decorated is invalid
  123. *
  124. * @return $this
  125. *
  126. * @throws InvalidArgumentException in case the decorated service id and the new decorated service id are equals
  127. */
  128. public function setDecoratedService($id, $renamedId = null, $priority = 0/*, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE*/)
  129. {
  130. if ($renamedId && $id === $renamedId) {
  131. throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
  132. }
  133. $invalidBehavior = 3 < \func_num_args() ? (int) func_get_arg(3) : ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  134. $this->changes['decorated_service'] = true;
  135. if (null === $id) {
  136. $this->decoratedService = null;
  137. } else {
  138. $this->decoratedService = [$id, $renamedId, (int) $priority];
  139. if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
  140. $this->decoratedService[] = $invalidBehavior;
  141. }
  142. }
  143. return $this;
  144. }
  145. /**
  146. * Gets the service that this service is decorating.
  147. *
  148. * @return array|null An array composed of the decorated service id, the new id for it and the priority of decoration, null if no service is decorated
  149. */
  150. public function getDecoratedService()
  151. {
  152. return $this->decoratedService;
  153. }
  154. /**
  155. * Sets the service class.
  156. *
  157. * @param string $class The service class
  158. *
  159. * @return $this
  160. */
  161. public function setClass($class)
  162. {
  163. if ($class instanceof Parameter) {
  164. @trigger_error(sprintf('Passing an instance of %s as class name to %s in deprecated in Symfony 4.4 and will result in a TypeError in 5.0. Please pass the string "%%%s%%" instead.', Parameter::class, __CLASS__, (string) $class), E_USER_DEPRECATED);
  165. }
  166. if (null !== $class && !\is_string($class)) {
  167. @trigger_error(sprintf('The class name passed to %s is expected to be a string. Passing a %s is deprecated in Symfony 4.4 and will result in a TypeError in 5.0.', __CLASS__, \is_object($class) ? \get_class($class) : \gettype($class)), E_USER_DEPRECATED);
  168. }
  169. $this->changes['class'] = true;
  170. $this->class = $class;
  171. return $this;
  172. }
  173. /**
  174. * Gets the service class.
  175. *
  176. * @return string|null The service class
  177. */
  178. public function getClass()
  179. {
  180. return $this->class;
  181. }
  182. /**
  183. * Sets the arguments to pass to the service constructor/factory method.
  184. *
  185. * @return $this
  186. */
  187. public function setArguments(array $arguments)
  188. {
  189. $this->arguments = $arguments;
  190. return $this;
  191. }
  192. /**
  193. * Sets the properties to define when creating the service.
  194. *
  195. * @return $this
  196. */
  197. public function setProperties(array $properties)
  198. {
  199. $this->properties = $properties;
  200. return $this;
  201. }
  202. /**
  203. * Gets the properties to define when creating the service.
  204. *
  205. * @return array
  206. */
  207. public function getProperties()
  208. {
  209. return $this->properties;
  210. }
  211. /**
  212. * Sets a specific property.
  213. *
  214. * @param string $name
  215. * @param mixed $value
  216. *
  217. * @return $this
  218. */
  219. public function setProperty($name, $value)
  220. {
  221. $this->properties[$name] = $value;
  222. return $this;
  223. }
  224. /**
  225. * Adds an argument to pass to the service constructor/factory method.
  226. *
  227. * @param mixed $argument An argument
  228. *
  229. * @return $this
  230. */
  231. public function addArgument($argument)
  232. {
  233. $this->arguments[] = $argument;
  234. return $this;
  235. }
  236. /**
  237. * Replaces a specific argument.
  238. *
  239. * @param int|string $index
  240. * @param mixed $argument
  241. *
  242. * @return $this
  243. *
  244. * @throws OutOfBoundsException When the replaced argument does not exist
  245. */
  246. public function replaceArgument($index, $argument)
  247. {
  248. if (0 === \count($this->arguments)) {
  249. throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
  250. }
  251. if (\is_int($index) && ($index < 0 || $index > \count($this->arguments) - 1)) {
  252. throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, \count($this->arguments) - 1));
  253. }
  254. if (!\array_key_exists($index, $this->arguments)) {
  255. throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
  256. }
  257. $this->arguments[$index] = $argument;
  258. return $this;
  259. }
  260. /**
  261. * Sets a specific argument.
  262. *
  263. * @param int|string $key
  264. * @param mixed $value
  265. *
  266. * @return $this
  267. */
  268. public function setArgument($key, $value)
  269. {
  270. $this->arguments[$key] = $value;
  271. return $this;
  272. }
  273. /**
  274. * Gets the arguments to pass to the service constructor/factory method.
  275. *
  276. * @return array The array of arguments
  277. */
  278. public function getArguments()
  279. {
  280. return $this->arguments;
  281. }
  282. /**
  283. * Gets an argument to pass to the service constructor/factory method.
  284. *
  285. * @param int|string $index
  286. *
  287. * @return mixed The argument value
  288. *
  289. * @throws OutOfBoundsException When the argument does not exist
  290. */
  291. public function getArgument($index)
  292. {
  293. if (!\array_key_exists($index, $this->arguments)) {
  294. throw new OutOfBoundsException(sprintf('The argument "%s" doesn\'t exist.', $index));
  295. }
  296. return $this->arguments[$index];
  297. }
  298. /**
  299. * Sets the methods to call after service initialization.
  300. *
  301. * @return $this
  302. */
  303. public function setMethodCalls(array $calls = [])
  304. {
  305. $this->calls = [];
  306. foreach ($calls as $call) {
  307. $this->addMethodCall($call[0], $call[1], $call[2] ?? false);
  308. }
  309. return $this;
  310. }
  311. /**
  312. * Adds a method to call after service initialization.
  313. *
  314. * @param string $method The method name to call
  315. * @param array $arguments An array of arguments to pass to the method call
  316. * @param bool $returnsClone Whether the call returns the service instance or not
  317. *
  318. * @return $this
  319. *
  320. * @throws InvalidArgumentException on empty $method param
  321. */
  322. public function addMethodCall($method, array $arguments = []/*, bool $returnsClone = false*/)
  323. {
  324. if (empty($method)) {
  325. throw new InvalidArgumentException('Method name cannot be empty.');
  326. }
  327. $this->calls[] = 2 < \func_num_args() && func_get_arg(2) ? [$method, $arguments, true] : [$method, $arguments];
  328. return $this;
  329. }
  330. /**
  331. * Removes a method to call after service initialization.
  332. *
  333. * @param string $method The method name to remove
  334. *
  335. * @return $this
  336. */
  337. public function removeMethodCall($method)
  338. {
  339. foreach ($this->calls as $i => $call) {
  340. if ($call[0] === $method) {
  341. unset($this->calls[$i]);
  342. break;
  343. }
  344. }
  345. return $this;
  346. }
  347. /**
  348. * Check if the current definition has a given method to call after service initialization.
  349. *
  350. * @param string $method The method name to search for
  351. *
  352. * @return bool
  353. */
  354. public function hasMethodCall($method)
  355. {
  356. foreach ($this->calls as $call) {
  357. if ($call[0] === $method) {
  358. return true;
  359. }
  360. }
  361. return false;
  362. }
  363. /**
  364. * Gets the methods to call after service initialization.
  365. *
  366. * @return array An array of method calls
  367. */
  368. public function getMethodCalls()
  369. {
  370. return $this->calls;
  371. }
  372. /**
  373. * Sets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
  374. *
  375. * @param ChildDefinition[] $instanceof
  376. *
  377. * @return $this
  378. */
  379. public function setInstanceofConditionals(array $instanceof)
  380. {
  381. $this->instanceof = $instanceof;
  382. return $this;
  383. }
  384. /**
  385. * Gets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
  386. *
  387. * @return ChildDefinition[]
  388. */
  389. public function getInstanceofConditionals()
  390. {
  391. return $this->instanceof;
  392. }
  393. /**
  394. * Sets whether or not instanceof conditionals should be prepended with a global set.
  395. *
  396. * @param bool $autoconfigured
  397. *
  398. * @return $this
  399. */
  400. public function setAutoconfigured($autoconfigured)
  401. {
  402. $this->changes['autoconfigured'] = true;
  403. $this->autoconfigured = $autoconfigured;
  404. return $this;
  405. }
  406. /**
  407. * @return bool
  408. */
  409. public function isAutoconfigured()
  410. {
  411. return $this->autoconfigured;
  412. }
  413. /**
  414. * Sets tags for this definition.
  415. *
  416. * @return $this
  417. */
  418. public function setTags(array $tags)
  419. {
  420. $this->tags = $tags;
  421. return $this;
  422. }
  423. /**
  424. * Returns all tags.
  425. *
  426. * @return array An array of tags
  427. */
  428. public function getTags()
  429. {
  430. return $this->tags;
  431. }
  432. /**
  433. * Gets a tag by name.
  434. *
  435. * @param string $name The tag name
  436. *
  437. * @return array An array of attributes
  438. */
  439. public function getTag($name)
  440. {
  441. return isset($this->tags[$name]) ? $this->tags[$name] : [];
  442. }
  443. /**
  444. * Adds a tag for this definition.
  445. *
  446. * @param string $name The tag name
  447. * @param array $attributes An array of attributes
  448. *
  449. * @return $this
  450. */
  451. public function addTag($name, array $attributes = [])
  452. {
  453. $this->tags[$name][] = $attributes;
  454. return $this;
  455. }
  456. /**
  457. * Whether this definition has a tag with the given name.
  458. *
  459. * @param string $name
  460. *
  461. * @return bool
  462. */
  463. public function hasTag($name)
  464. {
  465. return isset($this->tags[$name]);
  466. }
  467. /**
  468. * Clears all tags for a given name.
  469. *
  470. * @param string $name The tag name
  471. *
  472. * @return $this
  473. */
  474. public function clearTag($name)
  475. {
  476. unset($this->tags[$name]);
  477. return $this;
  478. }
  479. /**
  480. * Clears the tags for this definition.
  481. *
  482. * @return $this
  483. */
  484. public function clearTags()
  485. {
  486. $this->tags = [];
  487. return $this;
  488. }
  489. /**
  490. * Sets a file to require before creating the service.
  491. *
  492. * @param string $file A full pathname to include
  493. *
  494. * @return $this
  495. */
  496. public function setFile($file)
  497. {
  498. $this->changes['file'] = true;
  499. $this->file = $file;
  500. return $this;
  501. }
  502. /**
  503. * Gets the file to require before creating the service.
  504. *
  505. * @return string|null The full pathname to include
  506. */
  507. public function getFile()
  508. {
  509. return $this->file;
  510. }
  511. /**
  512. * Sets if the service must be shared or not.
  513. *
  514. * @param bool $shared Whether the service must be shared or not
  515. *
  516. * @return $this
  517. */
  518. public function setShared($shared)
  519. {
  520. $this->changes['shared'] = true;
  521. $this->shared = (bool) $shared;
  522. return $this;
  523. }
  524. /**
  525. * Whether this service is shared.
  526. *
  527. * @return bool
  528. */
  529. public function isShared()
  530. {
  531. return $this->shared;
  532. }
  533. /**
  534. * Sets the visibility of this service.
  535. *
  536. * @param bool $boolean
  537. *
  538. * @return $this
  539. */
  540. public function setPublic($boolean)
  541. {
  542. $this->changes['public'] = true;
  543. $this->public = (bool) $boolean;
  544. $this->private = false;
  545. return $this;
  546. }
  547. /**
  548. * Whether this service is public facing.
  549. *
  550. * @return bool
  551. */
  552. public function isPublic()
  553. {
  554. return $this->public;
  555. }
  556. /**
  557. * Sets if this service is private.
  558. *
  559. * When set, the "private" state has a higher precedence than "public".
  560. * In version 3.4, a "private" service always remains publicly accessible,
  561. * but triggers a deprecation notice when accessed from the container,
  562. * so that the service can be made really private in 4.0.
  563. *
  564. * @param bool $boolean
  565. *
  566. * @return $this
  567. */
  568. public function setPrivate($boolean)
  569. {
  570. $this->private = (bool) $boolean;
  571. return $this;
  572. }
  573. /**
  574. * Whether this service is private.
  575. *
  576. * @return bool
  577. */
  578. public function isPrivate()
  579. {
  580. return $this->private;
  581. }
  582. /**
  583. * Sets the lazy flag of this service.
  584. *
  585. * @param bool $lazy
  586. *
  587. * @return $this
  588. */
  589. public function setLazy($lazy)
  590. {
  591. $this->changes['lazy'] = true;
  592. $this->lazy = (bool) $lazy;
  593. return $this;
  594. }
  595. /**
  596. * Whether this service is lazy.
  597. *
  598. * @return bool
  599. */
  600. public function isLazy()
  601. {
  602. return $this->lazy;
  603. }
  604. /**
  605. * Sets whether this definition is synthetic, that is not constructed by the
  606. * container, but dynamically injected.
  607. *
  608. * @param bool $boolean
  609. *
  610. * @return $this
  611. */
  612. public function setSynthetic($boolean)
  613. {
  614. $this->synthetic = (bool) $boolean;
  615. return $this;
  616. }
  617. /**
  618. * Whether this definition is synthetic, that is not constructed by the
  619. * container, but dynamically injected.
  620. *
  621. * @return bool
  622. */
  623. public function isSynthetic()
  624. {
  625. return $this->synthetic;
  626. }
  627. /**
  628. * Whether this definition is abstract, that means it merely serves as a
  629. * template for other definitions.
  630. *
  631. * @param bool $boolean
  632. *
  633. * @return $this
  634. */
  635. public function setAbstract($boolean)
  636. {
  637. $this->abstract = (bool) $boolean;
  638. return $this;
  639. }
  640. /**
  641. * Whether this definition is abstract, that means it merely serves as a
  642. * template for other definitions.
  643. *
  644. * @return bool
  645. */
  646. public function isAbstract()
  647. {
  648. return $this->abstract;
  649. }
  650. /**
  651. * Whether this definition is deprecated, that means it should not be called
  652. * anymore.
  653. *
  654. * @param bool $status
  655. * @param string $template Template message to use if the definition is deprecated
  656. *
  657. * @return $this
  658. *
  659. * @throws InvalidArgumentException when the message template is invalid
  660. */
  661. public function setDeprecated($status = true, $template = null)
  662. {
  663. if (null !== $template) {
  664. if (preg_match('#[\r\n]|\*/#', $template)) {
  665. throw new InvalidArgumentException('Invalid characters found in deprecation template.');
  666. }
  667. if (false === strpos($template, '%service_id%')) {
  668. throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
  669. }
  670. $this->deprecationTemplate = $template;
  671. }
  672. $this->changes['deprecated'] = true;
  673. $this->deprecated = (bool) $status;
  674. return $this;
  675. }
  676. /**
  677. * Whether this definition is deprecated, that means it should not be called
  678. * anymore.
  679. *
  680. * @return bool
  681. */
  682. public function isDeprecated()
  683. {
  684. return $this->deprecated;
  685. }
  686. /**
  687. * Message to use if this definition is deprecated.
  688. *
  689. * @param string $id Service id relying on this definition
  690. *
  691. * @return string
  692. */
  693. public function getDeprecationMessage($id)
  694. {
  695. return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
  696. }
  697. /**
  698. * Sets a configurator to call after the service is fully initialized.
  699. *
  700. * @param string|array|Reference $configurator A PHP function, reference or an array containing a class/Reference and a method to call
  701. *
  702. * @return $this
  703. */
  704. public function setConfigurator($configurator)
  705. {
  706. $this->changes['configurator'] = true;
  707. if (\is_string($configurator) && false !== strpos($configurator, '::')) {
  708. $configurator = explode('::', $configurator, 2);
  709. } elseif ($configurator instanceof Reference) {
  710. $configurator = [$configurator, '__invoke'];
  711. }
  712. $this->configurator = $configurator;
  713. return $this;
  714. }
  715. /**
  716. * Gets the configurator to call after the service is fully initialized.
  717. *
  718. * @return callable|array|null
  719. */
  720. public function getConfigurator()
  721. {
  722. return $this->configurator;
  723. }
  724. /**
  725. * Is the definition autowired?
  726. *
  727. * @return bool
  728. */
  729. public function isAutowired()
  730. {
  731. return $this->autowired;
  732. }
  733. /**
  734. * Enables/disables autowiring.
  735. *
  736. * @param bool $autowired
  737. *
  738. * @return $this
  739. */
  740. public function setAutowired($autowired)
  741. {
  742. $this->changes['autowired'] = true;
  743. $this->autowired = (bool) $autowired;
  744. return $this;
  745. }
  746. /**
  747. * Gets bindings.
  748. *
  749. * @return array|BoundArgument[]
  750. */
  751. public function getBindings()
  752. {
  753. return $this->bindings;
  754. }
  755. /**
  756. * Sets bindings.
  757. *
  758. * Bindings map $named or FQCN arguments to values that should be
  759. * injected in the matching parameters (of the constructor, of methods
  760. * called and of controller actions).
  761. *
  762. * @return $this
  763. */
  764. public function setBindings(array $bindings)
  765. {
  766. foreach ($bindings as $key => $binding) {
  767. if (0 < strpos($key, '$') && $key !== $k = preg_replace('/[ \t]*\$/', ' $', $key)) {
  768. unset($bindings[$key]);
  769. $bindings[$key = $k] = $binding;
  770. }
  771. if (!$binding instanceof BoundArgument) {
  772. $bindings[$key] = new BoundArgument($binding);
  773. }
  774. }
  775. $this->bindings = $bindings;
  776. return $this;
  777. }
  778. /**
  779. * Add an error that occurred when building this Definition.
  780. *
  781. * @param string|\Closure|self $error
  782. *
  783. * @return $this
  784. */
  785. public function addError($error)
  786. {
  787. if ($error instanceof self) {
  788. $this->errors = array_merge($this->errors, $error->errors);
  789. } else {
  790. $this->errors[] = $error;
  791. }
  792. return $this;
  793. }
  794. /**
  795. * Returns any errors that occurred while building this Definition.
  796. *
  797. * @return array
  798. */
  799. public function getErrors()
  800. {
  801. foreach ($this->errors as $i => $error) {
  802. if ($error instanceof \Closure) {
  803. $this->errors[$i] = (string) $error();
  804. } elseif (!\is_string($error)) {
  805. $this->errors[$i] = (string) $error;
  806. }
  807. }
  808. return $this->errors;
  809. }
  810. public function hasErrors(): bool
  811. {
  812. return (bool) $this->errors;
  813. }
  814. }