Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit aa8a36e

Browse files
author
Florian Krämer
committed
Refactoring the StorageManager
1 parent 7ba05b6 commit aa8a36e

File tree

2 files changed

+62
-41
lines changed

2 files changed

+62
-41
lines changed

src/Storage/StorageManager.php

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
namespace Burzum\FileStorage\Storage;
33

44
use InvalidArgumentException;
5+
use ReflectionClass;
56
use 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
*/
1415
class 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
}

tests/TestCase/Storage/StorageManagerTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* StorageManagerTest
44
*
55
* @author Florian Krämer
6-
* @copyright 2012 - 2016 Florian Krämer
6+
* @copyright 2012 - 2017 Florian Krämer
77
* @license MIT
88
*/
99
namespace Burzum\FileStorage\Test\TestCase\Lib;
@@ -12,11 +12,12 @@
1212
use Burzum\FileStorage\Storage\StorageManager;
1313

1414
class StorageManagerTest extends FileStorageTestCase {
15-
/**
16-
* testAdapter
17-
*
18-
* @return void
19-
*/
15+
16+
/**
17+
* testAdapter
18+
*
19+
* @return void
20+
*/
2021
public function testAdapter() {
2122
$result = StorageManager::adapter('Local');
2223
$this->assertEquals(get_class($result), 'Gaufrette\Filesystem');
@@ -27,11 +28,11 @@ public function testAdapter() {
2728
} catch (\RuntimeException $e) {}
2829
}
2930

30-
/**
31-
* testConfig
32-
*
33-
* @return void
34-
*/
31+
/**
32+
* testConfig
33+
*
34+
* @return void
35+
*/
3536
public function testConfig() {
3637
$result = StorageManager::config('Local');
3738
$expected = [
@@ -42,16 +43,15 @@ public function testConfig() {
4243
'adapterClass' => '\Gaufrette\Adapter\Local',
4344
'class' => '\Gaufrette\Filesystem'
4445
];
45-
4646
$this->assertEquals($result, $expected);
4747
$this->assertFalse(StorageManager::config('Does not exist'));
4848
}
4949

50-
/**
51-
* testFlush
52-
*
53-
* @return void
54-
*/
50+
/**
51+
* testFlush
52+
*
53+
* @return void
54+
*/
5555
public function testFlush() {
5656
$config = StorageManager::config('Local');
5757
$result = StorageManager::flush('Local');

0 commit comments

Comments
 (0)