Plugin.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Grav\Common;
  3. use Grav\Common\Data\Data;
  4. use Grav\Common\Page\Page;
  5. use Grav\Common\Config\Config;
  6. use RocketTheme\Toolbox\Event\EventDispatcher;
  7. use RocketTheme\Toolbox\Event\EventSubscriberInterface;
  8. /**
  9. * The Plugin object just holds the id and path to a plugin.
  10. *
  11. * @author RocketTheme
  12. * @license MIT
  13. */
  14. class Plugin implements EventSubscriberInterface
  15. {
  16. /**
  17. * @var Grav
  18. */
  19. protected $grav;
  20. /**
  21. * @var Config
  22. */
  23. protected $config;
  24. protected $active = true;
  25. /**
  26. * @var \Grav\Common\string
  27. */
  28. protected $name;
  29. /**
  30. * By default assign all methods as listeners using the default priority.
  31. *
  32. * @return array
  33. */
  34. public static function getSubscribedEvents()
  35. {
  36. $methods = get_class_methods(get_called_class());
  37. $list = array();
  38. foreach ($methods as $method) {
  39. if (strpos($method, 'on') === 0) {
  40. $list[$method] = [$method, 0];
  41. }
  42. }
  43. return $list;
  44. }
  45. /**
  46. * Constructor.
  47. *
  48. * @param string $name
  49. * @param Grav $grav
  50. * @param Config $config
  51. */
  52. public function __construct($name, Grav $grav, Config $config)
  53. {
  54. $this->grav = $grav;
  55. $this->config = $config;
  56. $this->name = $name;
  57. }
  58. public function isAdmin()
  59. {
  60. if (isset($this->grav['admin'])) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. * @param array $events
  67. */
  68. protected function enable(array $events)
  69. {
  70. /** @var EventDispatcher $dispatcher */
  71. $dispatcher = $this->grav['events'];
  72. foreach ($events as $eventName => $params) {
  73. if (is_string($params)) {
  74. $dispatcher->addListener($eventName, array($this, $params));
  75. } elseif (is_string($params[0])) {
  76. $dispatcher->addListener($eventName, array($this, $params[0]), isset($params[1]) ? $params[1] : 0);
  77. } else {
  78. foreach ($params as $listener) {
  79. $dispatcher->addListener($eventName, array($this, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * @param array $events
  86. */
  87. protected function disable(array $events)
  88. {
  89. /** @var EventDispatcher $dispatcher */
  90. $dispatcher = $this->grav['events'];
  91. foreach ($events as $eventName => $params) {
  92. if (is_string($params)) {
  93. $dispatcher->removeListener($eventName, array($this, $params));
  94. } elseif (is_string($params[0])) {
  95. $dispatcher->removeListener($eventName, array($this, $params[0]));
  96. } else {
  97. foreach ($params as $listener) {
  98. $dispatcher->removeListener($eventName, array($this, $listener[0]));
  99. }
  100. }
  101. }
  102. }
  103. /**
  104. * This function will search a string for markdown links in a specific format. The link value can be
  105. * optionally compared against via the $internal_regex and operated on by the callback $function
  106. * provided.
  107. *
  108. * format: [plugin:myplugin_name](function_data)
  109. *
  110. * @param $content The string to perform operations upon
  111. * @param $function The anonymous callback function
  112. * @param string $internal_regex Optional internal regex to extra data from
  113. *
  114. * @return string
  115. */
  116. protected function parseLinks($content, $function, $internal_regex = '(.*)')
  117. {
  118. $regex = '/\[plugin:(?:'.$this->name.')\]\('.$internal_regex.'\)/i';
  119. return preg_replace_callback($regex, $function, $content);
  120. }
  121. /**
  122. * Merge global and page configurations.
  123. *
  124. * @param Page $page The page to merge the configurations with the
  125. * plugin settings.
  126. * @param bool $deep Should you use deep or shallow merging
  127. * @param array $params Array of additional configuration options to
  128. * merge with the plugin settings.
  129. *
  130. * @return \Grav\Common\Data\Data
  131. */
  132. protected function mergeConfig(Page $page, $deep = false, $params = [])
  133. {
  134. $class_name = $this->name;
  135. $class_name_merged = $class_name . '.merged';
  136. $defaults = $this->config->get('plugins.'. $class_name, []);
  137. $page_header = $page->header();
  138. $header = [];
  139. if (!isset($page_header->$class_name_merged) && isset($page_header->$class_name)) {
  140. // Get default plugin configurations and retrieve page header configuration
  141. $config = $page_header->$class_name;
  142. if (is_bool($config)) {
  143. // Overwrite enabled option with boolean value in page header
  144. $config = ['enabled' => $config];
  145. }
  146. // Merge page header settings using deep or shallow merging technique
  147. if ($deep) {
  148. $header = array_replace_recursive($defaults, $config);
  149. } else {
  150. $header = array_merge($defaults, $config);
  151. }
  152. // Create new config object and set it on the page object so it's cached for next time
  153. $page->modifyHeader($class_name_merged, new Data($header));
  154. } else if (isset($page_header->$class_name_merged)) {
  155. $merged = $page_header->$class_name_merged;
  156. $header = $merged->toArray();
  157. }
  158. if (empty($header)) {
  159. $header = $defaults;
  160. }
  161. // Merge additional parameter with configuration options
  162. if ($deep) {
  163. $header = array_replace_recursive($header, $params);
  164. } else {
  165. $header = array_merge($header, $params);
  166. }
  167. // Return configurations as a new data config class
  168. return new Data($header);
  169. }
  170. }