Skip to content

Conversation

distantnative
Copy link
Member

@distantnative distantnative commented Aug 15, 2025

Description

The motivation here is to break up some of the magic monster methods into separate, dedicated methods.

Thinking also about moving all permalink logic into its own Uuid\Permalink class.

Changelog

♻️ Refactored

  • New Kirby\Uuid\Uuid::from(string $uuid) method for creating an Uuid object from a UUID string. Kirby\Uuid\Uuid::for() remains to create a Uuid object for a model object.

🚨 Breaking changes

  • Kirby\Uuid\Uuid::for() cannot be called any longer with a string. Use Kirby\Uuid\Uuid::from(string $uuid) or Kirby\Uuid\Uuid::fromPermalink(string $permalink) instead.

For review team

  • Add changes & docs to release notes draft in Notion

@distantnative distantnative self-assigned this Aug 15, 2025
@distantnative distantnative force-pushed the v6/refact/uuids-uuid-from branch from 74daedf to 74c6e4c Compare August 15, 2025 11:51
@distantnative distantnative changed the title V6/refact/uuids UUID from refact!: Split Uuid::for() and Uuid::from() Aug 15, 2025
@distantnative distantnative force-pushed the v6/refact/uuids-uuid-from branch from 74c6e4c to 415ebac Compare August 15, 2025 18:03
@distantnative distantnative marked this pull request as ready for review August 15, 2025 18:15
@distantnative distantnative requested a review from a team August 15, 2025 18:15
@distantnative distantnative marked this pull request as draft August 15, 2025 18:16
@distantnative distantnative force-pushed the v6/refact/uuids-uuid-from branch 2 times, most recently from 0638184 to d43bc60 Compare August 15, 2025 18:35
@distantnative distantnative changed the base branch from v6/develop to v6/refact/uuids-permalink-class August 15, 2025 18:35
@distantnative distantnative force-pushed the v6/refact/uuids-permalink-class branch from f895088 to 1a44f8b Compare August 15, 2025 18:41
@distantnative distantnative force-pushed the v6/refact/uuids-uuid-from branch from d43bc60 to 953751b Compare August 15, 2025 18:43
@distantnative distantnative force-pushed the v6/refact/uuids-permalink-class branch 3 times, most recently from 5d5764a to f4edc38 Compare August 15, 2025 19:08
@distantnative distantnative force-pushed the v6/refact/uuids-uuid-from branch from 953751b to ba7e2a6 Compare August 15, 2025 19:09
@distantnative distantnative force-pushed the v6/refact/uuids-permalink-class branch from f4edc38 to 7cd7158 Compare August 16, 2025 18:52
@distantnative distantnative force-pushed the v6/refact/uuids-uuid-from branch from ba7e2a6 to c7138d8 Compare August 16, 2025 18:58
Copy link
Member

@lukasbestle lukasbestle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The for and from names are great here!

* - parent UUID including scheme
* - field name
* - UUID id string for model
*/
public function value(): array
{
/**
* @var \Kirby\Cms\Site|\Kirby\Cms\Page|\Kirby\Cms\User $model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Couldn't it also be a file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. It seems I was rather thinking about the result of $model->parent() here.

// 'block' => new BlockUuid(uuid: $seed, context: $context),
// 'struct' => new StructureUuid(uuid: $seed, context: $context),
default => throw new InvalidArgumentException(
message: 'Invalid UUID URI "' . $uri . '" in ' . $uuid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
message: 'Invalid UUID URI "' . $uri . '" in ' . $uuid
message: 'Invalid UUID type "' . $uri . '" in ' . $uuid

Probably makes sense to rename the variable as well. Not sure why we used $uri here.

*/
final public static function from(
string $uuid,
string|array|null $scheme = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Uuid::is() uses the name $type, I'd use the same here for consistency:

Suggested change
string|array|null $scheme = null,
string|array|null $type = null,

Comment on lines +209 to +210
if ($uuid = Uuid::from($tag->value, ['page', 'file'])) {
$tag->value = $uuid->toUrl();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: This changes behavior. Before, a valid but non-existent UUID would lead to an empty value, which would be handled below. Now, the original UUID URI would stay in the value unconverted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here we could solve it by just turning it into a one-liner:

$tag->value = Uuid::from($tag->value, ['page', 'file'])?->toUrl();

as this would also lead to $tag->value being null afterwards if it isn't a valid UUID.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite. Then the value will also be null for all non-UUID links (links that don't match the page://something or file://something patterns).

Comment on lines +75 to +76
if ($uuid = Uuid::from($filename, 'file', $this->$in())) {
return $uuid->model();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: Same here, but not as critical. Here, the valid but non-existent UUID will make Kirby execute all of the other checks instead of exiting early (but before with an error like Call to function model() on null).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That raises maybe a question: If something looks like a UUID but cannot be resolved to a model, would we rather have it throwing some form of error or just ignore it and try to resolve it with the other checks?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should throw an error. The UUID pattern is very unique (file://something) and it is quite unlikely that something else was meant. Especially not a filename as filenames cannot contain two slashes sequentially.

return $file;
}
if (method_exists($parent, 'file') === true) {
return $parent->file($path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: If called on $parent = Page, this will return null if that page doesn't have the file $path. The old code continued to the very last line of the method and checked for site files of that name.

@distantnative distantnative force-pushed the v6/refact/uuids-permalink-class branch from 34ebf2e to 999539b Compare August 18, 2025 19:49
Base automatically changed from v6/refact/uuids-permalink-class to v6/develop August 19, 2025 09:55
@distantnative distantnative force-pushed the v6/refact/uuids-uuid-from branch from c7138d8 to ad862b2 Compare August 19, 2025 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants