本文整理汇总了PHP中ReflectionClass::getTraits方法的典型用法代码示例。如果您正苦于以下问题:PHP ReflectionClass::getTraits方法的具体用法?PHP ReflectionClass::getTraits怎么用?PHP ReflectionClass::getTraits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReflectionClass
的用法示例。
在下文中一共展示了ReflectionClass::getTraits方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getTraits
/**
* Returns an array of traits used by this class
* @return array with trait names in keys and instances of trait's ReflectionClass in values. Returns NULL in case of an error.
*/
public function getTraits()
{
$traits = array();
if (method_exists('\\ReflectionClass', 'getTraits')) {
$traits = parent::getTraits();
}
return $traits;
}
示例2: __construct
/**
* Create a new Eloquent model instance, ensuring that any auditing
* columns are guarded.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = array())
{
// if we're using the auditing trait, guard the audit columns
$reflection = new \ReflectionClass($this);
if (array_key_exists('BishopB\\Forum\\AuditingTrait', $reflection->getTraits())) {
$this->guarded = $this->guarded + $this->getAuditors();
}
parent::__construct($attributes);
}
示例3: testDoubleOfInterface
public function testDoubleOfInterface()
{
$obj_creator = new FBMock_TestDoubleCreator();
$obj = $obj_creator->createTestDoubleFor('TestInterfaceA', array('TestInterfaceB'), array('TestTrait1', 'TestTrait2'));
$this->assertInstanceof('TestInterfaceA', $obj);
$this->assertInstanceof('TestInterfaceB', $obj);
$ref_class = new ReflectionClass($obj);
self::assertEquals(array('TestTrait1', 'TestTrait2', 'FBMock_TestDoubleObject'), array_keys($ref_class->getTraits()));
}
示例4: inspectTrait
/**
* Inspect trait.
*
* @return void
*/
protected function inspectTrait()
{
if (PHP_VERSION < '5.4') {
return;
}
$traits = $this->class->getTraits();
if (!empty($traits)) {
foreach ($traits as $trait) {
$this->inspection['use'][] = $this->getInheritanceInspection($trait);
}
}
}
示例5: getRecursiveTraits
/**
* Gets a recursive list of traits used by a class
*
* @param string $class Full class name
*
* @return array
*/
private function getRecursiveTraits($class = null)
{
if (null == $class) {
$class = get_class($this);
}
$reflection = new \ReflectionClass($class);
$traits = array_keys($reflection->getTraits());
foreach ($traits as $trait) {
$traits = array_merge($traits, $this->getRecursiveTraits($trait));
}
if ($parent = $reflection->getParentClass()) {
$traits = array_merge($traits, $this->getRecursiveTraits($parent->getName()));
}
return $traits;
}
示例6: index
function index()
{
$model = base64_decode(\Route::input('model'));
dump($model);
$r = new \ReflectionClass($model);
dd($r->getTraits());
$methods = [];
foreach ($r->getMethods() as $method) {
if ($method->class == $model) {
$methods[] = $this->sourceMethod($method);
}
}
dump($methods);
dump($methods = $r->getMethod('messageNotFound'));
dump($methods = $r->getMethod('messageNotFound')->getDocComment());
dump($this->sourceMethod($r->getMethod('messageNotFound')));
dump($methods = $r->getMethod('messageNotFound')->class);
// $models = rglob('*.php', 0, base_path('vendor/*/*/*/*/Models/'));
// dd(111, $models);
return $this->response(['rows' => CrudRow::$rows]);
}
示例7: boot
public static function boot()
{
$elasticClient = ClientBuilder::create()->build();
$updateIndex = function (ElasticModel $modelInstance) use($elasticClient) {
$params = ['index' => config('search.index'), 'type' => $modelInstance->getElasticType(), 'id' => $modelInstance->getKey(), 'body' => $modelInstance->toElasticArray()];
$elasticClient->index($params);
return true;
};
$deleteIndex = function (ElasticModel $modelInstance) use($elasticClient) {
$reflectionClass = new \ReflectionClass($modelInstance);
$traits = $reflectionClass->getTraits();
if (in_array(SoftDeletes::class, array_keys($traits)) && $modelInstance->trashed()) {
return true;
}
$params = ['index' => config('search.index'), 'type' => $modelInstance->getElasticType(), 'id' => $modelInstance->getKey()];
$elasticClient->delete($params);
return true;
};
static::created($updateIndex);
static::updated($updateIndex);
static::deleted($deleteIndex);
}
示例8: findSummary
protected function findSummary(\ReflectionClass $scopeClass, \ReflectionMethod $scopeMethod, Language $lang = null, &$alreadyScopedClasses = array())
{
foreach ($alreadyScopedClasses as $sc) {
/* @var \ReflectionClass $sc */
if ($sc->getName() === $scopeClass->getName()) {
return null;
}
}
$alreadyScopedClasses[] = $scopeClass;
$doc = new DocBlock($scopeMethod);
$result = $doc->getShortDescription();
if ('{@inheritdoc}' === \trim(\strtolower($result))) {
$result = null;
$typesToCheck = \array_merge($scopeClass->getTraits(), [$scopeClass->getParentClass()], $scopeClass->getInterfaces());
foreach ($typesToCheck as $type) {
if (!$type instanceof \ReflectionClass) {
continue;
}
$matchingMethod = null;
foreach ($type->getMethods() as $otherMethod) {
if ($otherMethod->getName() !== $scopeMethod->getName()) {
continue;
}
$matchingMethod = $otherMethod;
}
if (!$matchingMethod instanceof \ReflectionMethod) {
continue;
}
$summary = \trim($this->findSummary($type, $matchingMethod, $lang, $alreadyScopedClasses));
if ('' !== $summary) {
$result = $summary;
}
}
}
$result = \trim($result);
return '' !== $result ? $result : null;
}
示例9: get_class
/**
* クラスのアノテーションを取得する
* @param string $class 対象のクラス名
* @param string[] $names デコード対象のアノテーション名
* @param string $doc_name 説明を取得する場合の添字
* @param string $parent 遡る最上のクラス名
*/
public static function get_class($class, $names, $doc_name = null, $parent = null)
{
$return = [];
$t = new \ReflectionClass($class);
$d = null;
if (empty($parent)) {
$parent = 'stdClass';
}
while ($t->getName() != $parent) {
$d = $t->getDocComment() . $d;
foreach ($t->getTraits() as $trats) {
$d = $trats->getDocComment() . $d;
}
$t = $t->getParentClass();
if ($t === false) {
break;
}
}
$d = preg_replace("/^[\\s]*\\*[\\s]{0,1}/m", '', str_replace(['/' . '**', '*' . '/'], '', $d));
foreach (is_array($names) ? $names : [$names] as $name) {
$return[$name] = self::decode($d, $name, $doc_name);
}
return is_array($names) ? $return : $return[$names];
}
示例10: getTraits
/**
* Returns traits used by this class.
*
* @return array
*/
public function getTraits()
{
return NATIVE_TRAITS ? parent::getTraits() : array();
}
示例11: loadBusinessProperties
/**
* load business properties from ReflectionClass.
*
* @return array
**/
protected function loadBusinessProperties(\ReflectionClass $class)
{
$businessProperties = [];
$properties = $class->getProperties();
$traits = $class->getTraits();
$className = $class->getName();
// if the class is translatable, then parse annotations on it's translation class
if (array_key_exists(Translatable::class, $traits)) {
$translation = new \ReflectionClass($className::getTranslationEntityClass());
$translationProperties = $translation->getProperties();
$properties = array_merge($properties, $translationProperties);
}
foreach ($properties as $property) {
$annotations = $this->reader->getPropertyAnnotations($property);
foreach ($annotations as $key => $annotationObj) {
if ($annotationObj instanceof BusinessProperty && !in_array($class, $businessProperties)) {
if (!$annotations[$key]->getTypes()) {
$message = $class->name . ':$' . $property->name . '" field';
throw AnnotationException::requiredError('type', 'BusinessProperty annotation', $message, 'array or string');
}
foreach ($annotations[$key]->getTypes() as $type) {
$businessProperties[$type][] = $property->name;
}
}
}
}
// we load business properties of parents recursively
// because they are defined by an annotation not by the property type(private, protected, public)
$parentClass = $class->getParentClass();
if ($parentClass) {
//load parent properties recursively
$parentProperties = $this->loadBusinessProperties(new \ReflectionClass($parentClass->getName()));
foreach ($parentProperties as $key => $parentProperty) {
if (array_key_exists($key, $businessProperties)) {
//if parent and current have a same business property type we merge the properties and remove
//duplicates if properties are the same;
$businessProperties[$key] = array_unique(array_merge($parentProperty, $businessProperties[$key]));
} else {
//else we had a business property type for the parent properties
$businessProperties[$key] = $parentProperty;
}
}
}
return $businessProperties;
}
示例12: run
trait Trait1
{
public function run()
{
}
public function say()
{
}
}
trait Trait2
{
public function run()
{
}
public function say()
{
}
}
class MyClass
{
use Trait1, Trait2 {
Trait1::run as execute;
Trait1::say insteadof Trait2;
Trait2::run insteadof Trait1;
Trait2::say as talk;
}
}
$ref = new ReflectionClass('MyClass');
print_r($ref->getTraitAliases());
print_r($ref->getTraits());
示例13: testAllEntityFieldsRegistered
/**
* Tests if all entity fields are registered.
*/
public function testAllEntityFieldsRegistered()
{
$reflect = new \ReflectionClass($this->getClassName());
$properties = $reflect->getProperties();
$fields = [];
/** @var \ReflectionProperty $property */
foreach ($properties as $property) {
$fields[] = $property->getName();
}
$parentClass = $reflect->getParentClass();
if ($parentClass) {
$parentClassProperties = $parentClass->getProperties();
/** @var \ReflectionProperty $property */
foreach ($parentClassProperties as $property) {
$this->addIgnoredFields($property->getName());
}
}
$traits = $reflect->getTraits();
if ($traits) {
foreach ($traits as $trait) {
$traitProperties = $trait->getProperties();
/** @var \ReflectionProperty $property */
foreach ($traitProperties as $property) {
$this->addIgnoredFields($property->getName());
}
}
}
$registeredFields = [];
foreach ($this->getFieldsData() as $data) {
$registeredFields[] = $data[0];
}
$diff = array_diff($fields, $registeredFields, $this->getIgnoredFields());
if (count($diff) !== 0) {
$this->fail(sprintf('All entity fields must be registered in test. Please check field(s) "%s".', implode('", "', $diff)));
}
}
示例14: hasLinkTrait
/**
* Check if reflection class has LinkTrait.
*
* @param \ReflectionClass $reflect
*
* @return bool
*/
private function hasLinkTrait(\ReflectionClass $reflect)
{
$linkTraitName = 'Victoire\\Bundle\\WidgetBundle\\Entity\\Traits\\LinkTrait';
$traits = $reflect->getTraits();
foreach ($traits as $trait) {
if ($trait->getName() == $linkTraitName) {
return true;
}
}
if ($parentClass = $reflect->getParentClass()) {
if ($this->hasLinkTrait($parentClass)) {
return true;
}
}
return false;
}
示例15: getClassDependendFiles
/**
* Retrieve the classes and defining files the given class depends on (including the given class)
*
* @param ReflectionClass The class to get the dependend classes for.
* @param callable A callback function which takes a file name as argument
* and returns whether the file is blacklisted.
*
* @return string[] An array containing class names as keys and path to the
* file's defining class as value.
*
* @author Dominik del Bondio <dominik.del.bondio@bitextender.com>
* @since 1.1.0
*/
private function getClassDependendFiles(ReflectionClass $reflectionClass, $isBlacklisted)
{
$requires = array();
while ($reflectionClass) {
$file = $reflectionClass->getFileName();
// we don't care for duplicates since we're using require_once anyways
if (!$isBlacklisted($file) && is_file($file)) {
$requires[$reflectionClass->getName()] = $file;
}
foreach ($reflectionClass->getInterfaces() as $interface) {
$file = $interface->getFileName();
$requires = array_merge($requires, $this->getClassDependendFiles($interface, $isBlacklisted));
}
if (is_callable(array($reflectionClass, 'getTraits'))) {
// FIXME: remove check after bumping php requirement to 5.4
foreach ($reflectionClass->getTraits() as $trait) {
$file = $trait->getFileName();
$requires = array_merge($requires, $this->getClassDependendFiles($trait, $isBlacklisted));
}
}
$reflectionClass = $reflectionClass->getParentClass();
}
return $requires;
}