Problem/Motivation
Working on some contrib modules in my spare time I'm noticing that my {$module}.routing.yml is getting larger and larger.
In Behat you can nominate additional imports in your YAML - see http://docs.behat.org/guides/7.config.html#imports - allowing you to split your files.
Proposed resolution
Support 'imports' key in all of our YamlDiscovery.
Example
foo.routing.yml
foo.baz:
path: '/foo/baz
defaults:
_controller: '\Drupal\foo\FooController::baz'
requirements:
_access: 'TRUE'
imports:
- routes/foo.admin.routing.yml
- routes/foo.pages.routing.yml
Remaining tasks
Reviews
User interface changes
None
API changes
Additions only
| Comment | File | Size | Author |
|---|---|---|---|
| #63 | reroll_diff_57-63.txt | 1.13 KB | ravi.shankar |
| #63 | 2284787-63.patch | 3.46 KB | ravi.shankar |
| #57 | 2284787-57.patch | 3.43 KB | finnsky |
| #36 | 2284787-24.patch | 8.29 KB | Vikas.Kumar |
| #23 | interdiff-2284787-23.txt | 1.19 KB | damiankloip |
Comments
Comment #1
larowlanAnd again without the test commented out.
Comment #2
jibranOverall I like the idea but my only concern is we already have a lot of yml files in module directory.
If we are going to support this we should follow some kind of naming convention.
Comment #3
larowlan@jibran - you can put them where you like - eg the test uses 'routes' folder to keep the number of yml files in the module root to a minimum
Comment #4
jibranOk then I am +1 completely.
Comment #5
larowlanto quote @timplunkett from irc
Comment #6
larowlanComment #7
larowlanComment #8
larowlanComment #9
jibranThis definitely improves DX. It is nice and simple solution. Patch is green with tests. I am going to RTBC it because imo it can be added to core as is.
Comment #10
damiankloip commentedThis patch looks good!
But...
Sorry, We should really add unit test coverage in the YamlDiscoveryTest class for this though, as this is a component 'n' all.
Comment #11
dawehnerIs there a reason we don't just support multiple files with the same suffix? With this you would not have to specify the "imports"?
Comment #12
Crell commentedSymfony already does something like this for its services.yml file, although we don't support it yet because we can't use the same parser they do. We should use the same syntax as that, and then support it for both the services.yml and routing.yml files. Definitely +1 in general.
Comment #13
damiankloip commentediirc, Symfony does use 'imports', which this patch already does.
Another thought, if we are adding this to our generic YamlDiscovery component, we should probably d oa deep merge of the values rather than just += ?
Comment #14
damiankloip commentedReroll for now. Will action #13.
Comment #15
dawehnerMaybe you will hate me but have you thought about using vfsStream to provide the files? For an example have a look at #2295469: Add support for static permission definitions with *.permissions.yml maybe
What about using assertCount instead? This makes it a little bit more clear
pointless empty line.
Comment #16
wim leersNW for review in #15.,
Comment #17
damiankloip commentedHere are those changes to the test, also removed the Fixtures dir, but left that off the interdiff.
So, a couple of things we need to work out now:
- My question above about deep merging of array values. I think we probably do want to support that. Thoughts? And also, as a part of that:
- The current patch will not override values that already exist (+=) is this how we want it to behave?
- chx mentioned to me in IRC a little while ago that he would like to see this functionality work recursively. Thoughts on that also?
Comment #18
larowlanMy 2c: yes to all three but I think third one could be follow-up
Comment #19
dawehnerYeah certainly c) should be a follow up.
The other two ones aren't simple. I think there are many usecases to override a complete service, in a local environment or just
in production. This will get even more important if most Settings will be part of the container as well.
Comment #20
dawehner@damiankloip
What is your oppinion on the merging bit?
Comment #21
damiankloip commentedI agree with both #18 and #19. Let's implement those changes.
Comment #22
dawehnerThis is a virtual file system, why should the directory exist already?
the expected data should be on the left side here.
Great test coverage!
Comment #23
damiankloip commentedThanks!
1. This is because we are putting two files into the same directory. It helps to keep an array of directory data in this was so we can pass it directly to YamlDiscovery.
2. Changed
3. Hell yeah.
Comment #24
dawehnerNice! Just needs some change record.
Comment #25
herom commentedRight now, potx relies on file name patterns to detect translatable strings in yaml files. So, please either add a visible note in the change record / docs, that the imported files should have the same naming pattern as the parent yaml file, or if we cannot enforce that, open a followup issue in the potx module to handle "import" keys in yaml files.
Comment #26
alexpottThis is not at all related to configuration.
Comment #27
webchickThis looks like really useful functionality! However, we're not quite ready to go here yet because:
1) We still need a change record
2) We're missing the follow-up issue indicated by herom
3) We should also file an upstream issue w/ Symfony to request use of their "imports" YAML support without being forced to also use their Config component (Crell says this would allow us to throw out our routing yaml parser and container yaml parser, in addition to this newly-added code.)
Although, I do admit that I like the syntax used here better than:
which is what it seems to be in Symfony, if I'm reading http://symfony.com/doc/current/book/service_container.html#importing-con... properly. Still. It's a good "open source citizen" thing to do.
Comment #28
sunUnless I'm mistaken, no upstream library performs a deep-merge of imports. Instead, I think we need/want to merge on a selected depth only.
Closely related, order matters for any merge. However, the term "imports" encompasses a concept of base definitions, and I assume the term was deliberately chosen. Imports are loaded and merged first, in the specified order. Custom/local data is merged into the cumulative imported data.
In other words:
Comment #29
dawehnerWell, good luck with filing one, but technically their Config component exactly has the abstractions they need for their usecases. Well here is one https://github.com/symfony/symfony/issues/11751 but I don't see any really proper usecase.
I am not sure whether you will read this, but this is actual consistent behaviour inside symfony and TWIG itself, see http://twig.sensiolabs.org/doc/api.html#built-in-loaders
We also have support for that inside Drupal itself, so you can use @module inside your twig template. I think this itself is fine, using relative paths is just a different way.
@sun
Well, we want to be able to override existing services/yml files, otherwise you would need to write compiler passes for relative simple things.
Comment #30
sunThat seems to diverge from the original goal in the issue summary, which is seemingly limited to splitting up YAML files into multiple, for sanity/organization purposes?
I don't think the topic of overrides should be mixed into imports. Override logic typically requires much more granular considerations; e.g., see service provider YAML file processing in
DrupalKernel. IMO, imports should be local to a file and should only be used to construct your own definitions. You may only override (replace) definitions that you imported (into your local scope).Comment #31
dawehnerThe result of the upstream issue was that it does not make sense to be implement in the YAML component, as it is just a parser/writer.
On top of that, the logic for importing other definitions etc. is already handled by the config component, we could indeed use, as it is other kind
of config as we do have. Their config component handles every kind of config in code: routing.yml files, .services.yml files etc.
Comment #32
larowlanComment #33
dawehner@larowlan
It is still not clear how we will be able to support both imports and merges ... given that we kind of need both.
Note: http://symfony.com/doc/current/components/config/index.html would be a tool we could use, but I think you will be killed if you suggest it :)
Comment #34
larowlanCan we simplify back to the original intent, which was splitting up massive routing.yml files, and attack the more complex cases in a separate issue?
Comment #35
dawehnerWell, sure we could do that, but this would really just matter for routing.yml files and not longer for even just services files.
Comment #36
Vikas.Kumar commentedPlease review this patch.
Comment #37
siva_epari commentedComment #38
siva_epari commentedComment #40
Crell commentedComment #41
wim leersThis would be very valuable for splitting up
core.services.ymltoo.Here I have the pleasure of quoting @sun:
I agree that this most likely makes sense for 8.1, but if it's just reorganization/splitting up then it doesn't actually break BC in any way, it only improves DX. AFAICT at least. So, moving back to 8.0 just in case it's a very small amount of work for an excellent DX improvement.
Comment #42
Crell commentedI guess no one did it...
Comment #48
joelpittetThis probably doesn't need to be postponed anymore.
Comment #49
slootjes commentedI was really surprised this is currently not possible so +1 For improving DX.
Comment #55
andypostThe question of merge and override still open but there's initial patch, so valid status
Comment #57
finnsky commentedHi all!
Rerolled patch on 9.3.x
Also added same functional for THEME.libraries.yml because i need it for components libraries.
Tests still need reroll
Comment #58
andypostComment #61
johnpitcairn commentedComment #62
wim leersThis would be a hugely impactful feature. But would absolutely not be possible to land without tests 🤓
Comment #63
ravi.shankar commentedAdded a patch for Drupal 10.1.x, still needs work for test.