Problem/Motivation

When determining the namespaces to set in container.namespaces, which is used for annotated class discovery, DrupalKernel requires that a Plugin directory be present.

This is not true for modules, just for Core and Component.

Proposed resolution

In addition to the hardcoded check for Plugin, add one for Element and Entity.
Since this only affects code in \Drupal\Core and \Drupal\Component, we don't need to worry about extensibility

Remaining tasks

N/A

User interface changes

API changes

N/A

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

Status: Active » Needs review
FileSize
772 bytes
Berdir’s picture

I did add this to avoid having to parse tons of components folders for nothing. We're talking about 80 folders * dozens of plugin types, that's a lot of file_exists() checks...

sun’s picture

For now, let's simply make the condition more complex + add the expected directory names of core plugin types that may exist.

Alternatively, we could as well hard-code a few component names; e.g., $component === 'Form' || $component === 'Render' + skip the is_dir() checks altogether. That might make even more sense.

Perhaps we should even consider to convert the entire condition to that approach, but separate issue.

dawehner’s picture

--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php

+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -1017,7 +1017,7 @@ protected function compileContainer() {

@@ -1017,7 +1017,7 @@ protected function compileContainer() {
       $path = DRUPAL_ROOT . '/core/lib/Drupal/' . $parent_directory;

There is a little bit documentation above here

jibran queued 1: plugin-2309889-1.patch for re-testing.

dawehner’s picture

One alternative approach could be to just specify the Plugin directories directly in the core.services.yml file, for example.

tim.plunkett’s picture

Status: Needs review » Needs work

Not sure what #6 means.

Any patch should be sure to remove core/lib/Drupal/Core/Render/Plugin/README.txt to prove this works.

dawehner’s picture

Status: Needs work » Needs review
FileSize
2.01 KB

This is probably a really bad solution but this should just give some idea.

sun’s picture

     // Get a list of namespaces and put it onto the container.
     $namespaces = $this->getModuleNamespacesPsr4($this->getModuleFileNames());

Apparently, we're unconditionally adding all namespaces of all modules, regardless of whether they have any plugins. — FWIW, it's a bit pointless to do this optimization for Component + Core, while checking all (possibly a lot more) module namespaces anyway.

Thinking through all available options, I think the current implementation is fine. Adding an empty Plugin subdirectory isn't really a problem.

The only other sensible option would be to fully hard-code the entire list of component/core namespaces, so as to skip the directory scan altogether (because it will always yield the same result anyway).

Status: Needs review » Needs work

The last submitted patch, 8: plugin_namespaces-2309889-8.patch, failed testing.

tim.plunkett’s picture

Adding an empty Plugin subdirectory isn't really a problem.

I could not disagree more. First, you'd have to know that you need a Plugin directory. Second, you'd need to realize that git doesn't allow empty directories, so you'd add a README.txt like we do in core. But for me, the first step was only possible because I had detailed knowledge of how AnnotatedClassDiscovery and the namespaces were built. And even then, it took me at least an hour to figure out.

I really don't think that's reasonable.

Berdir’s picture

As discussed, I'm ok with either just hardcoding a list (but a container parameter seems overkill to me, nobody will add something in there, and if they really do, they could do it in service builder alter or something?) or just extend the existing folder check to include Render and Entity. I don't think we will be adding more top level directories like that?

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
2.44 KB

Talked with @Berdir some about this. @dawehner's approach looks promising, but we don't need this to change, as this *only* applies to core.
Trying just hardcoding it all.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

meh okay.

Berdir’s picture

Just wondering if it's worth to micro-optimize this and talk about the order of checks (e.g. plugin first because that is the most common one?). But OK with this as well.

Berdir’s picture

Does need an issue summary update, though.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I agree #15 we should optimise for the most common use case. Plus yep an issue summary update is needed.

tim.plunkett’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update
FileSize
793 bytes
2.44 KB

Did both of those.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 305b2f3 and pushed to 8.0.x. Thanks!

  • alexpott committed 305b2f3 on 8.0.x
    Issue #2309889 by tim.plunkett, dawehner: Fixed DrupalKernel requires a...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.