Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PluginService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PluginService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class PluginService |
||
|
|
|||
| 34 | { |
||
| 35 | const CONFIG_YML = 'config.yml'; |
||
| 36 | const EVENT_YML = 'event.yml'; |
||
| 37 | private $app; |
||
| 38 | |||
| 39 | 149 | public function __construct($app) |
|
| 43 | |||
| 44 | 9 | public function install($path, $source = 0) |
|
| 45 | { |
||
| 46 | 9 | $pluginBaseDir = null; |
|
| 47 | 9 | $tmp = null; |
|
| 48 | |||
| 49 | try { |
||
| 50 | 9 | $this->app->removePluginConfigCache(); |
|
| 51 | 9 | $tmp = $this->createTempDir(); |
|
| 52 | |||
| 53 | 9 | $this->unpackPluginArchive($path, $tmp); //一旦テンポラリに展開 |
|
| 54 | 9 | $this->checkPluginArchiveContent($tmp); |
|
| 55 | |||
| 56 | 7 | $config = $this->readYml($tmp.'/'.self::CONFIG_YML); |
|
| 57 | 7 | $event = $this->readYml($tmp.'/'.self::EVENT_YML); |
|
| 58 | 7 | $this->deleteFile($tmp); // テンポラリのファイルを削除 |
|
| 59 | |||
| 60 | 7 | $this->checkSamePlugin($config['code']); // 重複していないかチェック |
|
| 61 | |||
| 62 | 7 | $pluginBaseDir = $this->calcPluginDir($config['code']); |
|
| 63 | 7 | $this->createPluginDir($pluginBaseDir); // 本来の置き場所を作成 |
|
| 64 | |||
| 65 | 7 | $this->unpackPluginArchive($path, $pluginBaseDir); // 問題なければ本当のplugindirへ |
|
| 66 | |||
| 67 | 7 | $this->registerPlugin($config, $event, $source); // dbにプラグイン登録 |
|
| 68 | 6 | $this->app->writePluginConfigCache(); |
|
| 69 | 9 | } catch (PluginException $e) { |
|
| 70 | 4 | $this->deleteDirs(array($tmp, $pluginBaseDir)); |
|
| 71 | 4 | throw $e; |
|
| 72 | } catch (\Exception $e) { // インストーラがどんなExceptionを上げるかわからないので |
||
| 73 | |||
| 74 | $this->deleteDirs(array($tmp, $pluginBaseDir)); |
||
| 75 | throw $e; |
||
| 76 | } |
||
| 77 | |||
| 78 | 6 | return true; |
|
| 79 | } |
||
| 80 | |||
| 81 | 9 | public function createTempDir() |
|
| 92 | |||
| 93 | 4 | public function deleteDirs($arr) |
|
| 102 | |||
| 103 | 9 | public function unpackPluginArchive($archive, $dir) |
|
| 120 | |||
| 121 | 149 | public function checkPluginArchiveContent($dir, array $config_cache = array()) |
|
| 122 | { |
||
| 123 | try { |
||
| 124 | 149 | if (!empty($config_cache)) { |
|
| 125 | 141 | $meta = $config_cache; |
|
| 126 | 141 | } else { |
|
| 127 | 10 | $meta = $this->readYml($dir . '/config.yml'); |
|
| 128 | } |
||
| 129 | 149 | } catch (\Symfony\Component\Yaml\Exception\ParseException $e) { |
|
| 130 | throw new PluginException($e->getMessage(), $e->getCode(), $e); |
||
| 131 | } |
||
| 132 | |||
| 133 | 149 | if (!is_array($meta)) { |
|
| 134 | 2 | throw new PluginException('config.yml not found or syntax error'); |
|
| 135 | } |
||
| 136 | 147 | View Code Duplication | if (!isset($meta['code']) || !$this->checkSymbolName($meta['code'])) { |
| 137 | throw new PluginException('config.yml code empty or invalid_character(\W)'); |
||
| 138 | } |
||
| 139 | 147 | if (!isset($meta['name'])) { |
|
| 140 | // nameは直接クラス名やPATHに使われるわけではないため文字のチェックはなしし |
||
| 141 | 1 | throw new PluginException('config.yml name empty'); |
|
| 142 | } |
||
| 143 | 146 | View Code Duplication | if (isset($meta['event']) && !$this->checkSymbolName($meta['event'])) { // eventだけは必須ではない |
| 144 | throw new PluginException('config.yml event empty or invalid_character(\W) '); |
||
| 145 | } |
||
| 146 | 146 | if (!isset($meta['version'])) { |
|
| 147 | // versionは直接クラス名やPATHに使われるわけではないため文字のチェックはなしし |
||
| 148 | throw new PluginException('config.yml version invalid_character(\W) '); |
||
| 149 | } |
||
| 150 | 146 | if (isset($meta['orm.path'])) { |
|
| 151 | if (!is_array($meta['orm.path'])) { |
||
| 152 | throw new PluginException('config.yml orm.path invalid_character(\W) '); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | 146 | if (isset($meta['service'])) { |
|
| 156 | if (!is_array($meta['service'])) { |
||
| 157 | throw new PluginException('config.yml service invalid_character(\W) '); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | 146 | } |
|
| 161 | |||
| 162 | 10 | public function readYml($yml) |
|
| 163 | { |
||
| 164 | 10 | if (file_exists($yml)) { |
|
| 165 | 8 | return Yaml::parse(file_get_contents($yml)); |
|
| 166 | } |
||
| 167 | |||
| 168 | 7 | return false; |
|
| 169 | } |
||
| 170 | |||
| 171 | 147 | public function checkSymbolName($string) |
|
| 172 | { |
||
| 173 | 147 | return strlen($string) < 256 && preg_match('/^\w+$/', $string); |
|
| 174 | // plugin_nameやplugin_codeに使える文字のチェック |
||
| 175 | // a-z A-Z 0-9 _ |
||
| 176 | // ディレクトリ名などに使われれるので厳しめ |
||
| 177 | } |
||
| 178 | |||
| 179 | 7 | public function deleteFile($path) |
|
| 184 | |||
| 185 | 7 | public function checkSamePlugin($code) |
|
| 186 | { |
||
| 187 | 7 | $repo = $this->app['eccube.repository.plugin']->findOneBy(array('code' => $code)); |
|
| 188 | 7 | if ($repo) { |
|
| 189 | 1 | throw new PluginException('plugin already installed.'); |
|
| 190 | } |
||
| 191 | 7 | } |
|
| 192 | |||
| 193 | 7 | public function calcPluginDir($name) |
|
| 194 | { |
||
| 195 | 7 | return $this->app['config']['plugin_realdir'].'/'.$name; |
|
| 196 | } |
||
| 197 | |||
| 198 | 7 | public function createPluginDir($d) |
|
| 199 | { |
||
| 200 | 7 | $b = @mkdir($d); |
|
| 201 | 7 | if (!$b) { |
|
| 202 | throw new PluginException($php_errormsg); |
||
| 203 | } |
||
| 204 | 7 | } |
|
| 205 | |||
| 206 | 7 | public function registerPlugin($meta, $event_yml, $source = 0) |
|
| 256 | |||
| 257 | 7 | public function callPluginManagerMethod($meta, $method) |
|
| 258 | { |
||
| 259 | 7 | $class = '\\Plugin'.'\\'.$meta['code'].'\\'.'PluginManager'; |
|
| 260 | 7 | if (class_exists($class)) { |
|
| 261 | 3 | $installer = new $class(); // マネージャクラスに所定のメソッドがある場合だけ実行する |
|
| 262 | 3 | if (method_exists($installer, $method)) { |
|
| 263 | 3 | $installer->$method($meta, $this->app); |
|
| 264 | 2 | } |
|
| 265 | 2 | } |
|
| 266 | 6 | } |
|
| 267 | |||
| 268 | 5 | public function uninstall(\Eccube\Entity\Plugin $plugin) |
|
| 269 | { |
||
| 270 | 5 | $pluginDir = $this->calcPluginDir($plugin->getCode()); |
|
| 271 | 5 | $this->app->removePluginConfigCache(); |
|
| 272 | 5 | $this->callPluginManagerMethod(Yaml::parse(file_get_contents($pluginDir.'/'.self::CONFIG_YML)), 'disable'); |
|
| 273 | 5 | $this->callPluginManagerMethod(Yaml::parse(file_get_contents($pluginDir.'/'.self::CONFIG_YML)), 'uninstall'); |
|
| 274 | 5 | $this->unregisterPlugin($plugin); |
|
| 275 | 5 | $this->deleteFile($pluginDir); |
|
| 276 | 5 | $this->app->writePluginConfigCache(); |
|
| 277 | 5 | return true; |
|
| 278 | } |
||
| 279 | |||
| 280 | 5 | public function unregisterPlugin(\Eccube\Entity\Plugin $p) |
|
| 281 | { |
||
| 282 | try { |
||
| 283 | 5 | $em = $this->app['orm.em']; |
|
| 284 | 5 | $em->getConnection()->beginTransaction(); |
|
| 285 | |||
| 286 | 5 | $p->setDelFlg(Constant::ENABLED)->setEnable(Constant::DISABLED); |
|
| 287 | |||
| 288 | 5 | foreach ($p->getPluginEventHandlers()->toArray() as $peh) { |
|
| 289 | 1 | $peh->setDelFlg(Constant::ENABLED); |
|
| 290 | 5 | } |
|
| 291 | |||
| 292 | 5 | $em->persist($p); |
|
| 293 | 5 | $em->flush(); |
|
| 294 | 5 | $em->getConnection()->commit(); |
|
| 295 | 5 | } catch (\Exception $e) { |
|
| 296 | $em->getConnection()->rollback(); |
||
| 297 | throw $e; |
||
| 298 | } |
||
| 299 | 5 | } |
|
| 300 | |||
| 301 | 2 | public function disable(\Eccube\Entity\Plugin $plugin) |
|
| 305 | |||
| 306 | 3 | public function enable(\Eccube\Entity\Plugin $plugin, $enable = true) |
|
| 307 | { |
||
| 308 | 3 | $em = $this->app['orm.em']; |
|
| 309 | try { |
||
| 310 | 3 | $this->app->removePluginConfigCache(); |
|
| 311 | 3 | $pluginDir = $this->calcPluginDir($plugin->getCode()); |
|
| 312 | 3 | $em->getConnection()->beginTransaction(); |
|
| 313 | 3 | $plugin->setEnable($enable ? Constant::ENABLED : Constant::DISABLED); |
|
| 314 | 3 | $em->persist($plugin); |
|
| 315 | 3 | $this->callPluginManagerMethod(Yaml::parse(file_get_contents($pluginDir.'/'.self::CONFIG_YML)), $enable ? 'enable' : 'disable'); |
|
| 326 | |||
| 327 | 1 | public function update(\Eccube\Entity\Plugin $plugin, $path) |
|
| 367 | |||
| 368 | 1 | public function updatePlugin(\Eccube\Entity\Plugin $plugin, $meta, $event_yml) |
|
| 438 | } |
||
| 439 |