├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── VERSION
├── composer.json
├── examples
├── cluster.php
├── multiple.php
├── session.php
└── single.php
├── phpunit.xml.dist
├── src
├── ClientServiceProvider.php
├── ClientsServiceProvider.php
└── Container
│ └── ClientsContainer.php
└── tests
├── PHPUnit
└── ProviderTestCase.php
├── Predis
└── Silex
│ ├── MultiPredisServiceProviderTest.php
│ └── PredisServiceProviderTest.php
└── bootstrap.php
/.gitignore:
--------------------------------------------------------------------------------
1 | composer.lock
2 | vendor/
3 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 | php:
3 | - 5.3
4 | - 5.4
5 | - 5.5
6 | - 5.6
7 | - 7.0
8 | - hhvm
9 | services: redis-server
10 | before_script:
11 | - composer self-update
12 | - composer install --no-interaction --prefer-source --dev
13 | script:
14 | - vendor/bin/phpunit
15 | matrix:
16 | allow_failures:
17 | - php: hhvm
18 | fast_finish: true
19 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | v2.0.0 (2016-11-28)
2 | ================================================================================
3 |
4 | - Updated code to work with Pimple 3.0 and Silex 2.0.
5 |
6 |
7 | v1.0.0 (2014-08-06)
8 | ================================================================================
9 |
10 | - Bumped required version of Predis to ~1.0.
11 |
12 | - All of the service provider classes have been renamed.
13 |
14 | - Switched to PSR-4 for autoloading.
15 |
16 |
17 | v0.4.2 (2013-10-18)
18 | ================================================================================
19 |
20 | - Fix Silex dependency to allow stable and dev versions.
21 |
22 |
23 | v0.4.1 (2013-03-19)
24 | ================================================================================
25 |
26 | - Added `predis.client_constructor` to easily override the actual instance of
27 | `Predis\ClientInterface` returned by the provider when creating a new client
28 | object.
29 |
30 | - Minimum required version of Predis is now v0.8.3.
31 |
32 |
33 | v0.4.0 (2013-02-03)
34 | ================================================================================
35 |
36 | - The service provider now requires Predis v0.8.
37 |
38 | - Now `Predis\Silex\PredisServiceProvider` is used only to provide single-client
39 | configurations while the new `Predis\Silex\MultiPredisServiceProvider` can be
40 | used to configure multiple clients. This is a breaking change from previous
41 | versions in which both configurations were handled by the sameprovider class.
42 |
43 | - Due to the nature of the above mentioned change, it is no more possible to
44 | define a default client when using a multiple-clients configuration.
45 |
46 |
47 | v0.3.0 (2012-07-14)
48 | ================================================================================
49 |
50 | - Fixed service provider configuration after the recent addition of the `boot()`
51 | method in Silex.
52 |
53 | - Removed the `class_path` option, Silex removed its autoloader service and now
54 | relies on Composer.
55 |
56 |
57 | v0.2.4 (2012-06-01)
58 | ================================================================================
59 |
60 | - Implemented a empty Silex\ServiceProviderInterface::boot() needed after recent
61 | changes in Silex.
62 |
63 |
64 | v0.2.3 (2011-12-11)
65 | ================================================================================
66 |
67 | - There are no actual changes in the code of this release, aside from a bump in
68 | the required version of Predis in the composer.json file to target the tagged
69 | release of v0.7.0,
70 |
71 |
72 | v0.2.2 (2011-11-13)
73 | ================================================================================
74 |
75 | - Added the ability to specify multiple clients using 'predis.clients' with an
76 | array of connection parameters and client options for each client.
77 |
78 |
79 | v0.2.1 (2011-10-02)
80 | ================================================================================
81 |
82 | - Added support for Composer to manage installation and dependencies.
83 |
84 |
85 | v0.2.0 (2011-10-02)
86 | ================================================================================
87 |
88 | - Renamed PredisExtension to PredisServiceProvider after internal changes in the
89 | naming of some classes and interfaces of Silex.
90 |
91 |
92 | v0.1.0 (2011-05-07)
93 | ================================================================================
94 |
95 | - Initial release.
96 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | ## Filing bug reports ##
2 |
3 | Bugs reports or feature requests can be posted for further discussion on the issue tracker available
4 | on [GitHub](http://github.com/nrk/PredisServiceProvider/issues).
5 |
6 | When reporting bugs, in addition to the description of your issue you __must__ always provide some
7 | essential information about your environment such as:
8 |
9 | 1. version of this service provider library (check the `VERSION` file).
10 | 2. version of Redis (check `redis_version` returned by [`INFO`](http://redis.io/commands/info)).
11 | 3. version of PHP.
12 | 4. name and version of the operating system.
13 | 5. when possible, a small snippet of code that reproduces the issue.
14 |
15 | __Think about it__: we do not have a crystal ball and cannot predict things or peer into the unknown
16 | so please provide as much details as possible to help us isolating issues and fix them.
17 |
18 | __Never__ use GitHub issues to post generic questions about this library! When you have questions
19 | about how this library works or how it can be used, please just hop me an email and I will get back
20 | to you as soon as possible.
21 |
22 |
23 | ## Contributing code ##
24 |
25 | If you want to work on this library, it is highly recommended that you first run the test suite in
26 | order to check that everything is OK and report strange behaviours or bugs. When modifying the code
27 | please make sure that no warnings or notices are emitted by PHP running the interpreter in your
28 | development environment with the `error_reporting` variable set to `E_ALL | E_STRICT`.
29 |
30 | The recommended way to contribute is to fork the project on GitHub, create topic branches on your
31 | newly created repository to fix bugs or add new features (possibly with tests covering your changes)
32 | and then open a pull request with a description of the applied changes. Obviously you can use any
33 | other Git hosting provider of your preference.
34 |
35 | We always aim for consistency in our code base so you should follow basic coding rules as defined by
36 | [PSR-1](http://www.php-fig.org/psr/psr-1/) and [PSR-2](http://www.php-fig.org/psr/psr-2/) and stick
37 | with the conventions used in this library to name classes and interfaces. Indentation should be done
38 | with 4 spaces and code should be wrapped at 100 columns (please try to stay within this limit even
39 | if the above mentioned official coding guidelines set the soft limit to 120 columns).
40 |
41 | Please follow these [commit guidelines](http://git-scm.com/book/ch5-2.html#Commit-Guidelines) when
42 | committing your code to Git and always write a meaningful (not necessarily extended) description of
43 | your changes before opening pull requests.
44 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2011-2016 Daniele Alessandri
2 |
3 | Permission is hereby granted, free of charge, to any person
4 | obtaining a copy of this software and associated documentation
5 | files (the "Software"), to deal in the Software without
6 | restriction, including without limitation the rights to use,
7 | copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | copies of the Software, and to permit persons to whom the
9 | Software is furnished to do so, subject to the following
10 | conditions:
11 |
12 | The above copyright notice and this permission notice shall be
13 | included in all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Predis ServiceProvider #
2 |
3 | [](https://packagist.org/packages/predis/service-provider)
4 | [](https://packagist.org/packages/predis/service-provider)
5 | [](https://packagist.org/packages/predis/service-provider)
6 | [](https://travis-ci.org/nrk/PredisServiceProvider)
7 | [](http://hhvm.h4cc.de/package/predis/service-provider)
8 |
9 | This service provider for [Silex](http://silex-project.org/) allows developers to easily configure
10 | and expose [Predis](http://github.com/nrk/predis) enabling them to use [Redis](http://redis.io) in
11 | their applications.
12 |
13 |
14 | ### Getting started ###
15 |
16 | Supposing that the skeleton of your application is ready, you simply need to register this service
17 | provider by specifying the parameters and options needed to access Redis:
18 |
19 | ```php
20 | $app->register(new Predis\Silex\ClientServiceProvider(), [
21 | 'predis.parameters' => 'tcp://127.0.0.1:6379',
22 | 'predis.options' => [
23 | 'prefix' => 'silex:',
24 | 'profile' => '3.0',
25 | ],
26 | ]);
27 | ```
28 |
29 | This will register one instance of `Predis\Client` accessible from anywhere in your application by
30 | using `$app['predis']`. Both `predis.parameters` and `predis.options` are optional and they accept
31 | the same values accepted by the constructor of `Predis\Client` (see the documentation of Predis).
32 |
33 | Certain applications might need more than one client to reach different servers or configured with
34 | different options. In such cases you must use `Predis\Silex\ClientsServiceProvider` providing a list
35 | of clients with their own parameters and options using `predis.clients`:
36 |
37 | ```php
38 | $app->register(new Predis\Silex\ClientsServiceProvider(), [
39 | 'predis.clients' => [
40 | 'client1' => 'tcp://127.0.0.1:6379',
41 | 'client2' => [
42 | 'host' => '127.0.0.1',
43 | 'port' => 6380,
44 | ],
45 | 'client3' => [
46 | 'parameters' => 'tcp://127.0.0.1:6381',
47 | 'options' => [
48 | 'profile' => 'dev',
49 | 'prefix' => 'silex:',
50 | ],
51 | ],
52 | ),
53 | ]);
54 | ```
55 |
56 | Clients will be exposed to your application using `$app['predis'][$alias]` where `$alias` is the key
57 | used to populate the items of `predis.clients`. Optionally, it is possible to set a default client
58 | by specifying its alias in `predis.default_client` making it accessible simply by invoking methods
59 | of `Predis\Client` directly against `$app['predis']`. Client instances are lazily initialized upon
60 | the first access.
61 |
62 | __NOTE__: this is not the same as using a cluster of nodes or replication as it will only create and
63 | set up independent client instances. Cluster and replication thus work with both single and multiple
64 | client configurations, you just need to provide the needed parameters and options for each instance
65 | of `Predis\Client`.
66 |
67 | You can find more details on how to use this provider in the `examples` directory or the test suite.
68 |
69 | ### Reporting bugs and contributing code ###
70 |
71 | Contributions are highly appreciated either in the form of pull requests for new features, bug fixes
72 | or just bug reports. We only ask you to adhere to a [basic set of rules](CONTRIBUTING.md) before
73 | submitting your changes or filing bugs on the issue tracker to make it easier for everyone to stay
74 | consistent while working on the project.
75 |
76 |
77 | ### Project links ###
78 |
79 | - [Source code](http://github.com/nrk/PredisServiceProvider)
80 | - [Issue tracker](http://github.com/nrk/PredisServiceProvider/issues)
81 |
82 |
83 | ### Author ###
84 |
85 | - [Daniele Alessandri](mailto:suppakilla@gmail.com)
86 | ([github](http://github.com/nrk))
87 | ([twitter](http://twitter.com/JoL1hAHN))
88 |
89 |
90 | ### Contributors ###
91 |
92 | - Jérôme Macias ([github](http://github.com/jeromemacias))
93 |
94 |
95 | ### License ###
96 |
97 | The code for Predis ServiceProvider is distributed under the terms of the [MIT license](LICENSE).
98 |
--------------------------------------------------------------------------------
/VERSION:
--------------------------------------------------------------------------------
1 | 2.0.0
2 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "predis/service-provider",
3 | "type": "library",
4 | "description": "Predis service provider for the Silex microframework",
5 | "keywords": ["silex", "redis", "predis"],
6 | "homepage": "https://github.com/nrk/PredisServiceProvider",
7 | "license": "MIT",
8 | "authors": [
9 | {
10 | "name": "Daniele Alessandri",
11 | "email": "suppakilla@gmail.com",
12 | "homepage": "http://clorophilla.net"
13 | }
14 | ],
15 | "autoload": {
16 | "psr-4": {"Predis\\Silex\\": "src/"}
17 | },
18 | "require": {
19 | "php": ">=5.3.2",
20 | "pimple/pimple": "^3.0",
21 | "predis/predis": "~1.0"
22 | },
23 | "require-dev": {
24 | "phpunit/phpunit": "~4.0"
25 | },
26 | "extra": {
27 | "branch-alias": {
28 | "dev-master": "2.0-dev"
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/examples/cluster.php:
--------------------------------------------------------------------------------
1 | register(new Predis\Silex\ClientServiceProvider(), array(
8 | 'predis.parameters' => [
9 | 'tcp://127.0.0.1:6379?alias=node-01',
10 | 'tcp://127.0.0.1:6380?alias=node-02',
11 | 'tcp://127.0.0.1:6381?alias=node-03',
12 | ],
13 | 'predis.options' => array(
14 | 'profile' => '3.0',
15 | 'prefix' => 'silex:',
16 | ),
17 | ));
18 |
19 | /** routes **/
20 | $app->get('/', function () use ($app) {
21 | $cmdINFO = $app['predis']->createCommand('INFO', array('server'));
22 | $nodes = $app['predis']->getConnection()
23 | ->executeCommandOnNodes($cmdINFO);
24 |
25 | return var_export($nodes, true);
26 | });
27 |
28 | /** run application **/
29 | $app->run();
30 |
--------------------------------------------------------------------------------
/examples/multiple.php:
--------------------------------------------------------------------------------
1 | register(new Predis\Silex\ClientsServiceProvider(), array(
8 | 'predis.clients' => array(
9 | 'first' => 'tcp://127.0.0.1:6379',
10 | 'second' => array(
11 | 'host' => '127.0.0.1',
12 | 'port' => 6380,
13 | ),
14 | 'third' => array(
15 | 'parameters' => array(
16 | 'host' => '127.0.0.1',
17 | 'port' => 6381,
18 | ),
19 | 'options' => array(
20 | 'profile' => '2.2',
21 | 'prefix' => 2.4,
22 | ),
23 | ),
24 | ),
25 | 'predis.default_client' => 'first',
26 | 'predis.default_parameters' => array(
27 | 'timeout' => 2.0,
28 | 'read_write_timeout' => 2.0,
29 | ),
30 | 'predis.default_options' => array(
31 | 'connections' => array(
32 | 'tcp' => 'Predis\Connection\PhpiredisStreamConnection',
33 | 'unix' => 'Predis\Connection\PhpiredisStreamConnection',
34 | ),
35 | ),
36 | ));
37 |
38 | /** routes **/
39 |
40 | $app->get('/', function () use ($app) {
41 | $default = var_export($app['predis']->info('server'), true);
42 |
43 | $first = var_export($app['predis']['first']->info('server'), true);
44 | $second = var_export($app['predis']['second']->info('server'), true);
45 | $third = var_export($app['predis']['third']->info('server'), true);
46 |
47 | return "$default
\n$first
\n$second
\n$third
\n";
48 | });
49 |
50 | /** run application **/
51 | $app->run();
52 |
--------------------------------------------------------------------------------
/examples/session.php:
--------------------------------------------------------------------------------
1 | register(new Predis\Silex\ClientsServiceProvider(), array(
8 | 'predis.clients' => array(
9 | 'db' => 'tcp://127.0.0.1',
10 | 'session' => array(
11 | 'parameters' => 'tcp://127.0.0.1',
12 | 'options' => array(
13 | 'prefix' => 'sessions:'
14 | ),
15 | ),
16 | ),
17 | ));
18 |
19 | $app->register(new Silex\Provider\SessionServiceProvider(), array(
20 | 'session.storage.handler' => function ($app) {
21 | $client = $app['predis']['session'];
22 | $options = array('gc_maxlifetime' => 300);
23 |
24 | $handler = new Predis\Session\Handler($client, $options);
25 |
26 | return $handler;
27 | }
28 | ));
29 |
30 | $app->get('/', function () use ($app) {
31 | $app['session']->set('foo', mt_rand());
32 |
33 | return print_r($app['predis']['db']->keys('sessions:*'), true);
34 | });
35 |
36 | $app->run();
37 |
--------------------------------------------------------------------------------
/examples/single.php:
--------------------------------------------------------------------------------
1 | register(new Predis\Silex\ClientServiceProvider(), array(
8 | 'predis.parameters' => 'tcp://127.0.0.1:6379/',
9 | 'predis.options' => array(
10 | 'profile' => '2.2',
11 | 'prefix' => 'silex:',
12 | ),
13 | ));
14 |
15 | /** routes **/
16 | $app->get('/', function () use ($app) {
17 | return var_export($app['predis']->info(), true);
18 | });
19 |
20 | /** run application **/
21 | $app->run();
22 |
--------------------------------------------------------------------------------
/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | tests/Predis/Silex/
7 |
8 |
9 |
10 |
11 |
12 | src/
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/ClientServiceProvider.php:
--------------------------------------------------------------------------------
1 |
7 | *
8 | * For the full copyright and license information, please view the LICENSE
9 | * file that was distributed with this source code.
10 | */
11 |
12 | namespace Predis\Silex;
13 |
14 | use InvalidArgumentException;
15 | use Pimple\Container;
16 | use Pimple\ServiceProviderInterface;
17 | use Predis\Client;
18 | use Predis\Connection\Parameters;
19 |
20 |
21 | /**
22 | * Exposes a single instance of Predis\Client to Silex.
23 | *
24 | * @author Daniele Alessandri
25 | */
26 | class ClientServiceProvider implements ServiceProviderInterface
27 | {
28 | protected $prefix;
29 |
30 | /**
31 | * @param string $prefix Prefix name used to register the service provider in Silex.
32 | */
33 | public function __construct($prefix = 'predis')
34 | {
35 | if (empty($prefix)) {
36 | throw new InvalidArgumentException('The specified prefix is not valid.');
37 | }
38 |
39 | $this->prefix = $prefix;
40 | }
41 |
42 | /**
43 | * Returns an anonymous function used by the service provider initialize
44 | * lazily new instances of Predis\Client.
45 | *
46 | * @param Container $app
47 | * @param string $prefix
48 | *
49 | * @return \Closure
50 | */
51 | protected function getClientInitializer(Container $app, $prefix)
52 | {
53 | return $app->protect(function ($args) use ($app, $prefix) {
54 | $extract = function ($bag, $key) use ($app, $prefix) {
55 | $default = "default_$key";
56 |
57 | if ($bag instanceof Container) {
58 | $key = "$prefix.$key";
59 | }
60 |
61 | if (!isset($bag[$key])) {
62 | return $app["$prefix.$default"];
63 | }
64 |
65 | if (is_array($bag[$key])) {
66 | return array_merge($app["$prefix.$default"], $bag[$key]);
67 | }
68 |
69 | return $bag[$key];
70 | };
71 |
72 | if (isset($args['parameters']) && is_string($args['parameters'])) {
73 | $args['parameters'] = $app["$prefix.uri_parser"]($args['parameters']);
74 | }
75 |
76 | $parameters = $extract($args, 'parameters');
77 | $options = $extract($args, 'options');
78 |
79 | return $app["$prefix.client_constructor"]($parameters, $options);
80 | });
81 | }
82 |
83 | /**
84 | * Returns an anonymous function used by the service provider to handle
85 | * accesses to the root prefix.
86 | *
87 | * @param Container $app
88 | * @param string $prefix
89 | *
90 | * @return mixed
91 | */
92 | protected function getProviderHandler(Container $app, $prefix)
93 | {
94 | return function () use ($app, $prefix) {
95 | $initializer = $app["$prefix.client_initializer"];
96 |
97 | return $initializer($app);
98 | };
99 | }
100 |
101 | /**
102 | * {@inheritdoc}
103 | */
104 | public function register(Container $app)
105 | {
106 | $prefix = $this->prefix;
107 |
108 | $app["$prefix.default_parameters"] = array();
109 | $app["$prefix.default_options"] = array();
110 |
111 | $app["$prefix.uri_parser"] = $app->protect(function ($uri) {
112 | return Parameters::parse($uri);
113 | });
114 |
115 | $app["$prefix.client_constructor"] = $app->protect(function ($parameters, $options) {
116 | return new Client($parameters, $options);
117 | });
118 |
119 | $app["$prefix.client_initializer"] = $this->getClientInitializer($app, $prefix);
120 | $app["$prefix"] = $this->getProviderHandler($app, $prefix);
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/src/ClientsServiceProvider.php:
--------------------------------------------------------------------------------
1 |
7 | *
8 | * For the full copyright and license information, please view the LICENSE
9 | * file that was distributed with this source code.
10 | */
11 |
12 | namespace Predis\Silex;
13 |
14 | use Pimple\Container;
15 | use Predis\Silex\Container\ClientsContainer;
16 |
17 | /**
18 | * Exposes multiple instances of Predis\Client to Silex.
19 | *
20 | * @author Daniele Alessandri
21 | */
22 | class ClientsServiceProvider extends ClientServiceProvider
23 | {
24 | /**
25 | * {@inheritdoc}
26 | */
27 | protected function getProviderHandler(Container $app, $prefix)
28 | {
29 | return function () use ($app, $prefix) {
30 | $clients = $app["{$prefix}.clients_container"]($prefix);
31 |
32 | foreach ($app["$prefix.clients"] as $alias => $args) {
33 | $clients[$alias] = function () use ($app, $prefix, $args) {
34 | $initializer = $app["$prefix.client_initializer"];
35 |
36 | if (is_string($args)) {
37 | $args = array('parameters' => $args);
38 | } elseif (!isset($args['parameters']) && !isset($args['options'])) {
39 | $args = array('parameters' => $args);
40 | }
41 |
42 | return $initializer($args);
43 | };
44 | }
45 |
46 | return $clients;
47 | };
48 | }
49 |
50 | /**
51 | * {@inheritdoc}
52 | */
53 | public function register(Container $app)
54 | {
55 | $app["{$this->prefix}.clients"] = array();
56 |
57 | $app["{$this->prefix}.clients_container"] = $app->protect(function ($prefix) use ($app) {
58 | return new ClientsContainer($app, $prefix);
59 | });
60 |
61 | parent::register($app);
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/Container/ClientsContainer.php:
--------------------------------------------------------------------------------
1 |
7 | *
8 | * For the full copyright and license information, please view the LICENSE
9 | * file that was distributed with this source code.
10 | */
11 |
12 | namespace Predis\Silex\Container;
13 |
14 | use BadMethodCallException;
15 | use Pimple\Container;
16 |
17 | /**
18 | * Specialized Pimple container that supports the definition of a default client
19 | * responding to `$app['predis']`.
20 | *
21 | * @author Daniele Alessandri
22 | */
23 | class ClientsContainer extends Container
24 | {
25 | protected $application;
26 | protected $prefix;
27 |
28 | /**
29 | * {@inheritdoc}
30 | */
31 | public function __construct(Container $app, $prefix)
32 | {
33 | $this->application = $app;
34 | $this->prefix = $prefix;
35 | }
36 |
37 | /**
38 | * {@inheritdoc}
39 | */
40 | public function __call($method, $arguments)
41 | {
42 | if (isset($this->application["{$this->prefix}.default_client"])) {
43 | $default = $this->application["{$this->prefix}.default_client"];
44 |
45 | return call_user_func_array(array($this[$default], $method), $arguments);
46 | }
47 |
48 | throw new BadMethodCallException("Undefined method `$method`.");
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/tests/PHPUnit/ProviderTestCase.php:
--------------------------------------------------------------------------------
1 |
7 | *
8 | * For the full copyright and license information, please view the LICENSE
9 | * file that was distributed with this source code.
10 | */
11 |
12 | namespace Predis\Silex;
13 |
14 | use PHPUnit_Framework_TestCase as StandardTestCase;
15 | use Pimple\Container;
16 | use Predis\Client;
17 |
18 | /**
19 | *
20 | */
21 | abstract class ProviderTestCase extends StandardTestCase
22 | {
23 | abstract protected function getProviderInstance($prefix = 'predis');
24 |
25 | protected function register(Array $arguments = array(), ClientServiceProvider $provider = null)
26 | {
27 | $app = new Container();
28 |
29 | $app->register($provider ?: $this->getProviderInstance(), $arguments);
30 |
31 | return $app;
32 | }
33 |
34 | protected function getSomeParameters()
35 | {
36 | return array(
37 | 'scheme' => 'tcp',
38 | 'host' => '192.168.1.1',
39 | 'port' => 1000
40 | );
41 | }
42 |
43 | protected function getParametersAndOptions(Client $client)
44 | {
45 | $parameters = $client->getConnection()->getParameters();
46 | $options = $client->getOptions();
47 |
48 | return array($parameters, $options);
49 | }
50 |
51 | protected function checkParameters(Container $container, $clientID, $parameters)
52 | {
53 | list($params,) = $this->getParametersAndOptions($container[$clientID]);
54 |
55 | foreach ($parameters as $k => $v) {
56 | $this->assertSame($v, $params->{$k});
57 | }
58 | }
59 |
60 | public function testProviderRegistration()
61 | {
62 | $app = $this->register();
63 |
64 | $this->checkRegisteredProvider($app, 'predis');
65 | }
66 |
67 | public function testPrefixProviderRegistration()
68 | {
69 | $prefix = 'my_predis';
70 | $app = $this->register(array(), $this->getProviderInstance($prefix));
71 |
72 | $this->checkRegisteredProvider($app, $prefix);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/tests/Predis/Silex/MultiPredisServiceProviderTest.php:
--------------------------------------------------------------------------------
1 |
7 | *
8 | * For the full copyright and license information, please view the LICENSE
9 | * file that was distributed with this source code.
10 | */
11 |
12 | namespace Predis\Silex;
13 |
14 | use Pimple\Container;
15 | use Predis\Profile\Factory as ProfileFactory;
16 |
17 | class ClientsServiceProviderTest extends ProviderTestCase
18 | {
19 | protected function getProviderInstance($prefix = 'predis')
20 | {
21 | return new ClientsServiceProvider($prefix);
22 | }
23 |
24 | protected function checkRegisteredProvider(Container $app, $prefix)
25 | {
26 | $this->assertInstanceOf('Pimple\Container', $app[$prefix]);
27 | $this->assertInternalType('array', $app["$prefix.default_options"]);
28 | $this->assertInternalType('array', $app["$prefix.default_parameters"]);
29 | $this->assertInstanceOf('Closure', $app["$prefix.client_initializer"]);
30 | }
31 |
32 | public function testClientsIndexed()
33 | {
34 | $params = $this->getSomeParameters();
35 |
36 | $app = $this->register(array(
37 | 'predis.clients' => array(
38 | "{$params['scheme']}://{$params['host']}:{$params['port']}",
39 | $params,
40 | array('parameters' => $params),
41 | array('parameters' => $params, 'options' => array('profile' => 'dev')),
42 | )
43 | ));
44 |
45 | $this->checkParameters($app['predis'], 0, $params);
46 | $this->checkParameters($app['predis'], 1, $params);
47 | $this->checkParameters($app['predis'], 2, $params);
48 | $this->checkParameters($app['predis'], 3, $params);
49 |
50 | list(, $options) = $this->getParametersAndOptions($app['predis'][0]);
51 | $this->assertEquals(ProfileFactory::getDefault(), $options->profile);
52 |
53 | list(, $options) = $this->getParametersAndOptions($app['predis'][1]);
54 | $this->assertEquals(ProfileFactory::getDefault(), $options->profile);
55 |
56 | list(, $options) = $this->getParametersAndOptions($app['predis'][2]);
57 | $this->assertEquals(ProfileFactory::getDefault(), $options->profile);
58 |
59 | list(, $options) = $this->getParametersAndOptions($app['predis'][3]);
60 | $this->assertEquals(ProfileFactory::getDevelopment(), $options->profile);
61 | }
62 |
63 | public function testClientsAliased()
64 | {
65 | $params = $this->getSomeParameters();
66 |
67 | $app = $this->register(array(
68 | 'predis.clients' => array(
69 | '1st' => "{$params['scheme']}://{$params['host']}:{$params['port']}",
70 | '2nd' => $params,
71 | '3rd' => array('parameters' => $params),
72 | '4th' => array('parameters' => $params, 'options' => array('profile' => 'dev')),
73 | )
74 | ));
75 |
76 | $this->checkParameters($app['predis'], '1st', $params);
77 | $this->checkParameters($app['predis'], '2nd', $params);
78 | $this->checkParameters($app['predis'], '3rd', $params);
79 | $this->checkParameters($app['predis'], '4th', $params);
80 |
81 | list(, $options) = $this->getParametersAndOptions($app['predis']['1st']);
82 | $this->assertEquals(ProfileFactory::getDefault(), $options->profile);
83 |
84 | list(, $options) = $this->getParametersAndOptions($app['predis']['2nd']);
85 | $this->assertEquals(ProfileFactory::getDefault(), $options->profile);
86 |
87 | list(, $options) = $this->getParametersAndOptions($app['predis']['3rd']);
88 | $this->assertEquals(ProfileFactory::getDefault(), $options->profile);
89 |
90 | list(, $options) = $this->getParametersAndOptions($app['predis']['4th']);
91 | $this->assertEquals(ProfileFactory::getDevelopment(), $options->profile);
92 | }
93 |
94 | public function testClientsCluster()
95 | {
96 | $app = $this->register(array(
97 | 'predis.clients' => array(
98 | '1st' => array(
99 | 'tcp://127.0.0.1:7001',
100 | 'tcp://127.0.0.1:7002',
101 | ),
102 | '2nd' => array(
103 | 'tcp://127.0.0.1:7003',
104 | 'tcp://127.0.0.1:7004',
105 | ),
106 | '3rd' => array(
107 | 'tcp://127.0.0.1:7005',
108 | 'tcp://127.0.0.1:7006',
109 | ),
110 | ),
111 | ));
112 |
113 | $this->assertInstanceOf('Predis\Connection\Aggregate\PredisCluster', $app['predis']['1st']->getConnection());
114 | $this->assertInstanceOf('Predis\Connection\Aggregate\PredisCluster', $app['predis']['2nd']->getConnection());
115 | $this->assertInstanceOf('Predis\Connection\Aggregate\PredisCluster', $app['predis']['3rd']->getConnection());
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/tests/Predis/Silex/PredisServiceProviderTest.php:
--------------------------------------------------------------------------------
1 |
7 | *
8 | * For the full copyright and license information, please view the LICENSE
9 | * file that was distributed with this source code.
10 | */
11 |
12 | namespace Predis\Silex;
13 |
14 | use Pimple\Container;
15 | use Predis\Client;
16 | use Predis\Profile\Factory as ProfileFactory;
17 |
18 | class ClientServiceProviderTest extends ProviderTestCase
19 | {
20 | protected function getProviderInstance($prefix = 'predis')
21 | {
22 | return new ClientServiceProvider($prefix);
23 | }
24 |
25 | protected function checkRegisteredProvider(Container $app, $prefix)
26 | {
27 | $this->assertInstanceOf('Predis\Client', $app[$prefix]);
28 | $this->assertInternalType('array', $app["$prefix.default_options"]);
29 | $this->assertInternalType('array', $app["$prefix.default_parameters"]);
30 | $this->assertInstanceOf('Closure', $app["$prefix.client_initializer"]);
31 | }
32 |
33 | public function testProviderRegistration()
34 | {
35 | $app = $this->register();
36 |
37 | $this->checkRegisteredProvider($app, 'predis');
38 | }
39 |
40 | public function testPrefixProviderRegistration()
41 | {
42 | $prefix = 'my_predis';
43 | $app = $this->register(array(), $this->getProviderInstance($prefix));
44 |
45 | $this->checkRegisteredProvider($app, $prefix);
46 | }
47 |
48 | public function testClient()
49 | {
50 | $app = $this->register();
51 |
52 | list($parameters, $options) = $this->getParametersAndOptions($app['predis']);
53 |
54 | $this->assertEquals('tcp', $parameters->scheme);
55 | $this->assertEquals('127.0.0.1', $parameters->host);
56 | $this->assertEquals(6379, $parameters->port);
57 |
58 | $this->assertEquals(ProfileFactory::getDefault(), $options->profile);
59 | $this->assertNull($options->prefix);
60 | }
61 |
62 | public function testClientParametersString()
63 | {
64 | $scheme = 'tcp';
65 | $host = '192.168.1.1';
66 | $port = 1000;
67 |
68 | $app = $this->register(array(
69 | 'predis.parameters' => "$scheme://$host:$port"
70 | ));
71 |
72 | list($parameters,) = $this->getParametersAndOptions($app['predis']);
73 |
74 | $this->assertEquals($scheme, $parameters->scheme);
75 | $this->assertEquals($host, $parameters->host);
76 | $this->assertEquals($port, $parameters->port);
77 | }
78 |
79 | public function testClientParametersArray()
80 | {
81 | $params = $this->getSomeParameters();
82 |
83 | $app = $this->register(array(
84 | 'predis.parameters' => $params,
85 | ));
86 |
87 | $this->checkParameters($app, 'predis', $params);
88 | }
89 |
90 | public function testClientOptions()
91 | {
92 | $profile = 'dev';
93 | $prefix = 'silex:';
94 |
95 | $app = $this->register(array(
96 | 'predis.options' => array(
97 | 'profile' => $profile,
98 | 'prefix' => $prefix,
99 | ),
100 | ));
101 |
102 | list(, $options) = $this->getParametersAndOptions($app['predis']);
103 |
104 | $profile = ProfileFactory::get($profile);
105 | $profile->setProcessor($options->prefix);
106 |
107 | $this->assertEquals($prefix, $options->prefix->getPrefix());
108 | $this->assertEquals($profile, $options->profile);
109 | }
110 |
111 | public function testClientCluster()
112 | {
113 | $app = $this->register(array(
114 | 'predis.parameters' => array(
115 | 'tcp://127.0.0.1:7001',
116 | 'tcp://127.0.0.1:7002',
117 | 'tcp://127.0.0.1:7003',
118 | ),
119 | ));
120 |
121 | $this->assertInstanceOf('Predis\Connection\Aggregate\PredisCluster', $app['predis']->getConnection());
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/tests/bootstrap.php:
--------------------------------------------------------------------------------
1 |
7 | *
8 | * For the full copyright and license information, please view the LICENSE
9 | * file that was distributed with this source code.
10 | */
11 |
12 | require_once __DIR__.'/../vendor/autoload.php';
13 | require __DIR__.'/PHPUnit/ProviderTestCase.php';
14 |
--------------------------------------------------------------------------------