Skip to content

Commit

Permalink
Merge b7844c0 into dc8c692
Browse files Browse the repository at this point in the history
  • Loading branch information
s-nakajima committed Oct 27, 2018
2 parents dc8c692 + b7844c0 commit ce2914a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Model/Behavior/PluginBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,24 @@ public function runVersionUp(Model $model, $plugin) {
$model->begin();

if (! Hash::get($plugin, 'latest') && Hash::get($plugin, 'Plugin.id')) {
if (! $model->uninstallPlugin(Hash::get($plugin, 'Plugin.key'))) {
$pluginKey = Hash::get($plugin, 'Plugin.key');
if (! $model->uninstallPlugin($pluginKey)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$model->deleteOldPackageDir($plugin, true);

//img,js,cssをwebrootから削除。エラーとはしない
$model->deleteFromWebroot($plugin);
} else {
if (Hash::get($plugin, 'latest.packageType') === 'cakephp-plugin') {
if (! $model->runMigration(Hash::get($plugin, 'latest.key'))) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}

//img,js,cssをwebrootにコピー。エラーとはしない
$model->copyToWebroot($plugin);

if (! $model->updateVersion(array(Hash::get($plugin, 'latest')))) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
Expand Down
99 changes: 99 additions & 0 deletions Model/Behavior/PluginWebrootBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Plugin Behavior
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/

App::uses('ModelBehavior', 'Model');
App::uses('Folder', 'Utility');

/**
* Plugin Behavior
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\PluginManager\Model\Behavior
*/
class PluginWebrootBehavior extends ModelBehavior {

/**
* 各プラグインにあるapp/webroot/img(css,js)にコピーする
*
* @param Model $model 呼び出し元Model
* @param array $plugin プラグイン情報
* @return bool
*/
public function copyToWebroot(Model $model, $plugin) {
$pluginKey = Hash::get($plugin, 'Plugin.key');
if (! $pluginKey) {
return true;
}

//既存のapp/webroot/img(css,js)を削除する
$this->deleteFromWebroot($model, $plugin);
$camelPlugin = Inflector::camelize($pluginKey);
$originalSource = Hash::get($plugin, 'latest.originalSource');

if (CakePlugin::loaded($camelPlugin)) {
$pluginWebrootPath = CakePlugin::path($camelPlugin);
} elseif (file_exists(APP . 'View' . DS . 'Themed' . DS . $originalSource)) {
$pluginWebrootPath = APP . 'View' . DS . 'Themed' . DS . $originalSource . DS;
} else {
return true;
}
$pluginWebrootPath .= WEBROOT_DIR . DS;

if (file_exists($pluginWebrootPath . 'img')) {
$Folder = new Folder($pluginWebrootPath . 'img');
$Folder->copy(IMAGES . DS . $pluginKey);
}

if (file_exists($pluginWebrootPath . 'css')) {
$Folder = new Folder($pluginWebrootPath . 'css');
$Folder->copy(CSS . DS . $pluginKey);
}

if (file_exists($pluginWebrootPath . 'js')) {
$Folder = new Folder($pluginWebrootPath . 'js');
$Folder->copy(JS . DS . $pluginKey);
}

return true;
}

/**
* app/webroot/img(css,js)から削除する
*
* @param Model $model 呼び出し元Model
* @param array $plugin プラグイン情報
* @return bool
*/
public function deleteFromWebroot(Model $model, $plugin) {
$pluginKey = Hash::get($plugin, 'Plugin.key');
if (! $pluginKey) {
return true;
}

if (file_exists(IMAGES . $pluginKey)) {
$Folder = new Folder(IMAGES . $pluginKey);
$Folder->delete();
}

if (file_exists(CSS . $pluginKey)) {
$Folder = new Folder(CSS . $pluginKey);
$Folder->delete();
}

if (file_exists(JS . $pluginKey)) {
$Folder = new Folder(JS . $pluginKey);
$Folder->delete();
}

return true;
}

}
1 change: 1 addition & 0 deletions Model/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Plugin extends AppModel {
'PluginManager.PluginBower',
'PluginManager.PluginComposer',
'PluginManager.PluginTheme',
'PluginManager.PluginWebroot',
);

/**
Expand Down

0 comments on commit ce2914a

Please sign in to comment.