Issue #2204345 by tim.plunkett: Adjust PluginBag's instance handling methods.
parent
780add0535
commit
dd4931b012
|
@ -115,8 +115,10 @@ abstract class PluginBag implements \Iterator, \Countable {
|
|||
*
|
||||
* @param string $id
|
||||
* The ID of the plugin instance to add.
|
||||
* @param array|null $configuration
|
||||
* (optional) The configuration used by this instance. Defaults to NULL.
|
||||
*/
|
||||
public function addInstanceId($id) {
|
||||
public function addInstanceId($id, $configuration = NULL) {
|
||||
if (!isset($this->instanceIDs[$id])) {
|
||||
$this->instanceIDs[$id] = $id;
|
||||
}
|
||||
|
@ -132,21 +134,11 @@ abstract class PluginBag implements \Iterator, \Countable {
|
|||
return $this->instanceIDs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all instance IDs.
|
||||
*
|
||||
* @param array $instance_ids
|
||||
* An associative array of instance IDs.
|
||||
*/
|
||||
public function setInstanceIds(array $instance_ids) {
|
||||
$this->instanceIDs = $instance_ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an instance ID.
|
||||
*
|
||||
* @param string $instance_id
|
||||
* An image effect instance IDs.
|
||||
* The ID of the plugin instance to remove.
|
||||
*/
|
||||
public function removeInstanceId($instance_id) {
|
||||
unset($this->instanceIDs[$instance_id]);
|
||||
|
|
|
@ -138,15 +138,6 @@ class DefaultPluginBag extends PluginBag {
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setInstanceIds(array $instance_ids) {
|
||||
parent::setInstanceIds($instance_ids);
|
||||
// Ensure the new order matches the original order.
|
||||
$this->instanceIDs = $this->originalOrder = array_intersect_assoc($this->originalOrder, $this->instanceIDs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the configuration for a plugin instance.
|
||||
*
|
||||
|
@ -166,6 +157,19 @@ class DefaultPluginBag extends PluginBag {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addInstanceId($id, $configuration = NULL) {
|
||||
parent::addInstanceId($id);
|
||||
if ($configuration !== NULL) {
|
||||
$this->setInstanceConfiguration($id, $configuration);
|
||||
}
|
||||
if (!isset($this->originalOrder[$id])) {
|
||||
$this->originalOrder[$id] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -93,4 +93,14 @@ class DefaultSinglePluginBag extends PluginBag {
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addInstanceId($id, $configuration = NULL) {
|
||||
parent::addInstanceId($id, $configuration);
|
||||
if ($configuration !== NULL) {
|
||||
$this->setConfiguration($configuration);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -42,8 +42,7 @@ class ImageEffectBag extends DefaultPluginBag {
|
|||
$configuration['uuid'] = $uuid_generator->generate();
|
||||
}
|
||||
$instance_id = $configuration['uuid'];
|
||||
$this->setInstanceConfiguration($instance_id, $configuration);
|
||||
$this->addInstanceId($instance_id);
|
||||
$this->addInstanceId($instance_id, $configuration);
|
||||
return $instance_id;
|
||||
}
|
||||
|
||||
|
|
|
@ -127,6 +127,34 @@ class DefaultPluginBagTest extends PluginBagTestBase {
|
|||
$this->assertSame($expected, array_keys($ids), 'After sorting, the order of the instances is also sorted.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the addInstanceId() method.
|
||||
*/
|
||||
public function testAddInstanceId() {
|
||||
$this->setupPluginBag($this->exactly(4));
|
||||
$expected = array(
|
||||
'banana' => 'banana',
|
||||
'cherry' => 'cherry',
|
||||
'apple' => 'apple',
|
||||
);
|
||||
$this->defaultPluginBag->addInstanceId('apple');
|
||||
$result = $this->defaultPluginBag->getInstanceIds();
|
||||
$this->assertSame($expected, $result);
|
||||
$this->assertSame($expected, array_intersect_key($result, $this->defaultPluginBag->getConfiguration()));
|
||||
|
||||
$expected = array(
|
||||
'cherry' => 'cherry',
|
||||
'apple' => 'apple',
|
||||
'banana' => 'banana',
|
||||
);
|
||||
$this->defaultPluginBag->removeInstanceId('banana');
|
||||
$this->defaultPluginBag->addInstanceId('banana', $this->config['banana']);
|
||||
|
||||
$result = $this->defaultPluginBag->getInstanceIds();
|
||||
$this->assertSame($expected, $result);
|
||||
$this->assertSame($expected, array_intersect_key($result, $this->defaultPluginBag->getConfiguration()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the removeInstanceId() method.
|
||||
*
|
||||
|
@ -175,27 +203,6 @@ class DefaultPluginBagTest extends PluginBagTestBase {
|
|||
$this->defaultPluginBag->getConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the setInstanceIds() method.
|
||||
*/
|
||||
public function testSetInstanceIds() {
|
||||
$this->setupPluginBag($this->any());
|
||||
// Set the instance IDs in a different order than the original.
|
||||
$this->defaultPluginBag->setInstanceIds(array(
|
||||
'apple' => 'apple',
|
||||
'cherry' => 'cherry',
|
||||
));
|
||||
|
||||
$expected = array(
|
||||
'cherry' => 'cherry',
|
||||
'apple' => 'apple',
|
||||
);
|
||||
$config = $this->defaultPluginBag->getConfiguration();
|
||||
$instance_ids = $this->defaultPluginBag->getInstanceIds();
|
||||
$this->assertSame($expected, $instance_ids);
|
||||
$this->assertSame(array_keys($expected), array_keys($config));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the set() method.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue