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

Revise class loader #72

Merged
merged 8 commits into from
Nov 30, 2022
Merged

Revise class loader #72

merged 8 commits into from
Nov 30, 2022

Conversation

LukeTowers
Copy link
Member

@LukeTowers LukeTowers commented Mar 3, 2022

Fixes wintercms/winter#214.

This fixes a long standing issue with the ClassLoader that handles loading module and plugin classes where only lowercase paths to the (properly cased) class file were technically supported. This prevents PSR-4 folder structures in plugins and modules from working; and even worse it would usually only show up in production with an opaque error message since production machines typically have case sensitive filesystems and development machines tend to not.

The current logic uses a directory scanning approach of generating many different possible variations of a valid path for a given class and then looping over the "loaded directories" to scan each of them for a valid instance of a possible path.

The new logic instead switches to requiring class prefix to class path to be registered with the ClassLoader explicitly which not only makes the resolution logic much faster (since the prefix is directly matched with the request class instead of always having to scan for a potential match) but it also means that the autoloading can be completely disabled for plugins and modules that aren't currently enabled which isn't currently the case and will help with fully cutting off disabled plugins and modules from the application.

LukeTowers added a commit to wintercms/wn-debugbar-plugin that referenced this pull request Mar 3, 2022
Can go back to PSR4 folders once wintercms/storm#72 is merged.
@github-actions
Copy link

github-actions bot commented May 9, 2022

This pull request will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this is still being worked on, please respond and we will re-open this pull request.
If this pull request is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@bennothommo bennothommo modified the milestones: 1.2.1, v1.2.2 Sep 12, 2022
LukeTowers and others added 4 commits November 29, 2022 15:20
* develop: (63 commits)
  Resync model getAttribute override with base Laravel functionality
  Convert Markdown parser to CommonMark (#133)
  Improve support for multiple database connections (#132)
  Make the SectionParser more extendable (#131)
  Fix static analysis errors
  Fixed generation of thumbnails for remote disks
  Get local root path from configured disk
  Use named arguments
  add more testing with pivot data
  Add Str::isJson() | is_json() helpers
  Code analysis fixes
  Register slug rule as part of Validation singleton registration
  Add slug validation rule
  Use Laravel's CLI components
  Use Laravel's CLI components
  Prioritize local dynamic methods over behavior-provided methods (#130)
  Delete unneeded PHPUnit config
  Re-enable code analysis on develop branch
  Fix PHPStan testing, minor tweaks to docs
  Pass the full model through add() and remove() methods
  ...
@LukeTowers LukeTowers marked this pull request as ready for review November 30, 2022 00:43
@LukeTowers LukeTowers changed the title [WIP] Revise class loader Revise class loader Nov 30, 2022
@LukeTowers LukeTowers merged commit 2c6cf01 into develop Nov 30, 2022
@LukeTowers LukeTowers deleted the wip/1.2-revise-class-loader branch November 30, 2022 03:06
LukeTowers added a commit to wintercms/winter that referenced this pull request Nov 30, 2022
bennothommo pushed a commit to wintercms/wn-system-module that referenced this pull request Nov 30, 2022
bennothommo pushed a commit to wintercms/wn-backend-module that referenced this pull request Nov 30, 2022
bennothommo pushed a commit to wintercms/wn-cms-module that referenced this pull request Nov 30, 2022
@damsfx
Copy link
Contributor

damsfx commented Dec 13, 2022

@LukeTowers Is this PR responsible for the class loading issues in Windows?

@LukeTowers
Copy link
Member Author

@damsfx most likely yes. We still haven't encountered the issues you were reporting nor have we heard from anyone else about them so if you're able to track it down on your machine and submit a PR I would appreciate that :)

@damsfx
Copy link
Contributor

damsfx commented Dec 20, 2022

@LukeTowers
After investigating ... the resolvePath of the ClassLoader seems to be the faulty.

When running the Winter.Test plugin migration, I've got this issue.

In seed_tables.php line 31:
  Class "Winter\Test\Models\Person" not found

The resolvePath() method is called with the following argument in this case

$path = "P:\_Sites\_Labs\winterdev\plugins\winter\test\Models\Person.php"

but the return value is

$path = "P:\_Sites\_Labs\winterdev\P:\_Sites\_Labs\winterdev\plugins\winter\test\Models\Person.php"

The method adds $this->basePath . DIRECTORY_SEPARATOR unnecessarily.

It may be necesary to include the base path in the test :

    /**
     * Resolve the provided path, relative or absolute
     */
    protected function resolvePath(string $path): string
    {
        if (!Str::startsWith($path, ['/', '\\', $this->basePath . DIRECTORY_SEPARATOR])) {
            $path = $this->basePath . DIRECTORY_SEPARATOR . $path;
        }
        return $path;
    }

@mjauvin
Copy link
Member

mjauvin commented Dec 20, 2022

Oh, I see, on windows the basePath does not necessarily start with a slash or backslash...

Care to submit a PR @damsfx ?

damsfx added a commit to damsfx/storm that referenced this pull request Dec 20, 2022
On windows the basePath does not necessarily start with a slash or backslash.

See : wintercms#72 (comment)
LukeTowers pushed a commit that referenced this pull request Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants