summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Mermaid/src/HookRegistry.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Mermaid/src/HookRegistry.php')
-rw-r--r--www/wiki/extensions/Mermaid/src/HookRegistry.php116
1 files changed, 116 insertions, 0 deletions
diff --git a/www/wiki/extensions/Mermaid/src/HookRegistry.php b/www/wiki/extensions/Mermaid/src/HookRegistry.php
new file mode 100644
index 00000000..f6fac542
--- /dev/null
+++ b/www/wiki/extensions/Mermaid/src/HookRegistry.php
@@ -0,0 +1,116 @@
+<?php
+
+namespace Mermaid;
+
+use Hooks;
+
+/**
+ * @license GNU GPL v2+
+ * @since 1.0
+ *
+ * @author mwjames
+ */
+class HookRegistry {
+
+ /**
+ * @var array
+ */
+ private $handlers = array();
+
+ /**
+ * @since 1.0
+ *
+ * @param array $configuration
+ */
+ public function __construct( $configuration = array () ) {
+ $this->registerCallbackHandlers( $configuration );
+ }
+
+ /**
+ * @since 1.0
+ */
+ public function register() {
+ foreach ( $this->handlers as $name => $callback ) {
+ Hooks::register( $name, $callback );
+ }
+ }
+
+ /**
+ * @since 1.0
+ */
+ public function deregister() {
+ foreach ( array_keys( $this->handlers ) as $name ) {
+ Hooks::clear( $name );
+ }
+ }
+
+ /**
+ * @since 1.0
+ *
+ * @param string $name
+ *
+ * @return Callable|false
+ */
+ public function getHandlerFor( $name ) {
+ return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
+ }
+
+ /**
+ * @since 1.0
+ *
+ * @param string $name
+ *
+ * @return boolean
+ */
+ public function isRegistered( $name ) {
+ return Hooks::isRegistered( $name );
+ }
+
+ private function registerCallbackHandlers( $configuration ) {
+
+ /**
+ * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
+ */
+ $this->handlers['ParserFirstCallInit'] = function ( &$parser ) {
+ $defaultTheme = $GLOBALS['mermaidgDefaultTheme'];
+
+ $parser->setFunctionHook(
+ 'mermaid',
+ MermaidParserFunction::newCallback( $defaultTheme ),
+ 0
+ );
+
+ return true;
+ };
+
+ /**
+ * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
+ */
+ $this->handlers['ResourceLoaderGetConfigVars'] = function ( &$vars ) {
+
+ $vars['mermaid'] = [
+ 'theme' => $GLOBALS['mermaidgDefaultTheme']
+ ];
+
+ return true;
+ };
+
+ /**
+ * @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput
+ */
+ $this->handlers['OutputPageParserOutput'] = function ( $out, $parserOutput ) {
+
+ $out->addModuleStyles( 'ext.mermaid.styles' );
+
+ if ( $parserOutput->getExtensionData( 'ext-mermaid' ) === null ||
+ $parserOutput->getExtensionData( 'ext-mermaid' ) === false ) {
+ return true;
+ }
+
+ $out->addModules( 'ext.mermaid' );
+
+ return true;
+ };
+ }
+
+}