Skip to content

Commit

Permalink
Merge branch 'dev-#2773' of github.com:baserproject/basercms into dev-#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Oct 27, 2023
2 parents 6edd143 + bf04638 commit 34e6004
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 87 deletions.
53 changes: 25 additions & 28 deletions plugins/baser-core/src/Utility/BcUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
use Cake\Event\EventManagerInterface;
use Cake\Http\ServerRequest;
use Cake\Routing\Router;
use Cake\Filesystem\File;
use Cake\Filesystem\Folder;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use Cake\Database\Exception;
Expand Down Expand Up @@ -282,7 +280,7 @@ public static function getVersion($plugin = '')
return false;
}
}
$versionFile = new File($path);
$versionFile = new BcFile($path);
$versionData = $versionFile->read();
$aryVersionData = explode("\n", $versionData);
if (!empty($aryVersionData[0])) {
Expand Down Expand Up @@ -637,10 +635,10 @@ public static function getThemesPlugins($theme)
{
$path = BcUtil::getPluginPath($theme) . 'plugins';
if (!file_exists($path)) return [];
$Folder = new Folder($path);
$files = $Folder->read(true, true, false);
if (!empty($files[0])) {
return $files[0];
$Folder = new BcFolder($path);
$files = $Folder->getFolders();
if (!empty($files)) {
return $files;
}
return [];
}
Expand Down Expand Up @@ -781,8 +779,8 @@ public static function getTemplateList($path, $plugins)
self::getTemplatePath(Inflector::camelize(Configure::read('BcApp.coreAdminTheme'), '-')) . 'plugin' . DS . $plugin . DS
];
foreach($templatePaths as $templatePath) {
$folder = new Folder($templatePath . $path . DS);
$files = $folder->read(true, true)[1];
$folder = new BcFolder($templatePath . $path . DS);
$files = $folder->getFiles();
if ($files) {
$templates = array_merge($templates, $files);
}
Expand Down Expand Up @@ -861,12 +859,12 @@ public static function getAllThemeList()
$paths = [ROOT . DS . 'plugins'];
$themes = [];
foreach($paths as $path) {
$folder = new Folder($path);
$files = $folder->read(true);
if (!$files[0]) {
$Folder = new BcFolder($path);
$folders = $Folder->getFolders();
if (!$folders) {
continue;
}
foreach($files[0] as $name) {
foreach($folders as $name) {
$appConfigPath = BcUtil::getPluginPath($name) . 'config.php';
if ($name === '_notes' || !file_exists($appConfigPath)) {
continue;
Expand Down Expand Up @@ -1499,20 +1497,21 @@ public static function addSessionId($url, $force = false)
public static function emptyFolder($path)
{
$result = true;
$Folder = new Folder($path);
$files = $Folder->read(true, true, true);
if (is_array($files[1])) {
foreach($files[1] as $file) {
$Folder = new BcFolder($path);
$files = $Folder->getFiles(['full'=>true]);
if (is_array($files)) {
foreach($files as $file) {
if ($file != 'empty') {
if (!@unlink($file)) {
$result = false;
}
}
}
}
if (is_array($files[0])) {
foreach($files[0] as $file) {
if (!BcUtil::emptyFolder($file)) {
$folders = $Folder->getFolders(['full'=>true]);
if (is_array($folders)) {
foreach($folders as $folder) {
if (!BcUtil::emptyFolder($folder)) {
$result = false;
}
}
Expand Down Expand Up @@ -1689,12 +1688,11 @@ public static function checkTmpFolders()
if (!is_writable(TMP)) {
return;
}
$folder = new Folder();
$folder->create(TMP . 'sessions', 0777);
$folder->create(CACHE, 0777);
$folder->create(CACHE . 'models', 0777);
$folder->create(CACHE . 'persistent', 0777);
$folder->create(CACHE . 'environment', 0777);
(new BcFolder(TMP . 'sessions'))->create();
(new BcFolder(CACHE))->create();
(new BcFolder(CACHE . 'models'))->create();
(new BcFolder(CACHE . 'persistent'))->create();
(new BcFolder(CACHE . 'environment'))->create();
}

/**
Expand All @@ -1709,10 +1707,9 @@ public static function changePluginNameSpace($newPlugin)
{
$pluginPath = BcUtil::getPluginPath($newPlugin);
if (!$pluginPath) return false;
$file = new File($pluginPath . 'src' . DS . 'Plugin.php');
$file = new BcFile($pluginPath . 'src' . DS . 'Plugin.php');
$data = $file->read();
$file->write(preg_replace('/namespace .+?;/', 'namespace ' . $newPlugin . ';', $data));
$file->close();
return true;
}

Expand Down
5 changes: 2 additions & 3 deletions plugins/baser-core/src/Utility/BcZip.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace BaserCore\Utility;

use Cake\Filesystem\Folder;
use ZipArchive;
use BaserCore\Annotation\NoTodo;
use BaserCore\Annotation\Checked;
Expand Down Expand Up @@ -75,8 +74,8 @@ public function extract($source, $target)
}
if ($result) {
$extractedPath = $target . $this->topArchiveName;
$Folder = new Folder();
$Folder->chmod($extractedPath, 0777);
$Folder = new BcFolder($extractedPath);
$Folder->chmod( 0777);
$this->Zip->close();
return true;
} else {
Expand Down
89 changes: 40 additions & 49 deletions plugins/baser-core/tests/TestCase/Utility/BcUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
use BaserCore\Test\Factory\UserFactory;
use BaserCore\Test\Factory\UserGroupFactory;
use BaserCore\Test\Factory\UsersUserGroupFactory;
use BaserCore\Utility\BcFile;
use BaserCore\Utility\BcFolder;
use Cake\Core\App;
use Cake\Cache\Cache;
use Cake\Core\Plugin;
use Cake\Core\Configure;
use Cake\Event\EventManager;
use Cake\Filesystem\File;
use Cake\Filesystem\Folder;
use BaserCore\Utility\BcUtil;
use BaserCore\TestSuite\BcTestCase;
use Cake\Http\Session;
Expand Down Expand Up @@ -193,25 +193,26 @@ public function isInstallModeDataProvider()
public function testGetVersion(): void
{
// BaserCore
$file = new File(BASER . DS . 'VERSION.txt');
$file = new BcFile(BASER . DS . 'VERSION.txt');
$expected = preg_replace('/^(.+?)\n.+$/s', "$1", $file->read());
$result = BcUtil::getVersion();
$this->assertEquals($expected, $result);

// プラグイン
$file = new File(Plugin::path('bc-admin-third') . DS . 'VERSION.txt');
$file = new BcFile(Plugin::path('bc-admin-third') . DS . 'VERSION.txt');
$expected = preg_replace('/^(.+?)\n.*$/s', "$1", $file->read());
$result = BcUtil::getVersion('BcAdminThird');
$this->assertEquals($expected, $result);

// ダミーのプラグインを作成
$path = App::path('plugins')[0] . 'hoge' . DS;
$Folder = new Folder($path, true);
$File = new File($path . 'VERSION.txt', true);
$Folder = new BcFolder($path);
$Folder->create();
$File = new BcFile($path . 'VERSION.txt');
$File->create();
$File->write('1.2.3');
$result = BcUtil::getVersion('Hoge');

$File->close();
$Folder->delete();
$this->assertEquals('1.2.3', $result, 'プラグインのバージョンを取得できません');
}
Expand Down Expand Up @@ -263,14 +264,12 @@ public function testIncludePluginClass(): void
public function testClearAllCache(): void
{
// cacheファイルのバックアップ作成
$folder = new Folder();
$origin = CACHE;
$folder = new BcFolder($origin);
// $folder->create();
$backup = str_replace('cache', 'cache_backup', CACHE);
$folder->move($backup, [
'from' => $origin,
'mode' => 0777,
'schema' => Folder::OVERWRITE,
]);
(new BcFolder($backup))->create();
$folder->move($origin, $backup);

// cache環境準備
$cacheList = ['environment' => '_bc_env_', 'persistent' => '_cake_core_', 'models' => '_cake_model_'];
Expand All @@ -294,12 +293,8 @@ public function testClearAllCache(): void
}

// cacheファイル復元
$folder->move($origin, [
'from' => $backup,
'mode' => 0777,
'schema' => Folder::OVERWRITE,
]);
$folder->chmod($origin, 0777);
$folder->move($backup, $origin);
$folder->chmod(0777);
}

/**
Expand Down Expand Up @@ -456,8 +451,8 @@ public function testGetThemesPlugins()
$this->assertCount(1, $plugins);
$this->assertEquals($pluginName, $plugins[0]);

$folder = new Folder();
$folder->delete($themePath . 'plugins');
$folder = new BcFolder($themePath . 'plugins');
$folder->delete();
}

/**
Expand All @@ -470,8 +465,8 @@ public function testGetCurrentThemesPlugins()
$targetTheme = BcUtil::getCurrentTheme();
$themePath = BcUtil::getPluginPath($targetTheme);
$pluginName = 'test';
$folder = new Folder();
$folder->create($themePath . 'plugins/' . $pluginName, 0777);
$folder = new BcFolder($themePath . 'plugins/' . $pluginName);
$folder->create();
// プラグインが存在しているかどうか確認する
$plugins = BcUtil::getCurrentThemesPlugins();
$this->assertCount(1, $plugins);
Expand All @@ -484,8 +479,8 @@ public function testGetCurrentThemesPlugins()
$this->assertCount(0, $plugins);

// 作成したプラグインを削除する
$folder = new Folder();
$folder->delete($themePath . 'plugins');
$folder = new BcFolder($themePath . 'plugins');
$folder->delete();
}

/**
Expand Down Expand Up @@ -518,12 +513,12 @@ public function testGetSchemaPath()
*/
public function testGetDefaultDataPath($theme, $pattern, $expect)
{
$Folder = new Folder();
// 初期データ用のダミーディレクトリを作成
if (!$pattern) $pattern = 'default';
if ($theme) {
$path = BASER_THEMES . $theme . DS . 'config' . DS . 'data' . DS . $pattern;
$Folder->create($path);
$Folder = new BcFolder($path);
$Folder->create();
}
$result = BcUtil::getDefaultDataPath($theme, $pattern);
// 初期データ用のダミーディレクトリを削除
Expand Down Expand Up @@ -638,20 +633,20 @@ public function testGetAllThemeList()
{
$themePath = ROOT . DS . 'plugins' . DS . 'TestTheme';
$themeConfigPath = $themePath . DS . 'config.php';
$folder = new Folder();
$folder->create(ROOT . DS . 'plugins' . DS . 'TestTheme');
$file = new File($themeConfigPath);
$folder = new BcFolder(ROOT . DS . 'plugins' . DS . 'TestTheme');
$folder->create();
$file = new BcFile($themeConfigPath);
$file->create();
$file->write('<?php
return [
\'type\' => \'Theme\'
];
');
$file->close();
$themes = BcUtil::getAllThemeList();
$this->assertTrue(in_array('BcFront', $themes));
$this->assertTrue(in_array('BcAdminThird', $themes));
$this->assertTrue(in_array('TestTheme', $themes));
$folder->delete($themePath);
$folder->delete();
}

/**
Expand Down Expand Up @@ -981,20 +976,19 @@ public function testEmptyFolder()
];

// ダミーのフォルダとファイルを作成
$Folder = new Folder();
$Folder->create($dummyPath, 0755);
$Folder->create($dummyPath . $names['folder'][0], 0755);
$Folder->create($dummyPath . $names['folder'][1], 0755);
(new BcFolder($dummyPath))->create(0755);
(new BcFolder($dummyPath . $names['folder'][0]))->create(0755);
(new BcFolder($dummyPath . $names['folder'][1]))->create(0755);

// フォルダtestにファイルを追加する
new File($dummyPath . $names['file'][0], true);
new File($dummyPath . $names['file'][1], true);
(new BcFile($dummyPath . $names['file'][0]))->create();
(new BcFile($dummyPath . $names['file'][1]))->create();

// folder1とfolder2にfile1とfile2を追加する
foreach ($names['folder'] as $folder) {
$folderPath = $dummyPath . $folder . DS;
foreach ($names['file'] as $file) {
new File($folderPath . $file, true);
(new BcFile($folderPath . $file))->create();
}
}

Expand Down Expand Up @@ -1025,7 +1019,8 @@ public function testEmptyFolder()
@unlink($folderPath . $file);
}
}
$Folder->delete($dummyPath);
$Folder = new BcFolder($dummyPath);
$Folder->delete();

$this->assertTrue($result, 'フォルダの中のファイルのみを削除することができません');
}
Expand All @@ -1043,15 +1038,13 @@ public function testEmptyFolder()
*/
public function testFgetcsvReg($content, $length, $d, $e, $expect, $message)
{
$csv = new File(CACHE . 'test.csv');
$csv = new BcFile(CACHE . 'test.csv');
$csv->create();
$handle = fopen($csv->getPath(), 'r');
$csv->write($content);
$csv->close();
$csv->open();

$result = BcUtil::fgetcsvReg($csv->handle, $length, $d, $e);
$result = BcUtil::fgetcsvReg($handle, $length, $d, $e);
$this->assertEquals($expect, $result, $message);

$csv->close();
}

public function fgetcsvRegDataProvider()
Expand Down Expand Up @@ -1239,7 +1232,7 @@ public function test_changePluginNameSpace()
// 対象ファイルをopen
$theme = 'BcFront';
$pluginPath = BcUtil::getPluginPath($theme);
$file = new File($pluginPath . 'src' . DS . 'Plugin.php');
$file = new BcFile($pluginPath . 'src' . DS . 'Plugin.php');
// テーマ名とネームスペースが違う状態を作る
$data = $file->read();
$file->write(preg_replace('/namespace .+?;/', 'namespace WrongNamespace;', $data));
Expand All @@ -1254,8 +1247,6 @@ public function test_changePluginNameSpace()
$data = $file->read();
preg_match('/namespace .+?;/', $data, $match);
$this->assertEquals('namespace ' . $theme . ';', $match[0]);
// ファイルをclose
$file->close();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

use BaserCore\Controller\Component\BcAdminContentsComponent;
use BaserCore\Error\BcException;
use BaserCore\Utility\BcFolder;
use BaserCore\Utility\BcUtil;
use BcBlog\Service\Admin\BlogContentsAdminServiceInterface;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Filesystem\Folder;
use Cake\ORM\Exception\PersistenceFailedException;
use BaserCore\Annotation\NoTodo;
use BaserCore\Annotation\Checked;
Expand Down Expand Up @@ -125,9 +125,9 @@ protected function redirectEditBlog(string $template): ResponseInterface
if (!file_exists($target . DS . 'index' . $ext)) {
$source = Plugin::templatePath(Configure::read('BcApp.coreFrontTheme')) . DS . 'plugin' . DS . 'BcBlog' . DS . $path;
if (is_dir($source)) {
$folder = new Folder();
$folder->create(dirname($target), 0777);
$folder->copy($target, ['from' => $source, 'chmod' => 0777]);
$folder = new BcFolder(dirname($target));
$folder->create();
$folder->copy($source, $target);
}
}
$path = str_replace(DS, '/', $path);
Expand Down

0 comments on commit 34e6004

Please sign in to comment.