From 4ccca5cc8bd8c8c41cb6c594c11c2624484e04b4 Mon Sep 17 00:00:00 2001 From: IwasakiRyuichi Date: Wed, 8 Jan 2025 11:35:24 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=83=86=E3=83=BC=E3=83=9E=E3=82=92?= =?UTF-8?q?=E3=82=B3=E3=83=94=E3=83=BC=E3=81=97=E3=81=9F=E9=9A=9B=E3=81=AB?= =?UTF-8?q?=E3=83=98=E3=83=AB=E3=83=91=E3=83=BC=E3=81=AE=E9=87=8D=E8=A4=87?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=8C=E8=A1=A8=E7=A4=BA=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=82=8B=20fix=20#3978?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baser-core/src/Service/ThemesService.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/plugins/baser-core/src/Service/ThemesService.php b/plugins/baser-core/src/Service/ThemesService.php index e60a5eaeb8..734e123179 100644 --- a/plugins/baser-core/src/Service/ThemesService.php +++ b/plugins/baser-core/src/Service/ThemesService.php @@ -32,6 +32,7 @@ use Cake\Routing\Router; use Cake\Utility\Inflector; use Laminas\Diactoros\UploadedFile; +use BaserCore\Utility\BcFile; /** * ThemesService @@ -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; + } + /** * コピーする * @@ -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; } From a8bd016b966ffd9044f3024983178bbf775f842f Mon Sep 17 00:00:00 2001 From: IwasakiRyuichi Date: Fri, 10 Jan 2025 13:35:59 +0900 Subject: [PATCH 2/2] fix #3978 (#4110) --- .../baser-core/src/Service/ThemesService.php | 50 ++++++++----------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/plugins/baser-core/src/Service/ThemesService.php b/plugins/baser-core/src/Service/ThemesService.php index 734e123179..07b3798182 100644 --- a/plugins/baser-core/src/Service/ThemesService.php +++ b/plugins/baser-core/src/Service/ThemesService.php @@ -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)) { @@ -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; } /** @@ -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; }