Skip to content

Commit

Permalink
Adds a default container for box().
Browse files Browse the repository at this point in the history
  • Loading branch information
jails committed Feb 18, 2016
1 parent 09bd16f commit 8b32502
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
21 changes: 19 additions & 2 deletions spec/Suite/BoxSpec.php
Expand Up @@ -279,7 +279,7 @@ public function __construct()
box(false);
});

it("adds aa box", function() {
it("adds a box", function() {

$box = new Box();
$actual = box('box.spec', $box);
Expand All @@ -296,6 +296,23 @@ public function __construct()
expect($actual)->toBe($box);
});

it("adds a default box", function() {

$box = new Box();

expect(box($box))->toBe($box);
expect(box())->toBe($box);

});

it("gets a default box", function() {

$box = box();
expect($box)->toBeAn('object');
expect(box())->toBe($box);

});

it("removes a box", function() {

$box = new Box();
Expand Down Expand Up @@ -333,4 +350,4 @@ public function __construct()
expect($closure)->toThrow("Unexisting box `'box.spec'`.");
});

});
});
37 changes: 24 additions & 13 deletions src/init.php
@@ -1,5 +1,6 @@
<?php
use box\BoxException;
use Lead\Box\BoxException;
use Lead\Box\Box;

$defineFuctions = true;

Expand All @@ -14,23 +15,33 @@
if ($defineFuctions) {
define('BOX_FUNCTIONS_EXIST', true);

function box($name, $box = null) {
function box($name = '', $box = null) {
static $boxes = [];
if ($name === false) {
$boxes = [];
return;
}
if ($box === false) {
unset($boxes[$name]);
return;

if (func_num_args() === 1) {
if ($name === false) {
$boxes = [];
return;
}
if (is_object($name)) {
return $boxes[''] = $name;
}
if (isset($boxes[$name])) {
return $boxes[$name];
}
throw new BoxException("Unexisting box `'{$name}'`.");
}
if ($box) {
if (func_num_args() === 2) {
if ($box === false) {
unset($boxes[$name]);
return;
}
return $boxes[$name] = $box;
}
if (isset($boxes[$name])) {
return $boxes[$name];
if (!isset($boxes[''])) {
$boxes[''] = new Box();
}
throw new BoxException("Unexisting box `'{$name}'`.");
return $boxes[''];
}

}

0 comments on commit 8b32502

Please sign in to comment.