-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPluginThemeBehavior.php
More file actions
130 lines (116 loc) · 3.53 KB
/
PluginThemeBehavior.php
File metadata and controls
130 lines (116 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?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('File', 'Utility');
App::uses('Folder', 'Utility');
App::uses('Plugin', 'PluginManager.Model');
App::uses('Security', 'Utility');
/**
* Plugin Behavior
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\PluginManager\Model\Behavior
*/
class PluginThemeBehavior extends ModelBehavior {
/**
* Themesのファイル取得
*
* @var array
*/
public $themes = array();
/**
* themeの情報取得
*
* @param Model $model 呼び出し元Model
* @param string $namespace Pluginのnamespace
* @param string $dirPath themeのディレクトリ
* @return mixed array|bool
*/
public function getTheme(Model $model, $namespace = null, $dirPath = null) {
if ($this->themes && ! $dirPath) {
$themes = $this->themes;
} else {
if ($dirPath) {
$Folder = new Folder($dirPath);
} else {
$Folder = new Folder(APP . 'View' . DS . 'Themed');
}
$dirs = $Folder->read(Folder::SORT_NAME, false, true)[0];
$themes = array();
foreach ($dirs as $dir) {
$file = new File($dir . DS . 'theme.json');
$contents = $file->read();
$file->close();
$package = json_decode($contents, true);
$theme = $this->_parseTheme($package, $dir);
$themes[$theme['namespace']] = $theme;
}
if (! $dirPath) {
$this->themes = $themes;
}
}
if (! $namespace) {
return $themes;
}
return Hash::get($themes, array($namespace));
}
/**
* bowerの情報をパースする
*
* @param array $package jsonファイルの情報
* @param string $dir ディレクトリパス
* @return mixed array
*/
protected function _parseTheme($package, $dir) {
$result = array(
'name' => Hash::get($package, 'name'),
'key' => 'themed_' . Inflector::underscore(basename($dir)),
'namespace' => 'Themed/' . basename($dir),
'type' => Plugin::PLUGIN_TYPE_FOR_THEME,
'description' => Hash::get($package, 'description'),
'homepage' => Hash::get($package, 'homepage'),
'version' => Hash::get($package, 'version'),
'commit_version' => Hash::get($package, 'source.reference'),
'source' => Hash::get($package, 'source.url', ''),
'authors' => Hash::get($package, 'authors'),
'license' => Hash::get($package, 'license'),
'commited' => Hash::get($package, 'time'),
'packageType' => Hash::get($package, 'type'),
'originalSource' => basename($dir)
);
if (! $result['commit_version']) {
$result['commit_version'] = Security::hash($result['namespace'] . $result['version'], 'md5');
}
if (Hash::get($package, 'source.type') === 'git' &&
$result['source'] && $result['commit_version']) {
$result['commit_url'] = preg_replace('/\.git$/', '', $result['source']);
$result['commit_url'] .= '/tree/' . $result['commit_version'];
} else {
$result['commit_url'] = null;
}
return $result;
}
/**
* theme.jsonファイルからバージョン情報をDBに更新する
*
* @param Model $model Model using this behavior
* @param string $dirPath themeのディレクトリ
* @return bool
*/
public function updateVersionByTheme(Model $model, $dirPath = null) {
$model->loadModels([
'Plugin' => 'PluginManager.Plugin',
]);
$themes = $this->getTheme($model, null, $dirPath);
$model->Plugin->updateVersion($themes);
return true;
}
}