-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added FunctionCallable & MethodCallable, expressions representing fir…
…st-class callables
- Loading branch information
Showing
6 changed files
with
131 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (https://nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\DI\Definitions; | ||
|
||
use Nette; | ||
use Nette\DI\PhpGenerator; | ||
use Nette\DI\Resolver; | ||
use Nette\PhpGenerator as Php; | ||
|
||
|
||
final class FunctionCallable extends Expression | ||
{ | ||
public function __construct( | ||
public string $function, | ||
) { | ||
if (!Php\Helpers::isIdentifier($function)) { | ||
throw new Nette\InvalidArgumentException("Function name '$function' is not valid."); | ||
} | ||
} | ||
|
||
|
||
public function resolveType(Resolver $resolver): ?string | ||
{ | ||
return \Closure::class; | ||
} | ||
|
||
|
||
public function complete(Resolver $resolver): void | ||
{ | ||
} | ||
|
||
|
||
public function generateCode(PhpGenerator $generator): string | ||
{ | ||
return $this->function . '(...)'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (https://nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\DI\Definitions; | ||
|
||
use Nette; | ||
use Nette\DI\PhpGenerator; | ||
use Nette\DI\Resolver; | ||
use Nette\PhpGenerator as Php; | ||
|
||
|
||
final class MethodCallable extends Expression | ||
{ | ||
public function __construct( | ||
public Expression|string $objectOrClass, | ||
public string $method, | ||
) { | ||
if (is_string($objectOrClass) && !Php\Helpers::isNamespaceIdentifier($objectOrClass)) { | ||
throw new Nette\InvalidArgumentException("Class name '$objectOrClass' is not valid."); | ||
} | ||
if (!Php\Helpers::isIdentifier($method)) { | ||
throw new Nette\InvalidArgumentException("Method name '$method' is not valid."); | ||
} | ||
} | ||
|
||
|
||
public function resolveType(Resolver $resolver): ?string | ||
{ | ||
return \Closure::class; | ||
} | ||
|
||
|
||
public function complete(Resolver $resolver): void | ||
{ | ||
if ($this->objectOrClass instanceof Expression) { | ||
$this->objectOrClass->complete($resolver); | ||
} | ||
} | ||
|
||
|
||
public function generateCode(PhpGenerator $generator): string | ||
{ | ||
return is_string($this->objectOrClass) | ||
? $generator->formatPhp('?::?(...)', [new Php\Literal($this->objectOrClass), $this->method]) | ||
: $generator->formatPhp('?->?(...)', [new Php\Literal($this->objectOrClass->generateCode($generator)), $this->method]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters