Skip to content

Commit 945ecf6

Browse files
committed
vendorフォルダのプラグインを読み込めるように
1 parent 98793b5 commit 945ecf6

File tree

1 file changed

+82
-25
lines changed

1 file changed

+82
-25
lines changed

plugins/baser-core/src/BaserCorePlugin.php

Lines changed: 82 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
use BaserCore\Middleware\BcRequestFilterMiddleware;
3434
use BaserCore\ServiceProvider\BcServiceProvider;
3535
use BaserCore\Utility\BcEvent;
36+
use BaserCore\Utility\BcFolder;
3637
use BaserCore\Utility\BcLang;
3738
use BaserCore\Utility\BcUtil;
39+
use Cake\Cache\Cache;
3840
use Cake\Console\CommandCollection;
3941
use Cake\Core\Configure;
4042
use Cake\Core\ContainerInterface;
@@ -107,7 +109,7 @@ public function bootstrap(PluginApplicationInterface $app): void
107109
* 言語設定
108110
* ブラウザよりベースとなる言語を設定
109111
*/
110-
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && !BcUtil::isTest()) {
112+
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && !BcUtil::isTest()) {
111113
I18n::setLocale(BcLang::parseLang($_SERVER['HTTP_ACCEPT_LANGUAGE']));
112114
}
113115

@@ -128,6 +130,18 @@ public function bootstrap(PluginApplicationInterface $app): void
128130
*/
129131
parent::bootstrap($app);
130132

133+
/**
134+
* vendor 配下で baserCMSのプラグインを持っているもののパスを追加
135+
* bootstrap の後でないとキャッシュが使えないため、ここで呼び出す
136+
*/
137+
$pluginVendorPath = $this->getPluginVendorPath();
138+
if ($pluginVendorPath) {
139+
Configure::write('App.paths.plugins', array_merge(
140+
Configure::read('App.paths.plugins'),
141+
$this->getPluginVendorPath()
142+
));
143+
}
144+
131145
/**
132146
* 文字コードの検出順を指定
133147
*/
@@ -159,7 +173,7 @@ public function bootstrap(PluginApplicationInterface $app): void
159173
}
160174

161175
// CSRFトークンの場合は高速化のためここで処理を終了
162-
if(!empty($_SERVER['REQUEST_URI']) && preg_match('/\?requestview=false$/', $_SERVER['REQUEST_URI'])) {
176+
if (!empty($_SERVER['REQUEST_URI']) && preg_match('/\?requestview=false$/', $_SERVER['REQUEST_URI'])) {
163177
return;
164178
}
165179

@@ -207,7 +221,7 @@ public function addPlugin(PluginApplicationInterface $app): void
207221
$app->addPlugin('Migrations');
208222

209223
$plugins = BcUtil::getEnablePlugins();
210-
if(!$plugins) return;
224+
if (!$plugins) return;
211225
foreach($plugins as $plugin) {
212226
if (!BcUtil::includePluginClass($plugin->name)) continue;
213227
$this->loadPlugin($app, $plugin->name, $plugin->priority);
@@ -228,7 +242,7 @@ public function addTheme(PluginApplicationInterface $application, array $themes)
228242
if (!BcUtil::isInstalled()) return;
229243

230244
foreach($themes as $theme) {
231-
if(!BcUtil::includePluginClass($theme)) continue;
245+
if (!BcUtil::includePluginClass($theme)) continue;
232246
try {
233247
$application->addPlugin($theme);
234248
} catch (MissingPluginException $e) {
@@ -247,7 +261,7 @@ public function addTheme(PluginApplicationInterface $application, array $themes)
247261
public function setupThemePlugin(array $themes): void
248262
{
249263
if (!BcUtil::isInstalled()) return;
250-
if(!$themes) return;
264+
if (!$themes) return;
251265
$path = [];
252266
foreach($themes as $theme) {
253267
$pluginsPath = CorePlugin::path($theme) . 'plugins' . DS;
@@ -258,7 +272,7 @@ public function setupThemePlugin(array $themes): void
258272
if (!is_dir($pluginsPath)) continue;
259273
$path[] = $pluginsPath;
260274
}
261-
if(isset($path) && $path) {
275+
if (isset($path) && $path) {
262276
Configure::write('App.paths.plugins', array_merge(
263277
Configure::read('App.paths.plugins'),
264278
$path
@@ -293,12 +307,12 @@ public function getAvailableThemes(): array
293307
// path() を実行するとプラグインクラスがコレクションに登録されてしまう
294308
// ここで登録されてしまうと、対象プラグインの bootstrap() が正常に実行されないため
295309
// ここでは一旦削除する。
296-
CorePlugin::getCollection()->remove($site->theme);
297-
if(!is_dir($pluginPath)) {
298-
continue;
299-
}
310+
CorePlugin::getCollection()->remove($site->theme);
311+
if (!is_dir($pluginPath)) {
312+
continue;
313+
}
300314
}
301-
if(in_array($site->theme, $themes)) continue;
315+
if (in_array($site->theme, $themes)) continue;
302316
$themes[] = $site->theme;
303317
}
304318
}
@@ -376,15 +390,15 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
376390
$authSetting = Configure::read('BcPrefixAuth.' . $prefix);
377391

378392
// 設定ファイルでスキップの定義がされている場合はスキップ
379-
if($this->isSkipCsrfUrl($request->getPath())) return true;
393+
if ($this->isSkipCsrfUrl($request->getPath())) return true;
380394

381395
// 領域が REST API でない場合はスキップしない
382396
if (empty($authSetting['isRestApi'])) return false;
383397

384398
$authenticator = $request->getAttribute('authentication')->getAuthenticationProvider();
385-
if($authenticator) {
399+
if ($authenticator) {
386400
// 認証済の際、セッション認証以外はスキップ
387-
if(!$authenticator instanceof SessionAuthenticator) return true;
401+
if (!$authenticator instanceof SessionAuthenticator) return true;
388402
}
389403
return false;
390404
});
@@ -430,7 +444,7 @@ protected function isSkipCsrfUrl(string $path): bool
430444
{
431445
$skipUrls = $this->getSkipCsrfUrl();
432446

433-
foreach ($skipUrls as $skipUrl) {
447+
foreach($skipUrls as $skipUrl) {
434448
// 完全一致チェック(従来の動作を維持)
435449
if ($path === $skipUrl) {
436450
return true;
@@ -489,7 +503,7 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
489503
break;
490504
case 'Jwt':
491505
$this->setupJwtAuth($service, $authSetting, $prefix);
492-
if($prefix === 'Api/Admin' && BcUtil::isSameReferrerAsCurrent()) {
506+
if ($prefix === 'Api/Admin' && BcUtil::isSameReferrerAsCurrent()) {
493507
// セッションを持っている場合もログイン状態とみなす
494508
$service->loadAuthenticator('Authentication.Session', [
495509
'sessionKey' => $authSetting['sessionKey'],
@@ -512,9 +526,9 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
512526
*/
513527
public function isRequiredAuthentication(array $authSetting)
514528
{
515-
if(!$authSetting || empty($authSetting['type'])) return false;
516-
if(!empty($authSetting['disabled'])) return false;
517-
if(!BcUtil::isInstalled()) return false;
529+
if (!$authSetting || empty($authSetting['type'])) return false;
530+
if (!empty($authSetting['disabled'])) return false;
531+
if (!BcUtil::isInstalled()) return false;
518532
return true;
519533
}
520534

@@ -529,7 +543,7 @@ public function isRequiredAuthentication(array $authSetting)
529543
*/
530544
public function setupSessionAuth(AuthenticationService $service, array $authSetting)
531545
{
532-
if($authSetting['userModel'] === 'BaserCore.Users' && empty($authSetting['finder'])) {
546+
if ($authSetting['userModel'] === 'BaserCore.Users' && empty($authSetting['finder'])) {
533547
$authSetting['finder'] = 'available';
534548
}
535549
$service->setConfig([
@@ -548,9 +562,9 @@ public function setupSessionAuth(AuthenticationService $service, array $authSett
548562
]);
549563

550564
$passwordHasher = null;
551-
if(!empty($authSetting['passwordHasher'])) {
565+
if (!empty($authSetting['passwordHasher'])) {
552566
$passwordHasher = $authSetting['passwordHasher'];
553-
} elseif(env('HASH_TYPE') === 'sha1') {
567+
} elseif (env('HASH_TYPE') === 'sha1') {
554568
// .env に HASH_TYPE で sha1が設定されている場合 4系のハッシュアルゴリズムを使用
555569
$passwordHasher = [
556570
'className' => 'Authentication.Fallback',
@@ -612,7 +626,7 @@ public function setupJwtAuth(AuthenticationService $service, array $authSetting,
612626
'resolver' => [
613627
'className' => 'BaserCore.PrefixOrm',
614628
'userModel' => $authSetting['userModel'],
615-
'finder' => $authSetting['finder']?? 'available',
629+
'finder' => $authSetting['finder'] ?? 'available',
616630
'prefix' => $prefix,
617631
],
618632
]);
@@ -631,7 +645,7 @@ public function setupJwtAuth(AuthenticationService $service, array $authSetting,
631645
'resolver' => [
632646
'className' => 'Authentication.Orm',
633647
'userModel' => $authSetting['userModel'],
634-
'finder' => $authSetting['finder']?? 'available'
648+
'finder' => $authSetting['finder'] ?? 'available'
635649
],
636650
]);
637651
return $service;
@@ -767,7 +781,7 @@ public function disableRootRoutes(RouteBuilder $routes): void
767781
* @param array $options
768782
* @return void
769783
*/
770-
public function updateDefaultData($options = []) : void
784+
public function updateDefaultData($options = []): void
771785
{
772786
// コンテンツの作成日を更新
773787
$this->updateDateNow('BaserCore.Contents', ['created_date'], [], $options);
@@ -805,4 +819,47 @@ public function console(CommandCollection $commands): CommandCollection
805819
return $commands;
806820
}
807821

822+
/**
823+
* baserCMSのプラグインを保有しているベンダーパスを取得する
824+
* @param bool $force
825+
* @return array
826+
*/
827+
public function getPluginVendorPath(bool $force = false): array
828+
{
829+
if (!BcUtil::isInstalled() && !$force) return [];
830+
$pluginVendorPaths = Cache::read('plugin_vendor_paths', '_bc_env_');
831+
if (!Configure::read('debug') && !$force && $pluginVendorPaths) {
832+
return $pluginVendorPaths;
833+
}
834+
if (empty($pluginVendorPaths)) {
835+
$pluginVendorPaths = [];
836+
}
837+
$folder = new BcFolder(ROOT . DS . 'vendor');
838+
$vendorPaths = $folder->getFolders(['full' => true]);
839+
foreach($vendorPaths as $vendorPath) {
840+
if (preg_match('/baserproject$/', $vendorPath)) continue;
841+
$folder = new BcFolder($vendorPath);
842+
$packagePaths = $folder->getFolders(['full' => true]);
843+
$hasPlugin = false;
844+
foreach($packagePaths as $packagePath) {
845+
if (!file_exists($packagePath . DS . 'config.php')) continue;
846+
$config = require $packagePath . DS . 'config.php';
847+
$type = $config['type'] ?? null;
848+
if (!is_array($type)) $type = [$type];
849+
if (!in_array('Plugin', $type) && !in_array('CorePlugin', $type)) continue;
850+
if (!in_array($vendorPath . DS, $pluginVendorPaths)) {
851+
$hasPlugin = true;
852+
break;
853+
}
854+
}
855+
if ($hasPlugin) {
856+
$pluginVendorPaths[] = $vendorPath . DS;
857+
}
858+
}
859+
if ($pluginVendorPaths) {
860+
Cache::write('enable_plugin_paths', $pluginVendorPaths, '_bc_env_');
861+
}
862+
return $pluginVendorPaths;
863+
}
864+
808865
}

0 commit comments

Comments
 (0)