Skip to content

Commit

Permalink
テーマをコピーした際にヘルパーの重複エラーが表示される fix #3978
Browse files Browse the repository at this point in the history
  • Loading branch information
IwasakiRyuichi authored Jan 8, 2025
1 parent ae10652 commit 4ccca5c
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions plugins/baser-core/src/Service/ThemesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use Laminas\Diactoros\UploadedFile;
use BaserCore\Utility\BcFile;

/**
* ThemesService
Expand Down Expand Up @@ -362,6 +363,47 @@ public function loadDefaultDataPattern(string $currentTheme, string $dbDataPatte
return $result;
}

/**
* ヘルパーのnamespaceを変更する
*/
public function changeHelperNameSpace($newTheme)
{
$pluginPath = BcUtil::getPluginPath($newTheme);
if (!$pluginPath) return false;
if (file_exists($pluginPath . 'src' . DS .'View' . DS .'Helper'. DS . $newTheme. 'Helper.php')) {
$helperClassPath = $pluginPath . 'src' . DS .'View' . DS .'Helper'. DS . $newTheme. 'Helper.php';
}else{
return false;
}
$file = new BcFile($helperClassPath);
$data = $file->read();
$file->write(preg_replace('/namespace .+?;/', 'namespace ' . $newTheme . '\View\Helper;', $data));
return true;
}

/**
* ヘルパーのクラス名を変更する
*/
public function ChangeHelperClassName($oldTheme,$newTheme)
{
$pluginPath = BcUtil::getPluginPath($newTheme);
if (!$pluginPath) return false;
$oldPath = $pluginPath . 'src'. DS .'View' . DS .'Helper' . DS . $oldTheme . 'Helper.php';
$newPath = $pluginPath . 'src'. DS .'View' . DS .'Helper' . DS . $newTheme . 'Helper.php';
if(!file_exists($newPath))
{
if(file_exists($oldPath)) {
rename($oldPath, $newPath);
} else {
return false;
}
}
$file = new BcFile($newPath);
$data = $file->read();
$file->write(preg_replace('/class\s+.*?Helper/', 'class ' . $newTheme . 'Helper', $data));
return true;
}

/**
* コピーする
*
Expand Down Expand Up @@ -399,6 +441,8 @@ public function copy(string $theme): bool
}
BcUtil::changePluginClassName($oldTheme, $newTheme);
BcUtil::changePluginNameSpace($newTheme);
$this->ChangeHelperClassName($oldTheme,$newTheme);
$this->changeHelperNameSpace($newTheme);
return true;
}

Expand Down

0 comments on commit 4ccca5c

Please sign in to comment.