| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Aimeos (aimeos.org), 2016-2022 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package Base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @subpackage MQueue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Aimeos\Base\MQueue\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Standard message queue manager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package Base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @subpackage MQueue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class Standard implements Iface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	private $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 |  |  | 	 * Clones the objects inside. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	public function __clone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$this->objects[] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * Clean up the objects inside | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	public function __sleep() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$this->__destruct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$this->objects = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		return get_object_vars( $this ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * Returns the message queue for the given name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * @param string $name Resource name of the message queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @return \Aimeos\Base\MQueue\Iface Message queue object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @throws \Aimeos\Base\MQueue\Exception If an no configuration for that name is found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	public function get( string $name ) : \Aimeos\Base\MQueue\Iface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		if( !isset( $this->objects[$name] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			$this->objects[$name] = $this->create( $this->config( $name ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		return $this->objects[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * Returns the configuration for the given name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * @param string $name Name of the resource, e.g. "mq" or "mq-email" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * @return array Configuration values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 * @throws \Aimeos\Base\MQueue\Exception If an no configuration for that name is found | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 	protected function config( string $name ) : array | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 		foreach( [$name, 'mq'] as $mqname ) | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 			if( isset( $this->config[$mqname] ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  | 				return $this->config[$mqname]; | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 		$msg = sprintf( 'No resource configuration for "%1$s" available', $name ); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | 		throw new \Aimeos\Base\MQueue\Exception( $msg ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * Creates and returns a new message queue object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @param array $config Resource configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * @return \Aimeos\Base\MQueue\Iface Message queue object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @throws \Aimeos\Base\MQueue\Exception if message queue class isn't found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	protected function create( array $config ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		if( !isset( $config['adapter'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			throw new \Aimeos\Base\MQueue\Exception( 'Message queue not configured' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		$classname = '\Aimeos\Base\MQueue\\' . ucfirst( (string) $config['adapter'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		if( !class_exists( $classname ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			throw new \Aimeos\Base\MQueue\Exception( sprintf( 'Message queue "%1$s" not found', $config['adapter'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		$config['db'] = $this->config[$config['db'] ?? 'db'] ?? []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		return new $classname( $config ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  |