-
-
Notifications
You must be signed in to change notification settings - Fork 180
refact!: Split Uuid::for()
and Uuid::from()
#7544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v6/develop
Are you sure you want to change the base?
Conversation
74daedf
to
74c6e4c
Compare
Uuid::for()
and Uuid::from()
74c6e4c
to
415ebac
Compare
0638184
to
d43bc60
Compare
f895088
to
1a44f8b
Compare
d43bc60
to
953751b
Compare
5d5764a
to
f4edc38
Compare
953751b
to
ba7e2a6
Compare
f4edc38
to
7cd7158
Compare
ba7e2a6
to
c7138d8
Compare
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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, |
There was a problem hiding this comment.
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:
string|array|null $scheme = null, | |
string|array|null $type = null, |
if ($uuid = Uuid::from($tag->value, ['page', 'file'])) { | ||
$tag->value = $uuid->toUrl(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
if ($uuid = Uuid::from($filename, 'file', $this->$in())) { | ||
return $uuid->model(); |
There was a problem hiding this comment.
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
).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
34ebf2e
to
999539b
Compare
c7138d8
to
ad862b2
Compare
Description
Uuid::is()
to support multiple schemes #7543Uuid\Permalink
class #7545The 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
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. UseKirby\Uuid\Uuid::from(string $uuid)
orKirby\Uuid\Uuid::fromPermalink(string $permalink)
instead.For review team