diff --git a/model/PluginRegister.php b/model/PluginRegister.php
index b0583835b..4635c50e5 100644
--- a/model/PluginRegister.php
+++ b/model/PluginRegister.php
@@ -5,6 +5,10 @@ class PluginRegister {
public function __construct($requestedPlugins=array()) {
$this->requestedPlugins = $requestedPlugins;
+ $this->pluginOrder = array();
+ foreach ($this->requestedPlugins as $index => $value) {
+ $this->pluginOrder[$value] = $index;
+ }
}
/**
@@ -24,6 +28,16 @@ protected function getPlugins()
return $plugins;
}
+ /**
+ * Sort plugins by order defined in class constructor
+ * @return array
+ */
+ private function sortPlugins($plugins) {
+ $sortedPlugins = array();
+ $sortedPlugins = array_replace($this->pluginOrder, $plugins);
+ return $sortedPlugins;
+ }
+
/**
* Returns the plugin configurations found from plugin folders
* inside the plugins folder filtered by filetype.
@@ -33,6 +47,7 @@ protected function getPlugins()
*/
private function filterPlugins($type, $raw=false) {
$plugins = $this->getPlugins();
+ $plugins = $this->sortPlugins($plugins);
$ret = array();
if (!empty($plugins)) {
foreach ($plugins as $name => $files) {
@@ -130,21 +145,40 @@ public function getTemplates($names=null) {
}
/**
- * Returns an array of javascript function names to call when loading pages
+ * Returns an array of plugin callback function names
+ * @param array $names the plugin name strings (foldernames) in an array
+ * @return array
+ */
+ public function getPluginCallbacks($names=null) {
+ if ($names) {
+ $names = array_merge($this->requestedPlugins, $names);
+ return $this->filterPluginsByName('callback', $names);
+ }
+ return $this->filterPluginsByName('callback', $this->requestedPlugins);
+ }
+
+ /**
+ * Returns a sorted array of javascript function names to call when loading pages
+ * in order configured with skosmos:vocabularyPlugins
* @return array
*/
public function getCallbacks() {
$ret = array();
- $plugins = $this->filterPluginsByName('callback', $this->requestedPlugins);
+ $sortedCallbacks = array();
+ $plugins = $this->getPluginCallbacks($this->requestedPlugins);
foreach ($plugins as $callbacks) {
foreach ($callbacks as $callback) {
$split = explode('/', $callback);
- $ret[] = $split[2];
+ $sortedCallbacks[$split[1]] = $split[2];
}
}
+ $sortedCallbacks = array_replace($this->pluginOrder, $sortedCallbacks);
+ foreach ($sortedCallbacks as $callback) {
+ $ret[] = $callback;
+ }
return $ret;
}
-
+
/**
* Returns an array that is flattened from its possibly multidimensional form
* copied from https://stackoverflow.com/a/1320156
diff --git a/model/VocabularyConfig.php b/model/VocabularyConfig.php
index e33dc3249..32c6111a8 100644
--- a/model/VocabularyConfig.php
+++ b/model/VocabularyConfig.php
@@ -33,7 +33,6 @@ public function __construct($resource, $globalPlugins=array())
{
$this->resource = $resource;
$this->globalPlugins = $globalPlugins;
- $this->setParameterizedPlugins();
$this->setPropertyLabelOverrides();
$pluginArray = $this->getPluginArray();
$this->pluginRegister = new PluginRegister($pluginArray);
@@ -45,6 +44,7 @@ public function __construct($resource, $globalPlugins=array())
*/
public function getPluginArray() : array
{
+ $this->setParameterizedPlugins();
$pluginArray = array();
$vocabularyPlugins = $this->resource->getResource('skosmos:vocabularyPlugins');
if (!$vocabularyPlugins instanceof EasyRdf\Collection) {
@@ -62,6 +62,12 @@ public function getPluginArray() : array
}
$pluginArray = array_merge($pluginArray, $this->globalPlugins);
+ $paramPlugins = $this->resource->allResources('skosmos:useParamPlugin');
+ if ($paramPlugins) {
+ foreach ($paramPlugins as $plugin) {
+ $pluginArray[] = $plugin->getLiteral('skosmos:usePlugin')->getValue();
+ }
+ }
$plugins = $this->resource->allLiterals('skosmos:usePlugin');
if ($plugins) {
foreach ($plugins as $pluginlit) {
diff --git a/tests/PluginRegisterTest.php b/tests/PluginRegisterTest.php
index a60c86f70..8d4e1b5ee 100644
--- a/tests/PluginRegisterTest.php
+++ b/tests/PluginRegisterTest.php
@@ -8,9 +8,44 @@ class PluginRegisterTest extends PHPUnit\Framework\TestCase
protected function setUp() : void
{
- $this->mockpr = $this->getMockBuilder('PluginRegister')->setConstructorArgs(array(array('global-plugin')))->setMethods(array('getPlugins'))->getMock();
- $stubplugs = array ('test-plugin' => array ( 'js' => array ( 0 => 'first.js', 1 => 'second.min.js', ), 'css' => array ( 0 => 'stylesheet.css', ), 'templates' => array ( 0 => 'template.html', ), ), 'only-css' => array ( 'css' => array ( 0 => 'super.css')), 'global-plugin' => array('js' => array('everywhere.js')));
- $this->mockpr->method('getPlugins')->will($this->returnValue($stubplugs));
+ $this->mockpr = $this->getMockBuilder('PluginRegister')->setConstructorArgs(array(array('test-plugin2',
+ 'global-plugin-Bravo',
+ 'imaginary-plugin',
+ 'test-plugin1',
+ 'global-plugin-alpha',
+ 'global-plugin-charlie',
+ 'test-plugin3'
+ )))
+ ->setMethods(['getPlugins'])
+ ->getMock();
+ $this->stubplugs = array ('imaginary-plugin' => array ( 'js' => array ( 0 => 'imaginaryPlugin.js', ),
+ 'css' => array ( 0 => 'stylesheet.css', ),
+ 'templates' => array ( 0 => 'template.html', ),
+ 'callback' => array ( 0 => 'imaginaryPlugin')
+ ),
+ 'test-plugin1' => array ( 'js' => array ( 0 => 'plugin1.js', 1 => 'second.min.js'),
+ 'css' => array ( 0 => 'stylesheet.css', ),
+ 'templates' => array ( 0 => 'template.html', ),
+ 'callback' => array ( 0 => 'callplugin1')
+ ),
+ 'test-plugin2' => array ( 'js' => array ( 0 => 'plugin2.js', ),
+ 'css' => array ( 0 => 'stylesheet.css', ),
+ 'templates' => array ( 0 => 'template.html', ),
+ 'callback' => array ( 0 => 'callplugin2')
+ ),
+ 'test-plugin3' => array ( 'js' => array ( 0 => 'plugin3.js', ),
+ 'css' => array ( 0 => 'stylesheet.css', ),
+ 'templates' => array ( 0 => 'template.html', ),
+ 'callback' => array ( 0 => 'callplugin3')
+ ),
+ 'only-css' => array ( 'css' => array ( 0 => 'super.css')),
+ 'global-plugin-alpha' => array('js' => array('alpha.js'),
+ 'callback' => array ( 0 => 'alpha')),
+ 'global-plugin-Bravo' => array('js' => array('Bravo.js'),
+ 'callback' => array ( 0 => 'bravo')),
+ 'global-plugin-charlie' => array('js' => array('charlie.js'),
+ 'callback' => array ( 0 => 'charlie')));
+ $this->mockpr->method('getPlugins')->will($this->returnValue($this->stubplugs));
$this->model = new Model(new GlobalConfig('/../tests/testconfig.ttl'));
$this->vocab = $this->model->getVocabulary('test');
}
@@ -39,10 +74,32 @@ public function testGetPluginsJS()
* @covers PluginRegister::getPluginsJS
* @covers PluginRegister::filterPlugins
* @covers PluginRegister::filterPluginsByName
+ * @covers PluginRegister::sortPlugins
+ */
+ public function testGetPluginsJSInOrder()
+ {
+ $this->assertEquals(['test-plugin2',
+ 'global-plugin-Bravo',
+ 'imaginary-plugin',
+ 'test-plugin1',
+ 'global-plugin-alpha',
+ 'global-plugin-charlie',
+ 'test-plugin3'],
+ array_keys($this->mockpr->getPluginsJS()));
+
+ }
+
+ /**
+ * @covers PluginRegister::getPlugins
+ * @covers PluginRegister::getPluginsJS
+ * @covers PluginRegister::filterPlugins
+ * @covers PluginRegister::filterPluginsByName
+ * @covers PluginRegister::sortPlugins
*/
public function testGetPluginsJSWithName()
{
- $this->assertEquals(array('global-plugin' => array('plugins/global-plugin/everywhere.js'), 'test-plugin' => array('plugins/test-plugin/first.js', 'plugins/test-plugin/second.min.js')), $this->mockpr->getPluginsJS(array('test-plugin')));
+ $this->assertEquals(array('plugins/test-plugin1/plugin1.js', 'plugins/test-plugin1/second.min.js'),
+ $this->mockpr->getPluginsJS()['test-plugin1']);
}
/**
@@ -50,10 +107,12 @@ public function testGetPluginsJSWithName()
* @covers PluginRegister::getPluginsJS
* @covers PluginRegister::filterPlugins
* @covers PluginRegister::filterPluginsByName
+ * @covers PluginRegister::sortPlugins
*/
public function testGetPluginsJSWithGlobalPlugin()
{
- $this->assertEquals(array('global-plugin' => array('plugins/global-plugin/everywhere.js')), $this->mockpr->getPluginsJS());
+ $this->assertEquals(array('plugins/global-plugin-alpha/alpha.js'),
+ $this->mockpr->getPluginsJS()['global-plugin-alpha']);
}
/**
@@ -69,11 +128,25 @@ public function testGetPluginsCSS()
* @covers PluginRegister::getPluginsCSS
* @covers PluginRegister::filterPlugins
* @covers PluginRegister::filterPluginsByName
+ * @covers PluginRegister::sortPlugins
*/
public function testGetPluginsCSSWithName()
+ {
+ $this->assertEquals(array('plugins/test-plugin1/stylesheet.css'),
+ $this->mockpr->getPluginsCSS()['test-plugin1']);
+ }
+
+ /**
+ * @covers PluginRegister::getPluginCallbacks
+ * @covers PluginRegister::filterPlugins
+ * @covers PluginRegister::filterPluginsByName
+ * @covers PluginRegister::sortPlugins
+ */
+ public function testGetPluginCallbacks()
{
$plugins = new PluginRegister();
- $this->assertEquals(array('test-plugin' => array('plugins/test-plugin/stylesheet.css')), $this->mockpr->getPluginsCSS(array('test-plugin')));
+ $this->assertEquals(array('plugins/test-plugin1/callplugin1'),
+ $this->mockpr->getPluginCallbacks()['test-plugin1']);
}
/**
@@ -99,8 +172,9 @@ public function testGetTemplates()
*/
public function testGetCallbacks()
{
- $plugins = new PluginRegister();
- $this->assertEquals(array(), $plugins->getCallbacks());
+ $this->assertEquals(array('callplugin2', 'bravo', 'imaginaryPlugin', 'callplugin1', 'alpha', 'charlie', 'callplugin3'),
+ $this->mockpr->getCallbacks()
+ );
}
}
diff --git a/tests/VocabularyConfigTest.php b/tests/VocabularyConfigTest.php
index e87440883..15ecb9c6c 100644
--- a/tests/VocabularyConfigTest.php
+++ b/tests/VocabularyConfigTest.php
@@ -541,7 +541,7 @@ public function testGetVocabularyUseModifiedDate() {
* @covers VocabularyConfig::getPluginParameters
*/
public function testGetPluginParameters() {
- $vocab = $this->model->getVocabulary('paramPluginTest');
+ $vocab = $this->model->getVocabulary('paramPluginOrderTest');
$params = $vocab->getConfig()->getPluginParameters();
$this->assertEquals(array('imaginaryPlugin' => array('poem_fi' => "Roses are red", 'poem' => "Violets are blue", 'color' => "#800000")), $params);
}
@@ -550,16 +550,16 @@ public function testGetPluginParameters() {
* @covers VocabularyConfig::getPluginArray
*/
public function testGetOrderedPlugins() {
- $vocab = $this->model->getVocabulary('paramPluginTest');
+ $vocab = $this->model->getVocabulary('paramPluginOrderTest');
$plugins = $vocab->getConfig()->getPluginArray();
- $this->assertEquals(["plugin2", "Bravo", "imaginaryPlugin", "plugin1", "alpha", "charlie", "plugin3"], $plugins);
+ $this->assertEquals(["plugin2", "Bravo", "plugin1", "alpha", "charlie", "imaginaryPlugin", "plugin3"], $plugins);
}
/**
* @covers VocabularyConfig::getPluginArray
*/
public function testGetUnorderedVocabularyPlugins() {
- $vocab = $this->model->getVocabulary('paramPluginOrderTest');
+ $vocab = $this->model->getVocabulary('paramPluginTest');
$plugins = $vocab->getConfig()->getPluginArray();
$arrayElements = ["plugin2", "Bravo", "imaginaryPlugin", "plugin1", "alpha", "charlie", "plugin3"];
$this->assertEquals(sort($arrayElements), sort($plugins));
diff --git a/tests/testconfig.ttl b/tests/testconfig.ttl
index 923f404ec..924da08db 100644
--- a/tests/testconfig.ttl
+++ b/tests/testconfig.ttl
@@ -362,7 +362,7 @@
skosmos:sparqlGraph
{% trans %}There is no term for this concept in this language{% endtrans %}
{% endif %} +