Skip to content

Commit

Permalink
Merge pull request #360 from nanasess/improve/plugin-loader
Browse files Browse the repository at this point in the history
プラグインを任意のディレクトリに配置できるように修正
  • Loading branch information
okazy committed May 28, 2020
2 parents 6d68d2e + dcc550d commit 1b7d82b
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 11 deletions.
7 changes: 6 additions & 1 deletion data/app_initial.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@

// クラスのオートローディングを定義する
require_once(CLASS_EX_REALDIR . 'SC_ClassAutoloader_Ex.php');
spl_autoload_register(array('SC_ClassAutoloader_Ex', 'autoload'), true, true);
spl_autoload_register(
function ($class) {
SC_ClassAutoloader_Ex::autoload($class, __DIR__.'/downloads/plugin/');
},
true, true
);

SC_Helper_HandleError_Ex::load();

Expand Down
4 changes: 2 additions & 2 deletions data/class/SC_ClassAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SC_ClassAutoloader
* LC_* には対応していない。
* @return void
*/
public static function autoload($class)
public static function autoload($class, $plugin_upload_realdir = PLUGIN_UPLOAD_REALDIR)
{
$arrClassNamePart = explode('_', $class);
$is_ex = end($arrClassNamePart) === 'Ex';
Expand Down Expand Up @@ -66,7 +66,7 @@ public static function autoload($class)
// プラグイン向けフックポイント
// MEMO: プラグインのローダーがDB接続を必要とするため、SC_Queryがロードされた後のみ呼び出される。
// プラグイン情報のキャッシュ化が行われれば、全部にフックさせることを可能に?
$objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance(true);
$objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance(true, $plugin_upload_realdir);
if (is_object($objPlugin)) {
// 元の設定を一時保存
$plugin_class = $class;
Expand Down
10 changes: 5 additions & 5 deletions data/class/helper/SC_Helper_Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ class SC_Helper_Plugin
* @param bool $plugin_activate_flg プラグインを有効化する場合 true
* @return void
*/
public function load($plugin_activate_flg = true)
public function load($plugin_activate_flg = true, $plugin_upload_realdir = PLUGIN_UPLOAD_REALDIR)
{
if (!defined('CONFIG_REALFILE') || !file_exists(CONFIG_REALFILE)) return; // インストール前
if (GC_Utils_Ex::isInstallFunction()) return; // インストール中
if ($plugin_activate_flg === false) return;
// 有効なプラグインを取得
$arrPluginDataList = SC_Plugin_Util_Ex::getEnablePlugin();
// pluginディレクトリを取得
$arrPluginDirectory = SC_Plugin_Util_Ex::getPluginDirectory();
$arrPluginDirectory = SC_Plugin_Util_Ex::getPluginDirectory($plugin_upload_realdir);
foreach ($arrPluginDataList as $arrPluginData) {
// プラグイン本体ファイル名が取得したプラグインディレクトリ一覧にある事を確認
if (array_search($arrPluginData['plugin_code'], $arrPluginDirectory) !== false) {
$plugin_file_path = PLUGIN_UPLOAD_REALDIR . $arrPluginData['plugin_code'] . '/' . $arrPluginData['class_name'] . '.php';
$plugin_file_path = $plugin_upload_realdir . $arrPluginData['plugin_code'] . '/' . $arrPluginData['class_name'] . '.php';
// プラグイン本体ファイルが存在しない場合
if (!file_exists($plugin_file_path)) {
// エラー出力
Expand Down Expand Up @@ -90,7 +90,7 @@ public function load($plugin_activate_flg = true)
* @param bool $plugin_activate_flg プラグインを有効化する場合 true
* @return SC_Helper_Plugin SC_Helper_Pluginオブジェクト
*/
public static function getSingletonInstance($plugin_activate_flg = PLUGIN_ACTIVATE_FLAG)
public static function getSingletonInstance($plugin_activate_flg = PLUGIN_ACTIVATE_FLAG, $plugin_upload_realdir = PLUGIN_UPLOAD_REALDIR)
{
if (!isset($GLOBALS['_SC_Helper_Plugin_instance'])) {
// プラグインのローダーがDB接続を必要とするため、
Expand All @@ -101,7 +101,7 @@ public static function getSingletonInstance($plugin_activate_flg = PLUGIN_ACTIVA
}

$GLOBALS['_SC_Helper_Plugin_instance'] = new SC_Helper_Plugin_Ex();
$GLOBALS['_SC_Helper_Plugin_instance']->load($plugin_activate_flg);
$GLOBALS['_SC_Helper_Plugin_instance']->load($plugin_activate_flg, $plugin_upload_realdir);
}

return $GLOBALS['_SC_Helper_Plugin_instance'];
Expand Down
6 changes: 3 additions & 3 deletions data/class/plugin/SC_Plugin_Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ public function deletePluginByPluginId($plugin_id)
*
* @return array $arrPluginDirectory
*/
public function getPluginDirectory()
public function getPluginDirectory($plugin_upload_realdir = PLUGIN_UPLOAD_REALDIR)
{
$arrPluginDirectory = array();
if (is_dir(PLUGIN_UPLOAD_REALDIR)) {
if ($dh = opendir(PLUGIN_UPLOAD_REALDIR)) {
if (is_dir($plugin_upload_realdir)) {
if ($dh = opendir($plugin_upload_realdir)) {
while (($pluginDirectory = readdir($dh)) !== false) {
$arrPluginDirectory[] = $pluginDirectory;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/class/fixtures/plugin/FixturePlugin/FixturePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class FixturePlugin extends SC_Plugin_Base
{
public function loadClassFileChange(&$classname, &$classpath) {
if ($classname === "SC_Customer_Ex") {
$classpath = "FixturePlugin/Fixture_SC_Customer.php";
$classname = "Fixture_SC_Customer";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class Fixture_SC_Customer extends SC_Customer
{
public function getValue($key)
{
return $key;
}
}
51 changes: 51 additions & 0 deletions tests/class/plugin/LoadClassFileChangeCustomDirTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* @group classloader
*/
class LoadClassFileChangeCustomDirTest extends Common_TestCase
{
protected function setUp()
{
parent::setUp();
$plugin_id = $this->objQuery->nextVal('dtb_plugin_plugin_id');
$pluginValues = [
'plugin_id' => $plugin_id,
'plugin_name' => 'FixturePlugin',
'plugin_code' => 'FixturePlugin',
'class_name' => 'FixturePlugin',
'plugin_version' => '0.0.0',
'compliant_version' => '2.17',
'enable' => 1,
'create_date' => 'CURRENT_TIMESTAMP',
'update_date' => 'CURRENT_TIMESTAMP'
];
$this->objQuery->insert('dtb_plugin', $pluginValues);

$plugin_hookpoint_id = $this->objQuery->nextVal('dtb_plugin_hookpoint_plugin_hookpoint_id');
$hookpointValues = [
'plugin_hookpoint_id' => $plugin_hookpoint_id,
'plugin_id' => $plugin_id,
'hook_point' => 'loadClassFileChange',
'callback' => 'loadClassFileChange',
'update_date' => 'CURRENT_TIMESTAMP'
];
$this->objQuery->insert('dtb_plugin_hookpoint', $hookpointValues);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testLOading()
{
// __DIR__.'/../fixtures/plugin/ に配置したプラグインをオートロード対象にする
spl_autoload_register(function ($class){
SC_ClassAutoloader_Ex::autoload($class, __DIR__.'/../fixtures/plugin/');
}, true, true);

$objCustomer = new SC_Customer_Ex();
$this->assertInstanceOf('Fixture_SC_Customer', $objCustomer);
$this->assertEquals('loading', $objCustomer->getValue('loading'), __DIR__.'/../fixtures/plugin/Fixture_SC_Customer がロードされる');
}
}
2 changes: 2 additions & 0 deletions tests/class/plugin/LoadClassFileChangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ protected function tearDown()

/**
* loadClassFileChange で拡張したクラスのテストケース.
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testLoadExtendedClass()
{
Expand Down

0 comments on commit 1b7d82b

Please sign in to comment.