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 alternative way to provide the namespace via configuration #776

Merged
merged 3 commits into from
Aug 19, 2019
Merged

Add alternative way to provide the namespace via configuration #776

merged 3 commits into from
Aug 19, 2019

Conversation

daison12006013
Copy link
Contributor

@daison12006013 daison12006013 commented Jun 19, 2019

We could add a third option inside paths.generator.{alias} to have namespace

When adding parent folder, as part of the path, the current behaviour uses the path as well as the namespace, there is no way for us to easily provide the namespace, or else we are REQUIRED to extends the commands and modify the laravel module's ConsoleServiceProvider and the booted Provider.

What I could suggest is to add a 3rd parameter as optional, so there is no breaking change @nWidart

[
    'path' => 'src/Providers',
    'generate' => true,
    'namespace' => 'Providers',
]

Example modules that I have and their structure

image

@nWidart
Copy link
Owner

nWidart commented Jun 19, 2019

Hi,

Thanks for your contribution! However, I don't see a use-case for this. In php the namespace should always (or is recommended) match the folder structure.
Via the configuration, you can already override the default namespace used.

@daison12006013
Copy link
Contributor Author

daison12006013 commented Jun 19, 2019

@nWidart, there is no way for us to modify the path, your console commands were limited because of their default namespace.

You're basically using the path as their namespace.

Sometimes creating a module or a package you wrapped things up inside an src/ folder.

E.g, generate a module called PhotoProcessor If you will try to go inside PhotoProceessorServiceProvider.php the namespace will always be like this.

<?php

namespace Modules\src\PhotoProcessor\Providers;

Please try yourself, your code only bound to these kind of folder structure.

- PhotoProcessor/
    - Console/
    - Http/
    - Providers/
    - etc...

But what if we wanted it to be like this, which is normal for opensource developers and we could separate the package as reusable module for other purpose.

- PhotoProcessor/
    - src/
        - Console/
        - Http/
        - Providers/
        - etc...

And 1 more thing to note that path should always be different to namespace, the implementation is like psr-0 where folder name is equal to the namespace literally. Supposed to be like psr-4 to make it flexible.

And the pull request above is backward compatible, it is up to Developers if they want to fully customize the folder structure at all, just like me that I am planning to fully use the modular as package implementation.

@nWidart
Copy link
Owner

nWidart commented Jun 19, 2019

So this is a different issue? The src subfolder is something else, that was addressed in previous issues.

We shouldn't use the namespace to dictate the folder structure if we can do it the other way around.
The paths of each generated file can be configured via the configuration.

@daison12006013
Copy link
Contributor Author

daison12006013 commented Jun 19, 2019

The src subfolder is something else, that was addressed in previous issues.

So any solutions on this? I'm not referring to tha path, but the namespace itself, do we have solutions on this if I will be using the path value as src/Providers instead of Providers only for provider path.

<?php

namespace Modules\src\PhotoProcessor\Providers;

@nWidart
Copy link
Owner

nWidart commented Jun 19, 2019

Not really, just recommended to not do it.

Though if this PR enables this feature, why not add it indeed, I'll test it out in a bit (the configuration is already pretty big, I don't want to add too many things).

@nWidart
Copy link
Owner

nWidart commented Jun 20, 2019

Tested it and seems to work very well. 👌
I'll have a couple of comments on the code itself.

About the autoloading do you autoload every module manually, or is there a way to have one autoload line for all modules.

"Modules\\Blog\\": "Modules/Blog/src"

Could you also add tests for these use-cases please?

return $this->laravel['modules']->config('paths.generator.command.path', 'Console');
$module = $this->laravel['modules'];

return ($namespace = $module->config('paths.generator.command.namespace'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why catching this in a namespace variable? I think you can use ?: instead. In order to have it fit on one line this can be done:

    public function getDefaultNamespace() : string
    {
        $module = $this->laravel['modules'];

        $namespace = $module->config('paths.generator.command.namespace');
        
        return $namespace ?: $module->config('paths.generator.command.path', 'Console');
    }

(same for all)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy will add unit and some changes on it

@daison12006013
Copy link
Contributor Author

About the autoloading do you autoload every module manually, or is there a way to have one autoload line for all modules.

"Modules\\Blog\\": "Modules/Blog/src"

Should I add another config (by default it is empty) and if developers would like to modify the value into "src/" then inside composer.stub we could pull the value.

@nWidart
Copy link
Owner

nWidart commented Jun 24, 2019

I didn't understand your last comment, you're updating your PR?

@daison12006013
Copy link
Contributor Author

daison12006013 commented Jun 25, 2019

I didn't understand your last comment, you're updating your PR?

Forget my last comment, I am just thinking a different approach for the autoload path.
Will update my PR soon, quite busy yet, for the meantime will close this PR yet.

@daison12006013 daison12006013 reopened this Jul 5, 2019
@daison12006013
Copy link
Contributor Author

@nWidart updated

@nWidart nWidart merged commit 9620dc1 into nWidart:master Aug 19, 2019
@aimensasi
Copy link

any updates on the autoload issue, cause right now I had to do this #776 (comment) for every module

@daison12006013
Copy link
Contributor Author

@aimensasi there are things that limits us, since we're using stub/template thing, might be better to request a new feature for it referencing this Pull Request? so we could focus into that issue.

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.

3 participants