Skip to content

Commit 008a1cf

Browse files
committed
Conditions can now be more than just class names
1 parent 388f134 commit 008a1cf

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

spec/ClassDiscoverySpec.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,42 @@ function it_registers_a_class_with_a_condition()
3030
{
3131
$this->reset();
3232

33-
$this->register('class1', 'spec\Http\Discovery\ClassToFind', 'invalid');
33+
$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
3434
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', 'spec\Http\Discovery\TestClass');
3535

3636
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
3737
}
3838

39+
function it_registers_a_class_with_a_callable_condition()
40+
{
41+
$this->reset();
42+
43+
$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
44+
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', function() { return true; });
45+
46+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
47+
}
48+
49+
function it_registers_a_class_with_a_boolean_condition()
50+
{
51+
$this->reset();
52+
53+
$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
54+
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', true);
55+
56+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
57+
}
58+
59+
function it_registers_a_class_with_an_invalid_condition()
60+
{
61+
$this->reset();
62+
63+
$this->register('class1', 'spec\Http\Discovery\ClassToFind', new \stdClass);
64+
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', true);
65+
66+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
67+
}
68+
3969
function it_resets_cache_when_a_class_is_registered()
4070
{
4171
$this->reset();

src/ClassDiscovery.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function register($name, $class, $condition = null)
3131

3232
static::$classes[$name] = [
3333
'class' => $class,
34-
'condition' => $condition ?: $class,
34+
'condition' => isset($condition) ? $condition : $class,
3535
];
3636
}
3737

@@ -50,7 +50,7 @@ public static function find()
5050
}
5151

5252
foreach (static::$classes as $name => $definition) {
53-
if (class_exists($definition['condition'])) {
53+
if (static::evaluateCondition($definition['condition'])) {
5454
static::$cache = $definition['class'];
5555

5656
return new $definition['class'];
@@ -59,4 +59,25 @@ public static function find()
5959

6060
throw new NotFoundException('Not found');
6161
}
62+
63+
/**
64+
* Evaulates conditions to boolean
65+
*
66+
* @param mixed $condition
67+
*
68+
* @return boolean
69+
*/
70+
protected static function evaluateCondition($condition)
71+
{
72+
if (is_string($condition)) {
73+
// Should be extended for functions, extensions???
74+
return class_exists($condition);
75+
} elseif (is_callable($condition)) {
76+
return $condition();
77+
} elseif (is_bool($condition)) {
78+
return $condition;
79+
}
80+
81+
return false;
82+
}
6283
}

0 commit comments

Comments
 (0)