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

A little request #10

Open
ghost opened this issue Jan 31, 2020 · 6 comments
Open

A little request #10

ghost opened this issue Jan 31, 2020 · 6 comments

Comments

@ghost
Copy link

ghost commented Jan 31, 2020

Hi, first of all thank you for this class.

I need to know if you can use it to manage multiple directories.

In practice a coding similar to the following:

includes/my_classes and admin/includes/my__other_classes and public/includes/my_other_classes

and so on

Thanks in advance
Adriano

@pablo-sg-pacheco
Copy link
Owner

pablo-sg-pacheco commented Feb 1, 2020

Hi,
You're welcome :)

Yes, it is, but it depends of how you structured your code.
Let me try to explain.
Imagine if you were using just a single namespace (namespace ProjectA) for your project:

  • includes/my_classes/class-test1.php
namespace ProjectA/includes;
class Test1 {}
  • admin/includes/my_other_classes/class-test2.php
namespace ProjectA/Admin/Includes/My_Other_Classes;
class Test2 {}

Now, if you are using different namespaces for each one of these directories you'd have to instantiate new WP_Namespace_Autoloader() for each namespace

Does it help?

@ghost
Copy link
Author

ghost commented Feb 2, 2020

Hello Pablo, thanks for your reply.

First and foremost I deeply apologize but English is not my primary language and I expressed myself very badly.

What I wanted to know and if the class- suffix could only be used in certain namespaces and not in other.

Something similar:

  • includes/vendor/framework/Test1.php
namespace My_Project\Framework;
class Test1 {}
  • admin/includes/my-package/class-my-package.php
namespace Admin\Includes;
class My_Package {}

Sorry again for the bad expression

Adriano

@pablo-sg-pacheco
Copy link
Owner

No problem,
It's not mine too anyway :)

In that case you could use the Composer autoload function for the project you don't want the class- prefix, like this:

 "autoload": {
     "psr-4": {
         "My_Project\\Framework\\": "includes/vendor/framework/"
     }
 }

And for the other project you do want class- prefix, you can simply use my library

@ghost
Copy link
Author

ghost commented Feb 2, 2020

Before anything else I am allergic to two things in life: fish and the composer ecosystem. :)

Having said that, it was also my idea to use the devil's autoload. However looking at your code the following change came to my mind.

  1. Change the constructor.

  2. Create a method called for example add_namespace()

Obviously I'm not sure that this code will actually work because it's just a sketch of an idea.

public function __construct( $args = null ) {
	if ( $args && count( $args ) ) {
		$this->add_namespaces( $args );
	}
}

If the $args array is empty nothing happens.

public function add_namespace( $args ) {
	$defaults = array(
		'directory'            => $this->get_calling_directory(),
		'namespace_prefix'     => $this->get_calling_file_namespace(),
		'lowercase'            => array( 'file' ), // 'file' | folders
		'underscore_to_hyphen' => array( 'file' ), // 'file' | folders
		'prepend_class'        => true,
		'classes_dir'          => array( '.', 'vendor' ),
		'debug'                => false,
	);

	$parsed_args = array_merge( $defaults, $args );

	$this->set_args( $parsed_args );
}

Now a splash of magic

  • Create a class instance
$autoloader = new WP_Namespace_Autoloader();
  • Add the first namespace with the necessary parameters
$autoloader->add_namespace(
	$args = [
		'directory'          => __DIR__,
		'namespace_prefix'   => 'My_Project',
		'classes_dir'        => 'src',
	];
);
  • Add another namespace with other parameters
$autoloader->add_namespace(
	$args = [
		'directory'          => __DIR__,
		'namespace_prefix'   => 'My_Other_Project',
		'classes_dir'        => 'my_other_dir',
	];
);

This is my idea. What do you think about it? Do you think it can work?

@pablo-sg-pacheco
Copy link
Owner

:D

Hum, this library was made for WordPress coding standards. Probably there is another library that could do what you want, but apart from that, maybe it could work :)

But can I ask why you are allergic to Composer ecosystem?
It should work just fine for your case

@ghost
Copy link
Author

ghost commented Feb 3, 2020

I did some experiments this morning and the modification I was telling you about works.

I'm not against composer as an autoloader but only as a dependency manager.

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

No branches or pull requests

1 participant