dunglas / php-property-info
v0.2.3
2015-12-29 08:21 UTC
Requires
- php: >=5.4
Requires (Dev)
- doctrine/orm: ~2.3
- phpdocumentor/reflection: ~1.0
- phpspec/phpspec: ~2.1
Suggests
- doctrine/orm: To use Doctrine metadata
- phpdocumentor/reflection: To use the PHPDoc
- symfony/validator: To use Symfony validator metadata
README
已弃用:此库已合并到 Symfony 框架中。请使用并贡献到 Symfony PropertyInfo 组件。
PHP 不支持显式类型定义。这在进行元编程时尤其令人烦恼。包括但不限于 Doctrine ORM 和 Symfony Validator 等各种库都提供了它们自己的类型管理系统。此库从流行的来源提取各种信息,包括类型和文档
- 带有类型提示的设置方法
- PHPDoc DocBlock
- Doctrine ORM 映射(注解、XML、YML 或自定义格式)
- PHP 7 标量类型提示和返回类型
- Hack(hacklang)类型
PHP 属性信息是 API Platform 框架的一部分。
安装
使用 Composer 安装库
composer require dunglas/php-property-info
要使用 PHPDoc 提取器,请安装 phpDocumentor's Reflection 库。要使用 Doctrine 提取器,请安装 Doctrine ORM。
用法
<?php // Use Composer autoload require 'vendor/autoload.php'; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\Id; // PropoertyInfo uses use PropertyInfo\Extractors\DoctrineExtractor; use PropertyInfo\Extractors\PhpDocExtractor; use PropertyInfo\Extractors\SetterExtractor; use PropertyInfo\PropertyInfo; /** * @Entity */ class MyTestClass { /** * @Id * @Column(type="integer") */ public $id; /** * This is a date (short description). * * With a long description. * * @var \DateTime */ public $foo; private $bar; public function setBar(\SplFileInfo $bar) { $this->bar = $bar; } } // Doctrine initialization (necessary only to use the Doctrine Extractor) $config = Setup::createAnnotationMetadataConfiguration([__DIR__], true); $entityManager = EntityManager::create([ 'driver' => 'pdo_sqlite', // ... ], $config); $doctrineExtractor = new DoctrineExtractor($entityManager->getMetadataFactory()); $phpDocExtractor = new PhpDocExtractor(); $setterExtractor = new SetterExtractor(); $propertyInfo = new PropertyInfo([$doctrineExtractor, $setterExtractor, $phpDocExtractor], [$phpDocExtractor]); $fooProperty = new \ReflectionProperty('MyTestClass', 'foo'); var_dump($propertyInfo->getShortDescription($fooProperty)); var_dump($propertyInfo->getLongDescription($fooProperty)); var_dump($propertyInfo->getTypes($fooProperty)); var_dump($propertyInfo->getTypes(new \ReflectionProperty('MyTestClass', 'id'))); var_dump($propertyInfo->getTypes(new \ReflectionProperty('MyTestClass', 'bar')));
输出
string(35) "This is a date (short description)."
string(24) "With a long description."
array(1) {
[0] =>
class PropertyInfo\Type#162 (4) {
public $type =>
string(6) "object"
public $class =>
string(8) "DateTime"
public $collection =>
bool(false)
public $collectionType =>
NULL
}
}
array(1) {
[0] =>
class PropertyInfo\Type#172 (4) {
public $type =>
string(3) "int"
public $class =>
NULL
public $collection =>
bool(false)
public $collectionType =>
NULL
}
}
array(1) {
[0] =>
class PropertyInfo\Type#165 (4) {
public $type =>
string(6) "object"
public $class =>
string(11) "SplFileInfo"
public $collection =>
bool(false)
public $collectionType =>
NULL
}
}
使用 Melody 尝试它
php melody.phar run https://gist.github.com/dunglas/0a4982e4635c9514aede
待办事项
- Symfony Validator 组件支持
致谢
此库由 Kévin Dunglas 创建。