aimeos /
aimeos-base
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
||
| 5 | * @copyright Aimeos (aimeos.org), 2015-2025 |
||
| 6 | * @package Base |
||
| 7 | * @subpackage Filesystem |
||
| 8 | */ |
||
| 9 | |||
| 10 | |||
| 11 | namespace Aimeos\Base\Filesystem\Manager; |
||
| 12 | |||
| 13 | |||
| 14 | /** |
||
| 15 | * Standard file system manager |
||
| 16 | * |
||
| 17 | * @package Base |
||
| 18 | * @subpackage Filesystem |
||
| 19 | */ |
||
| 20 | class Standard implements Iface |
||
| 21 | { |
||
| 22 | private array $config; |
||
| 23 | private array $objects = []; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * Initializes the object |
||
| 28 | * |
||
| 29 | * @param array $config Associative multi-dimensional configuration |
||
| 30 | */ |
||
| 31 | public function __construct( array $config ) |
||
| 32 | { |
||
| 33 | $this->config = $config; |
||
| 34 | } |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * Cleans up the object |
||
| 39 | */ |
||
| 40 | public function __destruct() |
||
| 41 | { |
||
| 42 | foreach( $this->objects as $key => $object ) { |
||
| 43 | unset( $this->objects[$key] ); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Clean up the objects inside |
||
| 50 | * |
||
| 51 | * @return array List of properties to serialize |
||
| 52 | */ |
||
| 53 | public function __sleep() : array |
||
| 54 | { |
||
| 55 | $this->__destruct(); |
||
| 56 | $this->objects = []; |
||
| 57 | |||
| 58 | return array_keys( get_object_vars( $this ) ); |
||
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns the file system for the given name |
||
| 64 | * |
||
| 65 | * @param string $name Key for the file system |
||
| 66 | * @return \Aimeos\Base\Filesystem\Iface File system object |
||
| 67 | * @throws \Aimeos\Base\Filesystem\Exception If an no configuration for that name is found |
||
| 68 | */ |
||
| 69 | public function get( string $name ) : \Aimeos\Base\Filesystem\Iface |
||
| 70 | { |
||
| 71 | if( !isset( $this->objects[$name] ) ) { |
||
| 72 | $this->objects[$name] = $this->create( $this->config( $name ) ); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 73 | } |
||
| 74 | |||
| 75 | return $this->objects[$name]; |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Returns the configuration for the given name |
||
| 81 | * |
||
| 82 | * @param string $name Name of the resource, e.g. "fs" or "fs-media" |
||
| 83 | * @return array|string Configuration values or alias name |
||
| 84 | * @throws \Aimeos\Base\Filesystem\Exception If an no configuration for that name is found |
||
| 85 | */ |
||
| 86 | protected function config( string $name ) |
||
| 87 | { |
||
| 88 | foreach( [$name, 'fs'] as $fsname ) |
||
| 89 | { |
||
| 90 | if( isset( $this->config[$fsname] ) ) { |
||
| 91 | return $this->config[$fsname]; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | $msg = sprintf( 'No resource configuration for "%1$s" available', $name ); |
||
| 96 | throw new \Aimeos\Base\Filesystem\Exception( $msg ); |
||
| 97 | } |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Creates and returns a new file system object |
||
| 102 | * |
||
| 103 | * @param array $config Resource configuration |
||
| 104 | * @return \Aimeos\Base\Filesystem\Iface File system object |
||
| 105 | * @throws \Aimeos\Base\Filesystem\Exception if file system class isn't found |
||
| 106 | */ |
||
| 107 | protected function create( array $config ) : \Aimeos\Base\Filesystem\Iface |
||
| 108 | { |
||
| 109 | if( !isset( $config['adapter'] ) ) { |
||
| 110 | throw new \Aimeos\Base\Filesystem\Exception( 'File system not configured' ); |
||
| 111 | } |
||
| 112 | |||
| 113 | $classname = '\Aimeos\Base\Filesystem\\' . ucfirst( (string) $config['adapter'] ); |
||
| 114 | |||
| 115 | if( !class_exists( $classname ) ) { |
||
| 116 | throw new \Aimeos\Base\Filesystem\Exception( sprintf( 'File system "%1$s" not found', $config['adapter'] ) ); |
||
| 117 | } |
||
| 118 | |||
| 119 | return new $classname( $config ); |
||
| 120 | } |
||
| 121 | } |
||
| 122 |