截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Closure.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (https://nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. declare(strict_types=1);
  7. namespace Nette\PhpGenerator;
  8. use Nette;
  9. /**
  10. * Closure.
  11. *
  12. * @property string $body
  13. */
  14. final class Closure
  15. {
  16. use Nette\SmartObject;
  17. use Traits\FunctionLike;
  18. use Traits\AttributeAware;
  19. /** @var Parameter[] */
  20. private $uses = [];
  21. public static function from(\Closure $closure): self
  22. {
  23. return (new Factory)->fromFunctionReflection(new \ReflectionFunction($closure));
  24. }
  25. public function __toString(): string
  26. {
  27. try {
  28. return (new Printer)->printClosure($this);
  29. } catch (\Throwable $e) {
  30. if (PHP_VERSION_ID >= 70400) {
  31. throw $e;
  32. }
  33. trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
  34. return '';
  35. }
  36. }
  37. /**
  38. * @param Parameter[] $uses
  39. * @return static
  40. */
  41. public function setUses(array $uses): self
  42. {
  43. (function (Parameter ...$uses) {})(...$uses);
  44. $this->uses = $uses;
  45. return $this;
  46. }
  47. public function getUses(): array
  48. {
  49. return $this->uses;
  50. }
  51. public function addUse(string $name): Parameter
  52. {
  53. return $this->uses[] = new Parameter($name);
  54. }
  55. }