Skip to content

Commit

Permalink
Add NoFragmentDefined
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Feb 18, 2017
1 parent 8993501 commit 4bb0e67
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
9 changes: 6 additions & 3 deletions README.md
Expand Up @@ -44,10 +44,12 @@ The following example demonstrates how a closure can be used to synthesize multi
$config = $config->synthesize('core', function(array $fragments) {

return call_user_func_array('ICanBoogie\array_merge_recursive', $fragments);

});
```

The exception [NoFragmentDefined][] is thrown when no fragment of a specified type is defined.




Expand Down Expand Up @@ -220,5 +222,6 @@ The package is continuously tested by [Travis CI](http://about.travis-ci.org/).
[icanboogie/event]: https://github.com/ICanBoogie/Event
[ICanBoogie]: https://github.com/ICanBoogie

[Config]: http://api.icanboogie.org/config/1.1/class-ICanBoogie.Config.html
[Storage]: http://api.icanboogie.org/storage/2.0/class-ICanBoogie.Storage.Storage.html
[Config]: https://icanboogie.org/api/config/1.1/class-ICanBoogie.Config.html
[Storage]: https://icanboogie.org/api/storage/2.0/class-ICanBoogie.Storage.Storage.html
[NoFragmentDefined]: https://icanboogie.org/api/config/master/class-ICanBoogie.NoFragmentDefined.html
9 changes: 8 additions & 1 deletion lib/Config.php
Expand Up @@ -11,6 +11,7 @@

namespace ICanBoogie;

use ICanBoogie\Config\NoFragmentDefined;
use ICanBoogie\Storage\Storage;

/**
Expand Down Expand Up @@ -289,13 +290,19 @@ public function synthesize($name, $synthesizer, $from = null)
return $this->synthesized[$name] = $config;
}

/**
* @param string $name
* @param callable $synthesizer
*
* @return mixed
*/
private function synthesize_for_real($name, $synthesizer)
{
$fragments = $this->get_fragments($name);

if (!$fragments)
{
return null;
throw new NoFragmentDefined($name);
}

if ($synthesizer == 'merge')
Expand Down
20 changes: 20 additions & 0 deletions lib/Config/Exception.php
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\Config;

/**
* Config exceptions implement this interface so that they can be easily recognized.
*/
interface Exception
{

}
37 changes: 37 additions & 0 deletions lib/Config/NoFragmentDefined.php
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\Config;

/**
* Exception throw when no fragment of a specified type is defined.
*/
class NoFragmentDefined extends \LogicException implements Exception
{
/**
* @param string $fragment
* @param \Exception|null $previous
*/
public function __construct($fragment, \Exception $previous = null)
{
parent::__construct($this->format_message($fragment), 500, $previous);
}

/**
* @param $fragment
*
* @return string
*/
protected function format_message($fragment)
{
return "There is not `$fragment` fragment defined.";
}
}
7 changes: 5 additions & 2 deletions tests/ConfigTest.php
Expand Up @@ -27,10 +27,13 @@ static public function setupBeforeClass()
];
}

public function test_synthesize_undefined()
/**
* @expectedException \ICanBoogie\Config\NoFragmentDefined
*/
public function test_should_throw_exception_on_undefined_fragment()
{
$configs = new Config(self::$paths);
$this->assertNull($configs->synthesize('core', 'merge'));
$configs->synthesize(uniqid(), 'merge');
}

public function test_synthesize_with_array_merge()
Expand Down

0 comments on commit 4bb0e67

Please sign in to comment.