Plugin.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * @package Grav.Common
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  9. use Grav\Common\Data\Data;
  10. use Grav\Common\Page\Page;
  11. use Grav\Common\Config\Config;
  12. use RocketTheme\Toolbox\Event\EventDispatcher;
  13. use RocketTheme\Toolbox\Event\EventSubscriberInterface;
  14. use RocketTheme\Toolbox\File\YamlFile;
  15. use Symfony\Component\Console\Exception\LogicException;
  16. class Plugin implements EventSubscriberInterface, \ArrayAccess
  17. {
  18. /**
  19. * @var string
  20. */
  21. public $name;
  22. /**
  23. * @var array
  24. */
  25. public $features = [];
  26. /**
  27. * @var Grav
  28. */
  29. protected $grav;
  30. /**
  31. * @var Config
  32. */
  33. protected $config;
  34. protected $active = true;
  35. protected $blueprint;
  36. /**
  37. * By default assign all methods as listeners using the default priority.
  38. *
  39. * @return array
  40. */
  41. public static function getSubscribedEvents()
  42. {
  43. $methods = get_class_methods(get_called_class());
  44. $list = [];
  45. foreach ($methods as $method) {
  46. if (strpos($method, 'on') === 0) {
  47. $list[$method] = [$method, 0];
  48. }
  49. }
  50. return $list;
  51. }
  52. /**
  53. * Constructor.
  54. *
  55. * @param string $name
  56. * @param Grav $grav
  57. * @param Config $config
  58. */
  59. public function __construct($name, Grav $grav, Config $config = null)
  60. {
  61. $this->name = $name;
  62. $this->grav = $grav;
  63. if ($config) {
  64. $this->setConfig($config);
  65. }
  66. }
  67. /**
  68. * @param Config $config
  69. * @return $this
  70. */
  71. public function setConfig(Config $config)
  72. {
  73. $this->config = $config;
  74. return $this;
  75. }
  76. /**
  77. * Get configuration of the plugin.
  78. *
  79. * @return array
  80. */
  81. public function config()
  82. {
  83. return $this->config["plugins.{$this->name}"];
  84. }
  85. /**
  86. * Determine if this is running under the admin
  87. *
  88. * @return bool
  89. */
  90. public function isAdmin()
  91. {
  92. return Utils::isAdminPlugin();
  93. }
  94. /**
  95. * Determine if this route is in Admin and active for the plugin
  96. *
  97. * @param $plugin_route
  98. * @return bool
  99. */
  100. protected function isPluginActiveAdmin($plugin_route)
  101. {
  102. $should_run = false;
  103. $uri = $this->grav['uri'];
  104. if (strpos($uri->path(), $this->config->get('plugins.admin.route') . '/' . $plugin_route) === false) {
  105. $should_run = false;
  106. } elseif (isset($uri->paths()[1]) && $uri->paths()[1] === $plugin_route) {
  107. $should_run = true;
  108. }
  109. return $should_run;
  110. }
  111. /**
  112. * @param array $events
  113. */
  114. protected function enable(array $events)
  115. {
  116. /** @var EventDispatcher $dispatcher */
  117. $dispatcher = $this->grav['events'];
  118. foreach ($events as $eventName => $params) {
  119. if (is_string($params)) {
  120. $dispatcher->addListener($eventName, [$this, $params]);
  121. } elseif (is_string($params[0])) {
  122. $dispatcher->addListener($eventName, [$this, $params[0]], isset($params[1]) ? $params[1] : 0);
  123. } else {
  124. foreach ($params as $listener) {
  125. $dispatcher->addListener($eventName, [$this, $listener[0]], isset($listener[1]) ? $listener[1] : 0);
  126. }
  127. }
  128. }
  129. }
  130. /**
  131. * @param array $events
  132. */
  133. protected function disable(array $events)
  134. {
  135. /** @var EventDispatcher $dispatcher */
  136. $dispatcher = $this->grav['events'];
  137. foreach ($events as $eventName => $params) {
  138. if (is_string($params)) {
  139. $dispatcher->removeListener($eventName, [$this, $params]);
  140. } elseif (is_string($params[0])) {
  141. $dispatcher->removeListener($eventName, [$this, $params[0]]);
  142. } else {
  143. foreach ($params as $listener) {
  144. $dispatcher->removeListener($eventName, [$this, $listener[0]]);
  145. }
  146. }
  147. }
  148. }
  149. /**
  150. * Whether or not an offset exists.
  151. *
  152. * @param mixed $offset An offset to check for.
  153. * @return bool Returns TRUE on success or FALSE on failure.
  154. */
  155. public function offsetExists($offset)
  156. {
  157. $this->loadBlueprint();
  158. if ($offset === 'title') {
  159. $offset = 'name';
  160. }
  161. return isset($this->blueprint[$offset]);
  162. }
  163. /**
  164. * Returns the value at specified offset.
  165. *
  166. * @param mixed $offset The offset to retrieve.
  167. * @return mixed Can return all value types.
  168. */
  169. public function offsetGet($offset)
  170. {
  171. $this->loadBlueprint();
  172. if ($offset === 'title') {
  173. $offset = 'name';
  174. }
  175. return isset($this->blueprint[$offset]) ? $this->blueprint[$offset] : null;
  176. }
  177. /**
  178. * Assigns a value to the specified offset.
  179. *
  180. * @param mixed $offset The offset to assign the value to.
  181. * @param mixed $value The value to set.
  182. * @throws LogicException
  183. */
  184. public function offsetSet($offset, $value)
  185. {
  186. throw new LogicException(__CLASS__ . ' blueprints cannot be modified.');
  187. }
  188. /**
  189. * Unsets an offset.
  190. *
  191. * @param mixed $offset The offset to unset.
  192. * @throws LogicException
  193. */
  194. public function offsetUnset($offset)
  195. {
  196. throw new LogicException(__CLASS__ . ' blueprints cannot be modified.');
  197. }
  198. /**
  199. * This function will search a string for markdown links in a specific format. The link value can be
  200. * optionally compared against via the $internal_regex and operated on by the callback $function
  201. * provided.
  202. *
  203. * format: [plugin:myplugin_name](function_data)
  204. *
  205. * @param string $content The string to perform operations upon
  206. * @param callable $function The anonymous callback function
  207. * @param string $internal_regex Optional internal regex to extra data from
  208. *
  209. * @return string
  210. */
  211. protected function parseLinks($content, $function, $internal_regex = '(.*)')
  212. {
  213. $regex = '/\[plugin:(?:' . $this->name . ')\]\(' . $internal_regex . '\)/i';
  214. return preg_replace_callback($regex, $function, $content);
  215. }
  216. /**
  217. * Merge global and page configurations.
  218. *
  219. * @param Page $page The page to merge the configurations with the
  220. * plugin settings.
  221. * @param mixed $deep false = shallow|true = recursive|merge = recursive+unique
  222. * @param array $params Array of additional configuration options to
  223. * merge with the plugin settings.
  224. * @param string $type Is this 'plugins' or 'themes'
  225. *
  226. * @return Data
  227. */
  228. protected function mergeConfig(Page $page, $deep = false, $params = [], $type = 'plugins')
  229. {
  230. $class_name = $this->name;
  231. $class_name_merged = $class_name . '.merged';
  232. $defaults = $this->config->get($type . '.' . $class_name, []);
  233. $page_header = $page->header();
  234. $header = [];
  235. if (!isset($page_header->$class_name_merged) && isset($page_header->$class_name)) {
  236. // Get default plugin configurations and retrieve page header configuration
  237. $config = $page_header->$class_name;
  238. if (is_bool($config)) {
  239. // Overwrite enabled option with boolean value in page header
  240. $config = ['enabled' => $config];
  241. }
  242. // Merge page header settings using deep or shallow merging technique
  243. $header = $this->mergeArrays($deep, $defaults, $config);
  244. // Create new config object and set it on the page object so it's cached for next time
  245. $page->modifyHeader($class_name_merged, new Data($header));
  246. } else if (isset($page_header->$class_name_merged)) {
  247. $merged = $page_header->$class_name_merged;
  248. $header = $merged->toArray();
  249. }
  250. if (empty($header)) {
  251. $header = $defaults;
  252. }
  253. // Merge additional parameter with configuration options
  254. $header = $this->mergeArrays($deep, $header, $params);
  255. // Return configurations as a new data config class
  256. return new Data($header);
  257. }
  258. /**
  259. * Merge arrays based on deepness
  260. *
  261. * @param bool $deep
  262. * @param $array1
  263. * @param $array2
  264. * @return array|mixed
  265. */
  266. private function mergeArrays($deep = false, $array1, $array2)
  267. {
  268. if ($deep === 'merge') {
  269. return Utils::arrayMergeRecursiveUnique($array1, $array2);
  270. }
  271. if ($deep === true) {
  272. return array_replace_recursive($array1, $array2);
  273. }
  274. return array_merge($array1, $array2);
  275. }
  276. /**
  277. * Persists to disk the plugin parameters currently stored in the Grav Config object
  278. *
  279. * @param string $plugin_name The name of the plugin whose config it should store.
  280. *
  281. * @return true
  282. */
  283. public static function saveConfig($plugin_name)
  284. {
  285. if (!$plugin_name) {
  286. return false;
  287. }
  288. $grav = Grav::instance();
  289. $locator = $grav['locator'];
  290. $filename = 'config://plugins/' . $plugin_name . '.yaml';
  291. $file = YamlFile::instance($locator->findResource($filename, true, true));
  292. $content = $grav['config']->get('plugins.' . $plugin_name);
  293. $file->save($content);
  294. $file->free();
  295. return true;
  296. }
  297. /**
  298. * Simpler getter for the plugin blueprint
  299. *
  300. * @return mixed
  301. */
  302. public function getBlueprint()
  303. {
  304. if (!$this->blueprint) {
  305. $this->loadBlueprint();
  306. }
  307. return $this->blueprint;
  308. }
  309. /**
  310. * Load blueprints.
  311. */
  312. protected function loadBlueprint()
  313. {
  314. if (!$this->blueprint) {
  315. $grav = Grav::instance();
  316. $plugins = $grav['plugins'];
  317. $this->blueprint = $plugins->get($this->name)->blueprints();
  318. }
  319. }
  320. }