Skip to content

Commit 55bc574

Browse files
committed
Merge pull request #8 from php-http/priority
Name replaced with priority (Fix #5)
2 parents 0755b38 + e6d9e43 commit 55bc574

File tree

5 files changed

+35
-96
lines changed

5 files changed

+35
-96
lines changed

README.md

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,65 +19,9 @@ $ composer require php-http/discovery
1919
```
2020

2121

22-
## Usage
22+
## Documentation
2323

24-
Static containers to ease the auto initialization of objects.
25-
26-
Currently the following discovery strategies are available:
27-
28-
- Http Adapter discovery
29-
- Message Factory discovery
30-
- URI Factory discovery
31-
32-
33-
### Http Adapter discovery
34-
35-
HTTP Adapters provided by us are registered in the discovery by default.
36-
37-
``` php
38-
use Http\Discovery\HttpAdapterDiscovery;
39-
40-
HttpAdapterDiscovery::register('my_adapter', 'My\Adapter\Class');
41-
42-
$adapter = HttpAdapterDiscovery::find();
43-
```
44-
45-
46-
### Message Factory discovery
47-
48-
Two common message factories are bundled with this package. ([Guzzle](https://github.com/guzzle/psr7) and [Diactoros](https://github.com/zendframework/zend-diactoros))
49-
50-
``` php
51-
use Http\Discovery\MessageFactoryDiscovery;
52-
53-
MessageFactoryDiscovery::register('my_factory', 'My\Factory\Class', 'Psr\Request\Implementation\Class');
54-
55-
$factory = MessageFactoryDiscovery::find();
56-
```
57-
58-
59-
### URI Factory discovery
60-
61-
Two common URI factories are bundled with this package: ([Guzzle](https://github.com/guzzle/psr7) and [Diactoros](https://github.com/zendframework/zend-diactoros)).
62-
63-
``` php
64-
use Http\Discovery\UriFactoryDiscovery;
65-
66-
MessageFactoryDiscovery::register('my_factory', 'My\Factory\Class', 'Psr\Uri\Implementation\Class');
67-
68-
$factory = UriFactoryDiscovery::find();
69-
```
70-
71-
72-
### Custom conditions
73-
74-
You can use various types of conditions to check for a class:
75-
76-
- string (checked for class existence)
77-
- callable
78-
- boolean
79-
80-
Any further types are treated as invalid and evaulated to false boolean value.
24+
Please see the [official documentation](http://php-http.readthedocs.org/en/latest/discovery/).
8125

8226

8327
## Testing

spec/ClassDiscoverySpec.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function it_registers_a_class()
2121
{
2222
$this->reset();
2323

24-
$this->register('class1', 'spec\Http\Discovery\AnotherClassToFind');
24+
$this->register('spec\Http\Discovery\AnotherClassToFind');
2525

2626
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
2727
}
@@ -30,8 +30,8 @@ function it_registers_a_class_with_a_condition()
3030
{
3131
$this->reset();
3232

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

3636
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
3737
}
@@ -40,8 +40,8 @@ function it_registers_a_class_with_a_callable_condition()
4040
{
4141
$this->reset();
4242

43-
$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
44-
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', function() { return true; });
43+
$this->register('spec\Http\Discovery\AnotherClassToFind', function() { return true; });
44+
$this->register('spec\Http\Discovery\ClassToFind', false);
4545

4646
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
4747
}
@@ -50,8 +50,8 @@ function it_registers_a_class_with_a_boolean_condition()
5050
{
5151
$this->reset();
5252

53-
$this->register('class1', 'spec\Http\Discovery\ClassToFind', false);
54-
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', true);
53+
$this->register('spec\Http\Discovery\AnotherClassToFind', true);
54+
$this->register('spec\Http\Discovery\ClassToFind', false);
5555

5656
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
5757
}
@@ -60,8 +60,8 @@ function it_registers_a_class_with_an_invalid_condition()
6060
{
6161
$this->reset();
6262

63-
$this->register('class1', 'spec\Http\Discovery\ClassToFind', new \stdClass);
64-
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', true);
63+
$this->register('spec\Http\Discovery\AnotherClassToFind', true);
64+
$this->register('spec\Http\Discovery\ClassToFind', new \stdClass);
6565

6666
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
6767
}
@@ -72,7 +72,7 @@ function it_resets_cache_when_a_class_is_registered()
7272

7373
$this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind');
7474

75-
$this->register('class1', 'spec\Http\Discovery\AnotherClassToFind');
75+
$this->register('spec\Http\Discovery\AnotherClassToFind');
7676

7777
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
7878
}
@@ -83,16 +83,14 @@ function it_caches_a_found_class()
8383

8484
$this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind');
8585

86-
$this->registerWithoutCacheReset('class1', 'spec\Http\Discovery\AnotherClassToFind');
86+
$this->registerWithoutCacheReset('spec\Http\Discovery\AnotherClassToFind');
8787

8888
$this->find()->shouldhaveType('spec\Http\Discovery\ClassToFind');
8989
}
9090

9191
function it_throws_an_exception_when_no_class_is_found()
9292
{
93-
$this->reset();
94-
95-
$this->register('class1', 'invalid');
93+
$this->resetEmpty();
9694

9795
$this->shouldThrow('Http\Discovery\NotFoundException')->duringFind();
9896
}
@@ -115,19 +113,28 @@ public function reset()
115113
static::$cache = null;
116114

117115
static::$classes = [
118-
'class1' => [
116+
[
119117
'class' => 'spec\Http\Discovery\ClassToFind',
120118
'condition' => 'spec\Http\Discovery\ClassToFind'
121119
],
122120
];
123121
}
124122

125-
public function registerWithoutCacheReset($name, $class, $condition = null)
123+
public function registerWithoutCacheReset($class, $condition = null)
126124
{
127-
static::$classes[$name] = [
125+
$definition = [
128126
'class' => $class,
129-
'condition' => $condition ?: $class,
127+
'condition' => isset($condition) ? $condition : $class,
130128
];
129+
130+
array_unshift(static::$classes, $definition);
131+
}
132+
133+
public function resetEmpty()
134+
{
135+
static::$cache = null;
136+
137+
static::$classes = [];
131138
}
132139
}
133140

spec/MessageFactoryDiscoverySpec.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@ function it_is_a_class_discovery()
1616
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
1717
}
1818

19-
function it_finds_guzzle_then_zend_by_default()
19+
function it_finds_an_http_message_factory()
2020
{
21-
$this->find()->shouldHaveType('Http\Discovery\MessageFactory\GuzzleFactory');
22-
23-
$this->register('guzzle', 'invalid', '');
24-
25-
if (class_exists('Zend\Diactoros\Request')) {
26-
$this->find()->shouldHaveType('Http\Discovery\MessageFactory\DiactorosFactory');
27-
}
21+
$this->find()->shouldHaveType('Http\Message\MessageFactory');
2822
}
2923
}

spec/UriFactoryDiscoverySpec.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace spec\Http\Discovery;
44

55
use PhpSpec\ObjectBehavior;
6-
use Prophecy\Argument;
76

87
class UriFactoryDiscoverySpec extends ObjectBehavior
98
{
@@ -17,14 +16,8 @@ function it_is_a_class_discovery()
1716
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
1817
}
1918

20-
function it_finds_guzzle_then_zend_by_default()
19+
function it_finds_an_http_uri_factory()
2120
{
22-
$this->find()->shouldHaveType('Http\Discovery\UriFactory\GuzzleFactory');
23-
24-
$this->register('guzzle', 'invalid', '');
25-
26-
if (class_exists('Zend\Diactoros\Request')) {
27-
$this->find()->shouldHaveType('Http\Discovery\UriFactory\DiactorosFactory');
28-
}
21+
$this->find()->shouldHaveType('Http\Message\UriFactory');
2922
}
3023
}

src/ClassDiscovery.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ abstract class ClassDiscovery
2121
/**
2222
* Add a class (and condition) to the discovery registry
2323
*
24-
* @param string $name
2524
* @param string $class Class that will be instantiated if found
2625
* @param string $condition Optional other class to check for existence
2726
*/
28-
public static function register($name, $class, $condition = null)
27+
public static function register($class, $condition = null)
2928
{
3029
static::$cache = null;
3130

32-
static::$classes[$name] = [
31+
$definition = [
3332
'class' => $class,
3433
'condition' => isset($condition) ? $condition : $class,
3534
];
35+
36+
array_unshift(static::$classes, $definition);
3637
}
3738

3839
/**

0 commit comments

Comments
 (0)