Skip to content

Commit 08c79ad

Browse files
committed
Added registration and requirement check functions;
Moved previous factory checks into those new methods, with SHM automatically registering the bundled adapters; Avoided the APC errors by checking for the extension before its version.
1 parent 5da35ec commit 08c79ad

File tree

5 files changed

+90
-22
lines changed

5 files changed

+90
-22
lines changed

src/PEAR2/Cache/SHM.php

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
*/
2121
namespace PEAR2\Cache;
2222

23-
/**
24-
* Calls adapters.
25-
*/
26-
use PEAR2\Cache\SHM\Adapter;
27-
2823
/**
2924
* Main class for this package.
3025
*
@@ -38,6 +33,10 @@
3833
*/
3934
abstract class SHM implements \IteratorAggregate
4035
{
36+
/**
37+
* @var array An array of adapter names that meet their requirements.
38+
*/
39+
private static $_adapters = array();
4140

4241
/**
4342
* Creates a new shared memory storage.
@@ -47,21 +46,54 @@ abstract class SHM implements \IteratorAggregate
4746
*
4847
* @param string $persistentId The ID for the storage.
4948
*
50-
* @return self|SHM A new instance of an SHM adapter (child of this class).
49+
* @return static|SHM A new instance of an SHM adapter (child of this
50+
* class).
5151
*/
5252
public static function factory($persistentId)
5353
{
54-
if ('cli' === PHP_SAPI) {
55-
return new Adapter\Placebo($persistentId);
56-
} elseif (version_compare(phpversion('wincache'), '1.1.0', '>=')) {
57-
return new Adapter\Wincache($persistentId);
58-
} elseif (version_compare(phpversion('apc'), '3.0.13', '>=')) {
59-
return new Adapter\APC($persistentId);
60-
} else {
61-
throw new SHM\InvalidArgumentException(
62-
'No appropriate adapter available', 1
63-
);
54+
foreach (self::$_adapters as $adapter) {
55+
try {
56+
return new $adapter($persistentId);
57+
} catch (\Exception $e) {
58+
}
59+
}
60+
throw new SHM\InvalidArgumentException(
61+
'No appropriate adapter available', 1
62+
);
63+
}
64+
65+
/**
66+
* Checks if the adapter meets its requirements.
67+
*
68+
* @return bool TRUE on success, FALSE on failure.
69+
*/
70+
abstract public static function isMeetingRequirements();
71+
72+
/**
73+
* Registers an adapter.
74+
*
75+
* Registers an SHM adapter, allowing you to call it with {@link factory()}.
76+
*
77+
* @param string $adapter FQCN of adapter. A valid adapter is one that
78+
* extends this class.
79+
* @param bool $prepend Whether to prepend this adapter into the list of
80+
* possible adapters, instead of appending to it.
81+
*
82+
* @return bool TRUE on success, FALSE on failure.
83+
*/
84+
public static function registerAdapter($adapter, $prepend = false)
85+
{
86+
if (is_subclass_of($adapter, '\\' . __CLASS__)
87+
&& $adapter::isMeetingRequirements()
88+
) {
89+
if ($prepend) {
90+
self::$_adapters = array_merge(array($adapter), self::$_adapters);
91+
} else {
92+
self::$_adapters[] = $adapter;
93+
}
94+
return true;
6495
}
96+
return false;
6597
}
6698

6799
/**
@@ -272,4 +304,13 @@ abstract public function cas($key, $old, $new);
272304
* @return void
273305
*/
274306
abstract public function clear();
275-
}
307+
}
308+
309+
foreach (
310+
array('\Adapter\APC', '\Adapter\Placebo', '\Adapter\Wincache') as $adapter
311+
) {
312+
if (class_exists($adapter = '\\' . __NAMESPACE__ . $adapter, true)) {
313+
SHM::registerAdapter($adapter);
314+
}
315+
}
316+
unset($adapter);

src/PEAR2/Cache/SHM/Adapter/APC.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ public function __construct($persistentId)
8181
);
8282
}
8383

84+
/**
85+
* Checks if the adapter meets its requirements.
86+
*
87+
* @return bool TRUE on success, FALSE on failure.
88+
*/
89+
public static function isMeetingRequirements()
90+
{
91+
return extension_loaded('apc')
92+
&& version_compare(phpversion('apc'), '3.0.13', '>=');
93+
}
94+
8495
/**
8596
* Releases all locks in a storage.
8697
*

src/PEAR2/Cache/SHM/Adapter/Placebo.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
/**
1919
* The namespace declaration.
2020
*/
21-
2221
namespace PEAR2\Cache\SHM\Adapter;
2322

2423
/**
@@ -102,6 +101,16 @@ public function __destruct()
102101
}
103102
}
104103

104+
/**
105+
* Checks if the adapter meets its requirements.
106+
*
107+
* @return bool TRUE on success, FALSE on failure.
108+
*/
109+
public static function isMeetingRequirements()
110+
{
111+
return 'cli' === PHP_SAPI;
112+
}
113+
105114
/**
106115
* Pretends to obtain a lock.
107116
*

src/PEAR2/Cache/SHM/Adapter/Wincache.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
/**
1919
* The namespace declaration.
2020
*/
21-
2221
namespace PEAR2\Cache\SHM\Adapter;
2322

2423
/**
@@ -96,6 +95,17 @@ protected static function encodeLockName($name)
9695
return str_replace(array('%', '\\'), array('%25', '%5C'), $name);
9796
}
9897

98+
/**
99+
* Checks if the adapter meets its requirements.
100+
*
101+
* @return bool TRUE on success, FALSE on failure.
102+
*/
103+
public static function isMeetingRequirements()
104+
{
105+
return extension_loaded('wincache')
106+
&& version_compare(phpversion('wincache'), '1.1.0', '>=');
107+
}
108+
99109
/**
100110
* Releases any locks obtained by this instance as soon as there are no more
101111
* references to the object's persistent ID.

tests/PHPT/SHM-factory_CGI.phpt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@ Checks that SHM::factory() can be called from the command line.
77

88
--FILE_EXTERNAL--
99
SHM-factory.inc
10-
--XFAIL--
11-
Odd startup PCNTL related errors coming from APC...
12-
just for this test for some reason.
1310
--EXPECT--

0 commit comments

Comments
 (0)