settings = $settings; $this->paths = $paths; } /** * Settings accessor, handed to driver factories. * * @return Settings */ public function settings(): Settings { return $this->settings; } /** * Paths accessor, handed to driver factories. * * @return Paths */ public function paths(): Paths { return $this->paths; } /** * Registered driver factories. * * A factory receives this manager and must return a {@see Storage}. * * @return array */ public function drivers(): array { $drivers = apply_filters( 'backupino_storage_drivers', array(), $this ); if ( ! is_array( $drivers ) ) { $drivers = array(); } if ( ! isset( $drivers['local'] ) ) { $drivers['local'] = array( $this, 'local_driver' ); } return $drivers; } /** * The built in local driver. * * @return Storage */ public function local_driver(): Storage { return new LocalStorage( $this->paths->packages() ); } /** * Driver ids mapped to their human readable labels. * * @return array */ public function available(): array { $labels = array(); foreach ( $this->drivers() as $id => $factory ) { if ( ! is_callable( $factory ) ) { continue; } $storage = $factory( $this ); $labels[ (string) $id ] = $storage instanceof Storage ? $storage->label() : (string) $id; } return $labels; } /** * Configured driver. * * @return Storage */ public function driver(): Storage { return $this->driver_for( (string) $this->settings->get( 'storage.driver', 'local' ) ); } /** * Driver by id, falling back to local storage when the driver * is not available. * * @param string $id Driver id. * @return Storage */ public function driver_for( string $id ): Storage { $drivers = $this->drivers(); if ( isset( $drivers[ $id ] ) && is_callable( $drivers[ $id ] ) ) { return ( $drivers[ $id ] )( $this ); } return $this->local_driver(); } }