sprocketbox /
eloquence
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php /** @noinspection PhpUndefinedMethodInspection */ |
||
| 2 | |||
| 3 | namespace Sprocketbox\Eloquence\Concerns; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Model; |
||
| 6 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
| 7 | use Illuminate\Database\Eloquent\Relations\Relation; |
||
| 8 | use Illuminate\Support\Facades\Date; |
||
| 9 | use Laravel\Nova\Fields\BelongsToMany; |
||
| 10 | use LogicException; |
||
| 11 | use Sprocketbox\Eloquence\Facades\Eloquence; |
||
| 12 | use Sprocketbox\Eloquence\ModelIdentity; |
||
| 13 | use Sprocketbox\Eloquence\Query\Builder; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * MapsIdentity |
||
| 17 | * |
||
| 18 | * This trait provides identity map functionality for Eloquent models. |
||
| 19 | * |
||
| 20 | * @package Sprocketbox\Eloquence\Concerns |
||
| 21 | */ |
||
| 22 | trait MapsIdentity |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Boots the trait. |
||
| 26 | */ |
||
| 27 | public static function bootMapsIdentity(): void |
||
| 28 | { |
||
| 29 | // Add a deleted event so the identity is removed from the map |
||
| 30 | static::deleted(fn(Model $model) => Eloquence::removeIdentity($model->getModelIdentity())); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 31 | // Add a created event so newly created models are stored |
||
| 32 | static::created(fn(Model $model) => Eloquence::storeIdentity($model->getModelIdentity(), $model)); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Override the default newFromBuilder method to use the identity map. |
||
| 37 | * |
||
| 38 | * @see \Illuminate\Database\Eloquent\Model::newFromBuilder() |
||
| 39 | * |
||
| 40 | * @param array $attributes |
||
| 41 | * @param null $connection |
||
| 42 | * |
||
| 43 | * @return \Illuminate\Database\Eloquent\Model |
||
| 44 | * @noinspection PhpIncompatibleReturnTypeInspection |
||
| 45 | */ |
||
| 46 | public function newFromBuilder($attributes = [], $connection = null): Model |
||
| 47 | { |
||
| 48 | $attributes = (array) $attributes; |
||
| 49 | $key = $attributes[$this->getKeyName()] ?? null; |
||
| 50 | $identity = $model = null; |
||
| 51 | |||
| 52 | if ($key !== null) { |
||
| 53 | $identity = $this->getModelIdentity($key, $connection); |
||
| 54 | |||
| 55 | if (Eloquence::hasIdentity($identity)) { |
||
| 56 | $model = Eloquence::getIdentity($identity); |
||
| 57 | /** @noinspection NullPointerExceptionInspection */ |
||
| 58 | $this->updateModelAttributes($model, $attributes); |
||
| 59 | |||
| 60 | return $model; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | $model = parent::newFromBuilder($attributes, $connection); |
||
| 65 | |||
| 66 | if ($identity !== null) { |
||
| 67 | Eloquence::storeIdentity($model->getModelIdentity(), $model); |
||
| 68 | } |
||
| 69 | |||
| 70 | return $model; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the model identity. |
||
| 75 | * |
||
| 76 | * @param null $id |
||
| 77 | * @param string|null $connection |
||
| 78 | * |
||
| 79 | * @return \Sprocketbox\Eloquence\ModelIdentity |
||
| 80 | */ |
||
| 81 | public function getModelIdentity($id = null, ?string $connection = null): ModelIdentity |
||
| 82 | { |
||
| 83 | $connection = $connection ?? $this->getConnectionName() ?? static::getConnectionResolver()->getDefaultConnection(); |
||
| 84 | |||
| 85 | return new ModelIdentity(static::class, $id ?? $this->getKey(), $connection); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Create a new Eloquent query builder for the model. |
||
| 90 | * |
||
| 91 | * @param \Illuminate\Database\Query\Builder $query |
||
| 92 | * |
||
| 93 | * @return \Illuminate\Database\Eloquent\Builder|\Sprocketbox\Eloquence\Query\Builder|static |
||
| 94 | */ |
||
| 95 | public function newEloquentBuilder($query) |
||
| 96 | { |
||
| 97 | return new Builder($query); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Change the original attributes to match the new attributes, and re-add the dirty records. |
||
| 102 | * |
||
| 103 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 104 | * @param array $attributes |
||
| 105 | */ |
||
| 106 | protected function updateModelAttributes(Model $model, array $attributes = []): void |
||
| 107 | { |
||
| 108 | if (! $this->areAttributesMoreRecent($model, $attributes)) { |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | |||
| 112 | $dirtyAttributes = $model->getDirty(); |
||
| 113 | $model->setRawAttributes($attributes, true); |
||
| 114 | $model->setRawAttributes(array_merge($model->getAttributes(), $dirtyAttributes), false); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Check if the provided attributes are newer. |
||
| 119 | * |
||
| 120 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 121 | * @param array $attributes |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | protected function areAttributesMoreRecent(Model $model, array $attributes): bool |
||
| 126 | { |
||
| 127 | if (! $this->usesTimestamps()) { |
||
| 128 | return true; |
||
| 129 | } |
||
| 130 | |||
| 131 | $updatedAt = $attributes[$this->getUpdatedAtColumn()]; |
||
| 132 | |||
| 133 | if ($updatedAt !== null) { |
||
| 134 | $format = $this->getDateFormat(); |
||
| 135 | |||
| 136 | if (is_numeric($updatedAt)) { |
||
| 137 | $updatedAt = Date::createFromTimestamp($updatedAt); |
||
| 138 | } else if (Date::hasFormat($updatedAt, $format)) { |
||
| 139 | $updatedAt = Date::createFromFormat($format, $updatedAt); |
||
| 140 | } |
||
| 141 | |||
| 142 | return $model->getAttribute($this->getUpdatedAtColumn())->isBefore($updatedAt); |
||
| 143 | } |
||
| 144 | |||
| 145 | return true; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get a relationship value from a method. |
||
| 150 | * |
||
| 151 | * @param string $method |
||
| 152 | * |
||
| 153 | * @return mixed |
||
| 154 | * |
||
| 155 | * @throws \LogicException |
||
| 156 | */ |
||
| 157 | protected function getRelationshipFromMethod($method) |
||
| 158 | { |
||
| 159 | $relation = $this->$method(); |
||
| 160 | |||
| 161 | if (! $relation instanceof Relation) { |
||
| 162 | if (is_null($relation)) { |
||
| 163 | throw new LogicException(sprintf( |
||
| 164 | '%s::%s must return a relationship instance, but "null" was returned. Was the "return" keyword used?', static::class, $method |
||
| 165 | )); |
||
| 166 | } |
||
| 167 | |||
| 168 | throw new LogicException(sprintf( |
||
| 169 | '%s::%s must return a relationship instance.', static::class, $method |
||
| 170 | )); |
||
| 171 | } |
||
| 172 | |||
| 173 | return tap($this->getRelationshipResults($relation), function ($results) use ($method) { |
||
| 174 | $this->setRelation($method, $results); |
||
| 175 | }); |
||
| 176 | } |
||
| 177 | |||
| 178 | protected function getRelationshipResults(Relation $relation) |
||
| 179 | { |
||
| 180 | if ($relation instanceof BelongsToMany) { |
||
| 181 | return $relation->getResults(); |
||
| 182 | } |
||
| 183 | |||
| 184 | if ($relation instanceof BelongsTo) { |
||
| 185 | $related = $relation->getRelated(); |
||
| 186 | |||
| 187 | if (method_exists($related, 'getModelIdentity')) { |
||
| 188 | $identity = $related->getModelIdentity($this->getAttribute($relation->getForeignKeyName())); |
||
| 189 | |||
| 190 | if (Eloquence::hasIdentity($identity)) { |
||
| 191 | return Eloquence::getIdentity($identity); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | return $relation->getResults(); |
||
| 197 | } |
||
| 198 | } |