silverstripe-environmentcheck/src/Checks/SolrIndexCheck.php

66 lines
1.6 KiB
PHP
Raw Normal View History

<?php
2015-09-10 23:13:48 +02:00
namespace SilverStripe\EnvironmentCheck\Checks;
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
/**
* Check the availability of all Solr indexes of given class.
*
2015-09-10 23:13:48 +02:00
* If there are no indexes of given class found, the returned status will still be "OK".
*
* @package environmentcheck
*/
2015-11-21 07:18:35 +01:00
class SolrIndexCheck implements EnvironmentCheck
{
/**
* @var null|string
*/
protected $indexClass;
2015-09-10 23:13:48 +02:00
2015-11-21 07:18:35 +01:00
/**
* @param string $indexClass Limit the index checks to the specified class and all its subclasses.
*/
public function __construct($indexClass = null)
{
$this->indexClass = $indexClass;
}
2015-09-10 23:13:48 +02:00
2015-11-21 07:18:35 +01:00
/**
* {@inheritDoc}
2015-11-21 07:18:35 +01:00
*
* @return array
*/
public function check()
{
$brokenCores = [];
/**
* @todo Revisit this when silverstripe/fulltextsearch has 4.x compat
*/
if (!class_exists('\\Solr')) {
return [
2015-11-21 07:18:35 +01:00
EnvironmentCheck::ERROR,
'Class `Solr` not found. Is the fulltextsearch module installed?'
];
2015-11-21 07:18:35 +01:00
}
$service = \Solr::service();
foreach (\Solr::get_indexes($this->indexClass) as $index) {
2015-11-21 07:18:35 +01:00
$core = $index->getIndexName();
if (!$service->coreIsActive($core)) {
$brokenCores[] = $core;
}
}
2015-11-21 07:18:35 +01:00
if (!empty($brokenCores)) {
return [
2015-11-21 07:18:35 +01:00
EnvironmentCheck::ERROR,
'The following indexes are unavailable: ' . implode($brokenCores, ', ')
];
2015-11-21 07:18:35 +01:00
}
return [EnvironmentCheck::OK, 'Expected indexes are available.'];
2015-11-21 07:18:35 +01:00
}
}