PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
PharPluginLoader.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\plugin;
23 
28 
32 class PharPluginLoader implements PluginLoader{
33 
35  private $server;
36 
40  public function __construct(Server $server){
41  $this->server = $server;
42  }
43 
53  public function loadPlugin($file){
54  if(($description = $this->getPluginDescription($file)) instanceof PluginDescription){
55  $this->server->getLogger()->info("Loading " . $description->getFullName());
56  $dataFolder = dirname($file) . DIRECTORY_SEPARATOR . $description->getName();
57  if(file_exists($dataFolder) and !is_dir($dataFolder)){
58  throw new \InvalidStateException("Projected dataFolder '" . $dataFolder . "' for " . $description->getName() . " exists and is not a directory");
59  }
60  $file = "phar://$file";
61  $className = $description->getMain();
62  $this->server->getLoader()->addPath("$file/src");
63 
64  if(class_exists($className, true)){
65  $plugin = new $className();
66  $this->initPlugin($plugin, $description, $dataFolder, $file);
67 
68  return $plugin;
69  }else{
70  throw new PluginException("Couldn't load plugin " . $description->getName() . ": main class not found");
71  }
72  }
73 
74  return null;
75  }
76 
84  public function getPluginDescription($file){
85  $phar = new \Phar($file);
86  if(isset($phar["plugin.yml"])){
87  $pluginYml = $phar["plugin.yml"];
88  if($pluginYml instanceof \PharFileInfo){
89  return new PluginDescription($pluginYml->getContent());
90  }
91  }
92 
93  return null;
94  }
95 
101  public function getPluginFilters(){
102  return "/\\.phar$/i";
103  }
104 
111  private function initPlugin(PluginBase $plugin, PluginDescription $description, $dataFolder, $file){
112  $plugin->init($this, $this->server, $description, $dataFolder, $file);
113  $plugin->onLoad();
114  }
115 
119  public function enablePlugin(Plugin $plugin){
120  if($plugin instanceof PluginBase and !$plugin->isEnabled()){
121  $this->server->getLogger()->info("Enabling " . $plugin->getDescription()->getFullName());
122 
123  $plugin->setEnabled(true);
124 
125  $this->server->getPluginManager()->callEvent(new PluginEnableEvent($plugin));
126  }
127  }
128 
132  public function disablePlugin(Plugin $plugin){
133  if($plugin instanceof PluginBase and $plugin->isEnabled()){
134  $this->server->getLogger()->info("Disabling " . $plugin->getDescription()->getFullName());
135 
136  $this->server->getPluginManager()->callEvent(new PluginDisableEvent($plugin));
137 
138  $plugin->setEnabled(false);
139  }
140  }
141 }