Skip to content

Commit

Permalink
HasModels trait
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Feb 20, 2025
1 parent 453e99b commit 9d8eb4a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 53 deletions.
41 changes: 41 additions & 0 deletions src/Cms/HasModels.php
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);
}
}
27 changes: 1 addition & 26 deletions src/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Page extends ModelWithContent
use HasChildren;
use HasFiles;
use HasMethods;
use HasModels;
/**
* @use \Kirby\Cms\HasSiblings<\Kirby\Cms\Pages>
*/
Expand All @@ -49,11 +50,6 @@ class Page extends ModelWithContent
*/
public static array $methods = [];

/**
* Registry with all Page models
*/
public static array $models = [];

/**
* The PageBlueprint object
*/
Expand Down Expand Up @@ -790,27 +786,6 @@ public function mediaUrl(): string
return $this->kirby()->url('media') . '/pages/' . $this->id();
}

/**
* 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);
}

/**
* Returns the last modification date of the page
*/
Expand Down
29 changes: 2 additions & 27 deletions src/Cms/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class User extends ModelWithContent
{
use HasFiles;
use HasMethods;
use HasModels;
/**
* @use \Kirby\Cms\HasSiblings<\Kirby\Cms\Users>
*/
Expand All @@ -44,11 +45,6 @@ class User extends ModelWithContent
*/
public static array $methods = [];

/**
* Registry with all User models
*/
public static array $models = [];

protected UserBlueprint|null $blueprint = null;
protected array $credentials;
protected string|null $email;
Expand Down Expand Up @@ -224,11 +220,7 @@ public function exists(): bool
*/
public static function factory(mixed $props): static
{
if (empty($props['model']) === false) {
return static::model($props['model'], $props);
}

return new static($props);
return static::model($props['model'] ?? $props['role'] ?? 'default', $props);
}

/**
Expand Down Expand Up @@ -453,23 +445,6 @@ public function mediaUrl(): string
return $this->kirby()->url('media') . '/users/' . $this->id();
}

/**
* Creates a user model if it has been registered
* @internal
*/
public static function model(string $name, array $props = []): static
{
if ($class = (static::$models[$name] ?? null)) {
$object = new $class($props);

if ($object instanceof self) {
return $object;
}
}

return new static($props);
}

/**
* Returns the last modification date of the user
*/
Expand Down

0 comments on commit 9d8eb4a

Please sign in to comment.