-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
460 additions
and
2 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,28 @@ | ||
# Caching | ||
|
||
By default, Typhoon Reflection uses in-memory LRU cache which should be enough for the majority of use cases. | ||
|
||
However, if you need persistent cache, you can use any [PSR-16](https://www.php-fig.org/psr/psr-16/) implementation. We | ||
highly recommend [Typhoon OPcache](https://github.com/typhoon-php/opcache). It stores values as opcacheable php files. | ||
|
||
```php | ||
use Typhoon\Reflection\TyphoonReflector; | ||
use Typhoon\OPcache\TyphoonOPcache; | ||
|
||
$reflector = TyphoonReflector::build( | ||
cache: new TyphoonOPcache('path/to/cache/dir'), | ||
); | ||
``` | ||
|
||
To detect file changes during development, decorate your cache | ||
with [FreshCache](../../src/Reflection/Cache/FreshCache.php). | ||
|
||
```php | ||
use Typhoon\Reflection\TyphoonReflector; | ||
use Typhoon\Reflection\Cache\FreshCache; | ||
use Typhoon\OPcache\TyphoonOPcache; | ||
|
||
$reflector = TyphoonReflector::build( | ||
cache: new FreshCache(new TyphoonOPcache('path/to/cache/dir')), | ||
); | ||
``` |
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,62 @@ | ||
# Implementing custom types | ||
|
||
```php | ||
use Typhoon\Reflection\Annotated\CustomTypeResolver; | ||
use Typhoon\Reflection\Annotated\TypeContext; | ||
use Typhoon\Reflection\TyphoonReflector; | ||
use Typhoon\Type\Type; | ||
use Typhoon\Type\types; | ||
use Typhoon\Type\TypeVisitor; | ||
use function Typhoon\Type\stringify; | ||
|
||
/** | ||
* @implements Type<int|float> | ||
*/ | ||
enum binary: string implements Type, CustomTypeResolver | ||
{ | ||
case int16 = 'int16'; | ||
case int32 = 'int32'; | ||
case int64 = 'int64'; | ||
case float32 = 'float32'; | ||
case float64 = 'float64'; | ||
|
||
public function accept(TypeVisitor $visitor): mixed | ||
{ | ||
/** | ||
* We need to suppress here, because Psalm does not support var annotations on enum cases yet ;( | ||
* @psalm-suppress InvalidArgument | ||
*/ | ||
return match ($this) { | ||
self::int16 => $visitor->int($this, types::int(-32768), types::int(32767)), | ||
self::int32 => $visitor->int($this, types::int(-2147483648), types::int(2147483647)), | ||
self::int64 => $visitor->int($this, types::PHP_INT_MIN, types::PHP_INT_MAX), | ||
self::float32 => $visitor->float($this, types::float(-3.40282347E+38), types::float(3.40282347E+38)), | ||
self::float64 => $visitor->float($this, types::PHP_FLOAT_MIN, types::PHP_FLOAT_MAX), | ||
}; | ||
} | ||
|
||
public function resolveCustomType(string $name, array $typeArguments, TypeContext $context): ?Type | ||
{ | ||
return self::tryFrom($name); | ||
} | ||
} | ||
|
||
final readonly class Message | ||
{ | ||
/** | ||
* @param list<int16> $some16bitIntegers | ||
*/ | ||
public function __construct( | ||
public array $some16bitIntegers, | ||
) {} | ||
} | ||
|
||
$reflector = TyphoonReflector::build(customTypeResolvers: [binary::int16]); | ||
|
||
$propertyType = $reflector | ||
->reflectClass(Message::class) | ||
->properties()['some16bitIntegers'] | ||
->type(); | ||
|
||
var_dump(stringify($propertyType)); // "list<int<-32768, 32767>>" | ||
``` |
Oops, something went wrong.