AbstractSolrEntity.php in Search API Solr 8.3
File
src/Entity/AbstractSolrEntity.php
View source
<?php
namespace Drupal\search_api_solr\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\search_api_solr\SolrConfigInterface;
abstract class AbstractSolrEntity extends ConfigEntityBase implements SolrConfigInterface {
protected $id;
protected $label;
protected $minimum_solr_version;
protected $recommended = TRUE;
protected $solr_configs;
protected $text_files;
public abstract function getName() : string;
public function getPurposeId() : string {
return $this
->getName();
}
protected function buildXmlFromArray($root_element_name, array $attributes) {
$root = new \SimpleXMLElement('<' . $root_element_name . '/>');
self::buildXmlFromArrayRecursive($root, $attributes);
$dom = dom_import_simplexml($root)->ownerDocument;
$dom->formatOutput = TRUE;
$formatted_xml_string = $dom
->saveXML();
return preg_replace('/<\\?.*?\\?>\\s*\\n?/', '', $formatted_xml_string);
}
protected static function buildXmlFromArrayRecursive(\SimpleXMLElement $element, array $attributes) {
foreach ($attributes as $key => $value) {
if (is_scalar($value)) {
if (is_bool($value) === TRUE) {
$value = $value ? 'true' : 'false';
}
switch ($key) {
case 'VALUE':
$element[0] = $value;
break;
case 'CDATA':
$element[0] = '<![CDATA[' . $value . ']]>';
break;
default:
$element
->addAttribute($key, $value);
}
}
elseif (is_array($value)) {
if (array_key_exists(0, $value)) {
$key = rtrim($key, 's');
foreach ($value as $inner_attributes) {
$child = $element
->addChild($key);
self::buildXmlFromArrayRecursive($child, $inner_attributes);
}
}
else {
$child = $element
->addChild($key);
self::buildXmlFromArrayRecursive($child, $value);
}
}
}
}
public function getTextFiles() {
return $this->text_files;
}
public function addTextFile($name, $content) {
$this->text_files[$name] = preg_replace('/\\R/u', "\n", $content);
return $this;
}
public function setTextFiles(array $text_files) {
$this->text_files = [];
foreach ($text_files as $name => $content) {
$this
->addTextFile($name, $content);
}
return $this;
}
public function getSolrConfigs() {
return $this->solr_configs;
}
public function setSolrConfigs(array $solr_configs) {
return $this->solr_configs = $solr_configs;
}
public function getSolrConfigsAsXml($add_comment = TRUE) {
if (!$this->solr_configs) {
return '';
}
$formatted_xml_string = $this
->buildXmlFromArray('solrconfigs', $this->solr_configs);
$comment = '';
if ($add_comment) {
$comment = "<!--\n Special configs for " . $this
->label() . "\n " . $this
->getMinimumSolrVersion() . "\n-->\n";
}
return $comment . trim(preg_replace('@</?solrconfigs/?>@', '', $formatted_xml_string), "\n") . "\n";
}
public function getMinimumSolrVersion() {
return $this->minimum_solr_version;
}
public function setMinimumSolrVersion($minimum_solr_version) {
$this->minimum_solr_version = $minimum_solr_version;
return $this;
}
protected static function getAvailableOptions(string $key, string $default, string $prefix) {
$options = [
[
$default,
],
];
$config_factory = \Drupal::configFactory();
foreach ($config_factory
->listAll($prefix) as $config_name) {
$config = $config_factory
->get($config_name);
$value = $config
->get($key);
if (NULL !== $value) {
$options[] = $value;
}
}
$options = array_unique(array_merge(...$options));
sort($options);
return $options;
}
public abstract function getOptions();
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
if ('collection' === $rel || 'disable-for-server' === $rel || 'enable-for-server' === $rel) {
$uri_route_parameters['search_api_server'] = \Drupal::routeMatch()
->getRawParameter('search_api_server') ?: 'core_issue_2919648_workaround';
}
return $uri_route_parameters;
}
public function isRecommended() : bool {
return $this->recommended;
}
}