cybex/laravel-reflector
最新稳定版本:v1.1.1
Composer 安装命令:
composer require cybex/laravel-reflector
包简介
Provides structural information about data Models.
README 文档
README
This package allows you to get structural information for data models.
Requirements
- Illuminate/support: ^8.0
- PHP: ^8.0
Installation
You can install the package via composer:
composer require cybex/laravel-reflector
Usage
getModelRelations()
The getModelRelations() method returns a Collection of all relations of a Model, with additional information like the name of the relation, relation type, related class and
an empty base instance of the given Model from Eloquent.
ModelReflector::getModelRelations(User::class);
// Returns
Illuminate\Support\Collection {#5236
all: [
"Images" => [
"relation" => "Images",
"returnType" => "Illuminate\Database\Eloquent\Relations\HasMany",
"relatedClass" => "App\Models\Image",
"relatedModel" => App\Models\Image {#5237},
"relatedTable" => "images",
"foreignKeyName" => "id",
"qualifiedForeignKeyName" => "images.id",
"isRelationParent" => false,
]
]
}
getRelationByTarget()
The getRelationByTarget() method returns the name of a Relation between the Model and the Target.
$user = new User;
$image = new Image;
ModelReflector::getRelationByTarget($user, $image);
// Returns
'Images'
hasRelation()
The hasRelation() method returns true if a Model has a specific relation.
ModelReflector::hasRelation(User::class, 'Images');
// Returns
true
getMethodReturnType()
The getMethodReturnType() method returns the type of a specific method on a given Object or Model class. It returns false when the Method does not exist. It returns null if
no return type is type hinted.
$user = new User;
ModelReflector::getMethodReturnType($user, 'Images');
// Returns
'Illuminate\Database\Eloquent\Relations\HasMany'
getModelInstance()
The getModelInstance() method checks if the given model is an instance of a Model or the fully qualified class name of a Model, and returns the model or the empty Eloquent
base Model of the given class.
ModelReflector::getModelInstance('App\Models\User');
// Returns an empty object of the User class
getModelClass()
The getModelClass() method checks if the given model is an instance of a Model or the fully qualified class name of a Model, and returns the class of the given Model.
$user = new User;
ModelReflector::getModelClass($user);
// Returns
'App\Models\User'
resolveModelObject()
The resolveModelObject() method resolves a Model based on a given Model or a Class and the according identifier. If no identifier is given, it returns an empty Builder-Model.
If the desired Model can not be found, it will return null.
ModelReflector::resolveModelObject(Image::class, 10);
// Returns the Image object with the key 10
resolveRelatedModel()
The resolveRelatedModel() method resolves a related Model by the source and the given Relation. Currently, we only support HasOne or BelongsTo-Relations, as those only return
a single Model or null.
ModelReflector::resolveRelatedModel($image, 'User');
// Returns the User object that the Image object belongs to
resolveRelatedModelByTarget()
The resolveRelatedModelByTarget() method resolves a related Model by the source and the given TargetModel.
ModelReflector::resolveRelatedModelByTarget($image, User::class);
// Returns the User object that the Image object belongs to
getModelShortName()
The getModelShortName() method returns the Short-Name of a Model.
$image = new Image;
ModelReflector::getModelShortName($image);
// Returns
'Image'
getAllModels()
The getAllModels() method returns a Collection of all available Models via the Filesystem.
ModelReflector::getAllModels();
// Returns
Illuminate\Support\Collection {#384
all: [
"App\Models\User\Image",
"App\Models\User",
],
}
getAllInstantiatableModels()
The getAllInstantiatableModels() method returns a Collection of all instantiatable Model-Classes, which are not Abstract. It returns the full qualified Class-Name as key with
the according Short-Name as value.
ModelReflector::getAllInstantiatableModels();
// Returns all kinds of empty objects
Illuminate\Support\Collection {#4929
all: [
"App\Models\User\Image" => App\Models\User\Image {#4928},
"App\Models\User" => App\Models\User {#4921},
],
}
getInstantiatableModelStructureInformation()
The getInstantiatableModelStructureInformation() method returns a Collection of structure information for all instantiatable Model-Classes, which include the fully qualified
name of the parent class and the child classes.
ModelReflector::getInstantiatableModelStructureInformation();
// Returns
Illuminate\Support\Collection {#1469
all: [
"App\Models\User" => Illuminate\Support\Collection {#1482
all: [
"parentClass" => null,
"childClasses" => [
"App\Models\User\Image",
],
],
},
"App\Models\User\Image" => Illuminate\Support\Collection {#1483
all: [
"parentClass" => "App\Models\User",
"childClasses" => [],
],
}
],
}
getClassFromMorphMap()
The getClassFromMorphMap() method returns the class name from the Morph-Map alias (reverse lookup), the alias or null (if strict is true).
ModelReflector::getClassFromMorphMap('user');
// Returns
'App\Models\User'
getMorphAliasForClass()
The getMorphAliasForClass() method returns the morph alias for the specified Model.
ModelReflector::getMorphAliasForClass('App\Models\User');
// Returns
'user'
modelHasTraits()
The modelHasTraits() method validates if a Model implements one or more specific Traits.
ModelReflector::modelHasTraits(User::class, 'Illuminate\Database\Eloquent\Concerns\HasAttributes');
// Returns
true
cybex/laravel-reflector 适用场景与选型建议
cybex/laravel-reflector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 330 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「reflection」 「laravel」 「Reflector」 「cybex」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cybex/laravel-reflector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cybex/laravel-reflector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cybex/laravel-reflector 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Library emulating the PHP internal reflection using just the tokenized source code.
Laravel package for inspecting model classes on relations en attributes.
Reflective is a formally defined reflection mechanism in PHP, which is used to query detailed information about classes, methods, properties, functions, etc.
Parse use statements for a reflection object
Generate UML class diagrams by reflection for your PHP projects
Powerful class discovery system for Laravel with attribute-based scanning, directory traversal, and monorepo support
统计信息
- 总下载量: 330
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-01