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