Skip to content
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

#3978 #4110

Merged
merged 8 commits into from
Jan 10, 2025
50 changes: 22 additions & 28 deletions plugins/baser-core/src/Service/ThemesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,34 +362,19 @@ public function loadDefaultDataPattern(string $currentTheme, string $dbDataPatte

return $result;
}

/**
* ヘルパーのnamespaceを変更する
* helperとnamespaceを変更する
* @checked
* @notodo
*/
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)
public function changeHelper(string $newTheme, string $className): bool
{
$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';
$helperClassName = preg_replace('/Helper$/', '', $className) . 'CopyHelper';
$oldPath = $pluginPath . 'src'. DS .'View' . DS .'Helper' . DS . $className . '.php';
$newPath = $pluginPath . 'src'. DS .'View' . DS .'Helper' . DS . $helperClassName . '.php';
if(!file_exists($newPath))
{
if(file_exists($oldPath)) {
Expand All @@ -398,10 +383,12 @@ public function ChangeHelperClassName($oldTheme,$newTheme)
return false;
}
}
$file = new BcFile($newPath);
$data = $file->read();
$file->write(preg_replace('/class\s+.*?Helper/', 'class ' . $newTheme . 'Helper', $data));
return true;
$file = new BcFile($newPath);
$data = $file->read();
$tmpHelperNameSpace = preg_replace('/namespace .+?;/', 'namespace ' . $newTheme . '\View\Helper;', $data);
$helperNameSpace = preg_replace('/class\s+.*?Helper/', 'class ' . $helperClassName , $tmpHelperNameSpace);
$file->write($helperNameSpace);
return true;
}

/**
Expand Down Expand Up @@ -441,8 +428,15 @@ public function copy(string $theme): bool
}
BcUtil::changePluginClassName($oldTheme, $newTheme);
BcUtil::changePluginNameSpace($newTheme);
$this->ChangeHelperClassName($oldTheme,$newTheme);
$this->changeHelperNameSpace($newTheme);

$folder = new BcFolder(BASER_THEMES . $newTheme . DS . 'src' . DS . 'View' . DS . 'Helper');
$files = $folder->getFiles();

foreach($files as $file)
{
$className = basename($file, '.php');
$this->changeHelper($newTheme,$className);
}
return true;
}

Expand Down