feat: apply packages during imports
This commit is contained in:
@@ -8,15 +8,18 @@
|
||||
namespace WPContentSync\Admin;
|
||||
|
||||
use WPContentSync\Logging\LoggerInterface;
|
||||
use WPContentSync\Sync\SyncEngine;
|
||||
use WPContentSync\Transport\FileTransportInterface;
|
||||
|
||||
final class FileImportController {
|
||||
private FileTransportInterface $transport;
|
||||
private LoggerInterface $logger;
|
||||
private SyncEngine $sync_engine;
|
||||
|
||||
public function __construct( FileTransportInterface $transport, LoggerInterface $logger ) {
|
||||
$this->transport = $transport;
|
||||
$this->logger = $logger;
|
||||
public function __construct( FileTransportInterface $transport, LoggerInterface $logger, SyncEngine $sync_engine ) {
|
||||
$this->transport = $transport;
|
||||
$this->logger = $logger;
|
||||
$this->sync_engine = $sync_engine;
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
@@ -67,13 +70,23 @@ final class FileImportController {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->logger->info(
|
||||
'Validated imported content package.',
|
||||
array(
|
||||
'schema_version' => $package->schemaVersion(),
|
||||
'manifest' => $package->manifest(),
|
||||
)
|
||||
);
|
||||
$result = $this->sync_engine->importPackage( $package );
|
||||
|
||||
if ( ! $result->isSuccessful() ) {
|
||||
$this->logger->error(
|
||||
'Imported content package failed.',
|
||||
$result->toArray()
|
||||
);
|
||||
|
||||
$this->redirectToDashboard(
|
||||
array(
|
||||
'wpcs_import_error' => implode( ' ', $result->errors() ),
|
||||
)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->logger->info( 'Imported content package.', $result->toArray() );
|
||||
|
||||
$this->redirectToDashboard(
|
||||
array(
|
||||
|
||||
+86
-2
@@ -9,11 +9,18 @@ namespace WPContentSync;
|
||||
|
||||
use WPContentSync\Admin\AdminPage;
|
||||
use WPContentSync\Admin\FileImportController;
|
||||
use WPContentSync\Content\ContentHandlerRegistry;
|
||||
use WPContentSync\Content\ContentRecordNormalizer;
|
||||
use WPContentSync\Content\MediaContentHandler;
|
||||
use WPContentSync\Content\PostContentHandler;
|
||||
use WPContentSync\Content\TermContentHandler;
|
||||
use WPContentSync\Logging\LoggerInterface;
|
||||
use WPContentSync\Logging\OptionLogger;
|
||||
use WPContentSync\Package\PackageValidator;
|
||||
use WPContentSync\Rest\RestPackageController;
|
||||
use WPContentSync\Settings\SettingsRepository;
|
||||
use WPContentSync\Sync\SyncEngine;
|
||||
use WPContentSync\Sync\SyncStateRepository;
|
||||
use WPContentSync\Transport\FileTransportInterface;
|
||||
use WPContentSync\Transport\JsonFileTransport;
|
||||
use WPContentSync\Transport\RestTransportClient;
|
||||
@@ -67,6 +74,81 @@ final class Plugin {
|
||||
}
|
||||
);
|
||||
|
||||
$container->factory(
|
||||
ContentRecordNormalizer::class,
|
||||
static function (): ContentRecordNormalizer {
|
||||
return new ContentRecordNormalizer();
|
||||
}
|
||||
);
|
||||
|
||||
$container->factory(
|
||||
PostContentHandler::class,
|
||||
static function () use ( $container ): PostContentHandler {
|
||||
return new PostContentHandler(
|
||||
$container->get( ContentRecordNormalizer::class ),
|
||||
$container->get( UrlTransformer::class ),
|
||||
$container->get( MetadataUrlTransformer::class ),
|
||||
$container->get( LoggerInterface::class )
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$container->factory(
|
||||
TermContentHandler::class,
|
||||
static function () use ( $container ): TermContentHandler {
|
||||
return new TermContentHandler(
|
||||
$container->get( ContentRecordNormalizer::class ),
|
||||
$container->get( UrlTransformer::class ),
|
||||
$container->get( MetadataUrlTransformer::class ),
|
||||
$container->get( LoggerInterface::class )
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$container->factory(
|
||||
MediaContentHandler::class,
|
||||
static function () use ( $container ): MediaContentHandler {
|
||||
return new MediaContentHandler(
|
||||
$container->get( ContentRecordNormalizer::class ),
|
||||
$container->get( UrlTransformer::class ),
|
||||
$container->get( MetadataUrlTransformer::class ),
|
||||
$container->get( LoggerInterface::class )
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$container->factory(
|
||||
ContentHandlerRegistry::class,
|
||||
static function () use ( $container ): ContentHandlerRegistry {
|
||||
return new ContentHandlerRegistry(
|
||||
array(
|
||||
$container->get( PostContentHandler::class ),
|
||||
$container->get( TermContentHandler::class ),
|
||||
$container->get( MediaContentHandler::class ),
|
||||
)
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$container->factory(
|
||||
SyncStateRepository::class,
|
||||
static function (): SyncStateRepository {
|
||||
return new SyncStateRepository();
|
||||
}
|
||||
);
|
||||
|
||||
$container->factory(
|
||||
SyncEngine::class,
|
||||
static function () use ( $container ): SyncEngine {
|
||||
return new SyncEngine(
|
||||
$container->get( ContentHandlerRegistry::class ),
|
||||
$container->get( SyncStateRepository::class ),
|
||||
$container->get( SettingsRepository::class ),
|
||||
$container->get( LoggerInterface::class )
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$container->factory(
|
||||
FileTransportInterface::class,
|
||||
static function () use ( $container ): FileTransportInterface {
|
||||
@@ -81,7 +163,8 @@ final class Plugin {
|
||||
static function () use ( $container ): FileImportController {
|
||||
return new FileImportController(
|
||||
$container->get( FileTransportInterface::class ),
|
||||
$container->get( LoggerInterface::class )
|
||||
$container->get( LoggerInterface::class ),
|
||||
$container->get( SyncEngine::class )
|
||||
);
|
||||
}
|
||||
);
|
||||
@@ -97,7 +180,8 @@ final class Plugin {
|
||||
RestPackageController::class,
|
||||
static function () use ( $container ): RestPackageController {
|
||||
return new RestPackageController(
|
||||
$container->get( PackageValidator::class )
|
||||
$container->get( PackageValidator::class ),
|
||||
$container->get( SyncEngine::class )
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -9,12 +9,15 @@ namespace WPContentSync\Rest;
|
||||
|
||||
use WPContentSync\Package\ContentPackage;
|
||||
use WPContentSync\Package\PackageValidator;
|
||||
use WPContentSync\Sync\SyncEngine;
|
||||
|
||||
final class RestPackageController {
|
||||
private PackageValidator $validator;
|
||||
private SyncEngine $sync_engine;
|
||||
|
||||
public function __construct( PackageValidator $validator ) {
|
||||
$this->validator = $validator;
|
||||
public function __construct( PackageValidator $validator, SyncEngine $sync_engine ) {
|
||||
$this->validator = $validator;
|
||||
$this->sync_engine = $sync_engine;
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
@@ -82,11 +85,13 @@ final class RestPackageController {
|
||||
}
|
||||
|
||||
$package = ContentPackage::fromArray( $data['package'] );
|
||||
$import = $this->sync_engine->importPackage( $package );
|
||||
|
||||
return array(
|
||||
'accepted' => true,
|
||||
'accepted' => $import->isSuccessful(),
|
||||
'schema_version' => $package->schemaVersion(),
|
||||
'manifest' => $package->manifest(),
|
||||
'import' => $import->toArray(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user