From ada254e1de1fb43c493c47d2447df5e800018b4d Mon Sep 17 00:00:00 2001 From: Brian Weaver Date: Tue, 12 Nov 2024 16:53:17 -0500 Subject: [PATCH] Add new reload drush command --- src/Commands/Reload.php | 136 ++++++++++++++++++++++++++++++++++ src/Services/SchemaPoster.php | 2 +- 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 src/Commands/Reload.php diff --git a/src/Commands/Reload.php b/src/Commands/Reload.php new file mode 100644 index 00000000..9bf85913 --- /dev/null +++ b/src/Commands/Reload.php @@ -0,0 +1,136 @@ +logger = $loggerChannelFactory->get('SearchAPIPantheon Drush'); + $this->pantheonGuzzle = $pantheonGuzzle; + $this->schemaPoster = $schemaPoster; + } + + /** + * Search_api_pantheon:reloadSchema + * + * @usage search-api-pantheon:reloadSchema + * Reload the latest schema + * + * @command search-api-pantheon:reloadSchema + * + */ + public function reloadSchema() { + try { + $this->schemaPoster->reloadServer(); + } + catch (\Exception $e) { + $this->logger->error((string) $e); + } + } + + /** + * Search_api_pantheon:postSchema. + * + * @usage search-api-pantheon:postSchema [server_id] [path] + * Post the latest schema to the given Server. + * Default server ID = pantheon_solr8. + * Default path = empty (build files using search_api_solr mechanism). + * + * @command search-api-pantheon:postSchema + * + * @param $server_id + * Server id to post schema for. + * @param $path + * Path to schema files (Leave empty to use default schema). + * + * @aliases sapps + */ + public function postSchema(?string $server_id = NULL, ?string $path = NULL) { + if (!$server_id) { + $server_id = PantheonSolrConnector::getDefaultEndpoint(); + } + try { + $files = []; + if ($path) { + if (!is_dir($path)) { + throw new \Exception("Path '$path' is not a directory."); + } + $finder = new Finder(); + // Only work with direct children. + $finder->depth('== 0'); + $finder->files()->in($path); + if (!$finder->hasResults()) { + throw new \Exception("Path '$path' does not contain any files."); + } + foreach ($finder as $file) { + $files[$file->getfilename()] = $file->getContents(); + } + } + + $this->schemaPoster->postSchema($server_id, $files); + } + catch (\Exception $e) { + $this->logger->error((string) $e); + } + } + + /** + * View a Schema File. + * + * @param string $filename + * Filename to retrieve. + * + * @command search-api-pantheon:view-schema + * @aliases sapvs + * @usage sapvs schema.xml + * @usage search-api-pantheon:view-schema elevate.xml + * + * @throws \Exception + * @throws \Psr\Http\Client\ClientExceptionInterface + */ + public function viewSchema(string $filename = 'schema.xml') { + $currentSchema = $this->schemaPoster->viewSchema($filename); + $this->logger->notice($currentSchema); + } + +} diff --git a/src/Services/SchemaPoster.php b/src/Services/SchemaPoster.php index 76b7b8d8..545964ba 100644 --- a/src/Services/SchemaPoster.php +++ b/src/Services/SchemaPoster.php @@ -118,7 +118,7 @@ public function postSchema(string $server_id, $files = []): array { return $this->processResponse($response); } - protected function reloadServer(): void { + public function reloadServer(): void { // Schema upload URL. $uri = new Uri( $this->getClient()