-
-
Notifications
You must be signed in to change notification settings - Fork 186
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
Console create:plugin - PascalCase namespace #569
Comments
Some comments for clarification:
So, yes, could be an issue. |
Just need to change a single line here: https://github.com/wintercms/winter/blob/wip%2F1.2/modules/system/console/BaseScaffoldCommand.php#L47. PRs welcome. |
@LukeTowers Actually, to be synced with the scaffold methods' code style, we'll need another modification here to add the |
@RomainMazB I'm not sure I understand, could you expand on that? |
Sure @LukeTowers. To convert "WebVPF" to "WebVPF" and ensuring the first letter is uppercase to keep the PascalCase style, we need to use the php native So, to me, we have two ways to fix this: // Using native ucfirst function or Str::ucfirst directly inside the method you mentioned
$ucfirst_author = ucfirst($vars['author']);
$ucfirst_plugin = ucfirst($vars['plugin']);
$vars['plugin_namespace'] = "{$ucfirst_author}\\{$ucfirst_plugin}"; The good (to me): // GeneratorCommand.php
protected function processVars($vars)
{
$cases = ['upper', 'lower', 'snake', 'studly', 'camel', 'title', 'ucfirst']; // Add ucfirst casing // BaseScaffoldCommand.php
$vars['plugin_namespace'] = "{$vars['ucfirst_author']}\\{$vars['ucfirst_plugin']}"; The second one looks prettier with the rest of the method: $vars = parent::processVars($vars);
$vars['plugin_id'] = "{$vars['lower_author']}.{$vars['lower_plugin']}";
$vars['plugin_code'] = "{$vars['studly_author']}.{$vars['studly_plugin']}";
$vars['plugin_url'] = "{$vars['lower_author']}/{$vars['lower_plugin']}";
$vars['plugin_folder'] = "{$vars['lower_author']}/{$vars['lower_plugin']}";
$vars['plugin_namespace'] = "{$vars['ucfirst_author']}\\{$vars['ucfirst_plugin']}";
return $vars; instead of this: $vars = parent::processVars($vars);
$ucfirst_author = ucfirst($vars['author']);
$ucfirst_plugin = ucfirst($vars['plugin']);
$vars['plugin_id'] = "{$vars['lower_author']}.{$vars['lower_plugin']}";
$vars['plugin_code'] = "{$vars['studly_author']}.{$vars['studly_plugin']}";
$vars['plugin_url'] = "{$vars['lower_author']}/{$vars['lower_plugin']}";
$vars['plugin_folder'] = "{$vars['lower_author']}/{$vars['lower_plugin']}";
$vars['plugin_namespace'] = "{$ucfirst_author}\\{$ucfirst_plugin}";
return $vars; |
@LukeTowers I think that @RomainMazB was referrencing that we would need to add |
@arvislacis you've got a point. So now the question is: why "studly_author" => "Webvpf"
"studly_plugin" => "Myname" and
|
@RomainMazB Yeah, I came to same conclusion recently - that studly case for some reason doesn't work properly here. |
@arvislacis @LukeTowers , here is why. When the tiny problem becomes a "big" one 😄 |
Fair point. What I would like to see is that the namespace is generated from the provided input verbatim rather than trying to do any case modifications to it. It should be up to the dev whether their namespace is |
Yes, I agree on you with this - the best solution in this situation probably would be to take input verbatim..., the only modification maybe could be applied only to the first letter to make it always uppercase so it matches the coding standarts and guides (for example, if dev accidentally or by other means enter everything in the lowercase), anyways, that's also a disputable point. |
@arvislacis yes, their is chance where the dev will fail on typing, especially when creating model or controller after creating the plugin... I agree with the verbatim stuff but this introduce possible dev errors. We should warn this into the documentation. |
@RomainMazB If we look at Study/PascalCase from very basic point of view then it can also be interpreted as string in which the first character is always uppercase - all the rest characters can be interpreted verbatim from input as we don't know what acronyms dev has used for input etc. So maybe we could take user's input directly but ensure that the first character is always uppercase. |
@LukeTowers , I was about to submit a PR when discovering something that is strange but provides another way to treat two problems at once. winter/modules/system/classes/PluginManager.php Lines 575 to 589 in 5ccde6f
I thought this method was our perfect solution, but actually this method can't return anything but a lower-cased normalized identifier due to the fact that itself uses the normalizeIdentifier method which - as we defined previously - forcefully return a lower-cased identifier.
I've searched into the code where this method is used and I've found 7 places where the What I suggest is to introduce a
The Something like: public function normalizeIdentifier(string $code, bool $lower = true): string
{
$lowerCode = strtolower($code);
$code = $this->normalizedMap[$lowerCode] ?? $code;
return $lower ? strtolower($code) : $code;
}
/**
* Returns the normalized identifier (i.e. Winter.Blog) from the provided
* string or PluginBase instance.
*/
public function getNormalizedIdentifier(PluginBase|string $plugin, bool $lower = true): string
{
return $this->normalizeIdentifier($this->getIdentifier($plugin), $lower);
} |
@jaxwilko can you look at @RomainMazB's comment above when you've got a moment? Too late at night for me to fully parse it at the moment. |
Hey all, I've raised PR #588 which resolves the issue. The problem from my perspective is that To simplify things, I've moved this logic into Now when creating a plugin the casing is preserved from the user input, i.e. Please let me know if you have any thoughts or comments on this :) |
Fixed by #588. |
Winter CMS Build
dev-develop
PHP Version
8.0
Database engine
MySQL/MariaDB
Plugins installed
No response
Issue description
Winter CMS 1.2
Console command
create:plugin
incorrectly changes namespace. Camelcase names are changed to capitalized names.Example:
Plugin.php file generation result:
Must be:
Steps to replicate
Create a plugin
Workaround
No response
The text was updated successfully, but these errors were encountered: