2020 */
2121namespace PEAR2 \Cache ;
2222
23- /**
24- * Calls adapters.
25- */
26- use PEAR2 \Cache \SHM \Adapter ;
27-
2823/**
2924 * Main class for this package.
3025 *
3833 */
3934abstract 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 );
0 commit comments