-
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.
Merge pull request #4 from ddeboer/uri-factory
Add URI factory (fix #3)
- Loading branch information
Showing
10 changed files
with
226 additions
and
81 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
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,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'); | ||
} | ||
} |
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); | ||
} | ||
} |
Oops, something went wrong.