Skip to content

Commit

Permalink
[8.x] Add support for disallowing class morphs (#38656)
Browse files Browse the repository at this point in the history
* Add support for disallowing class morphs and thus requiring morph maps

* move method

* formatting - add method

Co-authored-by: Taylor Otwell <taylorotwell@gmail.com>
  • Loading branch information
Oliver Nybroe and taylorotwell authored Sep 3, 2021
1 parent fd21789 commit 4b5060d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ClassMorphViolationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Illuminate\Database;

use RuntimeException;

class ClassMorphViolationException extends RuntimeException
{
/**
* The name of the affected Eloquent model.
*
* @var string
*/
public $model;

/**
* Create a new exception instance.
*
* @param object $model
*/
public function __construct($model)
{
$class = get_class($model);

parent::__construct("No morph map defined for model [{$class}].");

$this->model = $class;
}
}
5 changes: 5 additions & 0 deletions Eloquent/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Database\Eloquent\Concerns;

use Closure;
use Illuminate\Database\ClassMorphViolationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -731,6 +732,10 @@ public function getMorphClass()
return array_search(static::class, $morphMap, true);
}

if (Relation::requiresMorphMap()) {
throw new ClassMorphViolationException($this);
}

return static::class;
}

Expand Down
42 changes: 42 additions & 0 deletions Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ abstract class Relation
*/
public static $morphMap = [];

/**
* Prevents morph relationships without a morph map.
*
* @var bool
*/
protected static $requireMorphMap = false;

/**
* The count of self joins.
*
Expand Down Expand Up @@ -376,6 +383,41 @@ protected function whereInMethod(Model $model, $key)
: 'whereIn';
}

/**
* Prevent polymorphic relationships from being used without model mappings.
*
* @param bool $requireMorphMap
* @return void
*/
public static function requireMorphMap($requireMorphMap = true)
{
static::$requireMorphMap = $requireMorphMap;
}

/**
* Determine if polymorphic relationships require explicit model mapping.
*
* @return bool
*/
public static function requiresMorphMap()
{
return static::$requireMorphMap;
}

/**
* Define the morph map for polymorphic relations and require all morphed models to be explicitly mapped.
*
* @param array|null $map
* @param bool $merge
* @return array
*/
public static function enforceMorphMap(array $map, $merge = true)
{
static::requireMorphMap();

return static::morphMap($map, $merge);
}

/**
* Set or get the morph map for polymorphic relations.
*
Expand Down

0 comments on commit 4b5060d

Please sign in to comment.