-
Notifications
You must be signed in to change notification settings - Fork 49
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
9 changed files
with
216 additions
and
78 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
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 $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'); | ||
} | ||
} |
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; | ||
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); | ||
} | ||
} |
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' => [ | ||
'class' => 'Http\Discovery\UriFactory\GuzzleFactory', | ||
'condition' => 'GuzzleHttp\Psr7\Uri', | ||
], | ||
'diactoros' => [ | ||
'class' => 'Http\Discovery\UriFactory\DiactorosFactory', | ||
'condition' => 'Zend\Diactoros\Uri', | ||
], | ||
]; | ||
|
||
/** | ||
* Find URI Factory | ||
* | ||
* @return UriFactory | ||
*/ | ||
public static function find() | ||
{ | ||
return parent::find(); | ||
} | ||
} |