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

Add a source locator that can detect "autoload" and "autoload-dev" when given a composer schema definition from either a composer.json or a composer.lock #442

Closed
Ocramius opened this issue Aug 22, 2018 · 8 comments · Fixed by #461
Assignees
Milestone

Comments

@Ocramius
Copy link
Member

Ocramius commented Aug 22, 2018

Given a composer.json-alike array and a string $sourcesBasePath

['autoload' => [
    'psr-0' => [/* ... */],
    'psr-4' => [/* ... */],
    'classmap' => [/* ... */],
    'files' => [/* ... */],
    'exclude-from-classmap' => [/* ... */],
]];

we can generate a SourceLocator instance that closes around it.

@Ocramius
Copy link
Member Author

Relevant: Roave/BackwardCompatibilityCheck#97

Ocramius added a commit to Roave/BackwardCompatibilityCheck that referenced this issue Aug 22, 2018
…me there's a `composer.json` with an `autoload` section

This means that your project should (in theory) have a `composer.json` at
its root, and the sources for it will automatically be inferred from the
`composer.json` definition.

Currently, this package declares a `SourceLocator` instance emulating the
`"autoload"` section of a `composer.json` configuration, but later it may
be provided by BetterReflection directly (Roave/BetterReflection#442).

This patch also changes the constructor and default behavior
of `AssertBackwardsCompatible`, so it is a major BC break that requires
a new major release.
@Ocramius
Copy link
Member Author

After working on Roave/BackwardCompatibilityCheck#97 and Roave/BackwardCompatibilityCheck#102, I noticed a massive performance slowdown in the discovery of symbols by name. This is because BetterReflection (by default) iterates over each source locator, asking for known symbols.

If we implement a PSR-0, PSR-4, and generally a composer.json-based source locator, we can make the lookups for existing classes direct hits, instead of recursive (directory) iterator operations, hence speeding up the entire process from O(n) to O(1) when the location of a class is known.

@Ocramius
Copy link
Member Author

An example implementation is available at https://github.com/Roave/BackwardCompatibilityCheck/blob/a239f81bdfb8c68b8cf6c064b75bc13d77d789b7/src/LocateSources/LocateSourcesViaComposerJson.php#L63-L96 - it works, but it is slow as heck due to it being an AggregateSourceLocator that contains all files in the discovered paths (and therefore iterates over existing files rather than performing direct hits)

@moufmouf
Copy link
Contributor

I haven't the time to work on this directly, but just wanted to mention I did something quite similar:

  • thecodingmachine/classname-mapper => given a composer.json file and a class name will give you the list of possible file paths. With a few changes to read composer.lock instead of composer.json and that should do the trick.
  • thecodingmachine/class-explorer (WIP): for a given namespace, will scan all file names and return possibles classes (it does not read the files so it is pretty fast, but can return false positives)

@prisis
Copy link
Contributor

prisis commented Sep 12, 2018

I have something like this https://github.com/narrowspark/automatic/blob/master/src/Common/ClassFinder.php, so maybe I can just port it

@asgrim
Copy link
Member

asgrim commented Jan 25, 2019

@Ocramius does the ComposerSourceLocator do this anyway...?

$composer = require "vendor/autoload.php';
$locator = new ComposerSourceLoucater($composer, $astLocator);

@Ocramius
Copy link
Member Author

Ocramius commented Feb 9, 2019

As discussed in person, the ComposerSourceLocator uses an autoloader for that, and that means creating a real ClassLoader:

use Composer\Autoload\ClassLoader;

I could probably try using it, but the code is not side-effect-free, sadly:

https://github.com/composer/composer/blob/568081f9b68b9f7191a90d8ff9289913ee6e0be8/src/Composer/Autoload/ClassLoader.php#L335

@Ocramius
Copy link
Member Author

Note that following monstrosity won't be implemented for now:

    'exclude-from-classmap' => [/* ... */],

@Ocramius Ocramius assigned Ocramius and unassigned asgrim Apr 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants