S
S
Stanislav Bareisha2020-05-07 21:33:37
PHP
Stanislav Bareisha, 2020-05-07 21:33:37

Dynamic namespace in Yii2, implementation?

Hello everyone, there is a project on Yii2 with the structure:

Modules
---- Company 1
---- Company 2 The

question is in the namespace, namely: If a company is connected, I clone Company 1 and create Company 3
but it still remains in the namespace How is it possible to dynamically change the namespace to ? Does anyone have other solutions to this problem.... namespace com\modules\Компания 1;
namespace com\modules\Компания 3;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Bareisha, 2020-05-08
@t1h_on

I solved the problem like this: There is a main module and we will clone it, I collected code for these purposes from the Internet:

public function actionCopy(
        $basePath = '/OSPanel/domains/КаталогДомена/company/modules/',
        $source = 'Компания 1',
        $dest = 'Компания 2',
        $overwrite = true,
        $sourcestr = 'Компания 1',
        $deststr = 'Компания 2'
    )
    {
        //Давайте просто убедимся, что наша новая папка уже создана.
        if (!is_dir($basePath . $dest))
            mkdir($basePath . $dest);
        if ($handle = opendir($basePath . $source)) {     
            while (false !== ($file = readdir($handle))) { 
                if ($file != '.' && $file != '..') {
                    $path = $source . '/' . $file;
                    $pathNew = $basePath . $dest . '/' . $file;

                    if (is_file($basePath . $path)) {

                        if (!is_file($basePath . $dest . '/' . $file) || $overwrite) {
                            if ([email protected]($basePath . $path, $basePath . $dest . '/' . $file)) {
                                echo '<font color="red">File (' . $path . ') could not be copied, likely a permissions problem.</font>';
                            }
                            $this->strFile($sourcestr, $deststr, $pathNew);
                        }
                    } elseif (is_dir($basePath . $path)) {
                        if (!is_dir($basePath . $dest . '/' . $file))
                            mkdir($basePath . $dest . '/' . $file); // создать подкаталог перед копированием подкаталога

                        $this->actionCopy($basePath, $path, $dest . '/' . $file, $overwrite, $sourcestr, $deststr); //рекурсия!
                    }
                }
            }
            closedir($handle);
        }
    }
    
    public function strFile($oldstr, $newstr, $yourfile)
    {
        $file = file($yourfile);
        echo 'Открыли файл ' . $yourfile.' ';

        if (is_array($file)) {
            foreach ($file as $key => $value) {
               $file[$key] = preg_replace('/'.$oldstr.'/', $newstr, $value);
            }
        } else {
            exit ("Ошибка");
        }
        $fp = fopen($yourfile, "w+"); // перезаписываем независимо от длины новой строки
        fwrite($fp, implode("", $file));
        fclose($fp);
    }

Company 1 is copied to Company 2 and the file that is copied is checked for Company 1 and Replaced with Company 2
1. If anyone has a code to copy the database
2. Connecting the module to a file (not manually)
I will be glad for the code))

I
Ivan Ivanov, 2020-05-07
@maksim_fix

Modern IDEs have a replace feature, just select the desired directory and replace Company 1 with Company 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question