Skip to content

Commit

Permalink
Add URI factory (fix #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeboer committed Jun 5, 2015
1 parent ed926a4 commit 7db7946
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 74 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ HttpAdapterDiscovery::register('my_adapter', 'My\Adapter\Class');
$adapter = HttpAdapterDiscovery::find();
```


### Message Factory discovery

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

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

$adapter = MessageFactoryDiscovery::find();
$factory = MessageFactoryDiscovery::find();
```

### URI Factory discovery

Two common URI factories are bundled with this package: ([Guzzle](https://github.com/guzzle/psr7)
and [Diactoros](https://github.com/zendframework/zend-diactoros)).

``` php
use Http\Discovery\UriFactoryDiscovery;

MessageFactoryDiscovery::register('my_factory', 'Psr\Uri\Implementation\Class', 'My\Factory\Class');

$factory = UriFactoryDiscovery::find();
```



## Testing

Expand Down
25 changes: 25 additions & 0 deletions spec/UriFactoryDiscoverySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace spec\Http\Discovery;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class UriFactoryDiscoverySpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Http\Discovery\UriFactoryDiscovery');
}

function it_finds_guzzle_then_zend_by_default()
{
$this->find()->shouldHaveType('Http\Discovery\UriFactory\GuzzleFactory');

$this->register('guzzle', 'invalid', '');

if (class_exists('Zend\Diactoros\Request')) {
$this->find()->shouldHaveType('Http\Discovery\UriFactory\DiactorosFactory');
}
}
}
65 changes: 65 additions & 0 deletions src/ClassDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Http\Discovery;

/**
* Registry that based find results on class existence
*
* @author David de Boer <david@ddeboer.nl>
*/
abstract class ClassDiscovery
{
/**
* Cached class
*
* @var object
*/
protected static $cache;

/**
* Conditions and classes
*
* @var array
*/
protected static $classes = [];

/**
* Add a condition (and class) to the discovery registry
*
* @param string $name
* @param string $condition
* @param string $class
*/
public static function register($name, $condition, $class = null)
{
static::$cache = null;

static::$classes[$name] = [
'condition' => $condition,
'class' => $class ?: $condition,
];
}

/**
* Finds a Factory
*
* @return object
*
* @throws NotFoundException
*/
public static function find()
{
// We have a cache
if (isset(static::$cache)) {
return static::$cache;
}

foreach (static::$classes as $name => $definition) {
if (class_exists($definition['condition'])) {
return static::$cache = new $definition['class'];
}
}

throw new NotFoundException('Not found');
}
}
45 changes: 15 additions & 30 deletions src/HttpAdapterDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,44 @@

namespace Http\Discovery;

use Http\Adapter\HttpAdapter;

/**
* Finds an HTTP Adapter
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
class HttpAdapterDiscovery
class HttpAdapterDiscovery extends ClassDiscovery
{
/**
* @var array
*/
protected static $adapters = [
'guzzle6' => 'Http\Adapter\Guzzle6HttpAdapter',
'guzzle5' => 'Http\Adapter\Guzzle5HttpAdapter',
protected static $classes = [
'guzzle6' => [
'condition' => 'Http\Adapter\Guzzle6HttpAdapter',
'class' => 'Http\Adapter\Guzzle6HttpAdapter'

],
'guzzle5' => [
'condition' => 'Http\Adapter\Guzzle5HttpAdapter',
'class' => 'Http\Adapter\Guzzle6HttpAdapter'
],
];

/**
* @var string
*/
protected static $cache;

/**
* Register an HTTP Adapter
*
* @param string $name
* @param string $class
*/
public static function register($name, $class)
{
static::$cache = null;

static::$adapters[$name] = $class;
}

/**
* Finds an HTTP Adapter
*
* @return object
* @return HttpAdapter
*
* @throws NotFoundException
*/
public static function find()
{
// We have a cache
if (isset(static::$cache)) {
return static::$cache;
}

foreach (static::$adapters as $name => $class) {
if (class_exists($class)) {
return static::$cache = new $class;
}
}

throw new NotFoundException('No HTTP Adapter found');
return parent::find();
}
}
53 changes: 11 additions & 42 deletions src/MessageFactoryDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,38 @@

namespace Http\Discovery;

use Http\Message\MessageFactory;

/**
* Finds a Message Factory
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
class MessageFactoryDiscovery
class MessageFactoryDiscovery extends ClassDiscovery
{
/**
* @var array
*/
protected static $messageFactories = [
protected static $factories = [
'guzzle' => [
'class' => 'GuzzleHttp\Psr7\Request',
'factory' => 'Http\Discovery\MessageFactory\GuzzleFactory',
'condition'=> 'GuzzleHttp\Psr7\Request',
'class' => 'Http\Discovery\MessageFactory\GuzzleFactory',
],
'diactoros' => [
'class' => 'Zend\Diactoros\Request',
'factory' => 'Http\Discovery\MessageFactory\DiactorosFactory',
'condition' => 'Zend\Diactoros\Request',
'class' => 'Http\Discovery\MessageFactory\DiactorosFactory',
],
];

/**
* @var string
*/
protected static $cache;

/**
* Register a Message Factory
* Find Message Factory
*
* @param string $name
* @param string $class
* @param string $factory
*/
public static function register($name, $class, $factory)
{
static::$cache = null;

static::$messageFactories[$name] = [
'class' => $class,
'factory' => $factory,
];
}

/**
* Finds a Message Factory
*
* @return object
* @return MessageFactory
*
* @throws NotFoundException
*/
public static function find()
{
// We have a cache
if (isset(static::$cache)) {
return static::$cache;
}

foreach (static::$messageFactories as $name => $definition) {
if (class_exists($definition['class'])) {
return static::$cache = new $definition['factory'];
}
}

throw new NotFoundException('No Message Factory found');
return parent::find();
}
}
22 changes: 22 additions & 0 deletions src/UriFactory/DiactorosFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Http\Discovery\UriFactory;

use Zend\Diactoros\Uri;
use Http\Message\UriFactory;

/**
* Creates a zend/diactoros URI object
*
* @author David de Boer <david@ddeboer.nl>
*/
class DiactorosFactory implements UriFactory
{
/**
* {@inheritdoc}
*/
public function createUri($uri)
{
return new Uri($uri);
}
}
22 changes: 22 additions & 0 deletions src/UriFactory/GuzzleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Http\Discovery\UriFactory;

use GuzzleHttp\Psr7\Uri;
use Http\Message\UriFactory;

/**
* Creates a guzzlehttp/psr7 URI object
*
* @author David de Boer <david@ddeboer.nl>
*/
class GuzzleFactory implements UriFactory
{
/**
* {@inheritdoc}
*/
public function createUri($uri)
{
return new Uri($uri);
}
}
37 changes: 37 additions & 0 deletions src/UriFactoryDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Http\Discovery;

use Http\Message\UriFactory;

/**
* Finds a URI Factory
*
* @author David de Boer <david@ddeboer.nl>
*/
class UriFactoryDiscovery extends ClassDiscovery
{
/**
* @var array
*/
protected static $classes = [
'guzzle' => [
'condition' => 'GuzzleHttp\Psr7\Uri',
'class' => 'Http\Discovery\UriFactory\GuzzleFactory',
],
'diactoros' => [
'condition' => 'Zend\Diactoros\Uri',
'class' => 'Http\Discovery\UriFactory\DiactorosFactory',
],
];

/**
* Find URI Factory
*
* @return UriFactory
*/
public static function find()
{
return parent::find();
}
}

0 comments on commit 7db7946

Please sign in to comment.