Medianet-Tunisia /
laravel-auth-api
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace MedianetDev\LaravelAuthApi\Models\Traits; |
||||
| 4 | |||||
| 5 | use Illuminate\Support\Str; |
||||
| 6 | use ReflectionClass; |
||||
| 7 | |||||
| 8 | /** |
||||
| 9 | * This trait helps a child model (ex: BackpackUser) inherit all relationships of its parent model (ex: User). |
||||
| 10 | * Laravel by default doesn't do that, so packages like Backpack\PermissionManager can't see relationships |
||||
| 11 | * on the BackpackUser model, because they haven't been inherited from User. |
||||
| 12 | * |
||||
| 13 | * The code below has been copy-pasted from https://github.com/tightenco/parental on Jul 19th 2019. |
||||
| 14 | */ |
||||
| 15 | trait InheritsRelationsFromParentModel |
||||
| 16 | { |
||||
| 17 | public $hasParent = true; |
||||
| 18 | |||||
| 19 | public static function bootHasParent() |
||||
| 20 | { |
||||
| 21 | static::creating(function ($model) { |
||||
| 22 | if ($model->parentHasHasChildrenTrait()) { |
||||
| 23 | $model->forceFill( |
||||
| 24 | [$model->getInheritanceColumn() => $model->classToAlias(get_class($model))] |
||||
| 25 | ); |
||||
| 26 | } |
||||
| 27 | }); |
||||
| 28 | static::addGlobalScope(function ($query) { |
||||
| 29 | $instance = new static(); |
||||
| 30 | if ($instance->parentHasHasChildrenTrait()) { |
||||
| 31 | $query->where($instance->getTable().'.'.$instance->getInheritanceColumn(), $instance->classToAlias(get_class($instance))); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It seems like
getInheritanceColumn() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 32 | } |
||||
| 33 | }); |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | public function parentHasHasChildrenTrait() |
||||
| 37 | { |
||||
| 38 | return $this->hasChildren ?? false; |
||||
| 39 | } |
||||
| 40 | |||||
| 41 | 5 | public function getTable() |
|||
| 42 | { |
||||
| 43 | 5 | if (! isset($this->table)) { |
|||
| 44 | return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getParentClass())))); |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | 5 | return $this->table; |
|||
| 48 | } |
||||
| 49 | |||||
| 50 | 2 | public function getForeignKey() |
|||
| 51 | { |
||||
| 52 | 2 | return Str::snake(class_basename($this->getParentClass())).'_'.$this->primaryKey; |
|||
| 53 | } |
||||
| 54 | |||||
| 55 | public function joiningTable($related, $instance = null) |
||||
|
0 ignored issues
–
show
The parameter
$instance is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 56 | { |
||||
| 57 | $relatedClassName = method_exists((new $related()), 'getClassNameForRelationships') |
||||
| 58 | ? (new $related())->getClassNameForRelationships() |
||||
| 59 | : class_basename($related); |
||||
| 60 | $models = [ |
||||
| 61 | Str::snake($relatedClassName), |
||||
| 62 | Str::snake($this->getClassNameForRelationships()), |
||||
| 63 | ]; |
||||
| 64 | sort($models); |
||||
| 65 | |||||
| 66 | return strtolower(implode('_', $models)); |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | public function getClassNameForRelationships() |
||||
| 70 | { |
||||
| 71 | return class_basename($this->getParentClass()); |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | public function getMorphClass() |
||||
| 75 | { |
||||
| 76 | if ($this->parentHasHasChildrenTrait()) { |
||||
| 77 | $parentClass = $this->getParentClass(); |
||||
| 78 | |||||
| 79 | return (new $parentClass())->getMorphClass(); |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | return parent::getMorphClass(); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | 2 | protected function getParentClass() |
|||
| 86 | { |
||||
| 87 | 2 | static $parentClassName; |
|||
| 88 | |||||
| 89 | 2 | return $parentClassName ?: $parentClassName = (new ReflectionClass($this))->getParentClass()->getName(); |
|||
| 90 | } |
||||
| 91 | } |
||||
| 92 |