确定哪些 类 具有注释并获取也具有注释的属性的值
Identify which classes have an annotation and get the value of the properties that have an annotation too
我正在阅读 this doctrine doc 在我的项目中使用自定义注释,但我不太明白该怎么做,我有点笨
我最初的想法是创建一个格式制作工具。就像写下一些文本,然后写下一些用大括号括起来的变量,然后是那些具有实际值的变量,我想让我的系统知道哪些 类 可以用于格式化以及系统可以使用它的哪些属性。
让我们假设,在此图像中,始终检索 ID 为 1 的对象并且该 ID 始终存在(忘记绘制它)
我想要做的是让一个或多个 类 带有注释,然后确定哪些 类 具有该注释并让我知道它们是哪个 类 .在上面的示例中,可用的 类 应该在下拉列表中
/*
* @Format
*/
public class Pizza {
public $hasCheese;
/*
* @FormatField("pizzaPrice")
*/
public $price;
假设我有一堆 类 并且唯一一个有我的注释 Format
并且它的一些属性有注释 FormatField
那么我想要一个让我知道类似 "It looks like the class Pizza can be used in a Format"
的方法,然后哪些属性可用于格式化,那些具有 FormatField
的属性,然后以某种方式通过别名 [=18] 检索 属性 的值=]
我写这种伪代码希望能更好地说明我想学的东西
var availableClasses = getClassesThatHaveFormatAnnotation();
foreach (availableClasses as availableClass) {
var properties = availableClass.findFormatFields()
print("Looks like the " + availableClass->name + " class has the Format annotation")
foreach (properties as property) {
print("Field available: " + property.name)
print("Value: " + property.value)
}
}
var pizza = findPizzaById(1);
有了这个披萨,我希望能够通过其别名仅访问上面带有 @FormatField 的属性,例如 {{pizzaPrice}} 但 {{hasCheese}} 不应该工作,因为它没有 @FormatField 注释;
这是一个查找实体及其注释属性的代码示例。你可以把结果放在某个地方。但是如果不替换带注释的实体,您将可以使用所有属性,因此您可能需要创建一个侦听器,它将侦听 symfony 内核事件并修改您的响应数据。
$annotationReader = new \Doctrine\Common\Annotations\AnnotationReader();
$entities = [];
foreach ($entities as $entity) {
$reflectionClass = new \ReflectionClass($entity);
if (empty($annotationReader->getClassAnnotation($reflectionClass, CustomAnnotation::class))) {
continue;
}
foreach ($reflectionClass->getProperties() as $property) {
if (empty($annotationReader->getPropertyAnnotation($property, CustomPropertyAnnotation::class))) {
continue;
}
$propertyName = $property->getName();
$propertyValue = $property->getValue($entity);
}
}
我正在阅读 this doctrine doc 在我的项目中使用自定义注释,但我不太明白该怎么做,我有点笨
我最初的想法是创建一个格式制作工具。就像写下一些文本,然后写下一些用大括号括起来的变量,然后是那些具有实际值的变量,我想让我的系统知道哪些 类 可以用于格式化以及系统可以使用它的哪些属性。
让我们假设,在此图像中,始终检索 ID 为 1 的对象并且该 ID 始终存在(忘记绘制它)
我想要做的是让一个或多个 类 带有注释,然后确定哪些 类 具有该注释并让我知道它们是哪个 类 .在上面的示例中,可用的 类 应该在下拉列表中
/*
* @Format
*/
public class Pizza {
public $hasCheese;
/*
* @FormatField("pizzaPrice")
*/
public $price;
假设我有一堆 类 并且唯一一个有我的注释 Format
并且它的一些属性有注释 FormatField
那么我想要一个让我知道类似 "It looks like the class Pizza can be used in a Format"
的方法,然后哪些属性可用于格式化,那些具有 FormatField
的属性,然后以某种方式通过别名 [=18] 检索 属性 的值=]
我写这种伪代码希望能更好地说明我想学的东西
var availableClasses = getClassesThatHaveFormatAnnotation();
foreach (availableClasses as availableClass) {
var properties = availableClass.findFormatFields()
print("Looks like the " + availableClass->name + " class has the Format annotation")
foreach (properties as property) {
print("Field available: " + property.name)
print("Value: " + property.value)
}
}
var pizza = findPizzaById(1);
有了这个披萨,我希望能够通过其别名仅访问上面带有 @FormatField 的属性,例如 {{pizzaPrice}} 但 {{hasCheese}} 不应该工作,因为它没有 @FormatField 注释;
这是一个查找实体及其注释属性的代码示例。你可以把结果放在某个地方。但是如果不替换带注释的实体,您将可以使用所有属性,因此您可能需要创建一个侦听器,它将侦听 symfony 内核事件并修改您的响应数据。
$annotationReader = new \Doctrine\Common\Annotations\AnnotationReader();
$entities = [];
foreach ($entities as $entity) {
$reflectionClass = new \ReflectionClass($entity);
if (empty($annotationReader->getClassAnnotation($reflectionClass, CustomAnnotation::class))) {
continue;
}
foreach ($reflectionClass->getProperties() as $property) {
if (empty($annotationReader->getPropertyAnnotation($property, CustomPropertyAnnotation::class))) {
continue;
}
$propertyName = $property->getName();
$propertyValue = $property->getValue($entity);
}
}