-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPluginComposerBehavior.php
More file actions
170 lines (153 loc) · 5 KB
/
PluginComposerBehavior.php
File metadata and controls
170 lines (153 loc) · 5 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?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');
/**
* Plugin Behavior
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\PluginManager\Model\Behavior
*/
class PluginComposerBehavior extends ModelBehavior {
/**
* Composersのファイル取得
*
* @var array
*/
public $composers = array();
/**
* Setup this behavior with the specified configuration settings.
*
* @param Model $model 呼び出し元のモデル
* @param array $config Configuration settings for $model
* @return void
*/
public function setup(Model $model, $config = array()) {
parent::setup($model, $config);
$this->ignorePatterns = Hash::get($config, 'ignorePatterns', ['#^netcommons/#']);
}
/**
* composer.lockから情報取得
*
* @param Model $model 呼び出し元Model
* @param string $namespace Pluginのnamespace
* @param string|array $filePath ファイルパスもしくはcomposer.lockファイルデータ
* @return mixed array|bool
*/
public function getComposer(Model $model, $namespace = null, $filePath = null) {
if ($this->composers && ! $filePath) {
$composers = $this->composers;
} else {
if ($filePath) {
$file = new File($filePath);
} else {
$file = new File(ROOT . DS . 'composer.lock');
}
$contents = $file->read();
$file->close();
$packages = json_decode($contents, true);
$composers = array();
foreach ($packages['packages'] as $package) {
$composer = $this->_parseComposer($package);
if ($composer['packageType'] !== 'netcommons-theme') {
//テーマは別途設定するため除外する
$composers[$composer['namespace']] = $composer;
}
}
if (! $filePath) {
$this->composers = $composers;
}
}
if (! $namespace) {
return $composers;
}
return Hash::get($composers, array($namespace));
}
/**
* bowerの情報をパースする
*
* @param array $package jsonファイルの情報
* @return mixed array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function _parseComposer($package) {
$composerExtType = true;
foreach ($this->ignorePatterns as $pattern) {
if (preg_match($pattern, $package['name'])) {
$key = strtr(preg_replace($pattern, '', $package['name']), '-', '_');
$name = Inflector::humanize($key);
$originalSource = Inflector::camelize($key);
$composerExtType = false;
}
}
if (Hash::get($package, 'type') === 'cakephp-plugin') {
$key = strtr(substr($package['name'], strrpos($package['name'], '/') + 1), '-', '_');
$name = Inflector::humanize(strtr($package['name'], '/', ' '));
$originalSource = Inflector::camelize($key);
} elseif (empty($key)) {
$key = $package['name'];
$name = $package['name'];
$originalSource = $package['name'];
}
$result = array(
'key' => $key,
'namespace' => Hash::get($package, 'name'),
'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' => $originalSource
);
if (isset($package['extra']['installer-name'])) {
$result['key'] = Inflector::underscore($package['extra']['installer-name']);
$result['name'] = $package['extra']['installer-name'];
$result['originalSource'] = $package['extra']['installer-name'];
} else {
$result['name'] = $name;
}
if (isset($package['plugin-type'])) {
$result['type'] = $package['plugin-type'];
} elseif ($composerExtType) {
$result['type'] = Plugin::PLUGIN_TYPE_FOR_EXT_COMPOSER;
}
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;
}
/**
* composer.lockファイルからバージョン情報をDBに更新する
*
* @param Model $model 呼び出し元Model
* @param string|array $filePath ファイルパスもしくはcomposer.lockファイルデータ
* @return bool
*/
public function updateVersionByComposer(Model $model, $filePath = null) {
$model->loadModels([
'Plugin' => 'PluginManager.Plugin',
]);
$composers = $this->getComposer($model, null, $filePath);
$model->Plugin->updateVersion($composers);
return true;
}
}