Skip to content

Commit

Permalink
Allow resource store via options
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jun 3, 2019
1 parent a17c2c6 commit 3a70053
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ResourceManager
const MAP_EXTERNAL = 'e';

const OPT_THROW_ON_FILE_NOT_FOUND = 'throw.file.not.found';
const OPT_RESOURCE_STORE = 'resource.store';

protected $_type = self::MAP_RESOURCES;
protected $_mapOptions = [];
Expand All @@ -59,6 +60,10 @@ public function __construct($type, array $mapOptions = [], array $options = [])
{
$this->_type = $type;
$this->_mapOptions = $mapOptions;
foreach($options as $option => $optionValue)
{
$this->setOption($option, $optionValue);
}
$this->_options = $options;
$this->_baseUri = array_merge([$type], $mapOptions);
}
Expand All @@ -76,6 +81,17 @@ public function hasResourceStore(): bool
return $this->_store !== null;
}

/**
* Remove any custom resource store set, and write to the global store
*
* @return $this
*/
public function useGlobalResourceStore()
{
$this->_store = null;
return $this;
}

/**
* @param ResourceStore $store
*
Expand Down Expand Up @@ -162,6 +178,10 @@ protected static function _componentManager($fullClass, Dispatch $dispatch = nul

public function setOption($option, $value)
{
if($option === self::OPT_RESOURCE_STORE && $value instanceof ResourceStore)
{
$this->setResourceStore($value);
}
$this->_options[$option] = $value;
return $this;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/ResourceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,22 @@ public function testSetResourceStore()
$this->assertSame($store2, $manager->getResourceStore());
$this->assertNotSame($store1, $manager->getResourceStore());
}

public function testSetResourceStoreConfig()
{
$store1 = new ResourceStore();
$store2 = new ResourceStore();

$this->assertFalse(ResourceManager::resources()->hasResourceStore());

$manager = ResourceManager::resources([ResourceManager::OPT_RESOURCE_STORE => $store1]);
$this->assertTrue($manager->hasResourceStore());

$manager->useGlobalResourceStore();
$this->assertFalse($manager->hasResourceStore());

$manager->setOption(ResourceManager::OPT_RESOURCE_STORE, $store1);
$this->assertSame($store1, $manager->getResourceStore());
$this->assertNotSame($store2, $manager->getResourceStore());
}
}

0 comments on commit 3a70053

Please sign in to comment.