Welcome Guest, Not a member yet? Register   Sign In
How can I activate Suffix without using --suffix?
#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


Messages In This Thread
RE: How can I activate Suffix without using --suffix? - by datamweb - 05-11-2022, 08:58 PM



Theme © iAndrew 2016 - Forum software by © MyBB