Following situation:
I'd like to make X installable with both Y 2 and Y 3. My approach would be to provide different versions of the code using Y and switch between the versions depending on the installed version, similar to C conditional compilation.
Questions:
If X uses composer, then you may find versions of installed packages using composer's runtime API: https://getcomposer.org/doc/07-runtime.md#installed-versions