From c47aada8c60bcd58809c90118d4b2cd9e67199ad Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Tue, 10 Oct 2023 16:27:03 +0900 Subject: [PATCH 1/7] BcPlugin --- plugins/baser-core/src/BcPlugin.php | 22 +-- plugins/baser-core/src/Utility/BcFolder.php | 29 ++++ .../tests/TestCase/BcPluginTest.php | 127 ++++++++++-------- 3 files changed, 108 insertions(+), 70 deletions(-) diff --git a/plugins/baser-core/src/BcPlugin.php b/plugins/baser-core/src/BcPlugin.php index 5cc02b6d3c..94cb9be9eb 100644 --- a/plugins/baser-core/src/BcPlugin.php +++ b/plugins/baser-core/src/BcPlugin.php @@ -18,6 +18,7 @@ use BaserCore\Service\PermissionGroupsService; use BaserCore\Service\PermissionGroupsServiceInterface; use BaserCore\Utility\BcContainerTrait; +use BaserCore\Utility\BcFolder; use BaserCore\Utility\BcUpdateLog; use BaserCore\Utility\BcUtil; use Cake\Core\BasePlugin; @@ -25,7 +26,6 @@ use Cake\Core\Configure\Engine\PhpConfig; use Cake\Core\PluginApplicationInterface; use Cake\Datasource\ConnectionManager; -use Cake\Filesystem\Folder; use Cake\Http\ServerRequestFactory; use Cake\I18n\FrozenTime; use Cake\Log\LogTrait; @@ -241,12 +241,12 @@ public function getUpdaters($name = '') // 有効化されていない可能性があるため CakePlugin::path() は利用しない $path = BcUtil::getPluginPath($name) . 'config' . DS . 'update'; - $folder = new Folder($path); - $files = $folder->read(true, true); + $folder = new BcFolder($path); + $files = $folder->getFolders(); $updaters = []; $updateVerPoints = []; - if (!empty($files[0])) { - foreach($files[0] as $folder) { + if (!empty($files)) { + foreach($files as $folder) { $updateVersion = $folder; $updateVerPoints[$updateVersion] = BcUtil::verpoint($updateVersion); } @@ -289,12 +289,12 @@ public function getUpdateScriptMessages($name = '') // 有効化されていない可能性があるため CakePlugin::path() は利用しない $path = BcUtil::getPluginPath($name) . 'config' . DS . 'update'; - $folder = new Folder($path); - $files = $folder->read(true, true); + $folder = new BcFolder($path); + $files = $folder->getFolders(); $messages = []; $updateVerPoints = []; - if (!empty($files[0])) { - foreach($files[0] as $folder) { + if (!empty($files)) { + foreach($files as $folder) { $updateVersion = $folder; $updateVerPoints[$updateVersion] = BcUtil::verpoint($updateVersion); } @@ -336,8 +336,8 @@ public function uninstall($options = []): bool $pluginPath = BcUtil::getPluginPath($pluginName); if ($pluginPath) { - $Folder = new Folder(); - $Folder->delete($pluginPath); + $Folder = new BcFolder($pluginPath); + $Folder->delete(); } /** @var PermissionGroupsService $permissionGroupsService */ $permissionGroupsService = $this->getService(PermissionGroupsServiceInterface::class); diff --git a/plugins/baser-core/src/Utility/BcFolder.php b/plugins/baser-core/src/Utility/BcFolder.php index 8f38aac69b..fc8d13cae3 100644 --- a/plugins/baser-core/src/Utility/BcFolder.php +++ b/plugins/baser-core/src/Utility/BcFolder.php @@ -155,4 +155,33 @@ public function delete() return true; } + public function copy($source, $dest): bool + { + if (!is_dir($source)) return false; + if(is_dir($source)) { + $dir_handle=opendir($source); + $sourceFolder = basename($source); + mkdir($dest."/".$sourceFolder); + while($file=readdir($dir_handle)){ + if($file!="." && $file!=".."){ + if(is_dir($source."/".$file)){ + self::copy($source."/".$file, $dest."/".$sourceFolder); + } else { + copy($source."/".$file, $dest."/".$file); + } + } + } + closedir($dir_handle); + } else { + copy($source, $dest); + } + return true; + } + + public function move($source, $dest): bool + { + if (!is_dir($source)) return false; + return $this->copy($source, $dest) && $this->delete(); + } + } diff --git a/plugins/baser-core/tests/TestCase/BcPluginTest.php b/plugins/baser-core/tests/TestCase/BcPluginTest.php index eb25c20c1d..3585206db4 100644 --- a/plugins/baser-core/tests/TestCase/BcPluginTest.php +++ b/plugins/baser-core/tests/TestCase/BcPluginTest.php @@ -17,11 +17,11 @@ use BaserCore\Test\Factory\PluginFactory; use BaserCore\Test\Factory\UserFactory; use BaserCore\TestSuite\BcTestCase; +use BaserCore\Utility\BcFile; +use BaserCore\Utility\BcFolder; use BaserCore\Utility\BcUtil; use Cake\Core\Plugin; use Cake\Datasource\ConnectionManager; -use Cake\Filesystem\File; -use Cake\Filesystem\Folder; use Cake\ORM\TableRegistry; use Cake\Routing\Router; use Cake\TestSuite\IntegrationTestTrait; @@ -103,22 +103,15 @@ public function testInstallAndUninstall() // アンインストール $from = BcUtil::getPluginPath('BcBlog'); $pluginDir = dirname($from); - $folder = new Folder(); + $folder = new BcFolder($from); $to = $pluginDir . DS . 'BcBlogBak'; - $folder->copy($to, [ - 'from' => $from, - 'mode' => 0777 - ]); - $folder->create($from, 0777); + $folder->copy($from, $to); + $folder->create(); $this->BcPlugin->uninstall(['connection' => 'test']); $this->assertFalse(is_dir($from)); $plugins = $this->getTableLocator()->get('BaserCore.Plugins')->find()->where(['name' => 'BcBlog'])->first(); $this->assertNull($plugins); - $folder->move($from, [ - 'from' => $to, - 'mode' => 0777, - 'schema' => Folder::OVERWRITE - ]); + $folder->move($from, $to); $this->BcPlugin->install(['connection' => 'test']); } @@ -207,39 +200,41 @@ public function test_getUpdateScriptMessagesAndGetUpdaters() $pluginPath = ROOT . DS . 'plugins' . DS . $name . DS; $updatePath = $pluginPath . 'config' . DS . 'update' . DS; PluginFactory::make(['name' => $name, 'title' => 'サンプル', 'version' => '1.0.0'])->persist(); - $folder = new Folder(); + $folder = new BcFolder($pluginPath); // 新バージョン - $folder->create($pluginPath); - $file = new File($pluginPath . 'VERSION.txt'); + $folder->create(); + $file = new BcFile($pluginPath . 'VERSION.txt'); + $file->create(); $file->write('1.0.3'); - $file->close(); // アップデートスクリプト 0.0.1 - $folder->create($updatePath . '0.0.1'); - $file = new File($updatePath . '0.0.1' . DS . 'config.php'); + $folder = new BcFolder($updatePath . '0.0.1'); + $folder->create(); + $file = new BcFile($updatePath . '0.0.1' . DS . 'config.php'); + $file->create(); $file->write(' \'test0\'];'); - $file->close(); // アップデートスクリプト 1.0.1 - $folder->create($updatePath . '1.0.1'); - $file = new File($updatePath . '1.0.1' . DS . 'config.php'); + $folder = new BcFolder($updatePath . '1.0.1'); + $folder->create(); + $file = new BcFile($updatePath . '1.0.1' . DS . 'config.php'); + $file->create(); $file->write(' \'test1\'];'); - $file->close(); - $file = new File($updatePath . '1.0.1' . DS . 'updater.php'); + $file = new BcFile($updatePath . '1.0.1' . DS . 'updater.php'); $file->create(); - $file->close(); // アップデートスクリプト 1.0.2 - $folder->create($updatePath . '1.0.2'); - $file = new File($updatePath . '1.0.2' . DS . 'config.php'); + $folder = new BcFolder($updatePath . '1.0.2'); + $folder->create(); + $file = new BcFile($updatePath . '1.0.2' . DS . 'config.php'); + $file->create(); $file->write(' \'test2\'];'); - $file->close(); - $file = new File($updatePath . '1.0.2' . DS . 'updater.php'); + $file = new BcFile($updatePath . '1.0.2' . DS . 'updater.php'); $file->create(); - $file->close(); // アップデートスクリプト 1.0.4 - $folder->create($updatePath . '1.0.4'); - $file = new File($updatePath . '1.0.4' . DS . 'config.php'); + $folder = new BcFolder($updatePath . '1.0.4'); + $folder->create(); + $file = new BcFile($updatePath . '1.0.4' . DS . 'config.php'); + $file->create(); $file->write(' \'test3\'];'); - $file->close(); $this->assertEquals( ['Sample-1.0.1' => 'test1', 'Sample-1.0.2' => 'test2'], @@ -249,7 +244,8 @@ public function test_getUpdateScriptMessagesAndGetUpdaters() ['Sample-1.0.1' => 1000001000, 'Sample-1.0.2' => 1000002000], $this->BcPlugin->getUpdaters($name) ); - $folder->delete($pluginPath); + $folder = new BcFolder($pluginPath); + $folder->delete(); } /** @@ -268,27 +264,27 @@ public function test_execScript() $this->assertTrue($this->BcPlugin->execScript($version)); // 有効スクリプトあり UserFactory::make(['id' => 1, 'name' => 'test'])->persist(); - $folder = new Folder(); - $folder->create($versionPath); - $file = new File($versionPath . DS . 'updater.php'); + $folder = new BcFolder($versionPath); + $folder->create(); + $file = new BcFile($versionPath . DS . 'updater.php'); + $file->create(); $file->write('get(\'BaserCore.Users\'); $user = $users->find()->where([\'id\' => 1])->first(); $user->name = \'hoge\'; $users->save($user);'); - $file->close(); $this->BcPlugin->execScript($version); $users = $this->getTableLocator()->get('BaserCore.Users'); $user = $users->find()->where(['id' => 1])->first(); $this->assertEquals('hoge', $user->name); // 無効スクリプトあり - $file = new File($versionPath . DS . 'updater.php'); + $file = new BcFile($versionPath . DS . 'updater.php'); + $file->create(); $file->write('log(\'test\');'); - $file->close(); $this->BcPlugin->execScript($version); - $file = new File(LOGS . 'cli-error.log'); + $file = new BcFile(LOGS . 'cli-error.log'); $log = $file->read(); $this->assertStringContainsString('test', $log); // 初期化 @@ -311,17 +307,20 @@ public function test_createAssetsSymlink() public function test_migrate() { $pluginPath = ROOT . DS . 'plugins' . DS . 'BcTest' . DS; - $folder = new Folder(); + $folder = new BcFolder($pluginPath); // プラグインフォルダを初期化 - $folder->delete($pluginPath); + $folder->delete(); $configPath = $pluginPath . 'config' . DS; $migrationPath = $configPath . 'Migrations' . DS; $seedPath = $configPath . 'Seeds' . DS; $srcPath = $pluginPath . 'src' . DS; - $folder->create($srcPath); - $folder->create($migrationPath); - $folder->create($seedPath); + $folder = new BcFolder($srcPath); + $folder->create(); + $folder = new BcFolder($migrationPath); + $folder->create(); + $folder = new BcFolder($seedPath); + $folder->create(); // VERSION.txt $this->createVersionFile($pluginPath, '0.0.1'); @@ -364,8 +363,9 @@ public function test_migrate() */ public function createPluginFile($srcPath) { - $file = new File($srcPath . 'Plugin.php'); - $file->write('create(); + $file->write('create(); $file->write('create(); $file->write('create(); $file->write($version); } @@ -440,7 +443,8 @@ public function createVersionFile($pluginPath, $version) */ public function createUpdater($updaterPath) { - $file = new File($updaterPath . 'updater.php', 'w'); + $file = new BcFile($updaterPath . 'updater.php', 'w'); + $file->create(); $file->write('delete($pluginPath); + $folder->delete(); $configPath = $pluginPath . 'config' . DS; $migrationPath = $configPath . 'Migrations' . DS; $seedPath = $configPath . 'Seeds' . DS; $srcPath = $pluginPath . 'src' . DS; - $folder->create($srcPath); - $folder->create($migrationPath); - $folder->create($seedPath); + $folder = new BcFolder($srcPath); + $folder->create(); + $folder = new BcFolder($migrationPath); + $folder->create(); + $folder = new BcFolder($seedPath); + $folder->create(); // VERSION.txt $this->createVersionFile($pluginPath, '0.0.1'); @@ -484,7 +491,8 @@ public function test_execUpdater() // config/update/0.0.2/updater.php $updaterPath = $configPath . 'update' . DS . '0.0.2' . DS; - $folder->create($updaterPath); + $folder = new BcFolder($updaterPath); + $folder->create(); $this->createUpdater($updaterPath); // アップデート実行 @@ -494,7 +502,8 @@ public function test_execUpdater() $this->assertEquals('2022-06-26', (string) $entity->name); // 初期化 - $folder->delete($pluginPath); + $folder = new BcFolder($pluginPath); + $folder->delete(); $this->dropTable('bc_test'); $this->dropTable('bc_test_phinxlog'); } From 4b104f14e175837aba0572f68285ca67ec5e6824 Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Tue, 10 Oct 2023 16:49:58 +0900 Subject: [PATCH 2/7] fix bug --- plugins/baser-core/src/Utility/BcFile.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/plugins/baser-core/src/Utility/BcFile.php b/plugins/baser-core/src/Utility/BcFile.php index 0b1913a920..d437c9b4a5 100644 --- a/plugins/baser-core/src/Utility/BcFile.php +++ b/plugins/baser-core/src/Utility/BcFile.php @@ -101,7 +101,6 @@ public function write($data) return (bool) file_put_contents($this->path, $data); } -<<<<<<< HEAD /** * ファイルを削除 * @return bool @@ -113,6 +112,4 @@ public function delete() } return unlink($this->path); } -======= ->>>>>>> dev-5 } From e3f10e944fad080827bf78a85fa7183090a4ed22 Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Tue, 10 Oct 2023 16:49:58 +0900 Subject: [PATCH 3/7] fix bug --- plugins/baser-core/src/Utility/BcFile.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/plugins/baser-core/src/Utility/BcFile.php b/plugins/baser-core/src/Utility/BcFile.php index 0b1913a920..d437c9b4a5 100644 --- a/plugins/baser-core/src/Utility/BcFile.php +++ b/plugins/baser-core/src/Utility/BcFile.php @@ -101,7 +101,6 @@ public function write($data) return (bool) file_put_contents($this->path, $data); } -<<<<<<< HEAD /** * ファイルを削除 * @return bool @@ -113,6 +112,4 @@ public function delete() } return unlink($this->path); } -======= ->>>>>>> dev-5 } From 13c25dfa1b3804a973810481ec930c1d3ee7640f Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Mon, 16 Oct 2023 10:35:10 +0900 Subject: [PATCH 4/7] add comment --- plugins/baser-core/src/Utility/BcFolder.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/baser-core/src/Utility/BcFolder.php b/plugins/baser-core/src/Utility/BcFolder.php index fc8d13cae3..b10d09503e 100644 --- a/plugins/baser-core/src/Utility/BcFolder.php +++ b/plugins/baser-core/src/Utility/BcFolder.php @@ -154,7 +154,12 @@ public function delete() rmdir($this->path); return true; } - + /** + * ディレクトリをコピーする + * @checked + * @noTodo + * @unitTest + */ public function copy($source, $dest): bool { if (!is_dir($source)) return false; @@ -177,6 +182,12 @@ public function copy($source, $dest): bool } return true; } + /** + * ディレクトリを移動する + * @checked + * @noTodo + * @unitTest + */ public function move($source, $dest): bool { From 03d9880722f6a4d49f4d4adcfafe807e223f3e67 Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Mon, 16 Oct 2023 14:20:50 +0900 Subject: [PATCH 5/7] copy folder test --- .../tests/TestCase/Utility/BcFolderTest.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php b/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php index b363087a0b..d84446d16c 100644 --- a/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php +++ b/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php @@ -126,6 +126,36 @@ public function test_tree() $folder->delete(); } + /** + * test copy + */ + public function test_copy() + { + $path = TMP_TESTS . 'test'; + $folder = new BcFolder($path); + $folder->create(); + $file = new BcFile($path. DS. 'test.txt'); + $file->create(); + $des = TMP_TESTS . 'test_des'; + $result = $folder->copy($path, $des); + $this->assertTrue($result); + } + + /** + * test move + */ + public function test_move() + { + // + + //ns + + //ُns + + + } + + } From d851f44214d8c83d45d2ebf56edafcc0fe87d8b9 Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Tue, 17 Oct 2023 21:27:21 +0900 Subject: [PATCH 6/7] add unit test --- .../tests/TestCase/Utility/BcFolderTest.php | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php b/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php index d84446d16c..6853bc4fd2 100644 --- a/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php +++ b/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php @@ -132,13 +132,18 @@ public function test_tree() public function test_copy() { $path = TMP_TESTS . 'test'; - $folder = new BcFolder($path); - $folder->create(); + $folder1 = new BcFolder($path); + $folder1->create(); $file = new BcFile($path. DS. 'test.txt'); $file->create(); $des = TMP_TESTS . 'test_des'; - $result = $folder->copy($path, $des); + $folder2 = new BcFolder($des); + $folder2->create(); + $result = $folder2->copy($path, $des); $this->assertTrue($result); + $this->assertFileExists($des. DS. 'test.txt'); + $folder1->delete(); + $folder2->delete(); } /** @@ -146,12 +151,19 @@ public function test_copy() */ public function test_move() { - // - - //ns - - //ُns - + $path = TMP_TESTS . 'test'; + $folder1 = new BcFolder($path); + $folder1->create(); + $file = new BcFile($path. DS. 'test.txt'); + $file->create(); + $des = TMP_TESTS . 'test_des'; + $folder2 = new BcFolder($des); + $folder2->create(); + $result = $folder1->move($path, $des); + $this->assertTrue($result); + $this->assertFileDoesNotExist($path. DS. 'test.txt'); + $this->assertFileExists($des. DS. 'test.txt'); + $folder2->delete(); } From dea3b9d46f30e56e7ba1cbef4aeefbf9747f0d3e Mon Sep 17 00:00:00 2001 From: ryuring Date: Thu, 19 Oct 2023 17:02:34 +0900 Subject: [PATCH 7/7] Update BcFolder.php --- plugins/baser-core/src/Utility/BcFolder.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/baser-core/src/Utility/BcFolder.php b/plugins/baser-core/src/Utility/BcFolder.php index 461096836e..f4be9937e8 100644 --- a/plugins/baser-core/src/Utility/BcFolder.php +++ b/plugins/baser-core/src/Utility/BcFolder.php @@ -173,6 +173,7 @@ public function delete() rmdir($this->path); return true; } + /** * ディレクトリをコピーする * @checked @@ -201,13 +202,13 @@ public function copy($source, $dest): bool } return true; } + /** * ディレクトリを移動する * @checked * @noTodo * @unitTest */ - public function move($source, $dest): bool { if (!is_dir($source)) return false;