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

Allow to disable the global search by admin #6609

Merged
merged 1 commit into from
Nov 25, 2020

Conversation

phansys
Copy link
Member

@phansys phansys commented Nov 20, 2020

Subject

Allow to disable the global search by admin.

I am targeting this branch, because these changes respect BC.

Closes #6102.

Changelog

### Added
- `AdminSearchCompilerPass` in order to configure which admins must support search.
- Support for `global_search` attribute in `sonata.admin` tags, which admits boolean values.

To do

  • Add tests;
  • Update the documentation.

@phansys phansys added the minor label Nov 20, 2020
{
public const TAG_ATTRIBUTE_TOGGLE_SEARCH = 'global_search';

private const TAG_DMIN = 'sonata.admin';
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
private const TAG_DMIN = 'sonata.admin';
private const TAG_ADMIN = 'sonata.admin';

Copy link
Member

Choose a reason for hiding this comment

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

And this tag is used in multiple compiler pass. It should be the same constant, isn't it ?

Copy link
Member Author

@phansys phansys Nov 20, 2020

Choose a reason for hiding this comment

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

Yes, I see this tag is used in other classes, but since we haven't defined a global place where it could be declared, I decided to use this only for the purposes of this class.

Copy link
Member

Choose a reason for hiding this comment

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

Should we declare this in the AdminInterface ?

Copy link
Member Author

Choose a reason for hiding this comment

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

IMO, we should not tie the classes outside the Symfony specific DI scope with something related to that configuration.

Copy link
Member

Choose a reason for hiding this comment

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

Then I think the tag should be in AddDependencyCallsCompilerPass which should mention Admin in the name (and maybe be splitted ?).

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'll add the constant at AddDependencyCallsCompilerPass in a new pedantic PR.

Copy link
Member

Choose a reason for hiding this comment

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

I have added it here: #6614

WDYT ?

@phansys phansys force-pushed the issue_6102 branch 9 times, most recently from c44a8cc to 1670087 Compare November 21, 2020 17:26
@phansys phansys marked this pull request as ready for review November 22, 2020 01:27
@phansys phansys requested a review from a team November 22, 2020 01:27
@phansys
Copy link
Member Author

phansys commented Nov 22, 2020

Do you know how to fix the PHPStan issue without widening the allowed types in the docblock? I would not like to add an ignore rule.

@VincentLanglet
Copy link
Member

Do you know how to fix the PHPStan issue without widening the allowed types in the docblock? I would not like to add an ignore rule.

I would say you cant since we're trusting the Phpdoc.

Either the phpdoc say that it should be an array of bool, either there is a check inside ; but phpstan doesnt allow to do both.

@franmomu
Copy link
Member

I don't know if we should mark as @internal these methods that are just intended to be called by, in this case, a compiler pass and then trust our input.

@phansys
Copy link
Member Author

phansys commented Nov 22, 2020

Thanks for the feedback.
Given this situation and your answers, I think we should treat phpdocs as they were type declarations (regardless the @internal annotation).
By instance, a method which comes with its corresponding docblocks introduced in the same commit where the method is declared, should be able to add a type check in future commits and throw an exception if the types are not considered valid in the docblock. That addition should be treated as a BC compatible change, since we always told that the expected type was the one declared in the docblock.
I think this decision will helps us a lot narrowing the scope of our BC promise.
WDYT?

/**
* @author Javier Spagnoletti <phansys@gmail.com>
*/
final class AdminSearchCompilerPassTest extends TestCase
Copy link
Member

Choose a reason for hiding this comment

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

What about using AbstractCompilerPassTestCase?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I thought I had written something more in the previous comment. What I meant was to use that class to make the test simpler and to use real objects, something like:

$adminFooDefinition = new Definition(PostAdmin::class);
$adminFooDefinition->addTag('sonata.admin', ['global_search' => true]);
$this->setDefinition('admin.foo', $adminFooDefinition);

$adminBarDefinition = new Definition(PostAdmin::class);
$adminBarDefinition->addTag('sonata.admin', ['global_search' => false]);
$this->setDefinition('admin.bar', $adminBarDefinition);

$adminBazDefinition = new Definition(PostAdmin::class);
$adminBazDefinition->addTag('sonata.admin', ['some_attribute' => 42]);
$this->setDefinition('admin.baz', $adminBazDefinition);

$searchHandlerDefinition = new Definition();
$this->setDefinition('sonata.admin.search.handler', $searchHandlerDefinition);

$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
    'sonata.admin.search.handler',
    'configureAdminSearch',
    [['admin.foo' => true, 'admin.bar' => false]]
);

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you. I've applied your suggestion.

VincentLanglet
VincentLanglet previously approved these changes Nov 22, 2020
VincentLanglet
VincentLanglet previously approved these changes Nov 23, 2020

private const TAG_ADMIN = 'sonata.admin';

public function process(ContainerBuilder $container): void
Copy link
Member

Choose a reason for hiding this comment

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

Can you try to extract some smaller methods to reduce complexity?

@VincentLanglet VincentLanglet merged commit 78b1db8 into sonata-project:3.x Nov 25, 2020
@VincentLanglet
Copy link
Member

Thanks @phansys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] Disable global search by admin
4 participants