Skip to content

Commit 033c51d

Browse files
committed
Merge pull request #4 from ddeboer/uri-factory
Add URI factory (fix #3)
2 parents ed926a4 + b4b5f66 commit 033c51d

10 files changed

+226
-81
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ HttpAdapterDiscovery::register('my_adapter', 'My\Adapter\Class');
4242
$adapter = HttpAdapterDiscovery::find();
4343
```
4444

45-
4645
### Message Factory discovery
4746

4847
Two common message factories are bundled with this package. ([Guzzle](https://github.com/guzzle/psr7), [Diactoros](https://github.com/zendframework/zend-diactoros))
@@ -52,9 +51,23 @@ use Http\Discovery\MessageFactoryDiscovery;
5251

5352
MessageFactoryDiscovery::register('my_factory', 'Psr\Request\Implementation\Class', 'My\Factory\Class');
5453

55-
$adapter = MessageFactoryDiscovery::find();
54+
$factory = MessageFactoryDiscovery::find();
5655
```
5756

57+
### URI Factory discovery
58+
59+
Two common URI factories are bundled with this package: ([Guzzle](https://github.com/guzzle/psr7)
60+
and [Diactoros](https://github.com/zendframework/zend-diactoros)).
61+
62+
``` php
63+
use Http\Discovery\UriFactoryDiscovery;
64+
65+
MessageFactoryDiscovery::register('my_factory', 'Psr\Uri\Implementation\Class', 'My\Factory\Class');
66+
67+
$factory = UriFactoryDiscovery::find();
68+
```
69+
70+
5871

5972
## Testing
6073

spec/HttpAdapterDiscoverySpec.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace spec\Http\Discovery;
44

5+
use Http\Discovery\MessageFactoryDiscovery;
6+
use Http\Discovery\UriFactoryDiscovery;
57
use PhpSpec\ObjectBehavior;
68

79
class HttpAdapterDiscoverySpec extends ObjectBehavior

spec/MessageFactoryDiscoverySpec.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function it_registers_a_factory()
1515
{
1616
$this->reset();
1717

18-
$this->register('guzzle', 'spec\Http\Discovery\TestClass', 'spec\Http\Discovery\Factory');
18+
$this->register('guzzle', 'spec\Http\Discovery\Factory', 'spec\Http\Discovery\TestClass');
1919

2020
$this->find()->shouldHaveType('spec\Http\Discovery\Factory');
2121
}
@@ -26,7 +26,7 @@ function it_resets_cache_when_a_factory_is_registered()
2626

2727
$firstGuess = $this->find();
2828

29-
$this->register('guzzle', 'spec\Http\Discovery\TestClass', 'spec\Http\Discovery\Factory');
29+
$this->register('guzzle', 'spec\Http\Discovery\Factory', 'spec\Http\Discovery\TestClass');
3030

3131
$this->find()->shouldNotBe($firstGuess);
3232
}
@@ -65,8 +65,8 @@ function it_throws_an_exception_when_no_message_factory_is_found()
6565

6666
function reset()
6767
{
68-
$this->register('guzzle', 'GuzzleHttp\Psr7\Request', 'Http\Discovery\MessageFactory\GuzzleFactory');
69-
$this->register('diactoros', 'Zend\Diactoros\Request', 'Http\Discovery\MessageFactory\DiactorosFactory');
68+
$this->register('guzzle', 'Http\Discovery\MessageFactory\GuzzleFactory', 'GuzzleHttp\Psr7\Request');
69+
$this->register('diactoros', 'Http\Discovery\MessageFactory\DiactorosFactory', 'Zend\Diactoros\Request');
7070
}
7171
}
7272

spec/UriFactoryDiscoverySpec.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace spec\Http\Discovery;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class UriFactoryDiscoverySpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('Http\Discovery\UriFactoryDiscovery');
13+
}
14+
15+
function it_finds_guzzle_then_zend_by_default()
16+
{
17+
$this->find()->shouldHaveType('Http\Discovery\UriFactory\GuzzleFactory');
18+
19+
$this->register('guzzle', 'invalid', '');
20+
21+
if (class_exists('Zend\Diactoros\Request')) {
22+
$this->find()->shouldHaveType('Http\Discovery\UriFactory\DiactorosFactory');
23+
}
24+
}
25+
}

src/ClassDiscovery.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Http\Discovery;
4+
5+
/**
6+
* Registry that based find results on class existence
7+
*
8+
* @author David de Boer <[email protected]>
9+
*/
10+
abstract class ClassDiscovery
11+
{
12+
/**
13+
* Add a condition (and class) to the discovery registry
14+
*
15+
* @param string $name
16+
* @param string $class Class that will be instantiated if found
17+
* @param string $condition Optional other class to check for existence
18+
*/
19+
public static function register($name, $class, $condition = null)
20+
{
21+
static::$cache = null;
22+
23+
static::$classes[$name] = [
24+
'class' => $class,
25+
'condition' => $condition ?: $class,
26+
];
27+
}
28+
29+
/**
30+
* Finds a Factory
31+
*
32+
* @return object
33+
*
34+
* @throws NotFoundException
35+
*/
36+
public static function find()
37+
{
38+
// We have a cache
39+
if (isset(static::$cache)) {
40+
return static::$cache;
41+
}
42+
43+
foreach (static::$classes as $name => $definition) {
44+
if (class_exists($definition['condition'])) {
45+
return static::$cache = new $definition['class'];
46+
}
47+
}
48+
49+
throw new NotFoundException('Not found');
50+
}
51+
}

src/HttpAdapterDiscovery.php

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,47 @@
1111

1212
namespace Http\Discovery;
1313

14+
use Http\Adapter\HttpAdapter;
15+
1416
/**
1517
* Finds an HTTP Adapter
1618
*
1719
* @author Márk Sági-Kazár <[email protected]>
1820
*/
19-
class HttpAdapterDiscovery
21+
class HttpAdapterDiscovery extends ClassDiscovery
2022
{
2123
/**
22-
* @var array
23-
*/
24-
protected static $adapters = [
25-
'guzzle6' => 'Http\Adapter\Guzzle6HttpAdapter',
26-
'guzzle5' => 'Http\Adapter\Guzzle5HttpAdapter',
27-
];
28-
29-
/**
30-
* @var string
24+
* Cached adapter
25+
*
26+
* @var HttpAdapter
3127
*/
3228
protected static $cache;
3329

3430
/**
35-
* Register an HTTP Adapter
36-
*
37-
* @param string $name
38-
* @param string $class
31+
* @var array
3932
*/
40-
public static function register($name, $class)
41-
{
42-
static::$cache = null;
43-
44-
static::$adapters[$name] = $class;
45-
}
33+
protected static $classes = [
34+
'guzzle6' => [
35+
'class' => 'Http\Adapter\Guzzle6HttpAdapter',
36+
'condition' => 'Http\Adapter\Guzzle6HttpAdapter'
37+
38+
],
39+
'guzzle5' => [
40+
'class' => 'Http\Adapter\Guzzle5HttpAdapter',
41+
'condition' => 'Http\Adapter\Guzzle6HttpAdapter'
42+
],
43+
];
4644

4745
/**
4846
* Finds an HTTP Adapter
4947
*
50-
* @return object
48+
* @return HttpAdapter
5149
*
5250
* @throws NotFoundException
5351
*/
5452
public static function find()
5553
{
56-
// We have a cache
57-
if (isset(static::$cache)) {
58-
return static::$cache;
59-
}
60-
61-
foreach (static::$adapters as $name => $class) {
62-
if (class_exists($class)) {
63-
return static::$cache = new $class;
64-
}
65-
}
66-
67-
throw new NotFoundException('No HTTP Adapter found');
54+
// Override only used for return type declaration
55+
return parent::find();
6856
}
6957
}

src/MessageFactoryDiscovery.php

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,69 +11,46 @@
1111

1212
namespace Http\Discovery;
1313

14+
use Http\Message\MessageFactory;
15+
1416
/**
1517
* Finds a Message Factory
1618
*
1719
* @author Márk Sági-Kazár <[email protected]>
1820
*/
19-
class MessageFactoryDiscovery
21+
class MessageFactoryDiscovery extends ClassDiscovery
2022
{
23+
/**
24+
* Cached factory
25+
*
26+
* @var MessageFactory
27+
*/
28+
protected static $cache;
29+
2130
/**
2231
* @var array
2332
*/
24-
protected static $messageFactories = [
33+
protected static $classes = [
2534
'guzzle' => [
26-
'class' => 'GuzzleHttp\Psr7\Request',
27-
'factory' => 'Http\Discovery\MessageFactory\GuzzleFactory',
35+
'class' => 'Http\Discovery\MessageFactory\GuzzleFactory',
36+
'condition'=> 'GuzzleHttp\Psr7\Request',
2837
],
2938
'diactoros' => [
30-
'class' => 'Zend\Diactoros\Request',
31-
'factory' => 'Http\Discovery\MessageFactory\DiactorosFactory',
39+
'class' => 'Http\Discovery\MessageFactory\DiactorosFactory',
40+
'condition' => 'Zend\Diactoros\Request',
3241
],
3342
];
3443

3544
/**
36-
* @var string
37-
*/
38-
protected static $cache;
39-
40-
/**
41-
* Register a Message Factory
45+
* Find Message Factory
4246
*
43-
* @param string $name
44-
* @param string $class
45-
* @param string $factory
46-
*/
47-
public static function register($name, $class, $factory)
48-
{
49-
static::$cache = null;
50-
51-
static::$messageFactories[$name] = [
52-
'class' => $class,
53-
'factory' => $factory,
54-
];
55-
}
56-
57-
/**
58-
* Finds a Message Factory
59-
*
60-
* @return object
47+
* @return MessageFactory
6148
*
6249
* @throws NotFoundException
6350
*/
6451
public static function find()
6552
{
66-
// We have a cache
67-
if (isset(static::$cache)) {
68-
return static::$cache;
69-
}
70-
71-
foreach (static::$messageFactories as $name => $definition) {
72-
if (class_exists($definition['class'])) {
73-
return static::$cache = new $definition['factory'];
74-
}
75-
}
76-
77-
throw new NotFoundException('No Message Factory found');
53+
// Override only used for return type declaration
54+
return parent::find();
7855
}
7956
}

src/UriFactory/DiactorosFactory.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Http\Discovery\UriFactory;
4+
5+
use Zend\Diactoros\Uri;
6+
use Http\Message\UriFactory;
7+
8+
/**
9+
* Creates a zend/diactoros URI object
10+
*
11+
* @author David de Boer <[email protected]>
12+
*/
13+
class DiactorosFactory implements UriFactory
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function createUri($uri)
19+
{
20+
return new Uri($uri);
21+
}
22+
}

src/UriFactory/GuzzleFactory.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Http\Discovery\UriFactory;
4+
5+
use GuzzleHttp\Psr7;
6+
use Http\Message\UriFactory;
7+
8+
/**
9+
* Creates a guzzlehttp/psr7 URI object
10+
*
11+
* @author David de Boer <[email protected]>
12+
*/
13+
class GuzzleFactory implements UriFactory
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function createUri($uri)
19+
{
20+
return Psr7\uri_for($uri);
21+
}
22+
}

0 commit comments

Comments
 (0)