sebastianfeldmann /
phpbu
| 1 | <?php |
||
| 2 | namespace phpbu\App; |
||
| 3 | |||
| 4 | use phpbu\App\Backup\Check; |
||
| 5 | use phpbu\App\Backup\Cleaner; |
||
| 6 | use phpbu\App\Backup\Crypter; |
||
| 7 | use phpbu\App\Backup\Source; |
||
| 8 | use phpbu\App\Backup\Sync; |
||
| 9 | use phpbu\App\Backup\Target; |
||
| 10 | use phpbu\App\Log\Logger; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Factory |
||
| 14 | * |
||
| 15 | * @package phpbu |
||
| 16 | * @subpackage App |
||
| 17 | * @author Sebastian Feldmann <[email protected]> |
||
| 18 | * @copyright Sebastian Feldmann <[email protected]> |
||
| 19 | * @license https://opensource.org/licenses/MIT The MIT License (MIT) |
||
| 20 | * @link https://phpbu.de/ |
||
| 21 | * @since Class available since Release 1.0.0 |
||
| 22 | */ |
||
| 23 | class Factory |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Map of available sources, checks, crypts, syncs and cleanups |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $classMap = [ |
||
| 31 | // type |
||
| 32 | // alias => fqcn |
||
| 33 | 'adapter' => [ |
||
| 34 | 'array' => '\\phpbu\\App\\Adapter\\PHPArray', |
||
| 35 | 'dotenv' => '\\phpbu\\App\\Adapter\\Dotenv', |
||
| 36 | 'env' => '\\phpbu\\App\\Adapter\\Env', |
||
| 37 | ], |
||
| 38 | 'logger' => [ |
||
| 39 | 'json' => '\\phpbu\\App\\Log\\Json', |
||
| 40 | 'mail' => '\\phpbu\\App\\Log\\Mail', |
||
| 41 | 'webhook' => '\\phpbu\\App\\Log\\Webhook', |
||
| 42 | 'telegram' => '\\phpbu\\App\\Log\\Telegram', |
||
| 43 | 'prometheus' => '\\phpbu\\App\\Log\\Prometheus', |
||
| 44 | ], |
||
| 45 | 'source' => [ |
||
| 46 | 'arangodump' => '\\phpbu\\App\\Backup\\Source\\Arangodump', |
||
| 47 | 'elasticdump' => '\\phpbu\\App\\Backup\\Source\\Elasticdump', |
||
| 48 | 'mongodump' => '\\phpbu\\App\\Backup\\Source\\Mongodump', |
||
| 49 | 'mysqldump' => '\\phpbu\\App\\Backup\\Source\\Mysqldump', |
||
| 50 | 'pgdump' => '\\phpbu\\App\\Backup\\Source\\Pgdump', |
||
| 51 | 'influxdump' => '\\phpbu\\App\\Backup\\Source\\Influxdump', |
||
| 52 | 'ldapdump' => '\\phpbu\\App\\Backup\\Source\\Ldapdump', |
||
| 53 | 'redis' => '\\phpbu\\App\\Backup\\Source\\Redis', |
||
| 54 | 'rsync' => '\\phpbu\\App\\Backup\\Source\\Rsync', |
||
| 55 | 'tar' => '\\phpbu\\App\\Backup\\Source\\Tar', |
||
| 56 | 'xtrabackup' => '\\phpbu\\App\\Backup\\Source\\XtraBackup', |
||
| 57 | ], |
||
| 58 | 'check' => [ |
||
| 59 | 'sizemin' => '\\phpbu\\App\\Backup\\Check\\SizeMin', |
||
| 60 | 'sizediffpreviouspercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffPreviousPercent', |
||
| 61 | 'sizediffavgpercent' => '\\phpbu\\App\\Backup\\Check\\SizeDiffAvgPercent', |
||
| 62 | ], |
||
| 63 | 'crypter' => [ |
||
| 64 | 'gpg' => '\\phpbu\\App\\Backup\\Crypter\\Gpg', |
||
| 65 | 'mcrypt' => '\\phpbu\\App\\Backup\\Crypter\\Mcrypt', |
||
| 66 | 'openssl' => '\\phpbu\\App\\Backup\\Crypter\\OpenSSL', |
||
| 67 | ], |
||
| 68 | 'sync' => [ |
||
| 69 | 'amazons3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3', |
||
| 70 | 'amazons3-v3' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v3', |
||
| 71 | 'amazons3-v2' => '\\phpbu\\App\\Backup\\Sync\\AmazonS3v2', |
||
| 72 | 'backblazes3' => '\\phpbu\\App\\Backup\\Sync\\BackblazeS3', |
||
| 73 | 'azureblob' => '\\phpbu\\App\\Backup\\Sync\\AzureBlob', |
||
| 74 | 'dropbox' => '\\phpbu\\App\\Backup\\Sync\\Dropbox', |
||
| 75 | 'ftp' => '\\phpbu\\App\\Backup\\Sync\\Ftp', |
||
| 76 | 'googledrive' => '\\phpbu\\App\\Backup\\Sync\\GoogleDrive', |
||
| 77 | 'rsync' => '\\phpbu\\App\\Backup\\Sync\\Rsync', |
||
| 78 | 'sftp' => '\\phpbu\\App\\Backup\\Sync\\Sftp', |
||
| 79 | 'softlayer' => '\\phpbu\\App\\Backup\\Sync\\SoftLayer', |
||
| 80 | 'openstack' => '\\phpbu\\App\\Backup\\Sync\\OpenStack', |
||
| 81 | 'yandex-disk' => '\\phpbu\\App\\Backup\\Sync\\YandexDisk', |
||
| 82 | ], |
||
| 83 | 'cleaner' => [ |
||
| 84 | 'capacity' => '\\phpbu\\App\\Backup\\Cleaner\\Capacity', |
||
| 85 | 'outdated' => '\\phpbu\\App\\Backup\\Cleaner\\Outdated', |
||
| 86 | 'stepwise' => '\\phpbu\\App\\Backup\\Cleaner\\Stepwise', |
||
| 87 | 'quantity' => '\\phpbu\\App\\Backup\\Cleaner\\Quantity', |
||
| 88 | ], |
||
| 89 | ]; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Backup Factory |
||
| 93 | * Creates 'Source', 'Check', 'Crypter', 'Sync' and 'Cleaner' Objects. |
||
| 94 | * |
||
| 95 | * @param string $type |
||
| 96 | * @param string $alias |
||
| 97 | 29 | * @throws \phpbu\App\Exception |
|
| 98 | * @return mixed |
||
| 99 | 29 | */ |
|
| 100 | 29 | protected function create($type, $alias) |
|
| 101 | { |
||
| 102 | 29 | self::checkType($type); |
|
| 103 | 1 | $alias = strtolower($alias); |
|
| 104 | |||
| 105 | 28 | if (!isset(self::$classMap[$type][$alias])) { |
|
| 106 | 28 | throw new Exception(sprintf('unknown %s: %s', $type, $alias)); |
|
| 107 | } |
||
| 108 | $class = self::$classMap[$type][$alias]; |
||
| 109 | return new $class(); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Adapter Factory |
||
| 114 | * |
||
| 115 | * @param string $alias |
||
| 116 | * @param array $conf |
||
| 117 | 6 | * @throws \phpbu\App\Exception |
|
| 118 | * @return \phpbu\App\Adapter |
||
| 119 | */ |
||
| 120 | 6 | public function createAdapter($alias, $conf = []) : Adapter |
|
| 121 | 6 | { |
|
| 122 | 1 | /** @var \phpbu\App\Adapter $adapter */ |
|
| 123 | $adapter = $this->create('adapter', $alias); |
||
| 124 | 5 | if (!($adapter instanceof Adapter)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 125 | 5 | throw new Exception(sprintf('adapter \'%s\' has to implement the \'Adapter\' interfaces', $alias)); |
|
| 126 | } |
||
| 127 | $adapter->setup($conf); |
||
| 128 | return $adapter; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Logger Factory |
||
| 133 | * |
||
| 134 | * @param string $alias |
||
| 135 | * @param array $conf |
||
| 136 | 3 | * @throws \phpbu\App\Exception |
|
| 137 | * @return \phpbu\App\Log\Logger |
||
| 138 | */ |
||
| 139 | 3 | public function createLogger($alias, $conf = []) : Logger |
|
| 140 | 3 | { |
|
| 141 | 1 | /** @var \phpbu\App\Log\Logger $logger */ |
|
| 142 | $logger = $this->create('logger', $alias); |
||
| 143 | 2 | if (!($logger instanceof Logger)) { |
|
|
0 ignored issues
–
show
|
|||
| 144 | 1 | throw new Exception(sprintf('logger \'%s\' has to implement the \'Logger\' interfaces', $alias)); |
|
| 145 | } |
||
| 146 | 1 | if (!($logger instanceof Listener)) { |
|
| 147 | 1 | throw new Exception(sprintf('logger \'%s\' has to implement the \'Listener\' interface', $alias)); |
|
| 148 | } |
||
| 149 | $logger->setup($conf); |
||
| 150 | return $logger; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Create a backup target |
||
| 155 | * |
||
| 156 | * @param \phpbu\App\Configuration\Backup\Target $conf |
||
| 157 | 1 | * @return \phpbu\App\Backup\Target |
|
| 158 | * @throws \phpbu\App\Exception |
||
| 159 | 1 | */ |
|
| 160 | 1 | public function createTarget(Configuration\Backup\Target $conf) : Target |
|
| 161 | { |
||
| 162 | 1 | $target = new Target($conf->dirname, $conf->filename); |
|
| 163 | 1 | $target->setupPath(); |
|
| 164 | 1 | // add possible compressor |
|
| 165 | if (!empty($conf->compression)) { |
||
| 166 | 1 | $compression = Target\Compression\Factory::create($conf->compression); |
|
| 167 | $target->setCompression($compression); |
||
| 168 | } |
||
| 169 | return $target; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Source Factory |
||
| 174 | * |
||
| 175 | * @param string $alias |
||
| 176 | * @param array $conf |
||
| 177 | 2 | * @throws \phpbu\App\Exception |
|
| 178 | * @return \phpbu\App\Backup\Source |
||
| 179 | */ |
||
| 180 | 2 | public function createSource($alias, $conf = []) : Source |
|
| 181 | 2 | { |
|
| 182 | 1 | /** @var \phpbu\App\Backup\Source $source */ |
|
| 183 | $source = $this->create('source', $alias); |
||
| 184 | 1 | if (!($source instanceof Source)) { |
|
|
0 ignored issues
–
show
|
|||
| 185 | 1 | throw new Exception(sprintf('source \'%s\' has to implement the \'Source\' interface', $alias)); |
|
| 186 | } |
||
| 187 | $source->setup($conf); |
||
| 188 | return $source; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Check Factory |
||
| 193 | * |
||
| 194 | * @param string $alias |
||
| 195 | 4 | * @throws \phpbu\App\Exception |
|
| 196 | * @return \phpbu\App\Backup\Check |
||
| 197 | */ |
||
| 198 | 4 | public function createCheck($alias) : Check |
|
| 199 | 4 | { |
|
| 200 | 1 | /** @var \phpbu\App\Backup\Check $check */ |
|
| 201 | $check = $this->create('check', $alias); |
||
| 202 | 3 | if (!($check instanceof Check)) { |
|
|
0 ignored issues
–
show
|
|||
| 203 | throw new Exception(sprintf('Check \'%s\' has to implement the \'Check\' interface', $alias)); |
||
| 204 | } |
||
| 205 | return $check; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Crypter Factory |
||
| 210 | * |
||
| 211 | * @param string $alias |
||
| 212 | * @param array $conf |
||
| 213 | 2 | * @throws \phpbu\App\Exception |
|
| 214 | * @return \phpbu\App\Backup\Crypter |
||
| 215 | */ |
||
| 216 | 2 | public function createCrypter($alias, $conf = []) : Crypter |
|
| 217 | 2 | { |
|
| 218 | 1 | /** @var \phpbu\App\Backup\Crypter $crypter */ |
|
| 219 | $crypter = $this->create('crypter', $alias); |
||
| 220 | 1 | if (!($crypter instanceof Crypter)) { |
|
|
0 ignored issues
–
show
|
|||
| 221 | 1 | throw new Exception(sprintf('Crypter \'%s\' has to implement the \'Crypter\' interface', $alias)); |
|
| 222 | } |
||
| 223 | $crypter->setup($conf); |
||
| 224 | return $crypter; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Sync Factory |
||
| 229 | * |
||
| 230 | * @param string $alias |
||
| 231 | * @param array $conf |
||
| 232 | 3 | * @throws \phpbu\App\Exception |
|
| 233 | * @return \phpbu\App\Backup\Sync |
||
| 234 | */ |
||
| 235 | 3 | public function createSync($alias, $conf = []) : Sync |
|
| 236 | 2 | { |
|
| 237 | 1 | /** @var \phpbu\App\Backup\Sync $sync */ |
|
| 238 | $sync = $this->create('sync', $alias); |
||
| 239 | 1 | if (!($sync instanceof Sync)) { |
|
|
0 ignored issues
–
show
|
|||
| 240 | 1 | throw new Exception(sprintf('sync \'%s\' has to implement the \'Sync\' interface', $alias)); |
|
| 241 | } |
||
| 242 | $sync->setup($conf); |
||
| 243 | return $sync; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Cleaner Factory |
||
| 248 | * |
||
| 249 | * @param string $alias |
||
| 250 | * @param array $conf |
||
| 251 | 9 | * @throws \phpbu\App\Exception |
|
| 252 | * @return \phpbu\App\Backup\Cleaner |
||
| 253 | */ |
||
| 254 | 9 | public function createCleaner($alias, $conf = []) : Cleaner |
|
| 255 | 9 | { |
|
| 256 | 1 | /** @var \phpbu\App\Backup\Cleaner $cleaner */ |
|
| 257 | $cleaner = $this->create('cleaner', $alias); |
||
| 258 | 8 | if (!($cleaner instanceof Cleaner)) { |
|
|
0 ignored issues
–
show
|
|||
| 259 | 8 | throw new Exception(sprintf('cleaner \'%s\' has to implement the \'Cleaner\' interface', $alias)); |
|
| 260 | } |
||
| 261 | $cleaner->setup($conf); |
||
| 262 | return $cleaner; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Extend the backup factory |
||
| 267 | * |
||
| 268 | * @param string $type Type to create 'adapter', 'source', 'check', 'sync' or 'cleaner' |
||
| 269 | * @param string $alias Name the class is registered at |
||
| 270 | * @param string $fqcn Full Qualified Class Name |
||
| 271 | 23 | * @param boolean $force Overwrite already registered class |
|
| 272 | * @throws \phpbu\App\Exception |
||
| 273 | 23 | */ |
|
| 274 | 21 | public static function register($type, $alias, $fqcn, $force = false) |
|
| 275 | { |
||
| 276 | 21 | self::checkType($type); |
|
| 277 | 1 | $alias = strtolower($alias); |
|
| 278 | |||
| 279 | 20 | if (!$force && isset(self::$classMap[$type][$alias])) { |
|
| 280 | 20 | throw new Exception(sprintf('%s is already registered use force parameter to overwrite', $type)); |
|
| 281 | } |
||
| 282 | self::$classMap[$type][$alias] = $fqcn; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Throws an exception if type is invalid |
||
| 287 | * |
||
| 288 | 36 | * @param string $type |
|
| 289 | * @throws \phpbu\App\Exception |
||
| 290 | 36 | */ |
|
| 291 | 2 | private static function checkType($type) |
|
| 292 | 2 | { |
|
| 293 | if (!isset(self::$classMap[$type])) { |
||
| 294 | throw new Exception( |
||
| 295 | 34 | 'invalid type, please use only \'' . implode('\', \'', array_keys(self::$classMap)) . '\'' |
|
| 296 | ); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } |
||
| 300 |