| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Database; |
| 4 |
|
| 5 |
use Depicter\Database\Repository\DocumentRepository; |
| 6 |
use WPEmerge\ServiceProviders\ServiceProviderInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Load document data manager. |
| 10 |
*/ |
| 11 |
class DatabaseServiceProvider implements ServiceProviderInterface { |
| 12 |
|
| 13 |
/** |
| 14 |
* {@inheritDoc} |
| 15 |
*/ |
| 16 |
/** |
| 17 |
* {@inheritDoc} |
| 18 |
*/ |
| 19 |
public function register( $container ) { |
| 20 |
$container[ 'depicter.database.migration' ] = function () { |
| 21 |
return new Migration(); |
| 22 |
}; |
| 23 |
|
| 24 |
$container[ 'depicter.database.repository.document' ] = function () { |
| 25 |
return new DocumentRepository(); |
| 26 |
}; |
| 27 |
|
| 28 |
$app = $container[ WPEMERGE_APPLICATION_KEY ]; |
| 29 |
$app->alias( 'documentRepository', 'depicter.database.repository.document' ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* {@inheritDoc} |
| 34 |
*/ |
| 35 |
public function bootstrap( $container ) { |
| 36 |
register_activation_hook( DEPICTER_PLUGIN_FILE, [ $this, 'activate' ] ); |
| 37 |
add_action( 'wp_insert_site', [ $this, 'activateSingle' ] ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Plugin activation. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function activate( $network_wide ) { |
| 46 |
if ( $network_wide ) { |
| 47 |
$sites = get_sites(); |
| 48 |
foreach( $sites as $site ) { |
| 49 |
$this->activateSingle( $site ); |
| 50 |
} |
| 51 |
} else { |
| 52 |
$this->migrate(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Plugin activation on a site |
| 58 |
* |
| 59 |
* @param \WP_Site $site |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function activateSingle( $site ) { |
| 64 |
switch_to_blog( $site->blog_id ); |
| 65 |
$this->migrate(); |
| 66 |
restore_current_blog(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Create or update plugin tables |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function migrate() { |
| 75 |
\Depicter::resolve( 'depicter.database.migration' )->migrate(true); |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|