Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Sep 9, 2017
2 parents c707c92 + 01ee5b7 commit 88d9d91
Show file tree
Hide file tree
Showing 9 changed files with 605 additions and 538 deletions.
469 changes: 469 additions & 0 deletions CHANGELOG-HISTORY.md

Large diffs are not rendered by default.

475 changes: 10 additions & 465 deletions CHANGELOG.md

Large diffs are not rendered by default.

52 changes: 8 additions & 44 deletions src/Framework/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace Parable\Framework;

class Config
class Config extends \Parable\GetSet\Base
{
/** @var string */
protected $resource = 'parable_config';

/** @var bool */
protected $useLocalResource = true;

/** @var string */
protected $mainConfigClass = \Config\App::class;

/** @var \Parable\Filesystem\Path */
protected $path;

/** @var array */
protected $config = [];

public function __construct(
\Parable\Filesystem\Path $path
) {
Expand All @@ -34,45 +37,6 @@ public function setMainConfigClassName($className)
return $this;
}

/**
* @param array $data
* @param array $keys
*
* @return mixed
*/
public function getNested(array &$data, array $keys)
{
foreach ($keys as $key) {
$data = &$data[$key];
}
return $data;
}

/**
* @param string $key
*
* @return mixed
*/
public function get($key)
{
$config = $this->getAll();
if (strpos($key, '.') !== false) {
return $this->getNested($config, explode('.', $key));
}
if (isset($config[$key])) {
return $config[$key];
}
return null;
}

/**
* @return array
*/
public function getAll()
{
return $this->config;
}

/**
* @return $this
*/
Expand Down Expand Up @@ -101,7 +65,7 @@ public function load()
*/
public function addConfig(\Parable\Framework\Interfaces\Config $config)
{
$this->config = array_merge($this->config, $config->get());
$this->setMany($config->get());
return $this;
}
}
67 changes: 43 additions & 24 deletions src/GetSet/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ public function getAll()
/**
* Get specific value by key if resource set
*
* $getSet->get("one.two.three", "value") would return $resource["one"]["two"]["three"];
*
* @param string $key
*
* @return mixed|null
*/
public function get($key)
{
if ($this->getResource()) {
// If local resource, set it as reference
if ($this->useLocalResource) {
$reference = $this->localResource;
} else {
$reference = $GLOBALS[$this->getResource()];
}
$resource = $this->getAll();

// Now check reference and whether the key exists
if (isset($reference[$key])) {
return $reference[$key];
$keys = explode(".", $key);
foreach ($keys as $key) {
if (!isset($resource[$key])) {
$resource = null;
break;
}
$resource = &$resource[$key];
}
return null;

return $resource;
}

/**
Expand Down Expand Up @@ -97,7 +97,9 @@ public function count()
}

/**
* Set specific value by key if resource set
* Set specific value by key if resource set. It's possible to set using . to separate keys by depth.
*
* $getSet->set("one.two.three", "value") is equal to $resource["one"]["two"]["three"] = $value;
*
* @param string $key
* @param mixed $value
Expand All @@ -106,14 +108,21 @@ public function count()
*/
public function set($key, $value)
{
if ($this->getResource()) {
// Decide where to store this key/value pair
if ($this->useLocalResource) {
$this->localResource[$key] = $value;
} else {
$GLOBALS[$this->getResource()][$key] = $value;
$keys = explode(".", $key);

$data = $this->getAll();

$resource = &$data;
foreach ($keys as $key) {
if (!isset($resource[$key]) || !is_array($resource[$key])) {
$resource[$key] = [];
}
$resource = &$resource[$key];
}
$resource = $value;

$this->setAll($data);

return $this;
}

Expand Down Expand Up @@ -160,14 +169,24 @@ public function setAll(array $values)
*/
public function remove($key)
{
if ($this->getResource()) {
// Decide where to store this key/value pair
if ($this->useLocalResource) {
unset($this->localResource[$key]);
} else {
unset($GLOBALS[$this->getResource()][$key]);
$keys = explode(".", $key);

$data = $this->getAll();

$resource = &$data;
foreach ($keys as $index => $key) {
if (!isset($resource[$key])) {
// We bail, the requested key could not be found
return $this;
}
if ($index < (count($keys) - 1)) {
$resource = &$resource[$key];
}
}
unset($resource[$key]);

$this->setAll($data);

return $this;
}

Expand Down
5 changes: 4 additions & 1 deletion src/GetSet/InputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
class InputStream extends \Parable\GetSet\Base
{
/** @var string */
protected $resource = 'input_stream';
protected $resource = 'parable_input_stream';

/** @var bool */
protected $useLocalResource = true;

/** @var string */
protected $inputSource = 'php://input';
Expand Down
4 changes: 2 additions & 2 deletions src/GetSet/Internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
class Internal extends \Parable\GetSet\Base
{
/** @var string */
protected $resource = 'internal';
protected $resource = 'parable_internal';

/** @var bool */
protected $useLocalResource = true;
}
67 changes: 67 additions & 0 deletions tests/Components/GetSet/GetSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,71 @@ public function testGetAllReturnsEmptyArrayIfNoResourceSet()
$getset = new \Parable\Tests\TestClasses\TestGetSetNoResource();
$this->assertSame([], $getset->getAll());
}

public function testGetSetAndRemoveWithHierarchalKeys()
{
$this->getSet->reset();

$this->getSet->set("one", ["this" => "should stay"]);
$this->getSet->set("one.two.three.four", "totally nested, yo");

$this->assertSame(
[
"this" => "should stay",
"two" => [
"three" => [
"four" => "totally nested, yo",
],
],
],
$this->getSet->get("one")
);

$this->assertSame(
[
"one" => [
"this" => "should stay",
"two" => [
"three" => [
"four" => "totally nested, yo",
],
],
],
],
$this->getSet->getAll()
);

$this->getSet->remove("one.this");

$this->assertSame(
[
"one" => [
"two" => [
"three" => [
"four" => "totally nested, yo",
],
],
],
],
$this->getSet->getAll()
);

$this->assertSame(
[
"four" => "totally nested, yo",
],
$this->getSet->getAndRemove("one.two.three")
);

// And since "three" is now removed, "two" will be empty.
$this->assertSame(
[
"one" => [
"two" => [
],
],
],
$this->getSet->getAll()
);
}
}
2 changes: 1 addition & 1 deletion tests/Components/GetSet/InputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function setJsonSource()

/**
* @param string $path
*
*
* @return \Parable\GetSet\InputStream
*/
protected function setSource($path)
Expand Down
2 changes: 1 addition & 1 deletion tests/Components/GetSet/InternalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function setUp()

public function testGetResource()
{
$this->assertSame("internal", $this->getSet->getResource());
$this->assertSame("parable_internal", $this->getSet->getResource());
}

public function testGetSetOnResource()
Expand Down

0 comments on commit 88d9d91

Please sign in to comment.