dunglas/php-property-info

该包已被弃用且不再维护。作者建议使用 symfony/property-info 包。

使用多种来源检索 PHP 属性的类型和描述

v0.2.3 2015-12-29 08:21 UTC

This package is auto-updated.

Last update: 2022-02-01 12:44:39 UTC


README

已弃用:此库已合并到 Symfony 框架中。请使用并贡献到 Symfony PropertyInfo 组件

PHP 不支持显式类型定义。这在进行元编程时尤其令人烦恼。包括但不限于 Doctrine ORM 和 Symfony Validator 等各种库都提供了它们自己的类型管理系统。此库从流行的来源提取各种信息,包括类型和文档

  • 带有类型提示的设置方法
  • PHPDoc DocBlock
  • Doctrine ORM 映射(注解、XML、YML 或自定义格式)
  • PHP 7 标量类型提示和返回类型
  • Hack(hacklang)类型

Build Status SensioLabsInsight

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 创建。