Skip to content

Commit e2517c2

Browse files
authored
[6.x] Alias models instead of mapping them in legacy services (#18171)
* Alias models instead of mapping them in legacy services * Remove legacy test
1 parent b7f9af7 commit e2517c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+294
-2659
lines changed

src/Component/Concerns/ConfigurableComponent.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
use craft\helpers\DateTimeHelper;
99
use CraftCms\Cms\Component\Events\DefineSettingsAttributes;
1010
use CraftCms\Cms\Support\Html;
11+
use CraftCms\Cms\Support\Str;
1112
use CraftCms\Cms\Support\Utils;
1213
use DateTimeInterface;
1314
use Illuminate\Support\Facades\Event;
1415
use ReflectionProperty;
16+
use RuntimeException;
1517

1618
trait ConfigurableComponent
1719
{
@@ -24,7 +26,7 @@ trait ConfigurableComponent
2426

2527
public function settingsAttributes(): array
2628
{
27-
$attributes = Utils::getPublicProperties($this, fn (ReflectionProperty $property) => $property->class === static::class);
29+
$attributes = array_keys(Utils::getPublicProperties($this, fn (ReflectionProperty $property) => $property->class === static::class));
2830

2931
if ($this->hasComponentListeners(self::EVENT_DEFINE_SETTINGS_ATTRIBUTES)) {
3032
$this->dispatchComponentEvent(self::EVENT_DEFINE_SETTINGS_ATTRIBUTES, $event = new DefineSettingsAttributes(
@@ -47,7 +49,17 @@ public function getSettings(): array
4749
{
4850
$settings = [];
4951

50-
foreach ($this->settingsAttributes() as $attribute => $value) {
52+
foreach ($this->settingsAttributes() as $attribute) {
53+
try {
54+
$value = match (true) {
55+
property_exists($this, $attribute) => $this->$attribute,
56+
method_exists($this, $method = 'get'.Str::studly($attribute)) => $this->$method(),
57+
default => throw new RuntimeException,
58+
};
59+
} catch (RuntimeException) {
60+
continue;
61+
}
62+
5163
$value = match (true) {
5264
$value instanceof DateTimeInterface => DateTimeHelper::toIso8601($value) ?: null,
5365
$value instanceof BackedEnum => $value->value,

src/Component/Contracts/ConfigurableComponentInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface ConfigurableComponentInterface
1212
* By default, this method returns all public non-static properties that were defined on the called class.
1313
* You may override this method to change the default behavior.
1414
*
15-
* @return array<string, mixed> The list of settings attribute names and values
15+
* @return string[] The list of settings attribute names and values
1616
*
1717
* @see getSettings()
1818
*/

src/Database/Queries/Concerns/Entry/QueriesAuthors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace CraftCms\Cms\Database\Queries\Concerns\Entry;
66

7-
use craft\models\UserGroup;
87
use CraftCms\Cms\Database\Queries\EntryQuery;
98
use CraftCms\Cms\Database\Table;
109
use CraftCms\Cms\Edition;
10+
use CraftCms\Cms\User\Data\UserGroup;
1111
use Illuminate\Support\Facades\DB;
1212
use Tpetry\QueryExpressions\Language\Alias;
1313

src/Entry/Data/EntryType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public function findUsages(): array
381381
$fields = Fields::getFieldsByType($type);
382382
foreach ($fields as $field) {
383383
foreach ($field->getFieldLayoutProviders() as $provider) {
384-
if ($provider instanceof \craft\models\EntryType && $provider->id === $this->id) {
384+
if ($provider instanceof self && $provider->id === $this->id) {
385385
$usages[] = $field;
386386
}
387387
}

src/Field/BaseOptionsField.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Illuminate\Support\Collection;
2828
use Illuminate\Support\Facades\Validator as ValidatorFacade;
2929
use Illuminate\Validation\Validator;
30+
use Override;
3031
use yii\db\Schema;
3132

3233
use function CraftCms\Cms\t;
@@ -69,7 +70,7 @@ abstract class BaseOptionsField extends Field implements CrossSiteCopyableFieldI
6970
/**
7071
* {@inheritdoc}
7172
*/
72-
#[\Override]
73+
#[Override]
7374
public static function phpType(): string
7475
{
7576
return sprintf('\\%s', static::$multi ? MultiOptionsFieldData::class : SingleOptionFieldData::class);
@@ -78,7 +79,7 @@ public static function phpType(): string
7879
/**
7980
* {@inheritdoc}
8081
*/
81-
#[\Override]
82+
#[Override]
8283
public static function dbType(): string
8384
{
8485
return static::$multi ? Schema::TYPE_JSON : Schema::TYPE_STRING;
@@ -87,7 +88,7 @@ public static function dbType(): string
8788
/**
8889
* {@inheritdoc}
8990
*/
90-
#[\Override]
91+
#[Override]
9192
public static function modifyQuery(Builder $query, array $instances, mixed $value): Builder
9293
{
9394
if (! static::$multi) {
@@ -180,13 +181,13 @@ public function __construct($config = [])
180181
public function settingsAttributes(): array
181182
{
182183
$attributes = parent::settingsAttributes();
183-
$attributes['options'] = $this->options;
184-
$attributes['customOptions'] = $this->customOptions;
184+
$attributes[] = 'options';
185+
$attributes[] = 'customOptions';
185186

186187
return $attributes;
187188
}
188189

189-
#[\Override]
190+
#[Override]
190191
public static function getRules(): array
191192
{
192193
return array_merge(parent::getRules(), [
@@ -351,7 +352,7 @@ public function getSettingsHtml(): string
351352
/**
352353
* {@inheritdoc}
353354
*/
354-
#[\Override]
355+
#[Override]
355356
public function normalizeValue(mixed $value, ?ElementInterface $element): mixed
356357
{
357358
if ($value instanceof MultiOptionsFieldData || $value instanceof SingleOptionFieldData) {
@@ -441,7 +442,7 @@ protected function isOptionSelected(array $option, mixed $value, array &$selecte
441442
/**
442443
* {@inheritdoc}
443444
*/
444-
#[\Override]
445+
#[Override]
445446
public function serializeValue(mixed $value, ?ElementInterface $element): mixed
446447
{
447448
if ($value instanceof MultiOptionsFieldData) {
@@ -471,7 +472,7 @@ public function serializeValue(mixed $value, ?ElementInterface $element): mixed
471472
/**
472473
* {@inheritdoc}
473474
*/
474-
#[\Override]
475+
#[Override]
475476
protected function searchKeywords(mixed $value, ElementInterface $element): string
476477
{
477478
$keywords = [];
@@ -504,7 +505,7 @@ public function getElementConditionRuleType(): array|string
504505
/**
505506
* {@inheritdoc}
506507
*/
507-
#[\Override]
508+
#[Override]
508509
public function getElementValidationRules(): array
509510
{
510511
return [
@@ -526,7 +527,7 @@ function (ElementInterface $element) {
526527
/**
527528
* {@inheritdoc}
528529
*/
529-
#[\Override]
530+
#[Override]
530531
public function isValueEmpty(mixed $value, ElementInterface $element): bool
531532
{
532533
if ($value instanceof MultiOptionsFieldData) {
@@ -539,7 +540,7 @@ public function isValueEmpty(mixed $value, ElementInterface $element): bool
539540
/**
540541
* {@inheritdoc}
541542
*/
542-
#[\Override]
543+
#[Override]
543544
public function getPreviewHtml(mixed $value, ElementInterface $element): string
544545
{
545546
if (static::$multi) {
@@ -596,7 +597,7 @@ public function getPreviewHtml(mixed $value, ElementInterface $element): string
596597
/**
597598
* {@inheritdoc}
598599
*/
599-
#[\Override]
600+
#[Override]
600601
public function previewPlaceholderHtml(mixed $value, ?ElementInterface $element): string
601602
{
602603
$options = array_values(array_filter($this->options, fn ($option) => ! empty($option['value'])));
@@ -627,7 +628,7 @@ public function getIsMultiOptionsField(): bool
627628
/**
628629
* {@inheritdoc}
629630
*/
630-
#[\Override]
631+
#[Override]
631632
public function getContentGqlType(): array
632633
{
633634
return [
@@ -641,7 +642,7 @@ public function getContentGqlType(): array
641642
/**
642643
* {@inheritdoc}
643644
*/
644-
#[\Override]
645+
#[Override]
645646
public function getContentGqlMutationArgumentType(): Type|array
646647
{
647648
$values = [];

src/Field/BaseRelationField.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -512,20 +512,20 @@ public function validateSources(string $attribute): void
512512
public function settingsAttributes(): array
513513
{
514514
$attributes = parent::settingsAttributes();
515-
$attributes['allowSelfRelations'] = $this->allowSelfRelations;
516-
$attributes['branchLimit'] = $this->branchLimit;
517-
$attributes['defaultPlacement'] = $this->defaultPlacement;
518-
$attributes['maintainHierarchy'] = $this->maintainHierarchy;
519-
$attributes['maxRelations'] = $this->maxRelations;
520-
$attributes['minRelations'] = $this->minRelations;
521-
$attributes['selectionLabel'] = $this->selectionLabel;
522-
$attributes['showSearchInput'] = $this->showSearchInput;
523-
$attributes['showSiteMenu'] = $this->showSiteMenu;
524-
$attributes['source'] = $this->source;
525-
$attributes['sources'] = $this->sources;
526-
$attributes['targetSiteId'] = $this->targetSiteId;
527-
$attributes['validateRelatedElements'] = $this->validateRelatedElements;
528-
$attributes['viewMode'] = $this->viewMode;
515+
$attributes[] = 'allowSelfRelations';
516+
$attributes[] = 'branchLimit';
517+
$attributes[] = 'defaultPlacement';
518+
$attributes[] = 'maintainHierarchy';
519+
$attributes[] = 'maxRelations';
520+
$attributes[] = 'minRelations';
521+
$attributes[] = 'selectionLabel';
522+
$attributes[] = 'showSearchInput';
523+
$attributes[] = 'showSiteMenu';
524+
$attributes[] = 'source';
525+
$attributes[] = 'sources';
526+
$attributes[] = 'targetSiteId';
527+
$attributes[] = 'validateRelatedElements';
528+
$attributes[] = 'viewMode';
529529

530530
return $attributes;
531531
}

src/Field/Field.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,12 @@ public function __construct($config = [])
356356

357357
foreach ($config as $name => $value) {
358358
if (! property_exists($this, $name)) {
359+
if (method_exists($this, $method = 'set'.Str::studly($name))) {
360+
$this->$method($value);
361+
362+
continue;
363+
}
364+
359365
continue;
360366
}
361367

@@ -490,7 +496,7 @@ public function __toString(): string
490496
public function attributes(): array
491497
{
492498
return Collection::make($this->settingsAttributes())
493-
->reject(fn ($value, $name): bool => in_array($name, [
499+
->reject(fn ($name): bool => in_array($name, [
494500
'validateHandleUniqueness',
495501
'layoutElement',
496502
'static',
@@ -1330,7 +1336,17 @@ public static function isSelectable(): bool
13301336

13311337
public function toArray(): array
13321338
{
1333-
return $this->attributes();
1339+
return collect($this->attributes())->mapWithKeys(function (string $attribute) {
1340+
if (property_exists($this, $attribute)) {
1341+
return [$attribute => $this->$attribute];
1342+
}
1343+
1344+
if (method_exists($this, $method = 'get'.Str::studly($attribute))) {
1345+
return [$attribute => $this->$method()];
1346+
}
1347+
1348+
return '__invalid__';
1349+
})->reject(fn ($value) => $value === '__invalid__')->all();
13341350
}
13351351

13361352
/**

src/FieldLayout/Concerns/HasFieldLayout.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use craft\base\ElementInterface;
88
use craft\base\FieldLayoutProviderInterface;
9-
use craft\models\EntryType;
109
use craft\models\FieldLayout;
10+
use CraftCms\Cms\Entry\Data\EntryType;
1111
use CraftCms\Cms\Field\Contracts\FieldInterface;
1212
use CraftCms\Cms\Support\Facades\Fields;
1313
use RuntimeException;
@@ -104,7 +104,8 @@ public function getFieldLayout(): FieldLayout
104104
// Set the provider to the original entry type, so it uses the original provider handle
105105
// (see https://github.com/craftcms/cms/pull/17213)
106106
// todo: FieldLayoutProviderInterface could define a getProvider() method
107-
$fieldLayout->provider = $this->owner->original ?? $this->owner;
107+
$fieldLayout->provider = $this->original ?? $this;
108+
/** @phpstan-ignore instanceof.alwaysFalse */
108109
} elseif ($this instanceof FieldLayoutProviderInterface) {
109110
$fieldLayout->provider = $this;
110111
}

yii2-adapter/legacy/elements/User.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use craft\helpers\Template;
3434
use craft\helpers\UrlHelper;
3535
use craft\models\FieldLayout;
36-
use craft\models\UserGroup;
3736
use craft\records\User as UserRecord;
3837
use craft\validators\DateTimeValidator;
3938
use craft\validators\UniqueValidator;
@@ -58,6 +57,7 @@
5857
use CraftCms\Cms\Support\PHP;
5958
use CraftCms\Cms\Support\Str;
6059
use CraftCms\Cms\Translation\Formatter;
60+
use CraftCms\Cms\User\Data\UserGroup;
6161
use CraftCms\Cms\User\Models\User as UserModel;
6262
use DateInterval;
6363
use DateTime;
@@ -1518,13 +1518,13 @@ public function setGroups(array $groups): void
15181518
*
15191519
* @param int|string|\CraftCms\Cms\User\Data\UserGroup $group The user group model, its handle, or ID.
15201520
*/
1521-
public function isInGroup(\CraftCms\Cms\User\Data\UserGroup|int|string $group): bool
1521+
public function isInGroup(UserGroup|int|string $group): bool
15221522
{
15231523
if (Edition::get() < Edition::Pro) {
15241524
return false;
15251525
}
15261526

1527-
if ($group instanceof \CraftCms\Cms\User\Data\UserGroup) {
1527+
if ($group instanceof UserGroup) {
15281528
$group = $group->id;
15291529
}
15301530

yii2-adapter/legacy/elements/conditions/entries/AuthorGroupConditionRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use craft\elements\db\ElementQueryInterface;
99
use craft\elements\db\EntryQuery;
1010
use craft\elements\Entry;
11-
use craft\models\UserGroup;
1211
use CraftCms\Cms\Support\Facades\UserGroups;
12+
use CraftCms\Cms\User\Data\UserGroup;
1313
use function CraftCms\Cms\t;
1414

1515
/**

0 commit comments

Comments
 (0)