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

Generate classmap #60

Closed
wants to merge 28 commits into from

Conversation

BrianHenryIE
Copy link
Contributor

@BrianHenryIE BrianHenryIE commented Aug 7, 2020

This PR adds creation of a classmap file from the PSR-0 "classmap_directory" files managed by Mozart.

Defaults to none.

Configured in composer.json with

"classmap_output": {
  "filename": "src/autoload_classmap.php",
  "relative_path": "/src"
},

Then used in the plugin with:

$class_map_file = __DIR__ . '/autoload_classmap.php';
if ( file_exists( $class_map_file ) ) {

	$class_map = include $class_map_file;

	if ( is_array( $class_map ) ) {
		spl_autoload_register(
			function ( $classname ) use ( $class_map ) {

				if ( array_key_exists( $classname, $class_map ) && file_exists( $class_map[ $classname ] ) ) {
					require_once $class_map[ $classname ];
				}
			}
		);
	}
}

@coenjacobs
Copy link
Owner

@BrianHenryIE Isn't this creating a duplicate file from what is already stored in the default classmap autoloader that Composer generates? I recall that the default classmap autoloader also contains the prefixed classes.

@BrianHenryIE
Copy link
Contributor Author

I'll fix this soon.

I don't use composer in my actual plugins, just in dev. Unfortunately, I don't have anything public on GitHub that's actually using the classmap but you can see the idea at this project – Mozart copies and prefixes packages into src/vendor, I use my own autoloader.php and then I zip up the whole src directory as the plugin.

I mentioned my lack of understanding in one of the issues, maybe you could add a couple of words/links in the README to explain things to novices. I think that could be valuable to many WordPress developers!

@coenjacobs
Copy link
Owner

@BrianHenryIE You know how much I appreciate all the good work you do for this project, right? For now, I'd like you to pause this particular functionality for a while. @markjaquith and I have been turning this whole idea upside down in #66 now. Even though we're not quite close to white smoke, I doubt that this current approach is going to survive eventually and I'd hate for you to spend much more time on this than you already have.

@BrianHenryIE BrianHenryIE marked this pull request as draft September 22, 2020 22:46
@BrianHenryIE
Copy link
Contributor Author

Sure. Since I'm already using it (with composer-patches) there might be a few commits to keep it working for me.

But.. I'm still in the dark as to how you are including "classmap" files in your projects. You run mozart compose, the files get copied, the classnames prefixed, then what? Does the Composer autoloader in vendor/autoload.php know where they are and what they're called? What exactly is composer dump-autoload doing?

@BrianHenryIE
Copy link
Contributor Author

I drastically simplified this. It uses Composer itself now to generate the classmap. Doesn't need any config in composer.json. Adds vendor/bin/mozart dump-autoload.

@BrianHenryIE
Copy link
Contributor Author

This is using ob_get_contents() and should be using ob_get_clean().

It was printing the entire files' contents in the console.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants