-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
212 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |