SemanticMediaWiki / SemanticResultFormats

Provides additional visualizations (result formats) for Semantic MediaWiki
https://www.semantic-mediawiki.org/wiki/Extension:Semantic_Result_Formats
Other
45 stars 76 forks source link

Provide a method to register local resources #320

Open mwjames opened 7 years ago

mwjames commented 7 years ago

Issue

Currently, all resources are defined in Resource.php which becomes rather "stuffed", so I was thinking what about an extra internal setting to map the location of a format specific Resource.php and do a require_once with the registration of a format?

Proposed solution

$GLOBALS['srfgFormatResources'] = [
 formatXyz => location/Resource.php
]
@@ -210,10 +210,16 @@ class SemanticResultFormats {
                if ( isset( $GLOBALS['smwgResultAliases'] ) && array_key_exists( $format, $formatAliases ) ) {
                    $GLOBALS['smwgResultAliases'][$format] = $formatAliases[$format];
                }
            }
        }
+
+       foreach ( $GLOBALS['srfgFormats'] as $format ) {
+           if ( isset( $GLOBALS['srfgFormatResources'][$format] ) && is_readable( $GLOBALS['srfgFormatResources'][$format] ) ) {
+               $GLOBALS['wgResourceModules'] = array_merge( $GLOBALS['wgResourceModules'], include( $GLOBALS['srfgFormatResources'][$format] ) );
+           }
+       }
    }
s7eph4n commented 7 years ago

I agree that we have an issue with a bloated Resources.php. But I really don't like the introduction of a new global.

What about something like ResultPrinter::getResources()?

mwjames commented 7 years ago

What about something like ResultPrinter::getResources()?

Then you would have path dependencies within a class object since you have to define the access to the files somehow. Of course, you could use a convention to overcome this but registration of $GLOBALS['wgResourceModules'] should not happen from within ResultPrinter so this needs to happen when SemanticResultFormats is loaded.

s7eph4n commented 7 years ago

Yes, we'd need a convention. I'd argue ResultPrinters should give paths in relation to their root folder. This way we could nudge them towards being better contained and staying in their folder.

And registration does not need to happen from within the ResultPrinter. You could still use that foreach loop, just instead of include( $GLOBALS['srfgFormatResources'][$format] ) you'd have something like ResultPrinter::getResources().