Skip to content
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

Add fluent builder for Laravel #67

Merged
merged 5 commits into from
Mar 13, 2024
Merged

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Mar 12, 2024

Related to PHPORM-103

Reopen #65

Create a fluent pipeline builder that can be used like this:

        $pipeline = (new class { use FluentFactoryTrait; })
            ->match(x: Query::eq(1))
            ->project(_id: false, x: true)
            ->sort(x: Sort::Asc)
            ->getPipeline();

generator/src/FluentStageFactoryGenerator.php Outdated Show resolved Hide resolved
generator/src/FluentStageFactoryGenerator.php Show resolved Hide resolved
private function addMethod(Method $factoryMethod, PhpNamespace $namespace, ClassType $class): void
{
if ($class->hasMethod($factoryMethod->getName())) {
return;
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to throw here? What is the legitimate case where the method already exists and you'd want to NOP?

Copy link
Member Author

Choose a reason for hiding this comment

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

The method match is redefined in the Stage class. The generated version from the trait must be ignored.

Copy link
Member

Choose a reason for hiding this comment

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

Noted. In this case, a comment would be helpful to clarify that you're giving preference to the redefined Stage methods instead of those from the trait. And this assumes you add the Stage methods before the trait (no need to repeat that, it's clear from the order of addMethod() calls in the generation method).

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's essentially what I said in the comment above.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, my mistake. I just realized this entire thread was incorrectly applied to the early return for non-public methods.

I see the comment below for skipping overridden methods. 👍

generator/src/FluentStageFactoryGenerator.php Outdated Show resolved Hide resolved
generator/src/FluentStageFactoryGenerator.php Show resolved Hide resolved
generator/src/FluentStageFactoryGenerator.php Show resolved Hide resolved
generator/src/FluentStageFactoryGenerator.php Outdated Show resolved Hide resolved
generator/src/FluentStageFactoryGenerator.php Outdated Show resolved Hide resolved
generator/src/FluentStageFactoryGenerator.php Outdated Show resolved Hide resolved
@GromNaN GromNaN requested a review from jmikola March 12, 2024 21:55
use MongoDB\Model\BSONArray;
use stdClass;

trait FluentFactoryTrait
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 converted the class to a trait, this allows to rename or change visibility of a method if necessary, and avoid issues that can be caused by inheritance.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

LGTM with the class/trait name replacements. Also note a suggestion to comment the addMethod() return statement.

use function sprintf;

/**
* Generates a fluent factory class for aggregation pipeline stages.
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
* Generates a fluent factory class for aggregation pipeline stages.
* Generates a fluent factory trait for aggregation pipeline stages.

Make a note to update other references of "class" to "trait" below. For instance, createFluentFactoryClass() should now be createFluentFactoryTrait().

This very class might also be better named FluentFactoryTraitGenerator().

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 want to keep "Stage" in the generator class name.

Copy link
Member

Choose a reason for hiding this comment

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

Should the generated file be FluentStageFactoryTrait.php in that case? I'll defer to you, as I'm not sure if you have future plans for more traits.

Copy link
Member Author

Choose a reason for hiding this comment

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

The file is in the Stage namespace.

<?php

/**
* THIS FILE IS AUTO-GENERATED. ANY CHANGES WILL BE LOST!
Copy link
Member

Choose a reason for hiding this comment

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

If this file is auto-generated, should it be labeled as such in .gitattributes?

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have /src/Builder/Stage/*.php linguist-generated=true

Copy link
Member

Choose a reason for hiding this comment

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

Noted. I'm not sure why GitHub opted not to present that explanation for hiding the file (maybe "large diffs" take precedence).

@GromNaN GromNaN requested a review from jmikola March 13, 2024 15:59
@GromNaN GromNaN merged commit 12d951c into mongodb:0.2 Mar 13, 2024
4 of 5 checks passed
@GromNaN GromNaN deleted the laravel-fluent branch March 13, 2024 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants