Skip to content

Commit

Permalink
Merge pull request #4 from ddeboer/uri-factory
Browse files Browse the repository at this point in the history
Add URI factory (fix #3)
  • Loading branch information
sagikazarmark committed Jun 5, 2015
2 parents ed926a4 + b4b5f66 commit 033c51d
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 81 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
2 changes: 2 additions & 0 deletions spec/HttpAdapterDiscoverySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace spec\Http\Discovery;

use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use PhpSpec\ObjectBehavior;

class HttpAdapterDiscoverySpec extends ObjectBehavior
Expand Down
8 changes: 4 additions & 4 deletions spec/MessageFactoryDiscoverySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function it_registers_a_factory()
{
$this->reset();

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

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

$firstGuess = $this->find();

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

$this->find()->shouldNotBe($firstGuess);
}
Expand Down Expand Up @@ -65,8 +65,8 @@ function it_throws_an_exception_when_no_message_factory_is_found()

function reset()
{
$this->register('guzzle', 'GuzzleHttp\Psr7\Request', 'Http\Discovery\MessageFactory\GuzzleFactory');
$this->register('diactoros', 'Zend\Diactoros\Request', 'Http\Discovery\MessageFactory\DiactorosFactory');
$this->register('guzzle', 'Http\Discovery\MessageFactory\GuzzleFactory', 'GuzzleHttp\Psr7\Request');
$this->register('diactoros', 'Http\Discovery\MessageFactory\DiactorosFactory', 'Zend\Diactoros\Request');
}
}

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');
}
}
}
51 changes: 51 additions & 0 deletions src/ClassDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Http\Discovery;

/**
* Registry that based find results on class existence
*
* @author David de Boer <david@ddeboer.nl>
*/
abstract class ClassDiscovery
{
/**
* Add a condition (and class) to the discovery registry
*
* @param string $name
* @param string $class Class that will be instantiated if found
* @param string $condition Optional other class to check for existence
*/
public static function register($name, $class, $condition = null)
{
static::$cache = null;

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

/**
* 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');
}
}
54 changes: 21 additions & 33 deletions src/HttpAdapterDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,47 @@

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',
];

/**
* @var string
* Cached adapter
*
* @var HttpAdapter
*/
protected static $cache;

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

static::$adapters[$name] = $class;
}
protected static $classes = [
'guzzle6' => [
'class' => 'Http\Adapter\Guzzle6HttpAdapter',
'condition' => 'Http\Adapter\Guzzle6HttpAdapter'

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

/**
* 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');
// Override only used for return type declaration
return parent::find();
}
}
61 changes: 19 additions & 42 deletions src/MessageFactoryDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,46 @@

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
{
/**
* Cached factory
*
* @var MessageFactory
*/
protected static $cache;

/**
* @var array
*/
protected static $messageFactories = [
protected static $classes = [
'guzzle' => [
'class' => 'GuzzleHttp\Psr7\Request',
'factory' => 'Http\Discovery\MessageFactory\GuzzleFactory',
'class' => 'Http\Discovery\MessageFactory\GuzzleFactory',
'condition'=> 'GuzzleHttp\Psr7\Request',
],
'diactoros' => [
'class' => 'Zend\Diactoros\Request',
'factory' => 'Http\Discovery\MessageFactory\DiactorosFactory',
'class' => 'Http\Discovery\MessageFactory\DiactorosFactory',
'condition' => 'Zend\Diactoros\Request',
],
];

/**
* @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');
// Override only used for return type declaration
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;
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 Psr7\uri_for($uri);
}
}
Loading

0 comments on commit 033c51d

Please sign in to comment.