Feature proposal: support for composer:// stream-wrapper
#9668
Replies: 5 comments 6 replies
-
|
I should mention the one function that doesn't work, which is
In other words, it does not support any type of stream-wrapper. (and custom stream-wrappers cannot support it.) |
Beta Was this translation helpful? Give feedback.
-
|
I think such stream wrapper will be complex enough to justify being maintained as a dedicated library rather than being generated code for which we cannot easily report bug fixes or even security fixes. And if it is a dedicated library, it does not even need to be maintainer by the composer core team then |
Beta Was this translation helpful? Give feedback.
-
|
note that such stream wrapper might leverage the |
Beta Was this translation helpful? Give feedback.
-
|
The code is now available to view here. It's ~200 LOC for the stream-wrapper, on top of the ~100 LOC or so for the underlying library, really nothing substantial. I do wish you would consider this as a standard Composer feature - it delivers the autoloading, but doesn't provide any means of locating your installed resources. As a result, people currently do "dumb things" (reflection, wrong assumptions about Note that I'm not suggesting the class/API generated by this library be included. I'm only maintaining it for backwards compatibility in the first place - it could be completely removed (refactored away) or could be considered just an internal implementation detail. So this wouldn't require code-generation like my package does - a simple dump of the package names/paths to a file would do just fine. Being able to |
Beta Was this translation helpful? Give feedback.
-
|
IMO #9648 is a better way to handle this as it gives you the base path without overhead of stream wrappers. And then you can do whatever you want with that path yourself. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
"How do I locate files in composer packages?"
This is a common question on any project that wasn't built using one of the large, mainstream frameworks - which offer concepts like "bundles" etc. to answer this question in various ways.
I personally build all my projects using an IOC container and hand-picked composer packages. I don't want a full framework.
Some people just hard-code the base-paths - and answer you tend to find on StackOverflow - but this creates problems, for one, the path is obviously different when the package is the root package (when you're running tests, etc.) and when it's installed in the
vendorfolder. Composer installer plugins can alter package paths,composer.jsoncan change thevendorfolder name, etc.There are packages available to deal with this problem as well - on the light end, my own package mindplay/composer-locator accounts for these problems; and on the heavy end (now dead?) packages like Puli basically built their own virtual file-systems to replace the native file-system.
Now, every template engine, markdown renderer, really anything that accepts a file path, generally uses
file_get_contentsorfopenor other standard functions - and so, they already support PHP-style URIs, and thereby support stream-wrappers.So I can make this work - by choosing a package (
mindplay/composer-locatoror something like it) I can find the physical path, and then pass that path to library X.But I'm introducing another dependency, at the source-code level, just to locate the file. This is all information that Composer has - packages like
mindplay/composer-locatorreally just take the information from Composer and exposes it. I already have composer, I have the file installed, but now I need to pick a library and write code just to locate the file...What I'd really like is to be able to just do this:
The
composer://stream-wrapper would resolve the base pathvendor/packageto the installed package root-folder, and the rest of the path is simply appended to that physical path.So I went ahead and added that to
mindplay/composer-locator.It passes all of the following tests, where
okassertstrue, andeqis a strict equality assertion:https://github.com/mindplay-dk/composer-locator/pull/9/files#diff-0e9f7e9fab2837fcff42dfc571491aeae61f1090a8c6351b93a49c30f0927e21
So this very simply virtualizes the logical Composer package hierarchy into a logical file-system - you can open files using standard file-system functions, and you can use
readdirorDirectoryIteratoretc. to iterate over the rootcomposer://folder, which will give you vendor-names, you can iterate over e.g.composer://mindplayto find packages from a specific vendor, etc.It "just works" with any old PHP library, and it's all pretty intuitive, I think.
To the point where I'm wondering why this feature isn't just built into Composer. 🤔
I know it's always been a policy to never introduce actual APIs, and thereby source-code dependencies, in Composer - I've asked in the past for an API to locate package paths and gotten a solid "no", and I understand why.
But our projects do depend on Composer for the generated autoloader - it's not a source-code level dependency, but it is a dependency, as otherwise nothing would be able to load, right?
If somebody wanted to create a competing loader, they could - it would just have to parse standard JSON and support standard PSR-4 paths, etc.
This doesn't seem that different.
Granted, there would be things to consider and discuss first. For example, is
composer://an appropriate name? It might not be - something likepackages://might be more neutral territory? And would thepackagesscheme need to be a PSR standard?I think it's worth discussing? 🙂
Beta Was this translation helpful? Give feedback.
All reactions