-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
453e99b
commit 9d8eb4a
Showing
3 changed files
with
44 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Kirby\Cms; | ||
|
||
/** | ||
* HasModels | ||
* | ||
* @package Kirby Cms | ||
* @author Bastian Allgeier <[email protected]> | ||
* @link https://getkirby.com | ||
* @copyright Bastian Allgeier | ||
* @license https://getkirby.com/license | ||
*/ | ||
trait HasModels | ||
{ | ||
/** | ||
* Registry with all custom models | ||
*/ | ||
public static array $models = []; | ||
|
||
/** | ||
* Creates a page model if it has been registered | ||
* @internal | ||
*/ | ||
public static function model(string $name, array $props = []): static | ||
{ | ||
$name = strtolower($name); | ||
$class = static::$models[$name] ?? null; | ||
$class ??= static::$models['default'] ?? null; | ||
|
||
if ($class !== null) { | ||
$object = new $class($props); | ||
|
||
if ($object instanceof self) { | ||
return $object; | ||
} | ||
} | ||
|
||
return new static($props); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters