Skip to content

Commit 6131fd0

Browse files
committed
Add URI factory (fix #3)
1 parent ed926a4 commit 6131fd0

File tree

7 files changed

+186
-72
lines changed

7 files changed

+186
-72
lines changed

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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
protected static $cache;
13+
protected static $classes = [];
14+
15+
/**
16+
* Register a factory
17+
*
18+
* @param string $name
19+
* @param string $condition
20+
* @param string $class
21+
*/
22+
public static function register($name, $condition, $class = null)
23+
{
24+
static::$cache = null;
25+
26+
static::$classes[$name] = [
27+
'condition' => $condition,
28+
'class' => $class ?: $condition,
29+
];
30+
}
31+
32+
/**
33+
* Finds a Factory
34+
*
35+
* @return object
36+
*
37+
* @throws NotFoundException
38+
*/
39+
public static function find()
40+
{
41+
// We have a cache
42+
if (isset(static::$cache)) {
43+
return static::$cache;
44+
}
45+
46+
foreach (static::$classes as $name => $definition) {
47+
if (class_exists($definition['condition'])) {
48+
return static::$cache = new $definition['class'];
49+
}
50+
}
51+
52+
throw new NotFoundException('Not found');
53+
}
54+
}

src/HttpAdapterDiscovery.php

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,44 @@
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
/**
2224
* @var array
2325
*/
24-
protected static $adapters = [
25-
'guzzle6' => 'Http\Adapter\Guzzle6HttpAdapter',
26-
'guzzle5' => 'Http\Adapter\Guzzle5HttpAdapter',
26+
protected static $classes = [
27+
'guzzle6' => [
28+
'condition' => 'Http\Adapter\Guzzle6HttpAdapter',
29+
'class' => 'Http\Adapter\Guzzle6HttpAdapter'
30+
31+
],
32+
'guzzle5' => [
33+
'condition' => 'Http\Adapter\Guzzle5HttpAdapter',
34+
'class' => 'Http\Adapter\Guzzle6HttpAdapter'
35+
],
2736
];
2837

2938
/**
3039
* @var string
3140
*/
3241
protected static $cache;
3342

34-
/**
35-
* Register an HTTP Adapter
36-
*
37-
* @param string $name
38-
* @param string $class
39-
*/
40-
public static function register($name, $class)
41-
{
42-
static::$cache = null;
43-
44-
static::$adapters[$name] = $class;
45-
}
46-
4743
/**
4844
* Finds an HTTP Adapter
4945
*
50-
* @return object
46+
* @return HttpAdapter
5147
*
5248
* @throws NotFoundException
5349
*/
5450
public static function find()
5551
{
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');
52+
return parent::find();
6853
}
6954
}

src/MessageFactoryDiscovery.php

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,69 +11,38 @@
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
{
2123
/**
2224
* @var array
2325
*/
24-
protected static $messageFactories = [
26+
protected static $factories = [
2527
'guzzle' => [
26-
'class' => 'GuzzleHttp\Psr7\Request',
27-
'factory' => 'Http\Discovery\MessageFactory\GuzzleFactory',
28+
'condition'=> 'GuzzleHttp\Psr7\Request',
29+
'class' => 'Http\Discovery\MessageFactory\GuzzleFactory',
2830
],
2931
'diactoros' => [
30-
'class' => 'Zend\Diactoros\Request',
31-
'factory' => 'Http\Discovery\MessageFactory\DiactorosFactory',
32+
'condition' => 'Zend\Diactoros\Request',
33+
'class' => 'Http\Discovery\MessageFactory\DiactorosFactory',
3234
],
3335
];
3436

3537
/**
36-
* @var string
37-
*/
38-
protected static $cache;
39-
40-
/**
41-
* Register a Message Factory
38+
* Find Message Factory
4239
*
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
40+
* @return MessageFactory
6141
*
6242
* @throws NotFoundException
6343
*/
6444
public static function find()
6545
{
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');
46+
return parent::find();
7847
}
7948
}

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\Uri;
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 new Uri($uri);
21+
}
22+
}

src/UriFactoryDiscovery.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Http\Discovery;
4+
5+
use Http\Message\UriFactory;
6+
7+
/**
8+
* Finds a URI Factory
9+
*
10+
* @author David de Boer <[email protected]>
11+
*/
12+
class UriFactoryDiscovery extends ClassDiscovery
13+
{
14+
/**
15+
* @var array
16+
*/
17+
protected static $classes = [
18+
'guzzle' => [
19+
'condition' => 'GuzzleHttp\Psr7\Uri',
20+
'class' => 'Http\Discovery\UriFactory\GuzzleFactory',
21+
],
22+
'diactoros' => [
23+
'condition' => 'Zend\Diactoros\Uri',
24+
'class' => 'Http\Discovery\UriFactory\DiactorosFactory',
25+
],
26+
];
27+
28+
/**
29+
* Find URI Factory
30+
*
31+
* @return UriFactory
32+
*/
33+
public static function find()
34+
{
35+
return parent::find();
36+
}
37+
}

0 commit comments

Comments
 (0)