-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a basic StorageFactory for creating storage instances
The Presque object can now be provided a StorageInterface, which will be passed to the QueueFactory by default.
- Loading branch information
1 parent
03acd02
commit 7cb7150
Showing
3 changed files
with
66 additions
and
3 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,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Presque package. | ||
* | ||
* (c) Justin Rainbow <justin.rainbow@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Presque\Storage; | ||
|
||
class StorageFactory | ||
{ | ||
public static function create($dsn, $prefix = null) | ||
{ | ||
$url = static::parse($dsn); | ||
|
||
if (!isset($url['host'])) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'"%s" is missing the required "host" part of the URL. '. | ||
'Please make sure the DSN is a valid URL.', | ||
$dsn | ||
)); | ||
} | ||
|
||
if (extension_loaded('redis')) { | ||
$redis = new \Redis(); | ||
$redis->connect($url['host'], isset($url['port']) ? $url['port'] : 6379); | ||
|
||
$storage = new PhpredisStorage($redis); | ||
} else { | ||
$redis = new \Predis\Client($url); | ||
|
||
$storage = new PredisStorage($redis); | ||
} | ||
|
||
if (null !== $prefix) { | ||
$storage->setPrefix($prefix); | ||
} | ||
|
||
return $storage; | ||
} | ||
|
||
protected static function parse($dsn) | ||
{ | ||
$url = parse_url($dsn); | ||
|
||
return $url; | ||
} | ||
} |
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