2
2
3
3
namespace Monospice \LaravelRedisSentinel \Manager ;
4
4
5
+ use Illuminate \Contracts \Container \Container ;
5
6
use Illuminate \Support \Arr ;
6
7
use Monospice \LaravelRedisSentinel \Configuration \Loader as ConfigurationLoader ;
7
8
use Monospice \LaravelRedisSentinel \RedisSentinelManager ;
18
19
*/
19
20
class VersionedManagerFactory
20
21
{
22
+ /**
23
+ * The current application instance that Laravel's RedisManager depends on
24
+ * in version 5.7+.
25
+ *
26
+ * @var Container
27
+ */
28
+ protected $ app ;
29
+
21
30
/**
22
31
* Detects the application version and provides configuration values.
23
32
*
@@ -28,26 +37,31 @@ class VersionedManagerFactory
28
37
/**
29
38
* Create a factory using the provided configuration.
30
39
*
40
+ * @param Container $app The current application instance that
41
+ * Laravel's RedisManager depends on in version 5.7+.
31
42
* @param ConfigurationLoader $config Detects the application version and
32
43
* provides configuration values.
33
44
*/
34
- public function __construct (ConfigurationLoader $ config )
45
+ public function __construct (Container $ app , ConfigurationLoader $ config )
35
46
{
47
+ $ this ->app = $ app ;
36
48
$ this ->config = $ config ;
37
49
}
38
50
39
51
/**
40
52
* Create an instance of the package's core Redis Sentinel service.
41
53
*
54
+ * @param Container $app The current application instance that
55
+ * Laravel's RedisManager depends on in version 5.7+.
42
56
* @param ConfigurationLoader $config Detects the application version and
43
57
* provides configuration values.
44
58
*
45
59
* @return \Monospice\LaravelRedisSentinel\Contracts\Factory A configured
46
60
* Redis Sentinel connection manager.
47
61
*/
48
- public static function make (ConfigurationLoader $ config )
62
+ public static function make (Container $ app , ConfigurationLoader $ config )
49
63
{
50
- return (new static ($ config ))->makeInstance ();
64
+ return (new static ($ app , $ config ))->makeInstance ();
51
65
}
52
66
53
67
/**
@@ -59,15 +73,15 @@ public static function make(ConfigurationLoader $config)
59
73
public function makeInstance ()
60
74
{
61
75
$ class = $ this ->getVersionedRedisSentinelManagerClass ();
62
- $ app = $ this ->config ->getApplication ();
63
76
$ config = $ this ->config ->get ('database.redis-sentinel ' , [ ]);
64
77
$ driver = Arr::pull ($ config , 'client ' , 'predis ' );
65
78
66
- if (version_compare ($ this ->config ->getApplicationVersion (), '5.6 ' ) <= 0 ) {
79
+ // Laravel 5.7 introduced the app as the first parameter:
80
+ if ($ this ->appVersionLessThan ('5.7 ' )) {
67
81
return new RedisSentinelManager (new $ class ($ driver , $ config ));
68
82
}
69
83
70
- return new RedisSentinelManager (new $ class ($ app , $ driver , $ config ));
84
+ return new RedisSentinelManager (new $ class ($ this -> app , $ driver , $ config ));
71
85
72
86
}
73
87
@@ -80,18 +94,32 @@ public function makeInstance()
80
94
*/
81
95
protected function getVersionedRedisSentinelManagerClass ()
82
96
{
83
- $ appVersion = $ this ->config ->getApplicationVersion ();
84
-
85
97
if ($ this ->config ->isLumen ) {
86
98
$ frameworkVersion = '5.4 ' ;
87
99
} else {
88
100
$ frameworkVersion = '5.4.20 ' ;
89
101
}
90
102
91
- if (version_compare ( $ appVersion , $ frameworkVersion, ' lt ' )) {
103
+ if ($ this -> appVersionLessThan ( $ frameworkVersion )) {
92
104
return Laravel540RedisSentinelManager::class;
93
105
}
94
106
95
107
return Laravel5420RedisSentinelManager::class;
96
108
}
109
+
110
+ /**
111
+ * Determine whether the current Laravel framework version is less than the
112
+ * specified version.
113
+ *
114
+ * @param string $version The version to compare to the current version.
115
+ *
116
+ * @return bool TRUE current framework version is less than the specified
117
+ * version.
118
+ */
119
+ protected function appVersionLessThan ($ version )
120
+ {
121
+ $ appVersion = $ this ->config ->getApplicationVersion ();
122
+
123
+ return version_compare ($ appVersion , $ version , 'lt ' );
124
+ }
97
125
}
0 commit comments