22namespace Burzum \FileStorage \Storage ;
33
44use InvalidArgumentException ;
5+ use ReflectionClass ;
56use RuntimeException ;
67
78/**
89 * StorageManager - manages and instantiates Gaufrette storage engine instances
910 *
1011 * @author Florian Krämer
11- * @copyright 2012 - 2016 Florian Krämer
12+ * @copyright 2012 - 2017 Florian Krämer
1213 * @license MIT
1314 */
1415class StorageManager {
@@ -26,6 +27,13 @@ class StorageManager {
2627 ]
2728 ];
2829
30+ /**
31+ * Adapter objects
32+ *
33+ * @var array
34+ */
35+ protected $ _adapterInstances = [];
36+
2937 /**
3038 * Return a singleton instance of the StorageManager.
3139 *
@@ -71,22 +79,23 @@ public static function config($adapter, $options = array()) {
7179 public static function setConfig ($ configName , array $ configOptions ) {
7280 $ _this = StorageManager::getInstance ();
7381 $ _this ->_adapterConfig [$ configName ] = $ configOptions ;
82+ unset($ _this ->_adapterInstances [$ configName ]);
7483 }
7584
7685 /**
7786 * Get an adapter config
7887 *
79- * @param string $configName Configuration name
88+ * @param string $name Configuration name
8089 * @return array
8190 */
82- public static function getConfig ($ configName ) {
91+ public static function getConfig ($ name ) {
8392 $ _this = StorageManager::getInstance ();
8493
85- if (!isset ($ _this ->_adapterConfig [$ configName ])) {
86- throw new RuntimeException (sprintf ('Storage adapter config `%s` does not exist ' , $ configName ));
94+ if (!isset ($ _this ->_adapterConfig [$ name ])) {
95+ throw new RuntimeException (sprintf ('Storage adapter config `%s` does not exist ' , $ name ));
8796 }
8897
89- return $ _this ->_adapterConfig [$ configName ];
98+ return $ _this ->_adapterConfig [$ name ];
9099 }
91100
92101 /**
@@ -100,6 +109,7 @@ public static function flush($name = null) {
100109
101110 if (isset ($ _this ->_adapterConfig [$ name ])) {
102111 unset($ _this ->_adapterConfig [$ name ]);
112+ unset($ _this ->_adapterInstances [$ name ]);
103113 return true ;
104114 }
105115
@@ -113,6 +123,7 @@ public static function flush($name = null) {
113123 * @param boolean $renewObject Creates a new instance of the given adapter in the configuration
114124 * @throws \RuntimeException
115125 * @return \Gaufrette\Filesystem
126+ * @deprecated Use StorageManager::getAdapter() instead
116127 */
117128 public static function adapter ($ adapterName , $ renewObject = false ) {
118129 return self ::getAdapter ($ adapterName , $ renewObject );
@@ -121,44 +132,54 @@ public static function adapter($adapterName, $renewObject = false) {
121132 /**
122133 * Gets a configured instance of a storage adapter.
123134 *
124- * @param mixed $adapterName string of adapter configuration or array of settings
135+ * @param mixed $name string of adapter configuration or array of settings
125136 * @param boolean $renewObject Creates a new instance of the given adapter in the configuration
126137 * @throws \RuntimeException
127138 * @return \Gaufrette\Filesystem
128139 */
129- public static function getAdapter ($ adapterName , $ renewObject = false ) {
140+ public static function getAdapter ($ name , $ renewObject = false ) {
130141 $ _this = StorageManager::getInstance ();
131142
132- $ isConfigured = true ;
133- if (is_string ($ adapterName )) {
134- if (!empty ($ _this ->_adapterConfig [$ adapterName ])) {
135- $ adapter = $ _this ->_adapterConfig [$ adapterName ];
136- } else {
137- throw new RuntimeException (sprintf ('Invalid Storage Adapter %s! ' , $ adapterName ));
143+ if (is_string ($ name )) {
144+ if (!empty ($ _this ->_adapterInstances [$ name ]) && $ renewObject === false ) {
145+ return $ _this ->_adapterInstances [$ name ];
138146 }
139147
140- if (!empty ($ _this ->_adapterConfig [$ adapterName ]['object ' ]) && $ renewObject === false ) {
141- return $ _this ->_adapterConfig [$ adapterName ]['object ' ];
148+ if (!empty ($ _this ->_adapterConfig [$ name ])) {
149+ $ adapter = $ _this ->_adapterConfig [$ name ];
150+ } else {
151+ throw new RuntimeException (sprintf ('Invalid Storage Adapter %s! ' , $ name ));
142152 }
143153 }
144154
145- if (is_array ($ adapterName )) {
146- $ adapter = $ adapterName ;
147- $ isConfigured = false ;
155+ if (is_array ($ name )) {
156+ $ adapter = $ name ;
148157 }
149158
150159 $ class = $ adapter ['adapterClass ' ];
151- $ Reflection = new \ ReflectionClass ($ class );
160+ $ Reflection = new ReflectionClass ($ class );
152161 if (!is_array ($ adapter ['adapterOptions ' ])) {
153- throw new InvalidArgumentException (sprintf ('%s: The adapter options must be an array! ' , $ adapterName ));
162+ throw new InvalidArgumentException (sprintf ('%s: The adapter options must be an array! ' , $ name ));
154163 }
155164
156165 $ adapterObject = $ Reflection ->newInstanceArgs ($ adapter ['adapterOptions ' ]);
157166 $ engineObject = new $ adapter ['class ' ]($ adapterObject );
158- if ($ isConfigured ) {
159- $ _this ->_adapterConfig [$ adapterName ]['object ' ] = &$ engineObject ;
160- }
167+ $ _this ->_adapterInstances [$ name ] = &$ engineObject ;
161168
162169 return $ engineObject ;
163170 }
171+
172+ /**
173+ * Returns an array that can be used to describe the internal state of this
174+ * object.
175+ *
176+ * @return array
177+ */
178+ public function __debugInfo () {
179+ $ _this = StorageManager::getInstance ();
180+ return [
181+ '_adapterConfig ' => $ _this ->_adapterConfig ,
182+ '_adapterInstances ' => $ _this ->_adapterInstances ,
183+ ];
184+ }
164185}
0 commit comments