Welcome Guest, Not a member yet? Register   Sign In
How can I activate Suffix without using --suffix?
#1

Hello friends.
How can I enable Suffix by default without using the command(php spark mycommand --suffix)?

Is there anyone here who can help?
Reply
#2

It seems $enabledSuffixing can only disable `--suffix` option.

If you want to make a new command, implement it as you like.

If you want to change the existing spark command behavior,
it seems there is no easy way to do so.
Reply
#3

Quote:If you want to make a new command, implement it as you like.

In the my command I use the GeneratorTrait, so this method can not be used.
Reply
#4

Override the method in your command class.
Reply
#5

(This post was last modified: 05-11-2022, 09:22 PM by datamweb.)

(05-11-2022, 01:17 AM)kenjis Wrote: Override the method in your command class.

CodeIgniter4\system\Commands\Generators\ConfigGenerator.php
Code:
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands\Generators;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\GeneratorTrait;

/**
* Generates a skeleton config file.
*/
class ConfigGenerator extends BaseCommand
{
    use GeneratorTrait;

    /**
    * The Command's Group
    *
    * @var string
    */
    protected $group = 'Generators';

    /**
    * The Command's Name
    *
    * @var string
    */
    protected $name = 'make:config';

    /**
    * The Command's Description
    *
    * @var string
    */
    protected $description = 'Generates a new config file.';

    /**
    * The Command's Usage
    *
    * @var string
    */
    protected $usage = 'make:config <name> [options]';

    /**
    * The Command's Arguments
    *
    * @var array
    */
    protected $arguments = [
        'name' => 'The config class name.',
    ];

    /**
    * The Command's Options
    *
    * @var array
    */
    protected $options = [
        '--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
        '--suffix'    => 'Append the component title to the class name (e.g. User => UserConfig).',
        '--force'    => 'Force overwrite existing file.',
    ];

    /**
    * Actually execute a command.
    */
    public function run(array $params)
    {
        // Add by me
        $this->setEnabledSuffixing(true);
       
        $this->component = 'Config';
        $this->directory = 'Config';
        $this->template  = 'config.tpl.php';

        $this->classNameLang = 'CLI.generator.className.config';
        $this->execute($params);
    }

    /**
    * Prepare options and do the necessary replacements.
    */
    protected function prepare(string $class): string
    {
        $namespace = $this->getOption('namespace') ?? APP_NAMESPACE;

        if ($namespace === APP_NAMESPACE) {
            $class = substr($class, strlen($namespace . '\\'));
        }

        return $this->parseTemplate($class);

    }
}

CodeIgniter4\tests\system\Commands\ConfigGeneratorTest.php

Code:
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use CodeIgniter\CLI\GeneratorTrait;

/**
* @internal
*/
final class ConfigGeneratorTest extends CIUnitTestCase
{
    use GeneratorTrait;
    protected $streamFilter;

    protected function setUp(): void
    {
        CITestStreamFilter::$buffer = '';

        $this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
        $this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');
    }

    protected function tearDown(): void
    {
        stream_filter_remove($this->streamFilter);

        $result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', CITestStreamFilter::$buffer);
        $file  = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
        if (is_file($file)) {
            unlink($file);
        }
    }

    // public function testGenerateConfig()
    // {
    //    command('make:config auth');
    //    $this->assertFileExists(APPPATH . 'Config/Auth.php');
    // }

    public function testGenerateConfigWithOptionSuffix()
    {
        command('make:config auth -suffix');
        $this->assertFileExists(APPPATH . 'Config/AuthConfig.php');
    }
    // add by me
    public function testGenerateConfigWithSuffixIsConfig()
    {
        $this->setEnabledSuffixing(true);
        $this->assertTrue($this->enabledSuffixing);

        $this->setEnabledSuffixing(false);
        $this->assertFalse($this->enabledSuffixing);

        command('make:config MyExample');
        $this->assertFileExists(APPPATH . 'Config/MyExampleConfig.php');
    }
}

In this case, this test will be fielded.

if change line https://github.com/codeigniter4/CodeIgni...it.php#L76 to private $enabledSuffixing = false;
and line https://github.com/codeigniter4/CodeIgni...t.php#L211 to
if (($this->enabledSuffixing || $this->getOption('suffix')) && ! strripos($class, $component)) {
In this case, this test will be OK.

That the user can easily change the value of $enabledSuffixing in command class.
Do you have a specific reason for not changing it?
Reply
#6

(This post was last modified: 05-17-2022, 09:19 AM by paulbalandan.)

That one was added by me so I would answer.

PHP Code:
/**
   * Whether the `--suffix` option has any effect.
   *
   * @internal
   *
   * @var bool
   */
private $enabledSuffixing true

As you can see, the $enabledSuffixing flag controls whether or not the option flag --suffix makes any effect when passed to the spark call. This flag is enabled by default. Why I did this? Because of the original refactor of the Generator, no matter what I do the component suffix is always attaching to the name of the output. So I need a way that developers could control whether the `--suffix` flag has any effect.

Changing the logic to your proposal would be a deviation from the original intent of $enabledSuffixing. That means to say, that line is not the right line to solve your use case.

I propose this.

1. Add a protected method to the GeneratorTrait:
PHP Code:
protected function getSuffixedName(string $classstring $component): string
{
        if (
$this->enabledSuffixing && $this->getOption('suffix') && ! strripos($class$component)) {
                
$class .= ucfirst($component);
        }

        return 
$class;


2. These lines (https://github.com/codeigniter4/CodeIgni...#L211-L214) get changed to:
PHP Code:
$class $this->getSuffixedName($class$component); 

3. In your own generator class, override the `getSuffixedName()` method:
PHP Code:
protected function getSuffixedName(string $classstring $component): string
{
        if (! 
strripos($class$component)) {
                
$class .= ucfirst($component);
        }

        return 
$class;

Reply
#7

Thanks @paulbalandan , the explanation was complete and clear. This works well.
@kenjis  Thank you too.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB